-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyrmont.py
146 lines (124 loc) · 3.97 KB
/
pyrmont.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
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
Static skin generator for Rainmeter.
Example of template:
[[
for i in range(25):
e(f'''
[MeasureBand{i}]
Measure=Plugin
Plugin=AudioLevel
Parent=MeasureAudio
Type=Band
BandIdx={i}
''')
]]
"""
import logging
from interpreter import InterpreterError, execute_macro
from pathlib import Path
from typing import Union, List
from macro import Macro
def find_pyrm_files_in(directory: Path):
return [x for x in directory.rglob("*.pyrm")]
def parse_pyrm(in_lines: List[str], pyrm_path: Path) -> List[Union[str, Macro]]:
"""
Reads .pyrm file line-by-line and wraps
all found macros into objects.
"""
out_lines = []
gen_span_start = -1
in_span = False
for i, raw_line in enumerate(in_lines):
line = raw_line.strip()
if line == "[[":
if in_span:
logging.error(f"Macros cannot be nested (line {i + 1})")
exit(3)
in_span = True
gen_span_start = i
elif in_span:
if line == "]]":
in_span = False
indent_size = len(in_lines[gen_span_start]) - len(in_lines[gen_span_start].lstrip())
out_lines.append(Macro(
gen_span_start + 1,
in_lines[gen_span_start + 1: i],
indent_size,
pyrm_path
))
else:
out_lines.append(raw_line)
return out_lines
def write_skin_config_file(file_path: Path):
"""
Generates .ini file from .pyrm file.
"""
import io
from textwrap import dedent, indent
try:
f = io.open(str(file_path), encoding="utf-16")
in_lines = f.readlines()
except UnicodeError:
f = open(str(file_path))
in_lines = f.readlines()
f.close()
out_lines = parse_pyrm(in_lines, file_path)
out_file = file_path.with_suffix(".ini")
logging.info("-- Writing .ini and executing macros...")
with io.open(str(out_file), "w+", newline="", encoding="utf-16") as f:
f.write(dedent("""\
; This config is autogenerated with pyrmont static skin generator.
; It's not recommended to edit this file manually.
"""))
for line in out_lines:
if isinstance(line, Macro):
try:
result = execute_macro(line)
except InterpreterError as e:
logging.error(e)
exit(4)
f.write(indent(
dedent("".join(result)),
" " * line.indent_size
))
else:
f.write(line)
def main():
import sys
logging.basicConfig(format="%(message)s", level=logging.INFO)
# processing arguments
if len(sys.argv) < 2:
logging.error(
"1-st argument should be a path to Rainmeter skin pack directory."
)
exit(1)
skin_dir = Path(sys.argv[1])
if not skin_dir.exists():
logging.error("Specified directory does not exists.")
exit(2)
if len(sys.argv) > 2:
if "silent" in sys.argv[2]:
logging.getLogger().setLevel(logging.ERROR)
if "debug" in sys.argv[2]:
logging.getLogger().setLevel(logging.DEBUG)
logging.info(
r"""
_
| |
_ __ _ _ _ __ _ __ ___ ___ _ __ | |_
| '_ \| | | | '__| '_ ` _ \ / _ \| '_ \| __|
| |_) | |_| | | | | | | | | (_) | | | | |_
| .__/ \__, |_| |_| |_| |_|\___/|_| |_|\__|
| | __/ |
|_| |___/ by @f1uctus
"""[1:]
)
logging.info("Working in " + str(skin_dir.absolute()))
logging.info("Processing .pyrm-s:")
pyrm_file_paths = find_pyrm_files_in(skin_dir)
for pyrm_path in pyrm_file_paths:
logging.info(pyrm_path)
write_skin_config_file(pyrm_path)
logging.info("All done ;)")
if __name__ == "__main__":
main()