r21325: delete children in reverse order since the array is manipulated during the...
[samba.git] / webapps / qooxdoo-0.6.3-sdk / frontend / framework / tool / modules / stringoptimizer.py
1 #!/usr/bin/env python
2
3 import tree
4
5
6 def search(node, verbose=False):
7   return search_loop(node, {}, verbose)
8
9
10 def search_loop(node, stringMap={}, verbose=False):
11   if node.type == "constant" and node.get("constantType") == "string":
12
13     if verbose:
14       print "    - Found: %s" % node.get("value")
15
16     if node.get("detail") == "singlequotes":
17       quote = "'"
18     elif node.get("detail") == "doublequotes":
19       quote = '"'
20
21     value = "%s%s%s" % (quote, node.get("value"), quote)
22
23     if value in stringMap:
24       stringMap[value] += 1
25     else:
26       stringMap[value] = 1
27
28   if check(node, verbose):
29     for child in node.children:
30       search_loop(child, stringMap, verbose)
31
32   return stringMap
33
34
35
36 def check(node, verbose=False):
37   # Needs children
38   if not node.hasChildren():
39     return False
40
41   # Try to find all output statements
42   if node.type == "call":
43     cu = node
44     nx = cu.getChild("operand", False)
45
46     if nx != None:
47       cu = nx
48
49     all = cu.getAllChildrenOfType("identifier")
50
51     for ch in all:
52       if ch.get("name", False) in [ "Error", "debug", "info", "warning", "error", "alert" ]:
53         if verbose:
54           print "    - Ignore output statement at line: %s" % ch.get("line")
55         return False
56
57   # Try to find all constant assignments (ns.UPPER = string)
58   elif node.type == "assignment":
59     left = node.getChild("left", False)
60     if left != None:
61       var = left.getChild("variable", False)
62
63       if var != None:
64         last = var.getLastChild()
65
66         if last.type == "identifier" and last.get("name").isupper():
67           if verbose:
68             print "    - Ignore constant assignment at line: %s" % last.get("line")
69           return False
70
71   # Try to find all constant assignments from Maps ({ UPPER : string })
72   elif node.type == "keyvalue":
73     if node.get("key").isupper():
74       if verbose:
75         print "    - Ignore constant key value at line: %s" % node.get("line")
76       return False
77
78   return True
79
80
81
82 def sort(stringMap):
83   stringList = []
84
85   for value in stringMap:
86     stringList.append({ "value" : value, "number" : stringMap[value] })
87
88   stringList.sort(lambda x, y: y["number"]-x["number"])
89
90   return stringList
91
92
93
94
95 def replace(node, stringList, var="$", verbose=False):
96   if node.type == "constant" and node.get("constantType") == "string":
97     if node.get("detail") == "singlequotes":
98       quote = "'"
99     elif node.get("detail") == "doublequotes":
100       quote = '"'
101
102     oldvalue = "%s%s%s" % (quote, node.get("value"), quote)
103
104     pos = 0
105     for item in stringList:
106       if item["value"] == oldvalue:
107         newvalue = "%s[%s]" % (var, pos)
108
109         if verbose:
110           print "    - Replace: %s => %s" % (oldvalue, newvalue)
111
112         line = node.get("line")
113
114
115         # GENERATE IDENTIFIER
116
117         newidentifier = tree.Node("identifier")
118         newidentifier.set("line", line)
119
120         childidentifier = tree.Node("identifier")
121         childidentifier.set("line", line)
122         childidentifier.set("name", var)
123
124         newidentifier.addChild(childidentifier)
125
126
127
128         # GENERATE KEY
129
130         newkey = tree.Node("key")
131         newkey.set("line", line)
132
133         newconstant = tree.Node("constant")
134         newconstant.set("line", line)
135         newconstant.set("constantType", "number")
136         newconstant.set("value", "%s" % pos)
137
138         newkey.addChild(newconstant)
139
140
141
142         # COMBINE CHILDREN
143
144         newnode = tree.Node("accessor")
145         newnode.set("line", line)
146         newnode.set("optimized", True)
147         newnode.set("original", oldvalue)
148         newnode.addChild(newidentifier)
149         newnode.addChild(newkey)
150
151
152         # REPLACE NODE
153
154         node.parent.replaceChild(node, newnode)
155         break
156
157       pos += 1
158
159   if check(node, verbose):
160     for child in node.children:
161       replace(child, stringList, var, verbose)
162
163
164
165 def replacement(stringList, var="$"):
166   repl = "%s=[" % var
167
168   for item in stringList:
169     repl += "%s," % (item["value"])
170
171   repl = repl[:-1] + "];"
172
173   return repl