-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenvsetup.py
76 lines (59 loc) · 2.1 KB
/
envsetup.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
"""This module is associated to the latex notes template.
It is needed to:
- clean the tikzit styles file
- create the temporary folder for the tikz images
- delete the temporary folder
- delete the temporary files associated with the tex document
"""
from sys import argv
from re import M, sub
from pathlib import Path
# constants
TEMP_FOLDERS = ["tikztemp"]
TEMP_EXTENSIONS = ["aux", "auxlock", "log", "pdf", "out", "toc"]
PLACEHOLDER_EXT = ".placeholder"
ARROW_STYLE = "{Triangle[scale=0.8]}"
def clean_styles() -> None:
"""Clean the tikzstyles file as it was created by tikzit."""
# load styles files
with open("tikzit.tikzstyles", "r") as f:
data = f.read()
# remove tikzit attributes
data = sub("tikzit [a-z]+=(([a-zA-Z]+)|(\{.+\}))(, ){0,1}", "", data)
# remove none fields
data = sub("[a-z]*=none(, ){0,1}", "", data)
# replace arrows
data = sub("[<>]", ARROW_STYLE, data)
# strip comments
data = sub("^%.*\n", "", data, flags=M)
# remove empty lines
data = sub("^\s*$\n", "", data, flags=M)
# save files
with open("tikzstyle.tikzstyles", "w") as f:
f.write(data)
def make_folder() -> None:
"""Create the temporary folder for the tikz images."""
for f in TEMP_FOLDERS:
Path(f).mkdir(parents=True, exist_ok=True)
Path(f"{f}/{PLACEHOLDER_EXT}").touch()
def delete_folder() -> None:
"""Delete the temporary folder, thus deleting all the rendered tikz images."""
for folder in TEMP_FOLDERS:
for file in Path(folder).iterdir():
file.unlink()
Path(folder).rmdir()
def delete_temp() -> None:
"""Delete all the temporary files associated with the tex document images."""
for e in TEMP_EXTENSIONS:
for file in Path(".").glob(f"*.{e}"):
file.unlink(missing_ok=True)
def main(argv: list[str]) -> None:
"""Read the arguments and call the appropriate functions."""
if "deletetemp" in argv:
delete_temp()
delete_folder()
if "cleanstyle" in argv:
clean_styles()
make_folder()
if __name__ == "__main__":
main(argv)