generated from rose-pine/rose-pine-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
379 lines (316 loc) · 11.1 KB
/
main.py
File metadata and controls
379 lines (316 loc) · 11.1 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
"""Rose pine theme generator for notepad++."""
import subprocess
from argparse import ArgumentParser
from dataclasses import asdict, dataclass
from enum import StrEnum
from xml.etree import ElementTree as ET
import yaml
@dataclass
class Style:
"""Model for style."""
names: list[str]
fgColor: str | None = None
bgColor: str | None = None
def __post_init__(self):
"""Validate inputs."""
if not self.names:
raise ValueError("names list cannot be empty")
if self.fgColor is None and self.bgColor is None:
raise ValueError("At least one of fgColor or bgColor must be provided")
if self.fgColor and self.fgColor.startswith("$") is False:
raise ValueError("fgColor must start with '$' if provided")
if self.bgColor and self.bgColor.startswith("$") is False:
raise ValueError("bgColor must start with '$' if provided")
self.names = sorted(set(self.names))
def update_fg_bg_color(
element: ET.Element,
style_name: str,
new_fg_color: str | None = None,
new_bg_color: str | None = None,
) -> ET.Element:
"""Update foreground and background colors.
:param element: xml style element
:type element: ET.Element
:param style_name: style name to update color
:type style_name: str
:param new_fg_color: new foreground color, defaults to None
:type new_fg_color: str | None, optional
:param new_bg_color: new background color, defaults to None
:type new_bg_color: str | None, optional
:return: updated style element with new colors
:rtype: ET.Element
"""
if element.get("name") == style_name:
if new_fg_color is not None:
element.set("fgColor", new_fg_color)
if new_bg_color is not None:
element.set("bgColor", new_bg_color)
return element
def parse_xml_file(file_path: str) -> ET.Element:
"""Parse xml file and return root.
:param file_path: path of xml file
:type file_path: str
:return: root of xml file
:rtype: ET.Element
"""
tree = ET.parse(file_path) # noqa: S314
root = tree.getroot()
return root
def get_lexer_styles(root: ET.Element) -> ET.Element:
"""Get lexer styles from source xml root.
:param root: xml root element.
:type root: ET.Element
:return: lexer styles xml element
:rtype: ET.Element
"""
lexer_styles = root.find("LexerStyles")
if lexer_styles is not None:
return lexer_styles
raise ValueError("LexerStyles element not found")
def get_global_styles(root: ET.Element) -> ET.Element:
"""Get global styles form source xml root.
:param root: xml root element
:type root: ET.Element
:return: global styles xml element
:rtype: ET.Element
"""
global_styles = root.find("GlobalStyles")
if global_styles is not None:
return global_styles
raise ValueError("GlobalStyles element not found")
def get_lexer_types(lexer_styles: ET.Element) -> list[ET.Element]:
"""Get lexer types from lexer styles.
:param lexer_styles: lexer styles xml element
:type lexer_styles: ET.Element
:return: lexer types xml element
:rtype: list[ET.Element]
"""
return lexer_styles.findall("LexerType")
def get_words_styles(lexer_type: ET.Element) -> list[ET.Element]:
"""Get words styles from lexer type.
:param lexer_type: lexer type xml element
:type lexer_type: ET.Element
:return: words styles xml element
:rtype: list[ET.Element]
"""
return lexer_type.findall("WordsStyle")
def get_widget_styles(global_styles: ET.Element) -> list[ET.Element]:
"""Get widget styles from global styles.
:param global_styles: global styles xml element
:type global_styles: ET.Element
:return: widget styles xml element
:rtype: list[ET.Element]
"""
return global_styles.findall("WidgetStyle")
def get_distinct_style_names(lexer_styles: ET.Element) -> list[str]:
"""Get distinct style names.
:param lexer_styles: lexer styles xml element
:type lexer_styles: ET.Element
:return: list of distinct style names
:rtype: list[str]
"""
distinct_style_names = set()
for lexer_type in get_lexer_types(lexer_styles):
for style in lexer_type.findall("WordsStyle"):
distinct_style_names.add(style.get("name"))
return sorted(distinct_style_names)
def get_distinct_missing_style_names(
lexer_styles: ET.Element,
styles: list[Style],
) -> list[str]:
"""Get distinct missing style names from config compared to source file.
:param lexer_styles: lexer styles xml element
:type lexer_styles: ET.Element
:param styles: styles
:type styles: list[Style]
:return: list of distinct missing style names
:rtype: list[str]
"""
existing_style_names = set(get_distinct_style_names(lexer_styles))
config_style_names = set()
for words_style_config in styles:
for name in words_style_config.names:
config_style_names.add(name)
missing_names = existing_style_names - config_style_names
return sorted(missing_names)
def load_styles_from_config(config_file_path: str) -> list[Style]:
"""Load styles from config.
:param config_file_path: config file path
:type config_file_path: str
:return: styles
:rtype: list[Style]
"""
styles: list[Style] = []
with open(config_file_path, encoding="utf-8") as config_file:
config_data = yaml.safe_load(config_file)
for data in config_data.get("styles", []):
styles.append(
Style(
names=data.get("names", []),
fgColor=data.get("fgColor"),
bgColor=data.get("bgColor"),
)
)
return styles
def format_config_file(
config_file_path: str,
styles: list[Style],
) -> None:
"""Format and save config file.
:param config_file_path: config file path
:type config_file_path: str
:param styles: styles
:type styles: list[Style]
"""
config_data = {"styles": [asdict(style) for style in styles]}
with open(config_file_path, "w", encoding="utf-8") as config_file:
yaml.dump(
config_data,
config_file,
sort_keys=True,
indent=2,
)
def create_or_update_template(
source_file_path: str,
target_file_path: str,
config_file_path: str,
) -> None:
"""Create or update template file from the source file.
:param source_file_path: source file path
:type source_file_path: str
:param target_file_path: target file path
:type target_file_path: str
:param config_file_path: config file path
:type config_file_path: str
"""
styles = load_styles_from_config(config_file_path)
# parse and update XML file
root = parse_xml_file(source_file_path)
# update lexer styles
lexer_styles = get_lexer_styles(root)
lexer_types = get_lexer_types(lexer_styles)
for lexer_type in lexer_types:
words_styles = get_words_styles(lexer_type)
for words_style in words_styles:
for style in styles:
for name in style.names:
_ = update_fg_bg_color(
words_style,
name,
style.fgColor,
style.bgColor,
)
# update global styles
global_styles = get_global_styles(root)
widget_styles = get_widget_styles(global_styles)
for widget_style in widget_styles:
for style in styles:
for name in style.names:
_ = update_fg_bg_color(
widget_style,
name,
style.fgColor,
style.bgColor,
)
# write updated XML to target file
tree = ET.ElementTree(root)
tree.write(
target_file_path,
encoding="utf-8",
xml_declaration=True,
short_empty_elements=False,
)
class Command(StrEnum):
"""Command options."""
create_or_update_template = "create-or-update-template"
build_theme_variants = "build-theme-variants"
format_config_file = "format-config-file"
get_distinct_style_names = "get-distinct-style-names"
get_distinct_missing_style_names = "get-distinct-missing-style-names"
def create_arg_parser() -> ArgumentParser:
"""Create argument parser with various arguments.
:return: argument parser
:rtype: ArgumentParser
"""
parser = ArgumentParser(
description="Create or update Notepad++ style template XML file based on a "
"configuration YAML file."
)
parser.add_argument(
"command",
type=Command,
help="Command to execute.",
choices=list(Command),
)
parser.add_argument(
"--source",
type=str,
help="Path to the source stylers.xml file.",
default="stylers.xml",
)
parser.add_argument(
"--target",
type=str,
help="Path to the target template XML file to be created or updated.",
default="template.xml",
)
parser.add_argument(
"--config",
type=str,
help="Path to the configuration YAML file.",
default="config.yaml",
)
return parser
def main():
"""Entry point."""
parser = create_arg_parser()
args = parser.parse_args()
match args.command:
case Command.create_or_update_template:
create_or_update_template(
source_file_path=args.source,
target_file_path=args.target,
config_file_path=args.config,
)
case Command.build_theme_variants:
subprocess.run( # noqa: S603
[ # noqa: S607
"npx",
"@rose-pine/build",
"-t",
args.target,
"-o",
".",
"-f",
"hex-ns",
],
check=True,
)
case Command.format_config_file:
styles = load_styles_from_config(args.config)
format_config_file(
config_file_path=args.config,
styles=styles,
)
case Command.get_distinct_style_names:
root = parse_xml_file(args.source)
lexer_styles = get_lexer_styles(root)
distinct_style_names = get_distinct_style_names(lexer_styles)
print("Distinct style names:")
for name in distinct_style_names:
print(name)
print(f"Distinct style count: {len(distinct_style_names)}")
case Command.get_distinct_missing_style_names:
styles = load_styles_from_config(args.config)
root = parse_xml_file(args.source)
lexer_styles = get_lexer_styles(root)
missing_style_names = get_distinct_missing_style_names(
lexer_styles,
styles,
)
print("Missing style names:")
for name in missing_style_names:
print(name)
print(f"Missing style count: {len(missing_style_names)}")
if __name__ == "__main__":
main()