]> sigrok.org Git - libsigrok.git/blobdiff - bindings/swig/doc.py
python: Add docstrings for enum constants.
[libsigrok.git] / bindings / swig / doc.py
index 10b0494e0970e098447cda4f950789ced7279854..ffde9331f6b045e0f253151f13669b5f114c2ab7 100644 (file)
@@ -17,6 +17,7 @@
 ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ##
 
+from __future__ import print_function
 from xml.etree import ElementTree
 import sys, os
 
@@ -35,23 +36,26 @@ for compound in index.findall('compound'):
     class_name = compound.find('name').text
     if not class_name.startswith('sigrok::'):
         continue
+    trimmed_name = class_name.split('::')[1]
     doc = ElementTree.parse("%s/%s.xml" % (input_dir, compound.attrib['refid']))
     cls = doc.find('compounddef')
     brief = get_text(cls.find('briefdescription'))
     if brief:
         if language == 'python':
-            print '%%feature("docstring") %s "%s";' % (class_name, brief)
+            print('%%feature("docstring") %s "%s";' % (class_name, brief))
         elif language == 'java':
-            print '%%typemap(javaclassmodifiers) %s "/** %s */\npublic class"' % (
-            class_name, brief)
+            print('%%typemap(javaclassmodifiers) %s "/** %s */\npublic class"' % (
+            class_name, brief))
+    constants = []
     for section in cls.findall('sectiondef'):
-        if section.attrib['kind'] != 'public-func':
+        kind = section.attrib['kind']
+        if kind not in ('public-func', 'public-static-attrib'):
             continue
-        for function in section.findall('memberdef'):
-            function_name = function.find('name').text
-            brief = get_text(function.find('briefdescription'))
+        for member in section.findall('memberdef'):
+            member_name = member.find('name').text
+            brief = get_text(member.find('briefdescription')).replace('"', '\\"')
             parameters = {}
-            for para in function.find('detaileddescription').findall('para'):
+            for para in member.find('detaileddescription').findall('para'):
                 paramlist = para.find('parameterlist')
                 if paramlist is not None:
                     for param in paramlist.findall('parameteritem'):
@@ -61,15 +65,30 @@ for compound in index.findall('compound'):
                         if description:
                             parameters[name] = description
             if brief:
-                if language == 'python':
-                    print str.join('\n', [
+                if language == 'python' and kind == 'public-func':
+                    print(str.join('\n', [
                         '%%feature("docstring") %s::%s "%s' % (
-                            class_name, function_name, brief)] + [
+                            class_name, member_name, brief)] + [
                         '@param %s %s' % (name, desc)
-                            for name, desc in parameters.items()]) + '";'
-                elif language == 'java':
-                    print str.join('\n', [
-                        '%%javamethodmodifiers %s::%s "/** %s' % (
-                            class_name, function_name, brief)] + [
-                        '   * @param %s %s' % (name, desc)
-                            for name, desc in parameters.items()]) + ' */\npublic"'
+                            for name, desc in parameters.items()]) + '";')
+                elif language == 'java' and kind == 'public-func':
+                        print(str.join('\n', [
+                            '%%javamethodmodifiers %s::%s "/** %s' % (
+                                class_name, member_name, brief)] + [
+                            '   * @param %s %s' % (name, desc)
+                                for name, desc in parameters.items()])
+                                    + ' */\npublic"')
+                elif kind == 'public-static-attrib':
+                    constants.append((member_name, brief))
+    if language == 'java' and constants:
+        print('%%typemap(javacode) %s %%{' % class_name)
+        for member_name, brief in constants:
+            print('  /** %s */\n  public static final %s %s = new %s(classesJNI.%s_%s_get(), false);\n' % (
+                brief, trimmed_name, member_name, trimmed_name,
+                trimmed_name, member_name))
+        print('%}')
+    elif language == 'python' and constants:
+        print('%%extend %s {\n%%pythoncode %%{' % class_name)
+        for member_name, brief in constants:
+            print('    ## @brief %s\n    %s = None' % (brief, member_name))
+        print('%}\n}')