]> sigrok.org Git - libsigrok.git/blob - bindings/swig/doc.py
bindings/ruby: Fix ruby SWIG bindings generation
[libsigrok.git] / bindings / swig / doc.py
1 ##
2 ## This file is part of the libsigrok project.
3 ##
4 ## Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li>
5 ##
6 ## This program is free software: you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation, either version 3 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 ##
19
20 from __future__ import print_function
21 from xml.etree import ElementTree
22 import sys, os
23
24 language, input_file = sys.argv[1:3]
25 if len(sys.argv) == 4:
26     mode = sys.argv[3]
27 input_dir = os.path.dirname(input_file)
28
29 index = ElementTree.parse(input_file)
30
31 def get_text(node):
32     paras = node.findall('para')
33     return str.join('\n\n', [("".join(l)).rstrip() for l in [list(p.itertext()) for p in paras] if l])
34
35 for compound in index.findall('compound'):
36     if compound.attrib['kind'] != 'class':
37         continue
38     class_name = compound.find('name').text
39     if not class_name.startswith('sigrok::'):
40         continue
41     trimmed_name = class_name.split('::')[1]
42     doc = ElementTree.parse("%s/%s.xml" % (input_dir, compound.attrib['refid']))
43     cls = doc.find('compounddef')
44     brief = get_text(cls.find('briefdescription'))
45     if brief:
46         if language == 'python':
47             print('%%feature("docstring") %s "%s";' % (class_name, brief))
48         elif language == 'ruby':
49             print('%%feature("docstring") %s "Document-class: %s\\n%s\\n";' % (class_name, class_name.replace("sigrok", "Sigrok", 1), brief))
50         elif language == 'java':
51             print('%%typemap(javaclassmodifiers) %s "/** %s */\npublic class"' % (
52             class_name, brief))
53     constants = []
54     for section in cls.findall('sectiondef'):
55         kind = section.attrib['kind']
56         if kind not in ('public-func', 'public-static-attrib'):
57             continue
58         for member in section.findall('memberdef'):
59             member_name = member.find('name').text
60             brief = get_text(member.find('briefdescription')).replace('"', '\\"')
61             parameters = {}
62             for para in member.find('detaileddescription').findall('para'):
63                 paramlist = para.find('parameterlist')
64                 if paramlist is not None:
65                     for param in paramlist.findall('parameteritem'):
66                         namelist = param.find('parameternamelist')
67                         name = namelist.find('parametername').text
68                         description = get_text(param.find('parameterdescription'))
69                         if description:
70                             parameters[name] = description
71             if brief:
72                 if language == 'python' and kind == 'public-func':
73                     print(str.join('\n', [
74                         '%%feature("docstring") %s::%s "%s' % (
75                             class_name, member_name, brief)] + [
76                         '@param %s %s' % (name, desc)
77                             for name, desc in parameters.items()]) + '";')
78                 if language == 'ruby' and kind == 'public-func':
79                     print(str.join('\n', [
80                         '%%feature("docstring") %s::%s "%s' % (
81                             class_name, member_name, brief)] + [
82                         '@param %s %s' % (name, desc)
83                             for name, desc in parameters.items()]) + '\\n";')
84                 elif language == 'java' and kind == 'public-func':
85                         print(str.join('\n', [
86                             '%%javamethodmodifiers %s::%s "/** %s' % (
87                                 class_name, member_name, brief)] + [
88                             '   * @param %s %s' % (name, desc)
89                                 for name, desc in parameters.items()])
90                                     + ' */\npublic"')
91                 elif kind == 'public-static-attrib':
92                     constants.append((member_name, brief))
93     if language == 'java' and constants:
94         print('%%typemap(javacode) %s %%{' % class_name)
95         for member_name, brief in constants:
96             print('  /** %s */\n  public static final %s %s = new %s(classesJNI.%s_%s_get(), false);\n' % (
97                 brief, trimmed_name, member_name, trimmed_name,
98                 trimmed_name, member_name))
99         print('%}')
100     elif language == 'python' and constants:
101         if mode == 'start':
102             print('%%extend %s {\n%%pythoncode %%{' % class_name)
103             for member_name, brief in constants:
104                 print('    ## @brief %s\n    %s = None' % (brief, member_name))
105             print('%}\n}')
106         elif mode == 'end':
107             print('%pythoncode %{')
108             for member_name, brief in constants:
109                 print('%s.%s.__doc__ = """%s"""' % (
110                     trimmed_name, member_name, brief))
111             print('%}')
112     elif language == 'ruby' and constants:
113         for member_name, brief in constants:
114             print('%%feature("docstring") %s::%s "%s\\n";' % (class_name, member_name, brief))