]> sigrok.org Git - libsigrok.git/blame - bindings/swig/doc.py
bindings/ruby: Fix ruby SWIG bindings generation
[libsigrok.git] / bindings / swig / doc.py
CommitLineData
bd4fda24
ML
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
8fa3fc7c 20from __future__ import print_function
bd4fda24
ML
21from xml.etree import ElementTree
22import sys, os
23
df979d6d
ML
24language, input_file = sys.argv[1:3]
25if len(sys.argv) == 4:
26 mode = sys.argv[3]
bd4fda24
ML
27input_dir = os.path.dirname(input_file)
28
29index = ElementTree.parse(input_file)
30
31def get_text(node):
32 paras = node.findall('para')
8eb4299c 33 return str.join('\n\n', [("".join(l)).rstrip() for l in [list(p.itertext()) for p in paras] if l])
bd4fda24
ML
34
35for 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
ef9643a2 41 trimmed_name = class_name.split('::')[1]
bd4fda24
ML
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':
8fa3fc7c 47 print('%%feature("docstring") %s "%s";' % (class_name, brief))
27d44cf6 48 elif language == 'ruby':
2e199405 49 print('%%feature("docstring") %s "Document-class: %s\\n%s\\n";' % (class_name, class_name.replace("sigrok", "Sigrok", 1), brief))
bd4fda24 50 elif language == 'java':
8fa3fc7c
ML
51 print('%%typemap(javaclassmodifiers) %s "/** %s */\npublic class"' % (
52 class_name, brief))
8fb7efe2 53 constants = []
bd4fda24 54 for section in cls.findall('sectiondef'):
8fb7efe2
ML
55 kind = section.attrib['kind']
56 if kind not in ('public-func', 'public-static-attrib'):
bd4fda24 57 continue
8fb7efe2
ML
58 for member in section.findall('memberdef'):
59 member_name = member.find('name').text
60 brief = get_text(member.find('briefdescription')).replace('"', '\\"')
5cad31c7 61 parameters = {}
8fb7efe2 62 for para in member.find('detaileddescription').findall('para'):
5cad31c7
ML
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
bd4fda24 71 if brief:
ef9643a2 72 if language == 'python' and kind == 'public-func':
8fa3fc7c 73 print(str.join('\n', [
5cad31c7 74 '%%feature("docstring") %s::%s "%s' % (
8fb7efe2 75 class_name, member_name, brief)] + [
5cad31c7 76 '@param %s %s' % (name, desc)
8fa3fc7c 77 for name, desc in parameters.items()]) + '";')
27d44cf6
AJ
78 if language == 'ruby' and kind == 'public-func':
79 print(str.join('\n', [
2e199405 80 '%%feature("docstring") %s::%s "%s' % (
27d44cf6
AJ
81 class_name, member_name, brief)] + [
82 '@param %s %s' % (name, desc)
2e199405 83 for name, desc in parameters.items()]) + '\\n";')
ef9643a2 84 elif language == 'java' and kind == 'public-func':
8fb7efe2
ML
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"')
ef9643a2
ML
91 elif kind == 'public-static-attrib':
92 constants.append((member_name, brief))
8fb7efe2
ML
93 if language == 'java' and constants:
94 print('%%typemap(javacode) %s %%{' % class_name)
95 for member_name, brief in constants:
8fb7efe2
ML
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('%}')
ef9643a2 100 elif language == 'python' and constants:
df979d6d
ML
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('%}')
27d44cf6
AJ
112 elif language == 'ruby' and constants:
113 for member_name, brief in constants:
2e199405 114 print('%%feature("docstring") %s::%s "%s\\n";' % (class_name, member_name, brief))