pycleaner.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import re
  2. VAR = r"([a-zA-Z_][a-zA-Z0-9_]*)\s*=[^\n]+$"
  3. FUNC = re.compile(r"def\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\([^\)]*\)\s*:\s*")
  4. FUNC_PARAM = re.compile(r"\(([^\)]*)\)\s*:\s*")
  5. class CodeCleaner:
  6. def __init__ (self):
  7. self.varCount = 0
  8. self.funcCount = 0
  9. self.symbolMap = {}
  10. def cleanCode (self,text):
  11. funcs = FUNC.finditer(text)
  12. for _,m in enumerate(funcs, start=1):
  13. #print("Func delc: {}".format(m.group()))
  14. if m == None:
  15. continue
  16. self.cleanFunc(m.group())
  17. params = FUNC_PARAM.finditer(m.group())
  18. #print("p",list(params))
  19. for _, p in enumerate(params, start=1):
  20. if p == None:
  21. continue
  22. #print("Func param: {}".format(p.group()))
  23. self.cleanFuncParam(p.group())
  24. nvars = re.finditer(VAR,text,re.MULTILINE)
  25. for _, m in enumerate(nvars, start=1):
  26. if m == None:
  27. continue
  28. #print("vars: {}".format(m.group()))
  29. self.cleanVar(m.group())
  30. #print(self.symbolMap)
  31. stringMode = False
  32. openChar = None
  33. lineComment = False
  34. output = []
  35. alpha = ""
  36. for c in text:
  37. if stringMode:
  38. stringMode = not (c == openChar)
  39. output.append(c)
  40. elif lineComment:
  41. lineComment = c != '\n'
  42. #output.append(c)
  43. elif c == '#':
  44. lineComment = True
  45. #output.append(c)
  46. elif c == '"' or c == '\'':
  47. alpha = ""
  48. stringMode = True
  49. openChar = c
  50. output.append(c)
  51. elif re.match("[a-zA-Z0-9_]",c) != None:
  52. alpha += c
  53. else:
  54. if len(alpha) > 0 and alpha in self.symbolMap:
  55. #print("Replacing {} with {}.".format(alpha, self.symbolMap[alpha]))
  56. output.append(self.symbolMap[alpha])
  57. alpha = ""
  58. elif len(alpha) > 0:
  59. #print("Reinserting {}".format(alpha))
  60. output.append(alpha)
  61. alpha = ""
  62. output.append(c)
  63. output = "".join(output)
  64. return "".join([l for l in output.splitlines() if len(l.strip()) > 0])
  65. def cleanFunc (self,line):
  66. match = FUNC.search(line)
  67. varID = match.group(1)
  68. self.symbolMap[varID] = "f{}".format(self.funcCount)
  69. self.funcCount += 1
  70. def cleanFuncParam (self,line):
  71. match = FUNC_PARAM.findall(line)[0]
  72. if len(match.strip()) <= 0:
  73. return
  74. ids = match.split(",")
  75. for i in range(len(ids)):
  76. varID = ids[i].strip()
  77. if varID not in self.symbolMap:
  78. self.symbolMap[varID] = "v{}".format(self.varCount)
  79. self.varCount += 1
  80. def cleanVar (self,line):
  81. match = re.findall(VAR, line,re.M)
  82. ids = match
  83. for i in range(len(ids)):
  84. varID = ids[i].strip()
  85. if varID not in self.symbolMap:
  86. self.symbolMap[varID] = "v{}".format(self.varCount)
  87. self.varCount += 1