Advertisement
Guest User

Untitled

a guest
May 26th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  1. import re
  2.  
  3.  
  4. STATE_CODE = 0
  5. STATE_IMPORT = 1
  6. STATE_GLOBALS = 2
  7. STATE_CLASS = 3
  8. STATE_FUNCTION = 4
  9. STATES = {re.compile('import |from '): STATE_IMPORT,
  10. re.compile("[A-Z]+(_[A-Z]*)* = .+"): STATE_GLOBALS,
  11. re.compile('class '): STATE_CLASS,
  12. re.compile('def '): STATE_FUNCTION}
  13. RE_IMPORT = re.compile("(import \w+)")
  14. RE_FROM_IMPORT = re.compile("from \w+ import (\w+, )*\w+")
  15. RE_CLASS = re.compile("(class) ([A-Z][a-z]+)+((\(\w+)?(, \w+)*\))*:")
  16. RE_GLOBALS = re.compile("[A-Z]+(_[A-Z]*)* = .+")
  17. RE_FUNCTION = re.compile("(def) ((_?[a-z]+(_[a-z]*)*)|(__[a-z]+__))((\(\w+)?(, \w+)*\))*:")
  18. RE_TABS = re.compile("( )+|\t+|^(\w)")
  19. RE_INSTRUCTIONS = re.compile("(:.+)|(\);)")
  20. RE_COMMENTS = re.compile("( {2,}# )")
  21. RE_OPERATORS = re.compile("( = )|( \+= )|( -= )|( == )|( < )|( > )|( != )|"
  22. "( <> )|( <= )|( >= )|( \*= )|( /= )|( %= )")
  23.  
  24.  
  25. class Validator:
  26. def __init__(self, filename):
  27. self.filename = filename
  28. self.lines = self.get_lines()
  29.  
  30. def get_state(self, s, previous_state, state):
  31. new_state = STATE_CODE
  32. for regexp in STATES:
  33. if regexp.match(s):
  34. new_state = STATES[regexp]
  35. break
  36. if state == new_state:
  37. return previous_state, state
  38. return state, new_state
  39.  
  40. def get_lines(self):
  41. try:
  42. with open(self.filename, "r") as file:
  43. res = []
  44. line = file.readline()
  45. while line:
  46. res.append(line.strip())
  47. line = file.readline()
  48. return res
  49. except Exception as e:
  50. print(e)
  51.  
  52. def get_empty_lines_counts(self, i):
  53. empty_lines_count = 0
  54. for j in range(i+1, i+3, 1):
  55. line = self.lines[j]
  56. if not line:
  57. empty_lines_count += 1
  58. else:
  59. break
  60. return empty_lines_count
  61.  
  62. def validate(self):
  63. change_state = True
  64. previous_state, state = None, None
  65. for i in range(len(self.lines)):
  66. line = self.lines[i]
  67. if change_state:
  68. previous_state, state = self.get_state(line, previous_state, state)
  69. change_state = False
  70. if not self.validate_length(line):
  71. print("Line {}: Length > 79".format(i+1))
  72. if state == STATE_IMPORT:
  73. if self.check_import(line) and self.check_from_import(line):
  74. print("Line {}: Incorrect import".format(i+1))
  75. empty = self.get_empty_lines_counts(i)
  76. if empty != 0:
  77. if empty == 1:
  78. print("Line {}: Incorrect empty lines count".format(i+1))
  79. i += empty
  80. change_state = True
  81. if state == STATE_GLOBALS:
  82. if not self.check_globals(line):
  83. print("Line {}: Incorrect globals".format(i+1))
  84. empty = self.get_empty_lines_counts(i)
  85. if empty != 0:
  86. if empty == 1:
  87. print("Line {}: Incorrect empty lines count".format(i + 1))
  88. i += empty
  89. change_state = True
  90. if state == STATE_CLASS:
  91. if not self.check_class(line):
  92. print("Line {}: Incorrect class".format(i + 1))
  93. empty = self.get_empty_lines_counts(i)
  94. if empty != 0:
  95. if empty == 1:
  96. print("Line {}: Incorrect empty lines count".format(i + 1))
  97. i += empty
  98. change_state = True
  99. if state == STATE_FUNCTION:
  100. if not self.check_function(line):
  101. print("Line {}: Incorrect function".format(i + 1))
  102. empty = self.get_empty_lines_counts(i)
  103. if empty != 0:
  104. if previous_state == STATE_CLASS and empty != 1:
  105. print("Line {}: Expected 1 empty line".format(i + 1))
  106. elif empty != 2:
  107. print("Line {}: Expected 2 empty line".format(i + 1))
  108. i += empty
  109. change_state = True
  110. if state == STATE_CODE:
  111. pass
  112.  
  113.  
  114.  
  115.  
  116. def validate_length(self, s):
  117. return len(s) <= 79
  118.  
  119. def check_import(self, s):
  120. result = RE_IMPORT.match(s)
  121. if result is None:
  122. return False
  123. end = result.group(0)
  124. return end == s
  125.  
  126. def check_from_import(self, s):
  127. result = RE_FROM_IMPORT.match(s)
  128. if result is None:
  129. return False
  130. end = result.group(0)
  131. return end == s
  132.  
  133. def check_class(self, s):
  134. result = RE_CLASS.match(s)
  135. if result is None:
  136. return False
  137. end = result.group(0)
  138. print(end)
  139. return end == s
  140.  
  141. def check_globals(self, s):
  142. result = RE_GLOBALS.match(s)
  143. if result is None:
  144. return False
  145. end = result.group(0)
  146. print(end)
  147. return end == s
  148.  
  149. def check_function(self, s):
  150. result = RE_FUNCTION.match(s)
  151. if result is None:
  152. return False
  153. end = result.group(0)
  154. print(end)
  155. return end == s
  156.  
  157. def check_tabs(self, s):
  158. result = RE_TABS.match(s)
  159. if result is None:
  160. return False
  161. end = result.group(0)
  162. print(end)
  163. return end == s
  164.  
  165. def check_operators(self, s):
  166. result = RE_OPERATORS.match(s)
  167. if result is None:
  168. return False
  169. end = result.group(0)
  170. print(end)
  171. return end == s
  172.  
  173. def check_instructions(self, s):
  174. result = RE_INSTRUCTIONS.match(s)
  175. if result is None:
  176. return True
  177. end = result.group(0)
  178. print(end)
  179. return end != s
  180.  
  181. def check_comments(self, s):
  182. result = RE_COMMENTS.search(s)
  183. if result is None:
  184. return False
  185. return True
  186.  
  187.  
  188. if __name__ == '__main__':
  189. v = Validator(None)
  190. s = "a = p + 1 # ssasd "
  191. res = v.check_comments(s)
  192. print(res)
  193.  
  194.  
  195.  
  196. import argparse
  197. from validator import Validator
  198.  
  199.  
  200. def parse_args():
  201. parser = argparse.ArgumentParser()
  202. parser.add_argument('-f', "--file", type=str, help="Path to file", required=True)
  203. args = parser.parse_args()
  204. return args.file
  205.  
  206.  
  207. if __name__ == '__main__':
  208. filename = parse_args()
  209. v = Validator(filename)
  210. v.validate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement