Skip to content

Commit 5969094

Browse files
committed
new param, refactored
1 parent 14d619b commit 5969094

File tree

5 files changed

+50
-49
lines changed

5 files changed

+50
-49
lines changed
Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,45 @@
11
import argparse
22
import custom_log as l
33

4+
_dir_path: str = None
45
_file_path: str = None
5-
_keyword: str = None
6-
7-
def get_path() -> str:
8-
global _file_path
9-
if _file_path:
10-
return _file_path
11-
else:
12-
_get_arguments()
13-
return _file_path
146

157

16-
def get_keyword() -> str:
17-
global _keyword
18-
if _keyword:
19-
return _keyword
8+
def get_dir_path() -> str:
9+
global _dir_path
10+
if _dir_path:
11+
return _dir_path
2012
else:
2113
_get_arguments()
22-
return _keyword
14+
return _dir_path
2315

2416

25-
def get_print_line() -> bool:
26-
global _print_line
27-
if _print_line:
28-
return _print_line
17+
def get_file_path() -> str:
18+
global _file_path
19+
if _file_path:
20+
return _file_path
2921
else:
3022
_get_arguments()
31-
return _print_line
23+
return _file_path
3224

3325

3426
def _get_arguments():
3527
l.log("parsing arguments")
28+
global _dir_path
3629
global _file_path
37-
global _keyword
38-
global _ext
39-
global _print_line
40-
4130
parser = argparse.ArgumentParser()
4231

4332
parser.add_argument('--path',
44-
type=str, help='address from where tex files will be scanned',
45-
required=True)
33+
type=str, help='address from where tex files will be scanned')
34+
parser.add_argument('--file',
35+
type=str, help='address of tex file')
4636

4737
args = parser.parse_args()
38+
if args.path:
39+
_dir_path = args.path
40+
l.log("dir path: " + _dir_path)
41+
if args.file:
42+
_file_path = args.file
43+
l.log("file path: " + _file_path)
44+
4845

49-
_file_path = args.path
50-
l.log("path: "+_file_path)

tex_writing_issues/folder_walk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def walk() -> List:
1212
:return: List of string path of images
1313
"""
1414
file_paths = []
15-
path: str = argh.get_path()
15+
path: str = argh.get_dir_path()
1616
l.log(path)
1717
l.log("checking path integrity")
1818
extension: str = ".tex"

tex_writing_issues/line_checker.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ def check_line(line: str, rules_packs: List):
2020
regex = re.compile(r"\b{}\b".format(keyword))
2121
if len(regex.findall(line)) != 0:
2222
print("""
23-
issue found: {}
23+
issue type: {}, issue: {}
2424
line: {}
25-
issue: {}
26-
replace with: {}""".format(rule_name, line, keyword, rule[1:]))
27-
count_issues+=1
25+
replace with: {}""".format(rule_name, keyword, line, rule[1:]))
26+
count_issues += 1
2827
return count_issues

tex_writing_issues/main.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,34 @@ def will_i_process_line(line: str) -> bool:
1414
return True
1515

1616

17+
def process_file(file_path: str, all_rules: List):
18+
count_issues: int = 0
19+
print("at file: " + file_path)
20+
lines: List[str] = open(file_path).readlines()
21+
for line in lines:
22+
if will_i_process_line(line):
23+
line_issues = line_checker.check_line(line, all_rules)
24+
count_issues += line_issues
25+
print("done with: " + file_path)
26+
print("total issues found: " + str(count_issues))
27+
28+
1729
if __name__ == '__main__':
1830
import folder_walk as walk
1931
import argument_handler as argh
2032
from typing import List
2133

2234
l.disable()
23-
24-
keyword = argh.get_keyword()
25-
l.log("started parsing directories")
26-
file_paths: List[str] = walk.walk()
27-
l.log("will start scanning files now.")
2835
all_rules = rule_builder.get_rules()
29-
30-
for file_path in file_paths:
31-
count_issues: int = 0
32-
print("at file: " + file_path)
33-
lines: List[str] = open(file_path).readlines()
34-
for line in lines:
35-
if will_i_process_line(line):
36-
line_issues = line_checker.check_line(line, all_rules)
37-
count_issues += line_issues
38-
print("done with: " + file_path)
39-
print("total issues found: " + str(count_issues))
36+
if argh.get_file_path() is not None:
37+
print("single file given : "+argh.get_file_path())
38+
l.log("single file input")
39+
process_file(argh.get_file_path(), all_rules)
40+
elif argh.get_dir_path() is not None:
41+
l.log("directory is given. scanning files from directory")
42+
file_paths: List[str] = walk.walk()
43+
for file_path in file_paths:
44+
process_file(file_path, all_rules)
45+
print()
46+
else:
47+
print("neither path nor file was declared. Run with -h switch for help")

tex_writing_issues/rule_builder.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def _build_rules(rule_names: List) -> List:
1313
path_rules = "/home/amit/git/automating-boring-tasks-using-python/tex_writing_issues/"
1414
all_rules: List = []
1515
for rule_name in rule_names:
16-
1716
l.log("working with rule: " + rule_name)
1817
f_rule = path_rules + "words_" + rule_name
1918
l.log("rule file name: " + f_rule)

0 commit comments

Comments
 (0)