-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme_build.py
More file actions
548 lines (455 loc) · 14.2 KB
/
Copy paththeme_build.py
File metadata and controls
548 lines (455 loc) · 14.2 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
Copyright (c) 2026, GrandBIRDLizard
BSD 3-Clause, All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of Your Name nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#!/usr/bin/env python3
#Pythonic-Palette_Gen v0.0.2
"""
theme scaffold + palette consumer.
Design:
- Import palette_gen as a module (same interpreter, same process).
- Generate palette artifacts into the theme tree.
- Write minimal consumer files for GTK2 / GTK3.
- Do not overwrite user widget modules unless asked.
This is the "assembler" layer.
palette_gen.py remains the palette authority.
"""
from __future__ import annotations
import argparse
from pathlib import Path
from typing import Iterable
# Palette engine imports (same-process module import)
from palette_gen import (
generate_base16_palette,
base16_text,
css_palette_text,
gtk2_text,
)
DEFAULT_THEME_ROOT = Path.home() / ".local" / "share" / "themes" / "Dorakura-Kyoto"
WIDGET_FILES = [
"20-buttons.css",
"30-entries.css",
"40-headerbar.css",
"50-menus.css",
"60-notebook.css",
"70-controls.css",
"80-lists-sidebar.css",
"90-app-nemo.css",
"99-dorakura-overrides.css",
]
# Small filesystem helpers
def ensure_dir(path: Path) -> None:
"""mkdir -p equivalent."""
path.mkdir(parents=True, exist_ok=True)
def write_text(path: Path, text: str, overwrite: bool = True) -> bool:
"""
Write text to a file.
Returns:
True -> file written
False -> skipped because file exists and overwrite=False
"""
ensure_dir(path.parent)
if path.exists() and not overwrite:
return False
if not text.endswith("\n"):
text += "\n"
path.write_text(text, encoding="utf-8")
return True
def touch_if_missing(path: Path) -> bool:
"""
Create an empty file only if it does not exist.
Returns:
True -> created
False -> already existed
"""
ensure_dir(path.parent)
if path.exists():
return False
path.touch()
return True
# Theme tree layout
def ensure_theme_tree(theme_root: Path) -> None:
"""
Create the minimal sane owned theme tree.
"""
dirs = [
theme_root,
theme_root / "gtk-2.0",
theme_root / "gtk-3.0",
theme_root / "gtk-3.0" / "assets",
theme_root / "gtk-3.0" / "widgets",
]
for d in dirs:
ensure_dir(d)
def ensure_widget_placeholders(theme_root: Path) -> None:
"""
Create empty widget module files if missing.
Do not clobber user work.
"""
widgets_dir = theme_root / "gtk-3.0" / "widgets"
# 00-palette.css is generated elsewhere, but make sure path exists if user runs partial flows.
touch_if_missing(widgets_dir / "00-palette.css")
for name in WIDGET_FILES:
touch_if_missing(widgets_dir / name)
# Template generators
def index_theme_text(theme_name: str = "Theme palette") -> str:
"""
Minimal GTK theme metadata.
"""
return f"""[Desktop Entry]
Type=X-GNOME-Metatheme
Name={theme_name}
Comment=Owned dark GTK theme scaffold for Dorakura-Kyoto
GtkTheme={theme_name}
"""
def gtk2_gtkrc_text() -> str:
"""
Minimal GTK2 consumer file.
Pulls in generated colors.rc.
Keep this intentionally small.
"""
return """# Auto-generated minimal GTK2 consumer for Dorakura-Kyoto
# colors.rc is generated by theme_build.py / palette_gen.py
include "colors.rc"
# Minimal defaults for old GTK2 apps
style "dorakura-default"
{
fg[NORMAL] = @base05
fg[ACTIVE] = @base06
fg[PRELIGHT] = @base07
fg[SELECTED] = @base00
fg[INSENSITIVE] = @base04
bg[NORMAL] = @base00
bg[ACTIVE] = @base01
bg[PRELIGHT] = @base02
bg[SELECTED] = @base0E
bg[INSENSITIVE] = @base01
text[NORMAL] = @base06
text[SELECTED] = @base00
text[INSENSITIVE] = @base04
base[NORMAL] = @base01
base[ACTIVE] = @base02
base[PRELIGHT] = @base02
base[SELECTED] = @base0E
base[INSENSITIVE] = @base01
}
class "*" style "dorakura-default"
"""
def gtk3_gtk_css_text(widget_files: Iterable[str]) -> str:
"""
Main GTK3 entrypoint.
Imports generated palette first, then widget modules.
"""
lines = [
"/* Auto-generated minimal GTK3 entrypoint for Dorakura-Kyoto */",
'@import url("widgets/00-palette.css");',
"",
]
for name in widget_files:
lines.append(f'@import url("widgets/{name}");')
lines.append("")
return "\n".join(lines)
def gtk3_gtk_dark_css_text() -> str:
"""
Dark-only theme.
Keep gtk-dark.css as a simple local import, not a resource import.
"""
return """/* Dark-only entrypoint for Dorakura-Kyoto */
@import url("gtk.css");
"""
def widget_stub_text(name: str) -> str:
"""
Small starter stubs so files are not blank.
Safe placeholders you can expand later.
"""
stubs = {
"20-buttons.css": """/* Buttons */
button,
.button {
background-color: @card_bg_color;
color: @window_fg_color;
border: 1px solid @border_color;
border-radius: 6px;
box-shadow: none;
}
button:hover,
.button:hover {
background-color: alpha(@accent_alt, 0.12);
border-color: @accent_alt;
}
button:checked,
button:active,
.button:checked,
.button:active {
background-color: alpha(@accent_color, 0.20);
color: @heading_fg_color;
border-color: @accent_color;
}
button:disabled,
.button:disabled {
color: @insensitive_fg_color;
border-color: alpha(@border_color, 0.60);
}
""",
"30-entries.css": """/* Entries / inputs */
entry,
.entry {
background-color: @theme_base_color;
color: @theme_text_color;
border: 1px solid @border_color;
border-radius: 6px;
box-shadow: none;
}
entry:focus,
.entry:focus {
border-color: @accent_color;
}
""",
"40-headerbar.css": """/* Headerbars / toolbars */
headerbar,
.titlebar,
.toolbar {
background-color: @view_bg_color;
color: @window_fg_color;
border-bottom: 1px solid alpha(@border_color, 0.65);
}
""",
"50-menus.css": """/* Menus / popovers */
menu,
popover,
.popup {
background-color: @card_bg_color;
color: @window_fg_color;
border: 1px solid alpha(@border_color, 0.75);
}
""",
"60-notebook.css": """/* Tabs / notebook */
notebook,
.notebook {
background-color: @window_bg_color;
}
notebook tab,
.notebook tab {
background-color: @view_bg_color;
color: @muted_fg_color;
}
notebook tab:checked,
.notebook tab:checked {
background-color: alpha(@accent_alt, 0.12);
color: @heading_fg_color;
}
""",
"70-controls.css": """/* Checks / radios / switches (minimal first pass) */
check,
radio,
switch {
color: @window_fg_color;
}
check:checked,
radio:checked,
switch:checked {
color: @accent_color;
}
""",
"80-lists-sidebar.css": """/* Lists / sidebars */
list,
treeview,
sidebar {
background-color: @view_bg_color;
color: @window_fg_color;
}
list row:selected,
treeview:selected,
.sidebar row:selected {
background-color: alpha(@accent_color, 0.18);
color: @heading_fg_color;
}
""",
"90-app-nemo.css": """/* Nemo-focused tweaks (first-pass) */
.nemo-window,
.nemo-desktop-window,
.nemo-canvas-item {
color: @window_fg_color;
}
""",
"99-Theme-overrides.css": """/* Final local overrides */
*:selected {
background-color: alpha(@accent_color, 0.18);
color: @heading_fg_color;
}
""",
}
return stubs.get(name, f"/* {name} */\n")
# Palette generation + writes
def build_palette_files(
theme_root: Path,
image: Path,
scheme_name: str,
author: str,
quantize: int,
resize: int,
dedupe_distance: float,
surface_style: str,
) -> None:
"""
Generate palette once, serialize to all theme-consumer formats.
"""
palette = generate_base16_palette(
img_path=image,
quantize_colors=quantize,
resize_to=resize,
dedupe_distance=dedupe_distance,
surface_style=surface_style,
)
color_ini = base16_text(palette, scheme_name=scheme_name, author=author)
gtk3_css = css_palette_text(palette)
gtk2_rc = gtk2_text(palette)
write_text(theme_root / "color.ini", color_ini, overwrite=True)
write_text(theme_root / "gtk-3.0" / "widgets" / "00-palette.css", gtk3_css, overwrite=True)
write_text(theme_root / "gtk-2.0" / "colors.rc", gtk2_rc, overwrite=True)
# Theme consumer file writes
def build_consumer_files(theme_root: Path, theme_name: str, force: bool) -> None:
"""
Write minimal theme consumer files.
These are safe and intentionally small.
"""
write_text(theme_root / "index.theme", index_theme_text(theme_name), overwrite=force)
write_text(theme_root / "gtk-2.0" / "gtkrc", gtk2_gtkrc_text(), overwrite=force)
write_text(
theme_root / "gtk-3.0" / "gtk.css",
gtk3_gtk_css_text(WIDGET_FILES),
overwrite=force,
)
write_text(
theme_root / "gtk-3.0" / "gtk-dark.css",
gtk3_gtk_dark_css_text(),
overwrite=force,
)
def build_widget_stubs(theme_root: Path, force: bool) -> None:
"""
Create widget module files.
Default behavior: only create if missing.
"""
widgets_dir = theme_root / "gtk-3.0" / "widgets"
for name in WIDGET_FILES:
path = widgets_dir / name
if path.exists() and not force:
continue
write_text(path, widget_stub_text(name), overwrite=True)
# CLI
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Build Dorakura-Kyoto theme scaffold and generate palette artifacts."
)
# Required for first-pass build
parser.add_argument("image", help="Path to wallpaper / source image for palette extraction")
# Theme root + metadata
parser.add_argument(
"--theme-root",
default=str(DEFAULT_THEME_ROOT),
help=f"Theme root directory (default: {DEFAULT_THEME_ROOT})"
)
parser.add_argument(
"--theme-name",
default="aaatheme palette",
help="Theme display name (default: Theme palette)"
)
parser.add_argument(
"--scheme-name",
default="palette-Auto",
help="Generated palette scheme name (default: palette-Auto)"
)
parser.add_argument(
"--author",
default="theme_build.py",
help="Generated palette author string"
)
# Palette engine knobs (pass-through to palette_gen)
parser.add_argument("-q", "--quantize", type=int, default=32, help="Quantization color count (default: 32)")
parser.add_argument("-r", "--resize", type=int, default=192, help="Thumbnail max size (default: 192)")
parser.add_argument(
"--dedupe-distance",
type=float,
default=24.0,
help="Minimum RGB distance between kept colors (default: 24.0)"
)
parser.add_argument(
"--surface-style",
choices=["neutral", "tinted"],
default="neutral",
help="Dark surface policy (default: neutral)"
)
parser.add_argument(
"--force-consumers",
action="store_true",
help="Overwrite index.theme / gtkrc / gtk.css / gtk-dark.css"
)
parser.add_argument(
"--force-widgets",
action="store_true",
help="Overwrite widget module stubs (dangerous if you edited them)"
)
return parser.parse_args()
def main() -> int:
args = parse_args()
image = Path(args.image).expanduser().resolve()
theme_root = Path(args.theme_root).expanduser()
if not image.exists():
raise FileNotFoundError(f"Image not found: {image}")
# Ensure directory structure exists.
ensure_theme_tree(theme_root)
# Ensure empty widget placeholders exist before writes.
ensure_widget_placeholders(theme_root)
# Build / refresh generated palette artifacts.
# These are meant to be regenerated often.
build_palette_files(
theme_root=theme_root,
image=image,
scheme_name=args.scheme_name,
author=args.author,
quantize=args.quantize,
resize=args.resize,
dedupe_distance=args.dedupe_distance,
surface_style=args.surface_style,
)
# Build minimal consumer files.
# These are the "entrypoints" for GTK2 / GTK3.
build_consumer_files(
theme_root=theme_root,
theme_name=args.theme_name,
force=args.force_consumers,
)
# Build starter widget stubs only if missing by default.
# Keeps your edits safe unless you explicitly force.
build_widget_stubs(
theme_root=theme_root,
force=args.force_widgets,
)
# Print a small summary so you know where the generated artifacts landed.
print(f"[ok] Theme root: {theme_root}")
print(f"[ok] color.ini: {theme_root / 'color.ini'}")
print(f"[ok] GTK2 colors: {theme_root / 'gtk-2.0' / 'colors.rc'}")
print(f"[ok] GTK3 palette: {theme_root / 'gtk-3.0' / 'widgets' / '00-palette.css'}")
print(f"[ok] GTK3 entry: {theme_root / 'gtk-3.0' / 'gtk.css'}")
print(f"[ok] GTK3 dark entry: {theme_root / 'gtk-3.0' / 'gtk-dark.css'}")
return 0
if __name__ == "__main__":
raise SystemExit(main())