-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
54 lines (38 loc) · 1.38 KB
/
index.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
# pylint: disable=missing-module-docstring
from pathlib import Path
from src.args import args
from src.exceptions import FolderNotExists, MissingSearchWord
from src.map_folders import map_folders, CHARACTERS
# verify if exists folder
if not args.path_directory.exists():
raise FolderNotExists(f"The folder objective '{args.path_directory}' not exists.")
# create output file
if isinstance(args.path_output, Path) and args.path_output:
with open(args.path_output, "a", encoding="utf-8") as output:
output.write("")
# if exists a search word
if args.search_word:
key_word = r"%{{" + args.search_word + r"}}%"
else:
raise MissingSearchWord("")
# update characters
characters = CHARACTERS
# create map folder
text = (
f"{characters['init']}\n"
+ map_folders(args.path_directory)
+ characters["end_init"]
)
text_final = text
# verify if exists a template and read template to modify
if isinstance(args.template_output, Path) and args.template_output.exists():
with open(args.template_output, "r", encoding="utf-8") as template:
text_final = template.read()
text_final = text_final.replace(key_word, text)
# print by console the text
if args.print:
print(text_final)
# write text_final in path_output
if isinstance(args.path_output, Path):
with open(args.path_output, "w", encoding="utf-8") as output:
output.write(text_final)