This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall.py
executable file
·1542 lines (1327 loc) · 56.8 KB
/
install.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
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
'''This module is for installing the themes.'''
import os
import sys
import argparse
import subprocess
import shutil
import time
import threading
from re import sub
from urllib.request import urlopen
from glob import glob
from paths import HOME, REPO_DIR, CONFIG, OLD_CONFIG, SRC, FIREFOX_DIR, VSCODE_DIR, installed
NC = '\033[0m'
BOLD = '\033[1m'
BRED = '\033[1;31m'
BGREEN = '\033[1;32m'
BYELLOW = '\033[1;33m'
BLRED = '\033[1;91m'
BLYELLOW = '\033[1;93m'
BLBLUE = '\033[1;94m'
BLCYAN = '\033[1;96m'
VARIANTS = { # all of the possible configurations, name: pretty-name
'dir': {
'default': f'Default {BGREEN}(Recommended for most distros){NC}',
'home': f'{HOME}/.local/share {BYELLOW}(Only recommended for immutable distros){NC}',
'root': f'/usr/share {BYELLOW}(Do not choose if you use Flatpaks){NC}'
},
'color': {
'orange': 'Orange',
'bark': 'Bark',
'sage': 'Sage',
'olive': 'Olive',
'viridian': 'Viridian',
'prussiangreen': 'Prussian Green',
'lightblue': 'Light Blue',
'blue': 'Blue',
'purple': 'Purple',
'magenta': 'Magenta',
'pink': 'Pink',
'red': 'Red'
},
'theme': {
'light': 'Light',
'dark': 'Dark',
'auto': f'Auto {BYELLOW}(Only recommended for GNOME or Budgie){NC}'
},
'window-controls': {
'macos': 'macOS',
'symbolic': 'Libadwaita'
},
'enableable': {
'dg-adw-gtk3': {
'gtk3': 'GTK3',
'gtk4-libadwaita': 'Libadwaita'
},
'dg-libadwaita': {
'gtk4': 'GTK4'
},
'dg-yaru': {
'gnome-shell': 'GNOME Shell',
'cinnamon-shell': 'Cinnamon Shell',
'metacity': 'Marco (Metacity)',
'ubuntu-unity': 'Unity',
'xfwm4': 'Xfwm4',
'icons': 'Icon',
'cursors': 'Cursor',
'sounds': 'Sound',
'gtksourceview': 'GtkSourceView'
},
'dg-firefox-theme': {
'firefox': 'Firefox',
'settings_theme': ''
},
'dg-vscode-adwaita': {
'vscode': 'VS Code',
'default_syntax': ''
},
'qualia-gtk-theme-snap': {
'snap': 'Snap'
},
}
}
# Supported DE versions
VERSIONS = {
'gnome': (42, 43, 44),
'cinnamon': (4, 5),
'unity': (7,),
'xfce': (4,),
'mate': (1,),
'budgie': (10.6,)
}
DIR_MSG = f'{BLBLUE}Where do you want to {NC}{BLCYAN}install the theme{NC}{BLBLUE}?{NC}'
SETTINGS_MSG = f'{BLBLUE}Do you want to theme the {NC}{BLCYAN}settings pages{NC}{BLBLUE} in {BLCYAN}Firefox{BLBLUE}?{NC}{BOLD}'
SYNTAX_MSG = f'{BLBLUE}Do you want to keep the {NC}{BLCYAN}default syntax highlighting{NC}{BLBLUE} in {BLCYAN}VS Code{BLBLUE}?{NC}{BOLD}'
LIBADWAITA_MSG = f'{BLBLUE}Do you want to install {BLCYAN}Libadwaita{BLBLUE} as a {NC}{BLCYAN}GTK4 theme{NC}{BLBLUE}?{NC}{BOLD}'
GTK4_MSG = f'{BLBLUE}Do you want to install the {NC}{BLCYAN}custom GTK4 configuration{NC}{BLBLUE}?{NC}{BOLD}'
FLATPAK_MSG = f'{BLBLUE}Do you want to give {NC}{BLCYAN}Flatpak apps{NC}{BLBLUE} access to the {NC}{BLCYAN}GTK themes{NC}{BLBLUE}?{NC}{BOLD}'
# dg-gnome-theme
OLD_THEMES = [
'gtk3',
'gnome-shell',
'icons',
'cursors',
'gtksourceview',
'sounds',
'firefox',
'snap'
]
# anything in dg-yaru or dg-adw-gtk3
MESON_THEMES = list(VARIANTS['enableable']['dg-yaru'].keys()) + list(VARIANTS['enableable']['dg-adw-gtk3'].keys())
# Set some things to false
reinstall = no_update = update_color = update_theme = update_settings = update_syntax = reconfigure = verbose = force = configured = updated = False
#################
## Functions ##
#################
def cd(directory):
'''
Change directory.
Parameters:
dir (str) : The path of the directory to change to.
'''
if verbose:
print(f"Changing directory to '{directory}'")
os.chdir(directory)
def run_command(command, meson = False, override_verbose = None, show_ouput = False):
'''
Run an external command and handle errors.
Parameters:
command (list) : A list containing the command and each argument.
meson (bool) : If the option to clean build dir should be printed.
override_verbose (bool) : Override the global verbose value.
show_ouput (bool) : Show output of command.
'''
if override_verbose is None:
global verbose
else:
verbose = override_verbose
if verbose:
print("Running command '" + ' '.join(command) + "'")
try:
subprocess.run(command, stdout=None if verbose or show_ouput else subprocess.PIPE, stderr=subprocess.STDOUT, check=True)
except subprocess.CalledProcessError as error:
if not (verbose or show_ouput):
print('\n' + error.output.decode("utf-8"))
print(f"{BRED}Something went wrong, run {BLRED}'{sys.argv[0]} --verbose'{BRED} for more info.{NC}")
else:
print(f'{BRED}Something went wrong. Check the log above.{NC}')
if meson:
print(f"{BRED}Also, running {BLRED}'{sys.argv[0]} --clean'{BRED} might fix the issue.{NC}")
print('\033[?25h') # bring back cursor
os._exit(1)
def check_output(command):
'''
Check output of command.
Parameters:
command (list) : A list of the command and arguments to run.
Returns:
output (str) : The ouput of the command.
'''
output = subprocess.run(command, stdout=subprocess.PIPE, check=True).stdout.decode('utf-8').strip('\'\n')
return output
def main():
'''The main function.'''
#####################
## Configuration ##
#####################
if shutil.which('sassc') is None:
print(f"{BLRED}'sassc'{BRED} not found, exiting.{NC}")
sys.exit()
if shutil.which('git') is None:
print(f"{BLRED}'git'{BRED} not found, exiting.{NC}")
sys.exit()
conf = Config()
conf.read()
global configure_all
global reinstall
# Move old config file
if os.path.isfile(OLD_CONFIG) and not os.path.isfile(CONFIG):
shutil.move(OLD_CONFIG, CONFIG)
# Whether or not to configure
if not os.path.isfile(CONFIG) or reconfigure:
configure_all = True
conf.configure()
if os.path.isfile(CONFIG):
os.remove(CONFIG)
config = conf.ret_config()
# Reconfigure if config file has issues
try:
color_scheme = conf.theme_variants(config['theme'])
if not isinstance(config['enabled'], list) or color_scheme is None or len(config['enabled']) == 0:
configure_all = True
conf.configure()
except KeyError:
configure_all = True
conf.configure()
# Reconfigure color, theme variant, or syntax highlighting if user wants to
if ( update_color or update_theme or update_syntax or update_settings or update_dir or update_window_controls ) and not configure_all:
conf.configure()
if configured:
config = conf.ret_config()
conf.write(config)
if configure_all or force or update_color:
reinstall = True
######################
## Install Themes ##
######################
if not configured:
print(f'{BYELLOW}Updating themes using previous configuration.{NC}')
print(f"{BYELLOW}Use {BLYELLOW}'{sys.argv[0]} --reconfigure'{BYELLOW} if you want to change anything.{NC}")
paths = installed(new_only = True)
config['suffix']='-dark' if config['color_scheme'] == 'prefer-dark' else ''
config['variant']='dark' if config['color_scheme'] == 'prefer-dark' else 'light'
def check_path(name, paths, should_print = True):
'''
Check if the path to a theme exists, used for when a theme was previously installed but is disabled.
Parameters:
name (str) : The name of the part of the theme to check.
paths (dict) : The dict containing all of the paths.
should_print (bool) : If we should print that the path exists.
Returns:
exists (bool) : Whether or not the path exists.
'''
exists = False
for i in paths[name]:
if isinstance(i, list):
for path in i:
if name == 'icons' and path in paths['cursors']:
pass
else:
exists = os.path.exists(path)
else:
if name == 'icons' and i in paths['cursors']:
pass
else:
exists = os.path.exists(i)
if should_print and exists:
message = ''
for theme in VARIANTS['enableable']:
if name in VARIANTS['enableable'][theme]:
pretty = VARIANTS['enableable'][theme][name]
if name == 'gtk4-libadwaita':
message += f'{pretty} GTK4 theme{NC}'
elif name == 'gtk4':
message += f'{pretty} configuration{NC}'
else:
message += f'{pretty} theme{NC}'
print(f"The {message} was installed previously, use './uninstall.py {name}' to remove it.")
return exists
# Install dg-adw-gtk3
if 'gtk3' in config['enabled'] or 'gtk4-libadwaita' in config['enabled']:
# If user is installing in root dir, remove old symlinks if they exist
if config['dir'] == 'root':
dirs = paths['gtk3'] + paths['gtk4-libadwaita']
if isinstance(dirs, list):
for directory in dirs:
if os.path.islink(directory):
run_command(['sudo', 'rm', '-rf', directory])
gtk3 = InstallDgAdwGtk3(config)
config['dg-adw-gtk3_version'] = gtk3.get_version()
conf.write(config)
else:
check_path('gtk3', paths)
check_path('gtk4', paths)
# Install dg-yaru
yaru_parts_pretty = []
yaru_parts = []
yaru_disabled = []
for i in VARIANTS['enableable']['dg-yaru']:
pretty_name = VARIANTS['enableable']['dg-yaru'][i]
if i in config['enabled']:
yaru_parts_pretty.append(pretty_name)
yaru_parts.append(i)
else:
yaru_disabled.append(i)
if len(yaru_parts) > 0:
yaru = InstallDgYaru(config, yaru_parts, yaru_parts_pretty)
config['dg-yaru_version'] = yaru.get_version()
conf.write(config)
for i in yaru_disabled:
check_path(i, paths)
# Install dg-libadwaita
if 'gtk4' in config['enabled']:
gtk4 = InstallDgLibadwaita(config)
config['dg-libadwaita_version'] = gtk4.get_version()
conf.write(config)
else:
check_path('gtk4', paths)
# Install dg-firefox-theme
if 'firefox' in config['enabled']:
firefox = InstallDgFirefoxTheme(config)
config['dg-firefox-theme_version'] = firefox.get_version()
conf.write(config)
else:
check_path('firefox', paths)
# Install dg-vscode-adwiata
if 'vscode' in config['enabled']:
vscode = InstallDgVscodeAdwaita(config)
config['dg-vscode-adwaita_version'] = vscode.get_version()
conf.write(config)
else:
check_path('vscode', paths)
# Install snap theme
if 'snap' in config['enabled']:
try:
# Check that there is an internet connection
urlopen('https://www.google.com/', timeout=3)
InstallQualiaGtkThemeSnap(config)
except OSError:
pass
elif shutil.which('snap'):
snap_list = check_output(['snap', 'list'])
for line in snap_list:
line = line.split()
if 'qualia-gtk-theme' in line:
print("Snap theme was installed previously, use './uninstall.py snap' to remove it.")
#####################
## Enable Themes ##
#####################
gtk3_name = f"qualia{config['suffix']}-{config['window-controls']}" if config['color'] == 'orange' else f"qualia-{config['color']}{config['suffix']}-{config['window-controls']}"
theme_name = f"qualia{config['suffix']}" if config['color'] == 'orange' else f"qualia-{config['color']}{config['suffix']}"
icon_name = 'qualia-dark' if config['color'] == 'orange' else f"qualia-{config['color']}-dark"
cursor_name = 'qualia'
sourceview_name = f"qualia{config['suffix']}"
xfwm4_name = f"qualia{config['suffix']}-{config['window-controls']}"
if shutil.which('xfconf-query') is not None:
if 'xsettings' in check_output(['xfconf-query', '-l']) and check_output(['xfconf-query', '-c', 'xsettings', '-p', '/Gdk/WindowScalingFactor']) == '2':
xfwm4_name += '-xhdpi'
cd(REPO_DIR)
kwargs = {
'gtk3': gtk3_name,
'icons': icon_name,
'cursors': cursor_name,
'sounds': cursor_name,
'gnome-shell': theme_name,
'cinnamon-shell': theme_name,
'metacity': xfwm4_name,
'xfwm4': xfwm4_name,
'gtksourceview': sourceview_name
}
enable = Enable(config, verbose, **kwargs)
config = enable.enable_theme()
# Update config file one last time
conf.write(config)
# Set color scheme
try:
if config['color_scheme'] is not None and shutil.which('gsettings') is not None:
subprocess.run(['gsettings', 'set', 'org.gnome.desktop.interface', 'color-scheme', config['color_scheme']], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, check=True)
if config['desktop_versions']['budgie'] is not None:
dark = 'true' if config['color_scheme'] == 'prefer-dark' else 'false'
subprocess.run(['gsettings', 'set', 'com.solus-project.budgie-panel', 'dark-theme', dark ], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, check=True)
except subprocess.CalledProcessError:
pass
if config['flatpak']:
print('Giving Flatpak apps access to the GTK themes.')
run_command(['flatpak', 'override', '--user', '--filesystem=xdg-config/gtk-4.0', '--filesystem=xdg-data/themes'])
# Remove old variants of the theme
from uninstall import remove_theme, available_themes, remove_empty, delete
print('Cleaning up...')
old_paths = installed(old_only=True)
for theme in OLD_THEMES:
remove_theme(theme, available_themes[theme], old_paths, True, False, verbose)
if config['dir'] == 'root':
home_paths = installed(directory='home')
for theme in MESON_THEMES:
remove_theme(theme, available_themes[theme], home_paths, True, False, verbose)
elif config['dir'] == 'home':
root_paths = installed(directory='root')
for theme in MESON_THEMES:
remove_theme(theme, available_themes[theme], root_paths, True, False, verbose)
elif config['dir'] == 'default':
non_default_paths = installed(directory='not_default')
for theme in MESON_THEMES:
remove_theme(theme, available_themes[theme], non_default_paths, True, False, verbose, True)
theme_dirs = installed(new_only = True, just_theme_dirs = True)
for i in theme_dirs:
if 'share/themes' in i:
paths = glob(f'{i}/*')
else:
paths = [i]
for path in paths:
for variant in VARIANTS['window-controls']:
if variant != config['window-controls'] and variant in path:
delete(path)
for subdir in ('gtk-2.0', 'gtk-3.0', 'gtk-4.0', 'xfwm4', 'metacity', 'unity'):
if path.endswith(subdir) and config['window-controls'] not in path:
delete(path)
for color in VARIANTS['color']:
if color != config['color'] and color in path:
delete(path)
break
remove_empty()
theme_dirs = installed(new_only = True, just_theme_dirs = True, directory = 'home')
# Symlink themes to /usr dir if using mate
if config['desktop_versions']['mate'] is not None and config['dir'] == 'default':
if isinstance(theme_dirs, list):
for directory in theme_dirs:
paths = glob(f'{directory}/*')
for target in paths:
dest = '/usr' + target.split(f'{HOME}/.local')[1]
directory = os.path.dirname(dest)
if not os.path.isdir(directory):
run_command(['sudo', 'mkdir', '-p', directory])
if not os.path.exists(dest) and os.path.exists(target):
run_command(['sudo', 'ln', '-rsf', target, dest])
if updated:
print(f"{BYELLOW}Log out and log back in for everything to be updated.{NC}")
class Config:
'''Handles configuration of theme.'''
def __init__(self):
self.config = {}
self.config['desktop_versions'] = self.get_desktops()
self.config['enableable'] = self.get_enableable(self.config['desktop_versions'])
def ret_config(self):
'''
Returns the config dictionary.
Returns:
config (dict) : dictionary that stores the configuration.
'''
return self.config
def get_desktops(self):
'''
Find out what desktops are installed.
Returns:
desktop_versions (dict) : dictionary that stores the versions of the installed desktops.
'''
desktop_versions = {}
desktop_versions['gnome'] = self.check_de_version(['gnome-shell', '--version'], r'\.[0-9]?\n?$|\D', 'gnome')
desktop_versions['cinnamon'] = self.check_de_version(['cinnamon', '--version'], r'\.[0-9]\.?[0-9]?[0-9]?\n?$|^Cinnamon ', 'cinnamon')
desktop_versions['unity'] = self.check_de_version(['unity', '--version'], r'\.[0-9]\.[0-9]?\n?$|\D', 'unity')
desktop_versions['mate'] = self.check_de_version(['mate-session', '--version'], r'\.[0-9][0-9]?\.[0-9][0-9]?\n?$|^mate-session ', 'mate')
desktop_versions['budgie'] = self.check_de_version(['budgie-desktop', '--version'], r'Copyright.*\n|\.[0-9]\n|^budgie-desktop', 'budgie')
if shutil.which('xfce4-session') is not None:
desktop_versions['xfce'] = VERSIONS['xfce'][0]
else:
desktop_versions['xfce'] = None
return desktop_versions
def check_de_version(self, command, regex, name):
'''
Get the current version number of an installed desktop.
Parameters:
command (list) : list that contains command and arguments to run.
regex (str) : regex of what to ignore in output.
name (str) : name of part of theme.
Returns:
enableable (int or float) : version of desktop.
'''
try:
ver = subprocess.run(command, stdout=subprocess.PIPE).stdout.decode('utf-8')
ver = sub(regex, '', ver)
if '.' in ver and float(ver) in VERSIONS[name]:
version = float(ver)
elif int(ver) in VERSIONS[name]:
version = int(ver)
else:
version = None
except(FileNotFoundError, ValueError):
version = None
return version
def get_enableable(self, desktop_versions):
'''
Find out what themes can be enabled.
Parameters:
desktop_versions (dict): dictionary containing the desktop versions.
Returns:
enableable (dict) : {name: pretty_name}, themes that can be enabled.
'''
enableable = {}
config = self.config
for parts in VARIANTS["enableable"].values():
for part, pretty_name in parts.items():
enableable[part] = pretty_name
for i in desktop_versions:
if desktop_versions[i] is None:
if i == 'gnome':
del enableable['gnome-shell']
if i == 'cinnamon':
del enableable['cinnamon-shell']
if i == 'unity':
del enableable['ubuntu-unity']
if i == 'mate':
del enableable['metacity']
if i == 'xfce':
del enableable['xfwm4']
config['firefox'] = []
for variant, path in FIREFOX_DIR.items():
if os.path.isdir(path):
config['firefox'].append(variant)
if len(config['firefox']) == 0:
del enableable['firefox']
config['vscode'] = []
for variant, path in VSCODE_DIR.items():
if os.path.isdir(path):
config['vscode'].append(variant)
if len(config['vscode']) == 0:
del enableable['vscode']
if shutil.which('snap') is None:
del enableable['snap']
return enableable
def configure(self):
'''Prompts user to configure the theme.'''
global configured
config = self.config
if configure_all or update_dir:
config['dir'] = self.config_menu('', VARIANTS['dir'], custom_msg=DIR_MSG, no_columns=True)
if configure_all or update_color:
config['color'] = self.config_menu('accent color', VARIANTS['color'])
if configure_all or update_theme:
config['theme'] = self.config_menu('theme variant', VARIANTS['theme'])
if configure_all or update_window_controls:
config['window-controls'] = self.config_menu('window controls variant', VARIANTS['window-controls'])
if configure_all and shutil.which('flatpak') is not None:
config['flatpak'] = self.config_yn('flatpak', 'Flatpak', custom_msg=FLATPAK_MSG)
if configure_all:
config['enabled'] = []
for key, value in config['enableable'].items():
if key == 'settings_theme' and (update_settings or configure_all):
if 'firefox' in config['enabled']:
if self.config_yn(key, value, custom_msg=SETTINGS_MSG):
if key not in config['enabled']:
config['enabled'].append(key)
else:
if key in config['enabled']:
config['enabled'].remove(key)
elif update_settings:
print('Firefox theme is not enabled, exiting.')
sys.exit()
continue
if key == 'default_syntax' and (update_syntax or configure_all):
if 'vscode' in config['enabled']:
if self.config_yn(key, value, 'n', custom_msg=SYNTAX_MSG):
if key not in config['enabled']:
config['enabled'].append(key)
else:
if key in config['enabled']:
config['enabled'].remove(key)
elif update_syntax:
print('VS Code theme is not enabled, exiting.')
sys.exit()
continue
if configure_all:
if key == 'gtk4':
if self.config_yn(key, value, custom_msg=GTK4_MSG):
config['enabled'].append(key)
continue
if key == 'gtk4-libadwaita':
if self.config_yn(key, value, custom_msg=LIBADWAITA_MSG):
config['enabled'].append(key)
continue
if self.config_yn(key, value):
config['enabled'].append(key)
print() # blank line
configured = True
def config_menu(self, message, options, default=1, custom_msg=None, no_columns=False):
'''
Prompts user with a menu of options.
Parameters:
message (str) : Which {message} do you want?
options (dict) : {name: pretty_name} dictionary that contains options.
default (int) : Default option.
custom_msg (str) : Provide a custom message to print. Defaults to None.
no_colums (bool) : Each option takes up a full line.
'''
if custom_msg is not None:
question = custom_msg
else:
question = f'{BLBLUE}Which {NC}{BLCYAN}{message}{NC}{BLBLUE} do you want?{NC}'
print (question)
for i in range(len(options)):
array = list(options.keys())
pretty_name = list(options.values())[i]
num = f'{i+1}:'
if not no_columns and (i % 2) == 0 and i != len(array) - 1:
print(f' {num:<4}{pretty_name:<12}', end='')
else:
print(f' {num:<4}{pretty_name:<12}')
while True:
val = input(f'{BOLD}Enter the number corresponding to your choice [Default: {default}]: {NC}')
try:
if 1 <= int(val) <= len(array):
ret = array[int(val) - 1]
except(ValueError):
if val == '':
ret = array[int(default) - 1]
else:
continue
try:
if not self.theme_variants(ret): # If auto and cant detect theme
continue
except UnboundLocalError:
continue
break
return ret
def config_yn(self, name, pretty, default = 'y', custom_msg = None):
'''
Prompts user with a yes or no question.
Parameters:
name (str) : Name of part of themye.
pretty (str) : Name of part of theme to print.
default (str) : Default option, either 'y' or 'n'. Defaults to 'y'
custom_msg (str) : Provide a custom message to print. Defaults to None.
'''
while True:
if custom_msg is not None:
question = custom_msg
else:
question = f'{BLBLUE}Do you want to install the {NC}{BLCYAN}{pretty}{NC}{BLBLUE} theme?{NC}{BOLD}'
val = input(question + ' [y/n]'.replace(default, default.capitalize()) + f': {NC}')
if val.casefold() == 'y'.casefold() or ( default == 'y' and val == '' ):
if name in VARIANTS['enableable']['dg-yaru'] or name in VARIANTS['enableable']['dg-adw-gtk3']:
if shutil.which('meson') is None:
print(f"{BLRED}'meson'{BRED} not found, can't install {pretty} theme.{NC}")
continue
if shutil.which('ninja') is None:
print(f"{BLRED}'ninja'{BRED} not found, can't install {pretty} theme.{NC}")
continue
return True
if val.casefold() == 'n'.casefold() or ( default == 'n' and val == '' ):
return False
def read(self):
'''Reads config file.'''
config = self.config
config['old'] = {}
config['old_vscode'] = []
config['old_firefox'] = []
config['dir'] = 'default'
config['color'] = 'orange'
config['theme'] = 'light'
config['window-controls'] = 'macos'
config['flatpak'] = False
for theme in VARIANTS['enableable']:
if theme != 'qualia-gtk-theme-snap' and theme != 'extra':
config[theme + '_version'] = ''
try:
config_file = open(CONFIG, encoding='UTF-8')
for line in config_file.readlines():
if ': ' in line:
line = line.strip().split(': ')
else:
continue
key = line[0]
try:
value = line[1]
except IndexError:
continue
if ' ' in value:
value = value.split(' ')
if not configured and key in ('enabled', 'firefox', 'vscode'):
prefix = 'old_' if key in ('firefox', 'vscode') else ''
config[prefix + key] = []
if isinstance(value, list):
for theme in value:
if theme.startswith('firefox'):
theme = 'firefox'
if (theme in config['enableable'] or key != 'enabled') and theme not in config[prefix + key]:
if key == 'enabled' and theme == 'flatpak':
config['flatpak'] = True
else:
config[prefix + key].append(theme)
elif isinstance(value, str):
if value.startswith('firefox'):
value = 'firefox'
if (value in config['enableable'] or prefix + key != 'enabled') and value not in config[prefix + key]:
config[prefix + key].append(value)
elif key.startswith('old'): # Themes that were enabled before qualia
old = key.split('_')
theme = old[1]
desktop = old[2]
if desktop in VERSIONS:
config['old'][theme] = {}
config['old'][theme][desktop] = value
elif key == 'gnome':
try:
config['old_gnome'] = int(value)
except ValueError:
config['old_gnome'] = None
elif key == 'flatpak':
config['flatpak'] = (value == 'True')
elif isinstance(value, str):
try:
if not configured and key in VARIANTS and value in VARIANTS[key]:
config[key] = value
elif key.endswith('version') and len(value) == 40:
config[key] = value
except TypeError:
continue
config_file.close()
except FileNotFoundError:
pass
def write(self, config):
'''
Writes to config file.
Parameters:
config (dict) : Dictionary that contains the configuration.
'''
if os.path.isfile(CONFIG):
os.remove(CONFIG)
elif os.path.isdir(CONFIG):
shutil.rmtree(CONFIG)
f = open(CONFIG, 'x', encoding='UTF-8')
f.write("This file is generated and used by the install script.\n")
f.write("You probably shouldn't edit it.\n\n")
f.write('color: ' + config['color'] + '\n')
f.write('theme: ' + config['theme'] + '\n')
f.write('window-controls: ' + config['window-controls'] + '\n')
f.write('dir: ' + config['dir'] + '\n')
f.write('enabled: ' + ' '.join(config['enabled']) + '\n\n')
f.write('gnome: ' + str(config['desktop_versions']['gnome']) + '\n')
f.write('firefox: ' + ' '.join(config['firefox']) + '\n')
f.write('vscode: ' + ' '.join(config['vscode']) + '\n')
f.write(f"flatpak: {config['flatpak']}\n\n")
for theme in VARIANTS['enableable']:
if theme == 'extra':
continue
try:
key = f'{theme}_version'
version = config[key]
f.write(f'{key}: {version}\n')
except KeyError:
pass
f.write('\n')
for theme in config['old']:
for de in config['old'][theme]:
if config['old'][theme][de] != '' and not config['old'][theme][de].startswith('qualia'):
f.write(f"old_{theme}_{de}: {config['old'][theme][de]}\n")
f.close()
def theme_variants(self, pref):
'''
Finds out what variant of the themes should be installed (light or dark).
Parameters:
pref (str) : 'light' 'dark' or 'auto'.
'''
config = self.config
if pref == 'auto':
try:
theme_setting = check_output(['gsettings', 'get', 'org.gnome.desktop.interface', 'color-scheme'])
except(FileNotFoundError):
theme_setting=None
if theme_setting == 'prefer-dark':
config['color_scheme']='prefer-dark'
elif theme_setting == 'default':
config['color_scheme']='default'
else:
print(f"{BRED}Can't detect system light/dark theme preference.{NC}")
return False # Failed
elif pref == 'light':
config['color_scheme']='default'
elif pref == 'dark':
config['color_scheme']='prefer-dark'
return True
class Spinner(threading.Thread):
'''
Draws a spinner in terminal on long processes.
Attributes:
theme (str) : The theme name to be printed in the message.
directory (str) : The directory to be printed in the message.
process (Thread) : The thread of the install process we are waiting on.
'''
def __init__(self, theme, directory, process):
super().__init__(target=self._msg)
self.theme = theme
self.directory = directory
self.process = process
self.start()
def _msg(self):
'''Prints spinner with message'''
message = f'{BGREEN}Installing{NC} the {BOLD}{self.theme}{NC} in {BOLD}{self.directory}{NC}'
message_len = len(f'Installing the {self.theme} in {self.directory}') # without the color escape codes
if verbose:
print(message)
while self.process.is_alive():
pass
else:
while self.process.is_alive():
for cursor in '|/-\\':
columns = int(os.popen('stty size', 'r').read().split()[1])
sys.stdout.write(f'\r\033[?25l' + message + ' ' + f'\033[J\033[{columns}G' + cursor + '\n')
sys.stdout.write(f'\033[{(message_len + 2) // (columns) + 1}A')
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\r' + message + '\033[J\n\033[?25h')
sys.stdout.flush()
class InstallThread(threading.Thread):
'''
A thread for installing a part of the theme.
Attributes:
process (object) : Callable object to run in a thread.
'''
def __init__(self, process):
cd(REPO_DIR)
self.spinner = None
super().__init__(target=process)
self.start()
self.join()
while self.spinner is not None and self.spinner.is_alive(): # Wait for spinner thread to close
pass
def get_version(self):
'''
Return current version of the submodule.
Returns:
version (str) : 40 character commit hash
'''
version = check_output(['git', 'rev-parse', 'HEAD'])
return version
def updated(self):
'''
Sets the global variable 'updated' to true.
'''
global updated
updated = True
class InstallDgAdwGtk3(InstallThread):
'''
Installs the dg-adw-gtk3 theme if it was updated or if being forced.
Attributes:
config (dict) : Dictionary that contains the configuration.
'''
def __init__(self, config):
self.config = config
super().__init__(process=self._install)
def _install(self):
config = self.config
if not no_update:
run_command(['git', 'submodule', 'update', '--init', 'src/dg-adw-gtk3'])
cd(SRC['gtk3'])
install_dir = f'{HOME}/.local' if config['dir'] in ('default', 'home') else '/usr'
options = [f'-Dprefix={install_dir}']
if 'gtk4-libadwaita' in config['enabled'] and 'gtk3' in config['enabled']:
pretty_string = 'qualia GTK3 and Libadwaita GTK4 themes'
up_to_date = 'qualia GTK3 and Libadwaita GTK4 themes are'
options += ['-Dgtk4=true', '-Dgtk3=true']
elif 'gtk3' in self.config['enabled']:
pretty_string = 'qualia GTK3 theme'
up_to_date = 'qualia GTK3 theme is'
options += ['-Dgtk4=false', '-Dgtk3=true']
else:
pretty_string = 'Libadwaita GTK4 theme'
up_to_date = 'Libadwaita GTK4 theme is'
options += ['-Dgtk4=true', '-Dgtk3=false']
options += [f"-Dwindow-controls={config['window-controls']}"]
options += [f"-Daccent-colors={'' if config['color'] == 'orange' else config['color']}"]
if self.get_version() != self.config['dg-adw-gtk3_version'] or reinstall or update_dir or update_window_controls:
self.spinner = Spinner(f"{config['color']} {pretty_string}", f'{install_dir}/share/themes', self)
if not os.path.isdir('build'):
run_command(['meson', 'build'] + options, meson=True)
else:
run_command(['meson', 'configure', 'build'] + options, meson=True)
run_command(['meson', 'configure', 'build'] + options, meson=True)
run_command(['ninja', '-C', 'build', 'install'], meson=True)
self.updated()
else:
print(f'The {up_to_date} up to date.')