Imported Upstream version 3.2.2
[debian/gnuradio] / grc / python / extract_category.py
1 """
2 Copyright 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_TITLE_XPATH = '/doxygen/compounddef/title'
26 DOXYGEN_CLASS_XPATH = '/doxygen/compounddef/innerclass'
27
28 #map a group/category to a list of blocks
29 _category_map = dict()
30
31 #extract the group/category information
32 docs_dir = os.path.join(DOCS_DIR, 'xml')
33 if os.path.exists(docs_dir):
34         group_file_matcher = re.compile('^group__\w*\..*$') #xml or xml.gz
35         matches = filter(lambda f: group_file_matcher.match(f), os.listdir(docs_dir))
36         for match in matches:
37                 try:
38                         xml_file = os.path.join(docs_dir, match)
39                         xml = etree.parse(xml_file)
40                         category = xml.xpath(DOXYGEN_TITLE_XPATH)[0].text
41                         blocks = map(lambda x: x.text, xml.xpath(DOXYGEN_CLASS_XPATH))
42                         _category_map[category] = blocks
43                 except: pass
44
45 def extract(key):
46         """
47         Match the given key to a key in an existing category.
48         If no match can be made, return an empty string.
49         @param key the block key
50         @return the category or empty string
51         """
52         pattern = key.replace('_', '_*').replace('x', '\w')
53         class_name_matcher = re.compile('^%s$'%pattern)
54         for category, blocks in _category_map.iteritems():
55                 for block in blocks:
56                         if class_name_matcher.match(block): return category
57         return ''
58
59 if __name__ == '__main__':
60         import sys
61         print extract(sys.argv[1])