2 ## This file is part of the libsigrok project.
4 ## Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li>
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.
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.
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/>.
20 from __future__ import print_function
21 from xml.etree import ElementTree
24 language, input_file = sys.argv[1:3]
25 if len(sys.argv) == 4:
27 input_dir = os.path.dirname(input_file)
29 index = ElementTree.parse(input_file)
32 paras = node.findall('para')
33 return str.join('\n\n', [p.text.rstrip() for p in paras if p.text])
35 for compound in index.findall('compound'):
36 if compound.attrib['kind'] != 'class':
38 class_name = compound.find('name').text
39 if not class_name.startswith('sigrok::'):
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'))
46 if language == 'python':
47 print('%%feature("docstring") %s "%s";' % (class_name, brief))
48 elif language == 'java':
49 print('%%typemap(javaclassmodifiers) %s "/** %s */\npublic class"' % (
52 for section in cls.findall('sectiondef'):
53 kind = section.attrib['kind']
54 if kind not in ('public-func', 'public-static-attrib'):
56 for member in section.findall('memberdef'):
57 member_name = member.find('name').text
58 brief = get_text(member.find('briefdescription')).replace('"', '\\"')
60 for para in member.find('detaileddescription').findall('para'):
61 paramlist = para.find('parameterlist')
62 if paramlist is not None:
63 for param in paramlist.findall('parameteritem'):
64 namelist = param.find('parameternamelist')
65 name = namelist.find('parametername').text
66 description = get_text(param.find('parameterdescription'))
68 parameters[name] = description
70 if language == 'python' and kind == 'public-func':
71 print(str.join('\n', [
72 '%%feature("docstring") %s::%s "%s' % (
73 class_name, member_name, brief)] + [
74 '@param %s %s' % (name, desc)
75 for name, desc in parameters.items()]) + '";')
76 elif language == 'java' and kind == 'public-func':
77 print(str.join('\n', [
78 '%%javamethodmodifiers %s::%s "/** %s' % (
79 class_name, member_name, brief)] + [
80 ' * @param %s %s' % (name, desc)
81 for name, desc in parameters.items()])
83 elif kind == 'public-static-attrib':
84 constants.append((member_name, brief))
85 if language == 'java' and constants:
86 print('%%typemap(javacode) %s %%{' % class_name)
87 for member_name, brief in constants:
88 print(' /** %s */\n public static final %s %s = new %s(classesJNI.%s_%s_get(), false);\n' % (
89 brief, trimmed_name, member_name, trimmed_name,
90 trimmed_name, member_name))
92 elif language == 'python' and constants:
94 print('%%extend %s {\n%%pythoncode %%{' % class_name)
95 for member_name, brief in constants:
96 print(' ## @brief %s\n %s = None' % (brief, member_name))
99 print('%pythoncode %{')
100 for member_name, brief in constants:
101 print('%s.%s.__doc__ = """%s"""' % (
102 trimmed_name, member_name, brief))