Fix coding style (pycodestyle)

This commit is contained in:
Nicolo P 2022-02-15 17:13:28 +01:00
parent ed3c994421
commit a4441c227b
2 changed files with 34 additions and 27 deletions

View File

@ -4,6 +4,7 @@ import os
import sys import sys
import sty import sty
# TODO Avoid extra fname argument?? # TODO Avoid extra fname argument??
def find_lines(lines: list, pattern: str, fname: str, is_regexp=False) -> list: def find_lines(lines: list, pattern: str, fname: str, is_regexp=False) -> list:
""" """
@ -11,30 +12,31 @@ def find_lines(lines: list, pattern: str, fname: str, is_regexp=False) -> list:
""" """
found_lines = [] found_lines = []
for l in lines: for line in lines:
if is_regexp: if is_regexp:
matches = re.findall(pattern, l) matches = re.findall(pattern, line)
FIND_COND = len(matches) > 0 FIND_COND = len(matches) > 0
else: else:
FIND_COND = l.find(pattern) != -1 FIND_COND = line.find(pattern) != -1
if FIND_COND: if FIND_COND:
l = l.strip() line = line.strip()
found_lines.append(fname + l) found_lines.append(fname + line)
return found_lines return found_lines
def print_usage(): def print_usage():
usage = """ usage = """
Usage: pygrep.py [OPTIONS]... PATTERN [FILE]... Usage: crapgrep.py [OPTIONS]... PATTERN [FILE]...
Example: Example:
pygrep.py -i 'searching' target1.txt target2.txt crapgrep.py -i 'searching' target1.txt target2.txt
OPTIONS: OPTIONS:
-E : PATTERN is an extended regexp -E : PATTERN is a (Python) regexp
-i : case-insensitive search for PATTERN -i : case-insensitive search for PATTERN
-n : print line numbers -n : print line numbers
-r : search recursively in files in the given path -r : search recursively in files in the given path
@ -43,14 +45,16 @@ OPTIONS:
""" """
print(usage) print(usage)
def check_args(args: list) -> bool: def check_args(args: list) -> bool:
""" """
The arguments passed to pygrep.py The arguments passed to crapgrep.py
""" """
if len(args) < 2 or args[0] == '--help': if len(args) < 2 or args[0] == '--help':
return False return False
return True return True
def check_pattern(pattern: str) -> bool: def check_pattern(pattern: str) -> bool:
""" """
Compile pattern and return false if invalid Compile pattern and return false if invalid
@ -58,9 +62,10 @@ def check_pattern(pattern: str) -> bool:
try: try:
re.compile(pattern) re.compile(pattern)
return True return True
except: except re.error:
return False return False
def parse_args(args: list) -> dict: def parse_args(args: list) -> dict:
""" """
Parse cli arguments and return the tree as a dict Parse cli arguments and return the tree as a dict
@ -81,7 +86,6 @@ def parse_args(args: list) -> dict:
] ]
args_tree = dict() args_tree = dict()
options = [] options = []
# Get options # Get options
for arg in args: for arg in args:
@ -113,6 +117,7 @@ def parse_args(args: list) -> dict:
""" """
return args_tree return args_tree
def match_regexp(pattern: str, line: str) -> object: def match_regexp(pattern: str, line: str) -> object:
""" """
Search for pattern in the given line and return Search for pattern in the given line and return
@ -122,6 +127,7 @@ def match_regexp(pattern: str, line: str) -> object:
""" """
return re.search(pattern, line) return re.search(pattern, line)
def process_grep(args_tree: dict) -> list: def process_grep(args_tree: dict) -> list:
lines = [] lines = []
found_lines = [] found_lines = []
@ -154,8 +160,9 @@ def process_grep(args_tree: dict) -> list:
return found_lines return found_lines
# The main program...
def __main__(): # When the script is executed directly...
if __name__ == "__main__":
args = sys.argv[1:] args = sys.argv[1:]
if not check_args(args): if not check_args(args):
@ -170,10 +177,8 @@ def __main__():
sys.exit(1) sys.exit(1)
try: try:
for l in process_grep(args_tree): for line in process_grep(args_tree):
print(l) print(line)
except exceptions.InvalidPattern as e: except exceptions.InvalidPattern as e:
print(e.get_message()) print(e.get_message())
sys.exit(1) sys.exit(1)
__main__()

View File

@ -1,12 +1,14 @@
class InvalidOption(Exception): class InvalidOption(Exception):
def __init__(self, invalid_opt): def __init__(self, invalid_opt):
self.invalid_opt = invalid_opt self.invalid_opt = invalid_opt
def get_message(self): def get_message(self):
return f"pygrep: invalid option -- '{self.invalid_opt}'" return f"crapgrep: invalid option -- '{self.invalid_opt}'"
class InvalidPattern(Exception): class InvalidPattern(Exception):
def __init__(self, pattern): def __init__(self, pattern):
self.invalid = pattern self.invalid = pattern
def get_message(self):
return f"pygrep: invalid pattern -- '{self.invalid}'"
def get_message(self):
return f"crapgrep: invalid pattern -- '{self.invalid}'"