Merge branch 'master' of git.opendreambox.org:/git/enigma2
[enigma2.git] / lib / actions / parseactions.py
1 # takes a header file, outputs action ids
2
3 import tokenize, sys, string
4
5 def filter(g):
6         while 1:
7                 t = g.next()
8                 if t[1] == "/*":
9                         while g.next()[1] != "*/":
10                                 pass
11                         continue
12                 if t[1] == "//":
13                         while g.next()[1] != "\n":
14                                 pass
15                         continue
16                 
17                 if t[1] != "\n":
18 #                       print t
19                         yield t[1]
20
21 def do_file(f, mode):
22         tokens = filter(tokenize.generate_tokens(open(f, 'r').readline))
23         
24         sys.stderr.write("parsing %s\n" % f)
25         
26         state = 0
27         
28         classstate = 0
29         
30         firsthit = 1
31         
32         while 1:
33                 try:
34                         t = tokens.next()
35                 except:
36                         break
37                 
38                 if t == "class":
39                         classname = tokens.next()
40                         classstate = state
41                 
42                 if t == "{":
43                         state = state + 1
44                 
45                 if t == "}":
46                         state = state - 1
47                 
48                 if t == "enum" and state == classstate + 1:
49                         actionname = tokens.next()
50                         
51                         if actionname == "{":
52                                 while tokens.next() != "}":
53                                         pass
54                                 continue
55                         
56                         if actionname[-7:] == "Actions":
57                                 if tokens.next() != "{":
58                                         try:
59                                                 print classname
60                                         except:
61                                                 pass
62                                 
63                                         try:
64                                                 print actionname
65                                         except:
66                                                 pass
67                                 
68                                         raise Exception("action enum must be simple.")
69                         
70                                 counter = 0
71                         
72                                 while 1:
73         
74                                         t = tokens.next()
75                                         
76                                         if t == "=":
77                                                 tokens.next()
78                                                 t = tokens.next()
79         
80                                         if t == "}":
81                                                 break
82                                         
83                                         if counter:
84                                                 if t != ",":
85                                                         raise Exception("no comma")
86                                                 t = tokens.next()
87                                 
88                                         if firsthit:
89
90                                                 if mode == "include":
91                                                         # hack hack hack!!
92                                                         print "#include <lib" + f[2:] + ">"
93                                                 else:
94                                                         print "\t// " + f
95
96                                                 firsthit = 0
97
98                                         if mode == "parse":
99                                                 print "{\"" + actionname + "\", \"" + t + "\", " + string.join((classname, t), "::") + "},"
100
101
102                                         counter = counter + 1
103
104 mode = sys.argv[1]
105
106 if mode == "parse":
107         print """
108         /* generated by parseactions.py - do not modify! */
109 struct eActionList
110 {
111         const char *m_context, *m_action;
112         int m_id;
113 } actions[]={"""
114
115 for x in sys.argv[2:]:
116         do_file(x, mode)
117
118 if mode == "parse":
119         print "};"