]> sigrok.org Git - libsigrok.git/blob - bindings/swig/doc.py
bindings: Transfer C++ method parameter documentation to Python/Java bindings.
[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 xml.etree import ElementTree
21 import sys, os
22
23 language, input_file = sys.argv[1:]
24 input_dir = os.path.dirname(input_file)
25
26 index = ElementTree.parse(input_file)
27
28 def get_text(node):
29     paras = node.findall('para')
30     return str.join('\n\n', [p.text.rstrip() for p in paras if p.text])
31
32 for compound in index.findall('compound'):
33     if compound.attrib['kind'] != 'class':
34         continue
35     class_name = compound.find('name').text
36     if not class_name.startswith('sigrok::'):
37         continue
38     doc = ElementTree.parse("%s/%s.xml" % (input_dir, compound.attrib['refid']))
39     cls = doc.find('compounddef')
40     brief = get_text(cls.find('briefdescription'))
41     if brief:
42         if language == 'python':
43             print '%%feature("docstring") %s "%s";' % (class_name, brief)
44         elif language == 'java':
45             print '%%typemap(javaclassmodifiers) %s "/** %s */\npublic class"' % (
46             class_name, brief)
47     for section in cls.findall('sectiondef'):
48         if section.attrib['kind'] != 'public-func':
49             continue
50         for function in section.findall('memberdef'):
51             function_name = function.find('name').text
52             brief = get_text(function.find('briefdescription'))
53             parameters = {}
54             for para in function.find('detaileddescription').findall('para'):
55                 paramlist = para.find('parameterlist')
56                 if paramlist is not None:
57                     for param in paramlist.findall('parameteritem'):
58                         namelist = param.find('parameternamelist')
59                         name = namelist.find('parametername').text
60                         description = get_text(param.find('parameterdescription'))
61                         if description:
62                             parameters[name] = description
63             if brief:
64                 if language == 'python':
65                     print str.join('\n', [
66                         '%%feature("docstring") %s::%s "%s' % (
67                             class_name, function_name, brief)] + [
68                         '@param %s %s' % (name, desc)
69                             for name, desc in parameters.items()]) + '";'
70                 elif language == 'java':
71                     print str.join('\n', [
72                         '%%javamethodmodifiers %s::%s "/** %s' % (
73                             class_name, function_name, brief)] + [
74                         '   * @param %s %s' % (name, desc)
75                             for name, desc in parameters.items()]) + ' */\npublic"'