6 def search(node, verbose=False):
7 return search_loop(node, {}, verbose)
10 def search_loop(node, stringMap={}, verbose=False):
11 if node.type == "constant" and node.get("constantType") == "string":
14 print " - Found: %s" % node.get("value")
16 if node.get("detail") == "singlequotes":
18 elif node.get("detail") == "doublequotes":
21 value = "%s%s%s" % (quote, node.get("value"), quote)
23 if value in stringMap:
28 if check(node, verbose):
29 for child in node.children:
30 search_loop(child, stringMap, verbose)
36 def check(node, verbose=False):
38 if not node.hasChildren():
41 # Try to find all output statements
42 if node.type == "call":
44 nx = cu.getChild("operand", False)
49 all = cu.getAllChildrenOfType("identifier")
52 if ch.get("name", False) in [ "Error", "debug", "info", "warning", "error", "alert" ]:
54 print " - Ignore output statement at line: %s" % ch.get("line")
57 # Try to find all constant assignments (ns.UPPER = string)
58 elif node.type == "assignment":
59 left = node.getChild("left", False)
61 var = left.getChild("variable", False)
64 last = var.getLastChild()
66 if last.type == "identifier" and last.get("name").isupper():
68 print " - Ignore constant assignment at line: %s" % last.get("line")
71 # Try to find all constant assignments from Maps ({ UPPER : string })
72 elif node.type == "keyvalue":
73 if node.get("key").isupper():
75 print " - Ignore constant key value at line: %s" % node.get("line")
85 for value in stringMap:
86 stringList.append({ "value" : value, "number" : stringMap[value] })
88 stringList.sort(lambda x, y: y["number"]-x["number"])
95 def replace(node, stringList, var="$", verbose=False):
96 if node.type == "constant" and node.get("constantType") == "string":
97 if node.get("detail") == "singlequotes":
99 elif node.get("detail") == "doublequotes":
102 oldvalue = "%s%s%s" % (quote, node.get("value"), quote)
105 for item in stringList:
106 if item["value"] == oldvalue:
107 newvalue = "%s[%s]" % (var, pos)
110 print " - Replace: %s => %s" % (oldvalue, newvalue)
112 line = node.get("line")
115 # GENERATE IDENTIFIER
117 newidentifier = tree.Node("identifier")
118 newidentifier.set("line", line)
120 childidentifier = tree.Node("identifier")
121 childidentifier.set("line", line)
122 childidentifier.set("name", var)
124 newidentifier.addChild(childidentifier)
130 newkey = tree.Node("key")
131 newkey.set("line", line)
133 newconstant = tree.Node("constant")
134 newconstant.set("line", line)
135 newconstant.set("constantType", "number")
136 newconstant.set("value", "%s" % pos)
138 newkey.addChild(newconstant)
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)
154 node.parent.replaceChild(node, newnode)
159 if check(node, verbose):
160 for child in node.children:
161 replace(child, stringList, var, verbose)
165 def replacement(stringList, var="$"):
168 for item in stringList:
169 repl += "%s," % (item["value"])
171 repl = repl[:-1] + "];"