]>
Commit | Line | Data |
---|---|---|
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 | 20 | from __future__ import print_function |
bd4fda24 ML |
21 | from xml.etree import ElementTree |
22 | import sys, os | |
23 | ||
24 | language, input_file = sys.argv[1:] | |
25 | input_dir = os.path.dirname(input_file) | |
26 | ||
27 | index = ElementTree.parse(input_file) | |
28 | ||
29 | def get_text(node): | |
30 | paras = node.findall('para') | |
31 | return str.join('\n\n', [p.text.rstrip() for p in paras if p.text]) | |
32 | ||
33 | for compound in index.findall('compound'): | |
34 | if compound.attrib['kind'] != 'class': | |
35 | continue | |
36 | class_name = compound.find('name').text | |
37 | if not class_name.startswith('sigrok::'): | |
38 | continue | |
39 | doc = ElementTree.parse("%s/%s.xml" % (input_dir, compound.attrib['refid'])) | |
40 | cls = doc.find('compounddef') | |
41 | brief = get_text(cls.find('briefdescription')) | |
42 | if brief: | |
43 | if language == 'python': | |
8fa3fc7c | 44 | print('%%feature("docstring") %s "%s";' % (class_name, brief)) |
bd4fda24 | 45 | elif language == 'java': |
8fa3fc7c ML |
46 | print('%%typemap(javaclassmodifiers) %s "/** %s */\npublic class"' % ( |
47 | class_name, brief)) | |
8fb7efe2 | 48 | constants = [] |
bd4fda24 | 49 | for section in cls.findall('sectiondef'): |
8fb7efe2 ML |
50 | kind = section.attrib['kind'] |
51 | if kind not in ('public-func', 'public-static-attrib'): | |
bd4fda24 | 52 | continue |
8fb7efe2 ML |
53 | for member in section.findall('memberdef'): |
54 | member_name = member.find('name').text | |
55 | brief = get_text(member.find('briefdescription')).replace('"', '\\"') | |
5cad31c7 | 56 | parameters = {} |
8fb7efe2 | 57 | for para in member.find('detaileddescription').findall('para'): |
5cad31c7 ML |
58 | paramlist = para.find('parameterlist') |
59 | if paramlist is not None: | |
60 | for param in paramlist.findall('parameteritem'): | |
61 | namelist = param.find('parameternamelist') | |
62 | name = namelist.find('parametername').text | |
63 | description = get_text(param.find('parameterdescription')) | |
64 | if description: | |
65 | parameters[name] = description | |
bd4fda24 ML |
66 | if brief: |
67 | if language == 'python': | |
8fa3fc7c | 68 | print(str.join('\n', [ |
5cad31c7 | 69 | '%%feature("docstring") %s::%s "%s' % ( |
8fb7efe2 | 70 | class_name, member_name, brief)] + [ |
5cad31c7 | 71 | '@param %s %s' % (name, desc) |
8fa3fc7c | 72 | for name, desc in parameters.items()]) + '";') |
bd4fda24 | 73 | elif language == 'java': |
8fb7efe2 ML |
74 | if kind == 'public-func': |
75 | print(str.join('\n', [ | |
76 | '%%javamethodmodifiers %s::%s "/** %s' % ( | |
77 | class_name, member_name, brief)] + [ | |
78 | ' * @param %s %s' % (name, desc) | |
79 | for name, desc in parameters.items()]) | |
80 | + ' */\npublic"') | |
81 | elif kind == 'public-static-attrib': | |
82 | constants.append((member_name, brief)) | |
83 | if language == 'java' and constants: | |
84 | print('%%typemap(javacode) %s %%{' % class_name) | |
85 | for member_name, brief in constants: | |
86 | trimmed_name = class_name.split('::')[1] | |
87 | print(' /** %s */\n public static final %s %s = new %s(classesJNI.%s_%s_get(), false);\n' % ( | |
88 | brief, trimmed_name, member_name, trimmed_name, | |
89 | trimmed_name, member_name)) | |
90 | print('%}') |