Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- STATE_CODE = 0
- STATE_IMPORT = 1
- STATE_GLOBALS = 2
- STATE_CLASS = 3
- STATE_FUNCTION = 4
- STATES = {re.compile('import |from '): STATE_IMPORT,
- re.compile("[A-Z]+(_[A-Z]*)* = .+"): STATE_GLOBALS,
- re.compile('class '): STATE_CLASS,
- re.compile('def '): STATE_FUNCTION}
- RE_IMPORT = re.compile("(import \w+)")
- RE_FROM_IMPORT = re.compile("from \w+ import (\w+, )*\w+")
- RE_CLASS = re.compile("(class) ([A-Z][a-z]+)+((\(\w+)?(, \w+)*\))*:")
- RE_GLOBALS = re.compile("[A-Z]+(_[A-Z]*)* = .+")
- RE_FUNCTION = re.compile("(def) ((_?[a-z]+(_[a-z]*)*)|(__[a-z]+__))((\(\w+)?(, \w+)*\))*:")
- RE_TABS = re.compile("( )+|\t+|^(\w)")
- RE_INSTRUCTIONS = re.compile("(:.+)|(\);)")
- RE_COMMENTS = re.compile("( {2,}# )")
- RE_OPERATORS = re.compile("( = )|( \+= )|( -= )|( == )|( < )|( > )|( != )|"
- "( <> )|( <= )|( >= )|( \*= )|( /= )|( %= )")
- class Validator:
- def __init__(self, filename):
- self.filename = filename
- self.lines = self.get_lines()
- def get_state(self, s, previous_state, state):
- new_state = STATE_CODE
- for regexp in STATES:
- if regexp.match(s):
- new_state = STATES[regexp]
- break
- if state == new_state:
- return previous_state, state
- return state, new_state
- def get_lines(self):
- try:
- with open(self.filename, "r") as file:
- res = []
- line = file.readline()
- while line:
- res.append(line.strip())
- line = file.readline()
- return res
- except Exception as e:
- print(e)
- def get_empty_lines_counts(self, i):
- empty_lines_count = 0
- for j in range(i+1, i+3, 1):
- line = self.lines[j]
- if not line:
- empty_lines_count += 1
- else:
- break
- return empty_lines_count
- def validate(self):
- change_state = True
- previous_state, state = None, None
- for i in range(len(self.lines)):
- line = self.lines[i]
- if change_state:
- previous_state, state = self.get_state(line, previous_state, state)
- change_state = False
- if not self.validate_length(line):
- print("Line {}: Length > 79".format(i+1))
- if state == STATE_IMPORT:
- if self.check_import(line) and self.check_from_import(line):
- print("Line {}: Incorrect import".format(i+1))
- empty = self.get_empty_lines_counts(i)
- if empty != 0:
- if empty == 1:
- print("Line {}: Incorrect empty lines count".format(i+1))
- i += empty
- change_state = True
- if state == STATE_GLOBALS:
- if not self.check_globals(line):
- print("Line {}: Incorrect globals".format(i+1))
- empty = self.get_empty_lines_counts(i)
- if empty != 0:
- if empty == 1:
- print("Line {}: Incorrect empty lines count".format(i + 1))
- i += empty
- change_state = True
- if state == STATE_CLASS:
- if not self.check_class(line):
- print("Line {}: Incorrect class".format(i + 1))
- empty = self.get_empty_lines_counts(i)
- if empty != 0:
- if empty == 1:
- print("Line {}: Incorrect empty lines count".format(i + 1))
- i += empty
- change_state = True
- if state == STATE_FUNCTION:
- if not self.check_function(line):
- print("Line {}: Incorrect function".format(i + 1))
- empty = self.get_empty_lines_counts(i)
- if empty != 0:
- if previous_state == STATE_CLASS and empty != 1:
- print("Line {}: Expected 1 empty line".format(i + 1))
- elif empty != 2:
- print("Line {}: Expected 2 empty line".format(i + 1))
- i += empty
- change_state = True
- if state == STATE_CODE:
- pass
- def validate_length(self, s):
- return len(s) <= 79
- def check_import(self, s):
- result = RE_IMPORT.match(s)
- if result is None:
- return False
- end = result.group(0)
- return end == s
- def check_from_import(self, s):
- result = RE_FROM_IMPORT.match(s)
- if result is None:
- return False
- end = result.group(0)
- return end == s
- def check_class(self, s):
- result = RE_CLASS.match(s)
- if result is None:
- return False
- end = result.group(0)
- print(end)
- return end == s
- def check_globals(self, s):
- result = RE_GLOBALS.match(s)
- if result is None:
- return False
- end = result.group(0)
- print(end)
- return end == s
- def check_function(self, s):
- result = RE_FUNCTION.match(s)
- if result is None:
- return False
- end = result.group(0)
- print(end)
- return end == s
- def check_tabs(self, s):
- result = RE_TABS.match(s)
- if result is None:
- return False
- end = result.group(0)
- print(end)
- return end == s
- def check_operators(self, s):
- result = RE_OPERATORS.match(s)
- if result is None:
- return False
- end = result.group(0)
- print(end)
- return end == s
- def check_instructions(self, s):
- result = RE_INSTRUCTIONS.match(s)
- if result is None:
- return True
- end = result.group(0)
- print(end)
- return end != s
- def check_comments(self, s):
- result = RE_COMMENTS.search(s)
- if result is None:
- return False
- return True
- if __name__ == '__main__':
- v = Validator(None)
- s = "a = p + 1 # ssasd "
- res = v.check_comments(s)
- print(res)
- import argparse
- from validator import Validator
- def parse_args():
- parser = argparse.ArgumentParser()
- parser.add_argument('-f', "--file", type=str, help="Path to file", required=True)
- args = parser.parse_args()
- return args.file
- if __name__ == '__main__':
- filename = parse_args()
- v = Validator(filename)
- v.validate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement