Remove a debug print
[bertos.git] / wizard / newParser.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # Copyright 2009 Develer S.r.l. (http://www.develer.com/)
5 # All rights reserved.
6 #
7 # $Id:$
8 #
9 # Author: Lorenzo Berni <duplo@develer.com>
10 #
11
12 import re
13 import DefineException
14
15 def getCommentList(string):
16     commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
17     commentList = [re.findall(r"^\s*\* *(.*?)$", comment, re.MULTILINE) for comment in commentList]
18     return commentList
19
20 def loadModuleDefinition(first_comment):
21     toBeParsed = False
22     moduleDefinition = {}
23     for num, line in enumerate(first_comment):
24         index = line.find("$WIZ$")
25         if index != -1:
26             toBeParsed = True
27             try:
28                 exec line[index + len("$WIZ$ "):] in {}, moduleDefinition
29             except:
30                 raise ParseError(num, line[index:])
31         elif line.find("\\brief") != -1:
32             moduleDefinition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
33     moduleDict = {}
34     if "module_name" in moduleDefinition.keys():
35         moduleDict[moduleDefinition["module_name"]] = {}
36         if "module_depends" in moduleDefinition.keys():
37             if type(moduleDefinition["module_depends"]) == str:
38                 moduleDefinition["module_depends"] = (moduleDefinition["module_depends"],)
39             moduleDict[moduleDefinition["module_name"]]["depends"] = moduleDefinition["module_depends"]
40         else:
41             moduleDict[moduleDefinition["module_name"]]["depends"] = ()
42         if "module_configuration" in moduleDefinition.keys():
43             moduleDict[moduleDefinition["module_name"]]["configuration"] = moduleDefinition["module_configuration"]
44         else:
45             moduleDict[moduleDefinition["module_name"]]["configuration"] = ""
46         if "module_description" in moduleDefinition.keys():
47             moduleDict[moduleDefinition["module_name"]]["description"] = moduleDefinition["module_description"]
48         moduleDict[moduleDefinition["module_name"]]["enabled"] = False
49     return toBeParsed, moduleDict
50
51 def loadDefineLists(commentList):
52     defineList = {}
53     for comment in commentList:
54         for num, line in enumerate(comment):
55             index = line.find("$WIZ$")
56             if index != -1:
57                 try:
58                     exec line[index + len("$WIZ$ "):] in {}, defineList
59                 except:
60                     raise ParseError(num, line[index:])
61     for key, value in defineList.items():
62         if type(value) == str:
63             defineList[key] = (value,)
64     return defineList
65
66 def getDescriptionInformations(comment): 
67     """ 
68     Take the doxygen comment and strip the wizard informations, returning the tuple 
69     (comment, wizard_information) 
70     """
71     description = ""
72     information = {}
73     for num, line in enumerate(comment):
74         index = line.find("$WIZ$")
75         if index != -1:
76             description += " " + line[:index]
77             try:
78                 exec line[index + len("$WIZ$ "):] in {}, information
79             except:
80                 raise ParseError(num, line[index:])
81         else:
82             description += " " + line
83     return description.strip(), information
84
85 def getDefinitionBlocks(text):
86     """
87     Take a text and return a list of tuple (description, name-value).
88     """
89     block = []
90     block_tmp = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE)
91     for comment, define in block_tmp:
92         # Only the first element is needed
93         block.append(([re.findall(r"^\s*\* *(.*?)$", line, re.MULTILINE)[0] for line in comment.splitlines()], define))
94     for comment, define in re.findall(r"/{3}\s*([^<].*?)\s*#define\s+((?:[^/]*?/?)+)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
95         block.append(([comment], define))
96     for define, comment in re.findall(r"#define\s*(.*?)\s*/{3}<\s*(.+?)\s*?(?:/{2,3}[^<].*?)?$", text, re.MULTILINE):
97         block.append(([comment], define))
98     return block
99
100 class ParseError(Exception):
101     def __init__(self, line_number, line):
102         Exception.__init__(self)
103         self.line_number = line_number
104         self.line = line
105
106 def main():
107     try:
108         defineLists = {}
109         modules = {}
110         commentList = getCommentList(open("test/to_parse", "r").read())
111         toBeParsedm, moduleInfo = loadModuleDefinition(commentList[0])
112         modules.update(moduleInfo)
113         if toBeParsed:
114             defineLists.update(loadDefineList(commentList[1:]))
115         print modules
116         print defineLists
117     except ParseError, err:
118         print "Error: line %d - %s" % (err.line_number, err.line)
119
120 if __name__ == '__main__':
121     main()