Imported Upstream version 3.2.2
[debian/gnuradio] / grc / python / extract_docs.py
1 """
2 Copyright 2008, 2009 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 """
19
20 from Constants import DOCS_DIR
21 from lxml import etree
22 import os
23 import re
24
25 DOXYGEN_NAME_XPATH = '/doxygen/compounddef/compoundname'
26 DOXYGEN_BRIEFDESC_GR_XPATH = '/doxygen/compounddef/briefdescription'
27 DOXYGEN_DETAILDESC_GR_XPATH = '/doxygen/compounddef/detaileddescription'
28
29 def extract_txt(xml):
30         """
31         Recursivly pull the text out of an xml tree.
32         @param xml the xml tree
33         @return a string
34         """
35         text = (xml.text or '').replace('\n', '')
36         tail = (xml.tail or '').replace('\n', '')
37         if xml.tag == 'para': tail += '\n\n'
38         if xml.tag == 'linebreak': text += '\n'
39         if xml.tag == 'parametername': text += ': '
40         return text + ''.join(
41                 map(lambda x: extract_txt(x), xml)
42         ) + tail
43
44 def _extract(key):
45         """
46         Extract the documentation from the doxygen generated xml files.
47         If multiple files match, combine the docs.
48         @param key the block key
49         @return a string with documentation
50         """
51         docs_dir = os.path.join(DOCS_DIR, 'xml')
52         if not os.path.exists(docs_dir): return ''
53         #extract matches
54         pattern = key.replace('_', '_*').replace('x', '\w')
55         class_file_matcher = re.compile('^class%s\..*$'%pattern) #xml or xml.gz
56         matches = filter(lambda f: class_file_matcher.match(f), os.listdir(docs_dir))
57         #combine all matches
58         doc_strs = list()
59         for match in matches:
60                 try:
61                         xml_file = os.path.join(docs_dir, match)
62                         xml = etree.parse(xml_file)
63                         #extract descriptions
64                         comp_name = extract_txt(xml.xpath(DOXYGEN_NAME_XPATH)[0]).strip()
65                         comp_name = '   ---   ' + comp_name + '   ---   '
66                         if re.match('(gr|usrp2|trellis)_.*', key):
67                                 brief_desc = extract_txt(xml.xpath(DOXYGEN_BRIEFDESC_GR_XPATH)[0]).strip()
68                                 detailed_desc = extract_txt(xml.xpath(DOXYGEN_DETAILDESC_GR_XPATH)[0]).strip()
69                         else:
70                                 brief_desc = ''
71                                 detailed_desc = ''
72                         #combine
73                         doc_strs.append('\n\n'.join([comp_name, brief_desc, detailed_desc]).strip())
74                 except IndexError: pass #bad format
75         return '\n\n'.join(doc_strs)
76
77 _docs_cache = dict()
78 def extract(key):
79         """
80         Call the private extract and cache the result.
81         @param key the block key
82         @return a string with documentation
83         """
84         try: assert _docs_cache.has_key(key)
85         except: _docs_cache[key] = _extract(key)
86         return _docs_cache[key]
87
88 if __name__ == '__main__':
89         import sys
90         print extract(sys.argv[1])