-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgns3theme.py
More file actions
executable file
·1321 lines (1128 loc) · 44.5 KB
/
gns3theme.py
File metadata and controls
executable file
·1321 lines (1128 loc) · 44.5 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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import re
import sys
import json
import struct
import shutil
import subprocess
from pathlib import Path
from copy import deepcopy
from colorschemes import schemes as builtin_schemes
from argparser import parser
import custom_style as css
# ANSI color codes
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
CYAN = '\033[96m'
DIM = '\033[2m'
RESET = '\033[0m'
IS_MACOS = sys.platform == 'darwin'
MACOS_GNS3_APP = Path('/Applications/GNS3.app')
SCRIPT_DIR = Path(__file__).resolve().parent
BUNDLED_SYMBOLS_DIR = SCRIPT_DIR / 'symbols'
GNS3_SYMBOLS_DIR = Path.home() / 'GNS3' / 'symbols'
CONFIG_DIR = Path.home() / '.config' / 'gns3theme'
USER_SCHEMES_DIR = CONFIG_DIR / 'schemes'
CSS_PATH = CONFIG_DIR / 'custom_style.css'
CONFIG_PATH = CONFIG_DIR / 'config.json'
REQUIRED_SCHEME_KEYS = {'bg', 'bg2', 'fg', 'fg2', 'sbg', 'sfg', 'tbg', 'color'}
# Magic number mapping for Python version detection
MAGIC_TO_VERSION = {
3394: '3.8', 3401: '3.8', 3413: '3.9', 3425: '3.10',
3433: '3.10', 3438: '3.10', 3450: '3.11', 3495: '3.12',
3531: '3.13', 3571: '3.13', 3600: '3.14',
}
# --- Utilities ---
def error(tag, msg):
print(f"{RED}{tag}{RESET}: {msg}")
def success(tag, msg):
print(f"{GREEN}{tag}{RESET}: {msg}")
def warn(tag, msg):
print(f"{YELLOW}{tag}{RESET}: {msg}")
def is_valid_color(color):
return bool(re.fullmatch(r'#[0-9a-fA-F]{6}', color))
def mkdir(dir_path):
try:
Path(dir_path).mkdir(parents=True, exist_ok=True)
except OSError as err:
error("MkdirError", str(err))
sys.exit(1)
def save_file(data, file_path):
if not data:
return
try:
with open(file_path, 'w') as fh:
fh.write(data)
except (TypeError, OSError) as err:
error("WriteError", str(err))
sys.exit(1)
success("SaveFile", f"Wrote {file_path}")
def backup_file(file_path, backup_dir):
"""Back up file to the specified backup directory."""
mkdir(backup_dir)
bak = backup_dir / Path(file_path).name
if not bak.exists():
shutil.copy(file_path, bak)
success("Backup", f"Backed up {Path(file_path).name}")
else:
warn("Backup", f"Backup already exists: {bak}")
def load_user_schemes():
"""Load custom schemes from ~/.config/gns3theme/schemes/*.json"""
user = {}
if not USER_SCHEMES_DIR.is_dir():
return user
for f in sorted(USER_SCHEMES_DIR.glob('*.json')):
try:
data = json.loads(f.read_text())
name = f.stem
missing = REQUIRED_SCHEME_KEYS - set(data.keys())
if missing:
warn("Scheme", f"Skipping {f.name}: missing keys {', '.join(sorted(missing))}")
continue
user[name] = data
except (json.JSONDecodeError, OSError) as err:
warn("Scheme", f"Skipping {f.name}: {err}")
return user
def get_all_schemes():
"""Return merged dict of builtin + user schemes (user overrides builtin)."""
all_schemes = dict(builtin_schemes)
all_schemes.update(load_user_schemes())
return all_schemes
def print_colorschemes():
"""Print available schemes grouped by type (dark/light)."""
all_schemes = get_all_schemes()
user_names = set(load_user_schemes().keys())
dark = {k: v for k, v in all_schemes.items() if v.get('color') == 'dark'}
light = {k: v for k, v in all_schemes.items() if v.get('color') == 'light'}
if dark:
print(f"\n {CYAN}dark{RESET}")
for name in dark:
tag = f" {DIM}(custom){RESET}" if name in user_names else ""
print(f" {name}{tag}")
if light:
print(f"\n {CYAN}light{RESET}")
for name in light:
tag = f" {DIM}(custom){RESET}" if name in user_names else ""
print(f" {name}{tag}")
print(f"\n Add custom schemes as JSON files in ~/.config/gns3theme/schemes/\n")
def validate_scheme(name):
all_schemes = get_all_schemes()
if name not in all_schemes:
error("SchemeError", f"Unknown scheme '{name}'. Use --ls to list available schemes")
sys.exit(1)
# --- Path detection ---
def detect_pyc_version(pyc_path):
"""Detect Python version from .pyc magic number."""
try:
with open(pyc_path, 'rb') as f:
magic = struct.unpack('<H', f.read(2))[0]
return MAGIC_TO_VERSION.get(magic)
except Exception:
return None
def find_gns3_lib():
"""Auto-detect the lib directory containing gns3 package.
Returns (path, install_type) where install_type is 'app' or 'source'.
'app' means macOS GNS3.app bundle with .pyc files.
'source' means pip/brew install with .py files (Linux or macOS).
"""
# macOS GNS3.app bundle (cx_Freeze, .pyc files)
if IS_MACOS and MACOS_GNS3_APP.exists():
lib = MACOS_GNS3_APP / 'Contents' / 'Resources' / 'lib'
if (lib / 'gns3' / 'main_window.pyc').exists():
return lib, 'app'
# pip/brew/source install (.py files) — works on both macOS and Linux
for cmd in [
['python3', '-c',
'import importlib.util as u; print(u.find_spec("gns3").submodule_search_locations[0])'],
['pip3', 'show', 'gns3-gui'],
]:
try:
out = subprocess.check_output(cmd, stderr=subprocess.DEVNULL, text=True).strip()
if 'Location:' in out:
for line in out.splitlines():
if line.startswith('Location:'):
out = line.split(':', 1)[1].strip()
break
p = Path(out)
if p.name == 'gns3' and (p / 'main_window.py').exists():
return p.parent, 'source'
if (p / 'gns3' / 'main_window.py').exists():
return p, 'source'
except (subprocess.CalledProcessError, FileNotFoundError, OSError):
continue
# Fallback: scan common locations
home = Path.home()
candidates = []
for pyver in ['3.14', '3.13', '3.12', '3.11', '3.10', '3.9', '3.8']:
candidates.extend([
home / f'.local/lib/python{pyver}/site-packages',
Path(f'/usr/lib/python{pyver}/site-packages'),
Path(f'/usr/lib/python{pyver}/dist-packages'),
Path(f'/usr/local/lib/python{pyver}/site-packages'),
Path(f'/usr/local/lib/python{pyver}/dist-packages'),
Path(f'/usr/lib64/python{pyver}/site-packages'),
])
candidates.append(Path('/usr/lib/python3/dist-packages'))
# Homebrew on macOS
if IS_MACOS:
for pyver in ['3.14', '3.13', '3.12', '3.11', '3.10', '3.9']:
candidates.extend([
Path(f'/opt/homebrew/lib/python{pyver}/site-packages'),
Path(f'/usr/local/lib/python{pyver}/site-packages'),
])
# virtualenvs / pipx
for venv_base in [home / '.local/pipx/venvs/gns3-gui',
home / '.virtualenvs/gns3']:
if venv_base.exists():
for pyver in ['3.14', '3.13', '3.12', '3.11', '3.10', '3.9', '3.8']:
candidates.append(venv_base / 'lib' / f'python{pyver}' / 'site-packages')
for path in candidates:
if (path / 'gns3' / 'main_window.py').exists():
return path, 'source'
return None, None
def find_matching_python(pyc_path):
"""Find a Python interpreter matching the .pyc version."""
version = detect_pyc_version(pyc_path)
if not version:
return None, None
minor = version.split(".")[-1]
for cmd in [f'python{version}', f'python3.{minor}',
f'/opt/homebrew/bin/python{version}',
f'/opt/homebrew/bin/python3.{minor}',
f'/usr/local/bin/python{version}',
f'/usr/local/bin/python3.{minor}']:
try:
result = subprocess.run([cmd, '--version'], capture_output=True, text=True)
if result.returncode == 0:
return cmd, version
except FileNotFoundError:
continue
return None, version
# --- macOS bytecode patching ---
def _patch_settings_pyc_subprocess(python_cmd, pyc_path):
"""Patch settings.pyc: add 'Custom' to STYLES tuple."""
script = '''
import io, marshal, sys
pyc_path = sys.argv[1]
with open(pyc_path, 'rb') as f:
header = f.read(16)
code = marshal.load(f)
styles_tuple = ('Charcoal', 'Classic', 'Legacy')
new_styles = ('Custom', 'Charcoal', 'Classic', 'Legacy')
def patch_consts(code_obj):
changed = False
new_consts = list(code_obj.co_consts)
for i, const in enumerate(new_consts):
if const == styles_tuple:
new_consts[i] = new_styles
changed = True
elif hasattr(const, 'co_consts'):
patched = patch_consts(const)
if patched is not const:
new_consts[i] = patched
changed = True
if changed:
return code_obj.replace(co_consts=tuple(new_consts))
return code_obj
new_code = patch_consts(code)
if new_code is code:
for c in code.co_consts:
if isinstance(c, tuple) and 'Custom' in c:
print("ALREADY_PATCHED")
sys.exit(0)
if hasattr(c, 'co_consts'):
for cc in c.co_consts:
if isinstance(cc, tuple) and 'Custom' in cc:
print("ALREADY_PATCHED")
sys.exit(0)
print("STYLES_NOT_FOUND")
sys.exit(1)
buf = io.BytesIO()
buf.write(header)
marshal.dump(new_code, buf)
with open(pyc_path, 'wb') as f:
f.write(buf.getvalue())
print("PATCHED")
'''
ret = subprocess.run(
[python_cmd, '-c', script, str(pyc_path)],
capture_output=True, text=True)
output = ret.stdout.strip()
if ret.returncode != 0:
error("Patch", f"Failed to patch settings.pyc: {ret.stderr}")
sys.exit(1)
if output == 'ALREADY_PATCHED':
warn("Patch", "settings.pyc already patched")
elif output == 'PATCHED':
success("Patch", "Added 'Custom' to STYLES in settings.pyc")
def _patch_pycutext_pyc(python_cmd, pyc_path, hex_color):
"""Patch pycutext.pyc: replace hardcoded black QColor(0,0,0) in write()
and default (0,0,0) in SyntaxColor.get_color() with theme color."""
if not pyc_path.exists():
warn("Patch", "pycutext.pyc not found, skipping")
return
r, g, b = int(hex_color[1:3], 16), int(hex_color[3:5], 16), int(hex_color[5:7], 16)
script = f'''
import io, marshal, sys, types, opcode
pyc_path = sys.argv[1]
with open(pyc_path, 'rb') as f:
header = f.read(16)
code = marshal.load(f)
LOAD_CONST = opcode.opmap['LOAD_CONST']
R, G, B = {r}, {g}, {b}
def find_code(co, name):
for c in co.co_consts:
if isinstance(c, types.CodeType) and c.co_name == name:
return c
elif isinstance(c, types.CodeType):
r = find_code(c, name)
if r:
return r
return None
def patch_write(write_co):
consts = list(write_co.co_consts)
r_idx = len(consts)
consts.append(R)
g_idx = len(consts)
consts.append(G)
b_idx = len(consts)
consts.append(B)
bc = bytearray(write_co.co_code)
zero_idx = None
for i, c in enumerate(write_co.co_consts):
if c == 0 and isinstance(c, int):
zero_idx = i
break
if zero_idx is None:
print("ZERO_NOT_FOUND")
return write_co
# Find three consecutive LOAD_CONST zero_idx (the normal text color QColor(0,0,0))
# Skip the error color pattern (preceded by LOAD_CONST 255)
patched = False
for i in range(0, len(bc) - 5, 2):
if (bc[i] == LOAD_CONST and bc[i] == bc[i+2] == bc[i+4] and
bc[i+1] == bc[i+3] == bc[i+5] == zero_idx):
if i >= 2 and bc[i-2] == LOAD_CONST:
prev_val = write_co.co_consts[bc[i-1]] if bc[i-1] < len(write_co.co_consts) else None
if prev_val == 255:
continue
bc[i+1] = r_idx
bc[i+3] = g_idx
bc[i+5] = b_idx
patched = True
break
if not patched:
print("PATTERN_NOT_FOUND")
return write_co
return write_co.replace(co_consts=tuple(consts), co_code=bytes(bc))
def replace_code(co, old_code, new_code):
new_consts = list(co.co_consts)
changed = False
for i, c in enumerate(new_consts):
if c is old_code:
new_consts[i] = new_code
changed = True
elif isinstance(c, types.CodeType):
replaced = replace_code(c, old_code, new_code)
if replaced is not c:
new_consts[i] = replaced
changed = True
if changed:
return co.replace(co_consts=tuple(new_consts))
return co
cls_co = find_code(code, 'PyCutExt')
if not cls_co:
print("CLASS_NOT_FOUND")
sys.exit(1)
write_co = find_code(cls_co, 'write')
if not write_co:
print("WRITE_NOT_FOUND")
sys.exit(1)
new_code = code
patched_any = False
new_write = patch_write(write_co)
if new_write is not write_co:
new_code = replace_code(new_code, write_co, new_write)
patched_any = True
# Patch SyntaxColor.get_color: replace default (0,0,0) tuple with theme color
# This colors typed input text in the console
sc_co = find_code(code, 'SyntaxColor')
if sc_co:
gc_co = find_code(sc_co, 'get_color')
if gc_co:
gc_consts = list(gc_co.co_consts)
changed = False
for i, c in enumerate(gc_consts):
if c == (0, 0, 0):
gc_consts[i] = (R, G, B)
changed = True
if changed:
new_gc = gc_co.replace(co_consts=tuple(gc_consts))
new_code = replace_code(new_code, gc_co, new_gc)
patched_any = True
if not patched_any:
print("ALREADY_PATCHED")
sys.exit(0)
buf = io.BytesIO()
buf.write(header)
marshal.dump(new_code, buf)
with open(pyc_path, 'wb') as f:
f.write(buf.getvalue())
print("PATCHED")
'''
ret = subprocess.run(
[python_cmd, '-c', script, str(pyc_path)],
capture_output=True, text=True)
if ret.returncode != 0:
warn("Patch", f"pycutext.pyc patch failed: {ret.stderr.strip()}")
if ret.stdout.strip():
warn("Patch", f"pycutext.pyc: {ret.stdout.strip()}")
elif 'PATCHED' in ret.stdout:
success("Patch", f"Console text color set to {hex_color}")
else:
warn("Patch", f"pycutext.pyc: {ret.stdout.strip()}")
def _patch_link_pyc(python_cmd, pyc_path, hex_color):
"""Patch link item .pyc: replace fallback '#000000' with theme color."""
if not pyc_path.exists():
warn("Patch", f"{pyc_path.name} not found, skipping")
return
script = '''
import io, marshal, sys, types
pyc_path = sys.argv[1]
new_color = sys.argv[2]
with open(pyc_path, 'rb') as f:
header = f.read(16)
code = marshal.load(f)
def patch_consts(co):
changed = False
new_consts = list(co.co_consts)
for i, c in enumerate(new_consts):
if c == '#000000' and isinstance(c, str):
new_consts[i] = new_color
changed = True
elif isinstance(c, types.CodeType):
patched = patch_consts(c)
if patched is not c:
new_consts[i] = patched
changed = True
if changed:
return co.replace(co_consts=tuple(new_consts))
return co
new_code = patch_consts(code)
if new_code is code:
print("NO_CHANGE")
sys.exit(0)
buf = io.BytesIO()
buf.write(header)
marshal.dump(new_code, buf)
with open(pyc_path, 'wb') as f:
f.write(buf.getvalue())
print("PATCHED")
'''
ret = subprocess.run(
[python_cmd, '-c', script, str(pyc_path), hex_color],
capture_output=True, text=True)
if ret.returncode != 0:
warn("Patch", f"{pyc_path.name} patch failed: {ret.stderr.strip()}")
elif 'PATCHED' in ret.stdout:
success("Patch", f"{pyc_path.name} link color set to {hex_color}")
else:
warn("Patch", f"{pyc_path.name}: {ret.stdout.strip()}")
# --- macOS style.py template ---
STYLE_PY_TEMPLATE = '''\
"""GNS3 Style module - patched by gns3theme for Custom style support."""
import pathlib
import importlib.util
from gns3.qt import QtCore, QtGui, QtWidgets
# Load original Style class from backup
_orig_path = pathlib.Path("$backup_path")
_OrigStyle = None
if _orig_path.exists():
try:
_spec = importlib.util.spec_from_file_location(
'gns3._style_original', str(_orig_path))
_orig_mod = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_orig_mod)
_OrigStyle = _orig_mod.Style
except Exception as e:
import logging
logging.getLogger(__name__).warning(
"gns3theme: Could not load original style: %s", e)
def _hex_to_qcolor(hex_color):
r = int(hex_color[1:3], 16)
g = int(hex_color[3:5], 16)
b = int(hex_color[5:7], 16)
return QtGui.QColor(r, g, b)
class Style:
"""GNS3 GUI Style manager with Custom theme support."""
def __init__(self, main_window):
self._mw = main_window
self._orig = _OrigStyle(main_window) if _OrigStyle else None
def _getStyleIcon(self, normal_file, active_file):
if self._orig:
return self._orig._getStyleIcon(normal_file, active_file)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(normal_file),
QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
icon.addPixmap(QtGui.QPixmap(active_file),
QtGui.QIcon.Mode.Active, QtGui.QIcon.State.Off)
return icon
def setLegacyStyle(self):
try:
if self._mw._settings.get("style") == "Custom":
self.setCustomStyle()
return
except Exception:
pass
if self._orig:
self._orig.setLegacyStyle()
def setClassicStyle(self):
if self._orig:
self._orig.setClassicStyle()
def setCharcoalStyle(self):
if self._orig:
self._orig.setCharcoalStyle()
def setCustomStyle(self):
"""Apply custom theme from ~/.config/gns3theme/"""
self.setClassicStyle()
css_path = str(pathlib.Path.home() / '.config' / 'gns3theme' / 'custom_style.css')
style_file = QtCore.QFile(css_path)
if style_file.exists():
try:
style_file.open(QtCore.QIODeviceBase.OpenModeFlag.ReadOnly)
except AttributeError:
style_file.open(QtCore.QFile.ReadOnly)
css_text = QtCore.QTextStream(style_file).readAll()
self._mw.setStyleSheet(css_text)
config_path = pathlib.Path.home() / '.config' / 'gns3theme' / 'config.json'
config = {}
if config_path.exists():
import json
try:
config = json.loads(config_path.read_text())
except Exception:
pass
if not config:
return
try:
if hasattr(self._mw, 'uiGraphicsView'):
gv = self._mw.uiGraphicsView
settings = gv.settings()
if 'label_color' in config:
settings['default_label_color'] = config['label_color']
if 'note_color' in config:
settings['default_note_color'] = config['note_color']
if 'grid_color' in config:
gc = config['grid_color']
r = int(gc[1:3], 16)
g = int(gc[3:5], 16)
b = int(gc[5:7], 16)
gv._drawing_grid_color = QtGui.QColor(r, g, b)
lum = 0.1 if (r + g + b) / 3 < 128 else -0.08
nr = round(min(max(0, r + r * lum), 255))
ng = round(min(max(0, g + g * lum), 255))
nb = round(min(max(0, b + b * lum), 255))
gv._node_grid_color = QtGui.QColor(nr, ng, nb)
if 'sbg' in config:
hl = _hex_to_qcolor(config['sbg'])
palette = gv.palette()
try:
palette.setColor(QtGui.QPalette.ColorRole.Highlight, hl)
except AttributeError:
palette.setColor(QtGui.QPalette.Highlight, hl)
gv.setPalette(palette)
if hasattr(gv, 'viewport'):
gv.viewport().update()
except Exception:
pass
'''
def _write_and_compile_style(python_cmd, gns3_dir, backup_dir):
"""Write patched style.py and compile to .pyc using matching Python."""
gns3_pkg = Path(gns3_dir) / 'gns3'
style_pyc = gns3_pkg / 'style.pyc'
style_py = gns3_pkg / 'style.py'
backup_pyc = backup_dir / 'style.pyc'
if style_pyc.exists():
style_pyc.unlink()
from string import Template
style_content = Template(STYLE_PY_TEMPLATE).substitute(backup_path=backup_pyc)
style_py.write_text(style_content)
success("Patch", "Wrote patched style.py")
ret = subprocess.run(
[python_cmd, '-c',
f'import py_compile; py_compile.compile("{style_py}", "{style_pyc}", doraise=True)'],
capture_output=True, text=True)
if ret.returncode == 0:
success("Compile", f"Compiled style.pyc with {python_cmd}")
style_py.unlink()
else:
warn("Compile", f"Could not compile style.pyc: {ret.stderr}")
warn("Compile", "Leaving style.py as source (may not work with frozen apps)")
# --- macOS install ---
def macos_patch_app(src_app, scheme, backup_dir):
"""Patch GNS3.app on macOS: copy to /tmp, patch, move back to /Applications.
Requires sudo for the final move."""
import tempfile
# Always start from the original unpatched app so theme switches work
bak_app = Path('/Applications/GNS3.app.bak')
copy_from = bak_app if bak_app.exists() else src_app
tmp_dir = Path(tempfile.mkdtemp(prefix='gns3theme_'))
tmp_app = tmp_dir / 'GNS3.app'
try:
if copy_from == bak_app:
success("Copy", "Copying original GNS3.app.bak to temp (this may take a moment)...")
else:
success("Copy", "Copying GNS3.app to temp location (this may take a moment)...")
shutil.copytree(
copy_from, tmp_app,
copy_function=shutil.copy,
symlinks=True,
)
success("Copy", f"Created clean copy at {tmp_app}")
gns3_dir = tmp_app / 'Contents' / 'Resources' / 'lib'
gns3_pkg = gns3_dir / 'gns3'
sample_pyc = gns3_pkg / 'settings.pyc'
python_cmd, pyc_version = find_matching_python(sample_pyc)
if not python_cmd:
error("Install",
f"GNS3 uses Python {pyc_version} but it's not installed. "
f"Install it with: brew install python@{pyc_version}")
sys.exit(1)
success("Install", f"Using {python_cmd} for .pyc patching (matches GNS3's Python {pyc_version})")
# Backup originals
backup_file(sample_pyc, backup_dir)
backup_file(gns3_pkg / 'style.pyc', backup_dir)
for extra in ['pycutext.pyc', 'items/ethernet_link_item.pyc', 'items/serial_link_item.pyc']:
p = gns3_pkg / extra
if p.exists():
backup_file(p, backup_dir)
# Patch settings.pyc
_patch_settings_pyc_subprocess(python_cmd, gns3_pkg / 'settings.pyc')
# Patch console text color
fg = scheme.get('fg', '#00997a')
_patch_pycutext_pyc(python_cmd, gns3_pkg / 'pycutext.pyc', fg)
# Patch link colors
link_color = scheme.get('lc', scheme.get('fg', '#00997a'))
_patch_link_pyc(python_cmd, gns3_pkg / 'items' / 'ethernet_link_item.pyc', link_color)
_patch_link_pyc(python_cmd, gns3_pkg / 'items' / 'serial_link_item.pyc', link_color)
# Write patched style.py and compile
_write_and_compile_style(python_cmd, gns3_dir, backup_dir)
# Re-sign the app
subprocess.run(
['codesign', '--force', '--deep', '--sign', '-', str(tmp_app)],
capture_output=True)
success("Sign", "Ad-hoc signed the patched app")
# Move patched app to /Applications (requires sudo)
check_sudo('gns3theme -s <scheme>')
success("Install", "Moving patched GNS3.app to /Applications/...")
if not bak_app.exists():
ret = subprocess.run(['sudo', 'cp', '-a', str(src_app), str(bak_app)])
if ret.returncode != 0:
error("Install", "Failed to backup original app")
sys.exit(1)
success("Backup", f"Original app backed up to {bak_app}")
ret = subprocess.run(
['sudo', 'cp', '-a', str(tmp_app) + '/', str(src_app) + '/'])
if ret.returncode != 0:
error("Install", "Failed to copy patched app to /Applications/")
sys.exit(1)
success("Install", "Patched GNS3.app installed to /Applications/")
save_config(scheme)
return True
finally:
if tmp_dir.exists():
shutil.rmtree(tmp_dir, ignore_errors=True)
# --- Source .py patching ---
def patch_py_settings(file_path, backup_dir):
"""Add 'Custom' to STYLES in settings.py."""
with open(file_path, 'r') as f:
content = f.read()
if re.search(r'STYLES.*Custom', content):
warn("Patch", "settings.py already patched, skipping")
return
backup_file(file_path, backup_dir)
new_content = re.sub(
r'(STYLES\s*=\s*\[)',
r'\1"Custom", ',
content,
)
with open(file_path, 'w') as f:
f.write(new_content)
success("Patch", "Added 'Custom' to STYLES in settings.py")
def patch_py_main_window(file_path, backup_dir):
"""Add Custom style dispatch in main_window.py."""
with open(file_path, 'r') as f:
content = f.read()
if 'setCustomStyle' in content:
warn("Patch", "main_window.py already patched, skipping")
return
backup_file(file_path, backup_dir)
new_content = re.sub(
r'(\s+)(style\.setLegacyStyle\(\))',
r'\1if style_name == "Custom":\n\1 style.setCustomStyle()\n\1 return\n\1\2',
content,
)
with open(file_path, 'w') as f:
f.write(new_content)
success("Patch", "Added Custom dispatch in main_window.py")
CUSTOM_STYLE_METHOD = '''
@staticmethod
def _hex_to_qcolor(hex_color):
r = int(hex_color[1:3], 16)
g = int(hex_color[3:5], 16)
b = int(hex_color[5:7], 16)
return QtGui.QColor(r, g, b)
@staticmethod
def _patch_console_color(mw, theme_color):
try:
console = mw.findChild(QtCore.QObject, 'uiConsoleTextEdit')
if not console or not hasattr(console, 'write'):
return
klass = type(console)
if getattr(klass, '_gns3theme_patched', False):
return
base_stc = None
for base in klass.__mro__:
if base.__name__ in ('QTextEdit', 'QPlainTextEdit'):
base_stc = base.setTextColor
break
if base_stc is None:
base_stc = klass.setTextColor
tc = theme_color
def _themed_stc(self, color, _orig=base_stc, _tc=tc):
if color.red() == 0 and color.green() == 0 and color.blue() == 0:
_orig(self, _tc)
else:
_orig(self, color)
klass.setTextColor = _themed_stc
klass._gns3theme_patched = True
except Exception:
pass
def setCustomStyle(self):
"""Apply custom theme from ~/.config/gns3theme/"""
import pathlib, json
self.setClassicStyle()
css_path = str(pathlib.Path.home() / '.config' / 'gns3theme' / 'custom_style.css')
style_file = QtCore.QFile(css_path)
if style_file.exists():
try:
style_file.open(QtCore.QIODeviceBase.OpenModeFlag.ReadOnly)
except AttributeError:
style_file.open(QtCore.QFile.ReadOnly)
css_text = QtCore.QTextStream(style_file).readAll()
self._mw.setStyleSheet(css_text)
config_path = pathlib.Path.home() / '.config' / 'gns3theme' / 'config.json'
config = {}
if config_path.exists():
try:
config = json.loads(config_path.read_text())
except Exception:
pass
if not config:
return
try:
if hasattr(self._mw, 'uiGraphicsView'):
gv = self._mw.uiGraphicsView
settings = gv.settings()
if 'label_color' in config:
settings['default_label_color'] = config['label_color']
if 'note_color' in config:
settings['default_note_color'] = config['note_color']
if 'grid_color' in config:
gc = config['grid_color']
r = int(gc[1:3], 16)
g = int(gc[3:5], 16)
b = int(gc[5:7], 16)
gv._drawing_grid_color = QtGui.QColor(r, g, b)
lum = 0.1 if (r + g + b) / 3 < 128 else -0.08
nr = round(min(max(0, r + r * lum), 255))
ng = round(min(max(0, g + g * lum), 255))
nb = round(min(max(0, b + b * lum), 255))
gv._node_grid_color = QtGui.QColor(nr, ng, nb)
if 'sbg' in config:
hl = self._hex_to_qcolor(config['sbg'])
palette = gv.palette()
try:
palette.setColor(QtGui.QPalette.ColorRole.Highlight, hl)
except AttributeError:
palette.setColor(QtGui.QPalette.Highlight, hl)
gv.setPalette(palette)
if hasattr(gv, 'viewport'):
gv.viewport().update()
except Exception:
pass
if 'console_color' in config:
tc = self._hex_to_qcolor(config['console_color'])
self._patch_console_color(self._mw, tc)
QtCore.QTimer.singleShot(2000,
lambda mw=self._mw, c=tc: Style._patch_console_color(mw, c))
'''
def patch_py_style(file_path, backup_dir):
"""Add setCustomStyle method to style.py."""
with open(file_path, 'r') as f:
content = f.read()
if 'setCustomStyle' in content:
warn("Patch", "style.py already patched, skipping")
return
backup_file(file_path, backup_dir)
new_content = content.rstrip() + '\n' + CUSTOM_STYLE_METHOD
with open(file_path, 'w') as f:
f.write(new_content)
success("Patch", "Added setCustomStyle to style.py")
def restore_from_backup(file_path, backup_dir):
"""Restore a single file from backup if one exists (for re-patching)."""
bak = backup_dir / Path(file_path).name
if bak.exists():
shutil.copy(bak, file_path)
def patch_py_link_item(file_path, hex_color, backup_dir):
"""Patch link item .py: replace fallback '#000000' with theme color."""
if not file_path.exists():
warn("Patch", f"{file_path.name} not found, skipping")
return
# Restore original before patching so we always replace from clean state
restore_from_backup(file_path, backup_dir)
with open(file_path, 'r') as f:
content = f.read()
if '#000000' not in content:
warn("Patch", f"{file_path.name}: no #000000 to replace, skipping")
return
backup_file(file_path, backup_dir)
new_content = content.replace('#000000', hex_color)
with open(file_path, 'w') as f:
f.write(new_content)
success("Patch", f"{file_path.name} link color set to {hex_color}")
def patch_py_pycutext(file_path, hex_color, backup_dir):
"""Patch pycutext.py: replace hardcoded black colors in write() and
SyntaxColor.get_color() with the theme foreground color."""
if not file_path.exists():
warn("Patch", f"{file_path.name} not found, skipping")
return
# Restore original before patching so we always replace from clean state
restore_from_backup(file_path, backup_dir)
with open(file_path, 'r') as f:
content = f.read()
r, g, b = int(hex_color[1:3], 16), int(hex_color[3:5], 16), int(hex_color[5:7], 16)
new_qcolor = f'QColor({r}, {g}, {b})'
new_tuple = f'({r}, {g}, {b})'
patched = False
new_content = content
# Patch write() default color: QColor(0, 0, 0)
if 'QColor(0, 0, 0)' in new_content:
new_content = new_content.replace('QColor(0, 0, 0)', new_qcolor)
patched = True
# Patch SyntaxColor.get_color() default return: (0, 0, 0)
# This is the tuple returned for non-keyword input text
if 'return (0, 0, 0)' in new_content:
new_content = new_content.replace('return (0, 0, 0)', f'return {new_tuple}')
patched = True
if not patched:
warn("Patch", f"{file_path.name}: no black color patterns to replace, skipping")
return
backup_file(file_path, backup_dir)
with open(file_path, 'w') as f:
f.write(new_content)
success("Patch", f"Console text color set to {hex_color}")
def install_patches_source(gns3_dir, scheme, backup_dir):
"""Patch GNS3 .py source files (pip/brew/source installs)."""
gns3_pkg = Path(gns3_dir) / 'gns3'
import os
if not os.access(gns3_pkg / 'settings.py', os.W_OK):
error("PermissionError",
f"No write access to {gns3_pkg}. Try running with sudo")
sys.exit(1)
patch_py_settings(gns3_pkg / 'settings.py', backup_dir)
patch_py_main_window(gns3_pkg / 'main_window.py', backup_dir)
patch_py_style(gns3_pkg / 'style.py', backup_dir)
# Patch console text color
fg = scheme.get('fg', '#d8dee9')
patch_py_pycutext(gns3_pkg / 'pycutext.py', fg, backup_dir)
# Patch link colors
link_color = scheme.get('lc', scheme.get('fg', '#d8dee9'))
patch_py_link_item(gns3_pkg / 'items' / 'ethernet_link_item.py', link_color, backup_dir)
patch_py_link_item(gns3_pkg / 'items' / 'serial_link_item.py', link_color, backup_dir)
save_config(scheme)
success("Install", "Patching complete!")
# --- CSS generation ---
def generate_custom_css(data):
"""Generate CSS string from selector-properties dict."""
parts = []
for selector, properties in data.items():
parts.append(f'{selector}{{\n')
for prop, value in properties.items():
parts.append(f'\t{prop}: {value};\n')
parts.append('}\n')
return ''.join(parts)