-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.py
91 lines (76 loc) · 2.83 KB
/
todo.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
#!/usr/bin/env python3
"""Find all `@todo` and `TODO` comments in the `.` and `src` directories"""
from dataclasses import dataclass
from os import getenv, listdir, path, walk
from sys import platform
from typing import List
if platform == "win32":
from os import system
system("")
class Style:
RESET = "\u001b[0m"
BOLD = "\u001b[1m"
ITALICIZE = "\u001b[3m"
UNDERLINE = "\u001b[4m"
LABELS = ["@todo", "TODO"]
FILE_TYPES = ["js", "ejs", "ts"]
@dataclass
class ToDo:
filepath: str
message: str
def __repr__(self) -> str:
return f"{Style.BOLD}{self.message}{Style.RESET}\nIn: {Style.UNDERLINE}{self.filepath}{Style.RESET}"
def get_filepaths() -> List[str]:
filepaths = [
path.join(".", f)
for f in listdir(".")
if path.isfile(f) and f.split(".")[-1] in FILE_TYPES
]
for root, dirnames, filenames in walk("src"):
for filename in filenames:
filepaths.append(path.join(root, filename))
return filepaths
def get_todos() -> List[ToDo]:
todos = []
for filepath in get_filepaths():
with open(filepath, "r", encoding="utf-8") as file:
lines = file.readlines()
todo_start = 0
for i in range(0, len(lines)):
if True in [todo in lines[i] for todo in LABELS]:
todo_start = i + 1
message = ""
while "//" in lines[i]:
message = f"{message}\n{lines[i].strip()}"
i += 1
message = message.strip().strip("\n").replace("//", "").strip()
for label in LABELS:
message = message.strip(label)
message = message.removeprefix(":").strip()
if message != "":
_message = list(message)
_message[0] = _message[0].upper()
todos.append(
ToDo(
filepath=f"{filepath}:{todo_start}",
message="".join(_message),
)
)
else:
todos.append(
ToDo(
filepath=f"{filepath}:{todo_start}",
message=f"Line {todo_start}",
)
)
todo_start = 0
return todos
if __name__ == "__main__":
todos = get_todos()
if len(todos) > 0:
for todo in todos:
print(f"{todo}\n")
else:
print(
f"{Style.BOLD}No {Style.RESET}{Style.ITALICIZE if (str(getenv('TERM_PROGRAM')) != 'vscode' or getenv('TERM') is not None) else Style.BOLD}`@todo`{Style.RESET}{Style.BOLD}(s) found.{Style.RESET}"
)