-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtodo_parser.py
133 lines (110 loc) · 4.42 KB
/
todo_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import os
import re
class TodoItem:
def __init__(self, file_path, line_number, content) -> None:
self.file_path = file_path
self.line_number = line_number
self.content = content.replace("*/}", "").strip()
self.id = self.get_id()
def get_id(self):
id_regex = r"(NO-)(\d+)(\s)?(:\s)?"
if id_match := re.search(id_regex, self.content):
self.content = self.content.replace(str(id_match.start()), "")
return int(id_match.group(2))
else:
return None
def assign_id(self, id):
self.id = id
try:
with open(self.file_path, 'r', encoding='utf-8', errors='ignore') as f:
lines = f.readlines()
insert = f"TODO NO-{self.id} {self.content}\n"
lines[self.line_number - 1] = lines[self.line_number - 1].split("TODO", 1)[0] + insert
with open(self.file_path, 'w', encoding='utf-8', errors='ignore') as f:
f.writelines(lines)
except Exception as e:
print(e)
def to_dict(self):
return {
'file_path': self.file_path,
'line_number': self.line_number,
'content': self.content,
'id': self.id
}
def __str__(self) -> str:
return f'{self.file_path}:{self.line_number}\n{self.content}'
class TodoFinder:
def __init__(self, root_path, blacklist) -> None:
self.root_path = root_path
self.todos = []
self.blacklist = blacklist
self.id_pool = []
def find_todos(self):
for root, dirs, files in os.walk(self.root_path):
if self.path_checker(root, ""): continue
try:
for file in files:
if self.path_checker(root, file): continue
file_path = os.path.join(root, file)
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
for line_number, line in enumerate(f, 1):
if 'TODO' in line:
todo = TodoItem(file_path, line_number, line.split("TODO", 1)[1].strip())
self.todos.append(todo)
if todo.id is not None: self.id_pool.append(todo.get_id())
except Exception as e:
print(e)
continue
def assign_ids(self):
if len(self.id_pool) == 0: return
counter = max(self.id_pool) + 1
for todo in self.todos:
if todo.id is None:
todo.assign_id(counter)
counter += 1
def print_todos(self):
for todo in self.todos:
if len(todo.content) > 200:
todo.content = todo.content[:100] + "..."
print(f"TODO NO-{todo.id} {todo}\n")
def print_paths(self):
for todo in self.todos:
print(todo.file_path)
def get_length(self):
return len(self.todos)
def print_length(self):
print(self.get_length())
def write_to_json(self, filename):
dict_list = [todo.to_dict() for todo in self.todos]
with open(filename, 'w') as f:
json.dump(dict_list, f, indent=4)
def write_to_html(self, template_filename, insert_content_tag, insert_length_tag, output_filename='todo-sync/public/index.html', link_base='https://github.com/DeutscheModelUnitedNations/munify/blob/main/'):
content = ""
length = self.get_length()
for todo in self.todos:
content += f'<li><p><a href="{link_base + todo.file_path.replace("./", "")}">{todo.file_path}</a>:{todo.line_number}</p><p>{todo.content}</p></li>'
with open(template_filename, 'r') as f:
html = f.read()
html = html.replace(insert_content_tag, content)
html = html.replace(insert_length_tag, str(length))
with open(output_filename, 'w') as f:
f.write(html)
def path_checker(self, root, file):
for blacklisted in self.blacklist:
if blacklisted in os.path.join(root, file):
return True
return False
if __name__ == '__main__':
todo_finder = TodoFinder('.', [
"node_modules",
"todo-sync",
".next",
".git",
".vscode",
".postgres-data",
])
todo_finder.find_todos()
todo_finder.assign_ids()
todo_finder.print_paths()
todo_finder.print_length()