-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathchameleon.py
executable file
·462 lines (426 loc) · 14 KB
/
chameleon.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
#!/usr/bin/env python3
import argparse
import yaml
import os
import subprocess
from os.path import expanduser
from shutil import copyfile
from whichcraft import which
from shutil import which
# _ _ _ _ _ _ _ _
# | | | | |_(_) (_) |_(_) ___ ___
# | | | | __| | | | __| |/ _ \/ __|
# | |_| | |_| | | | |_| | __/\__ \
# \___/ \__|_|_|_|\__|_|\___||___/
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def print_status(status, program):
if(status == 0):
print(bcolors.OKGREEN + "⚡"+bcolors.ENDC+bcolors.OKBLUE+" Themed "+program + bcolors.ENDC)
elif(status == 1):
print(bcolors.FAIL + "X"+bcolors.ENDC+bcolors.WARNING+" Failed to theme "+program + bcolors.ENDC)
elif(status == 2):
print(bcolors.FAIL + "X"+bcolors.ENDC+bcolors.WARNING+" User hook "+program + " failed"+bcolors.ENDC)
elif(status == 3):
print(bcolors.OKGREEN + "⚡"+bcolors.ENDC+bcolors.OKBLUE+" User hook "+program + " succeeded"+bcolors.ENDC)
def is_tool(name):
"""Check whether `name` is on PATH and marked as executable."""
return which(name) is not None
# ____ __ _
# / ___|___ _ __ / _(_) __ _
# | | / _ \| '_ \| |_| |/ _` |
# | |__| (_) | | | | _| | (_| |
# \____\___/|_| |_|_| |_|\__, |
# |___/
# get home directory
home = expanduser("~")
# get config path
config_dir = home + '/.config/chameleon'
config_path = home + '/.config/chameleon/config.yaml'
# Parse command line arguments
def parse_args():
parser = argparse.ArgumentParser(description='Chameleon Arguments', usage='%(prog)s -i/t [image/theme] [arguments for wal]')
parser.add_argument('--theme', '-t', metavar='theme', type=str, nargs='?', help='a color scheme name to use as a theme')
parser.add_argument('--image', '-i', metavar='image', type=str, nargs='?', help='an image file to use as a theme')
args = parser.parse_known_args()
return args
# Parse user config file
def parse_yaml():
with open(config_path, mode='r') as file:
file_dict = yaml.full_load(file)
file.close()
return file_dict
# Print keys from a dictionary
def print_keys(dictionary):
for key in dictionary:
print(key)
if isinstance(dictionary[key], dict):
print_keys(dictionary[key])
# _____ _ _
# |_ _| |__ ___ _ __ ___ (_)_ __ __ _
# | | | '_ \ / _ \ '_ ` _ \| | '_ \ / _` |
# | | | | | | __/ | | | | | | | | | (_| |
# |_| |_| |_|\___|_| |_| |_|_|_| |_|\__, |
# |___/
# Detects and runs hooks set by user
def user_hooks(config):
# if the user has defined hooks
if("hooks" in config):
# iterate through the hooks
for value in config["hooks"].items():
# If the user has a simple command to run
if(type(value[1]) == str):
# print("single command found")
try:
arglist = value[1].split(' ')
p = subprocess.Popen(arglist)
p.wait()
except:
print_status(2, value[0])
return
# User has specified options for the hook
elif(type(value[1]) == dict):
path = value[1].get('directory', './')
arglist = value[1].get('command').split(' ')
try:
p = subprocess.Popen(arglist, cwd=path)
p.wait()
except:
print_status(2, value[0])
return
print_status(3, value[0])
def call_wal(args, walargs):
# if we are calling wal on an image
if(args.image):
try:
imagepath = os.path.abspath(args.image)
commandlist = ["wal", "-i", imagepath]
commandlist.extend(walargs)
p = subprocess.Popen(commandlist)
p.wait()
except:
print_status(1, "pywal")
return
# if we are using a prebuilt or custom colorscheme
elif(args.theme):
try:
commandlist = ["wal", "--theme", args.theme]
commandlist.extend(walargs)
p = subprocess.Popen(commandlist)
p.wait()
except:
print_status(1, "pywal")
return
print_status(0, "pywal")
def call_slickpywal(config):
# Check to see if the user defined a custom path
if("slickpywal" in config):
try:
p = subprocess.Popen(["slick-pywal"], cwd=config["slickpywal"]["path"])
p.wait()
except:
print_status(1, "SlickGreeter Pywal")
return
# Check to see if it exists somewhere in the path
elif(is_tool("slick-pywal")):
try:
p = subprocess.Popen(["slick-pywal"])
p.wait()
except:
print_status(1, "SlickGreeter Pywal")
return
else:
return
print_status(0, "SlickGreeter Pywal")
return
def call_pywalneopixels(config):
# Check to see if the user defined a custom path
if("pywalneopixels" in config):
try:
commandstring = config["pywalneopixels"]["path"]+"startLEDs"
os.system(commandstring)
except:
print_status(1, "Pywal NeoPixel")
# Check to see if it exists somewhere in the path
elif(is_tool("startLEDS")):
try:
os.system("startLEDS")
except:
print_status(1, "Pywal NeoPixel")
return
# it is not detected it all
else:
return
print_status(0, "Pywal NeoPixel")
def call_wal_discord(config):
# Check to see if the user defined a custom path
if("waldiscord" in config):
try:
m = subprocess.Popen(["wal-discord", "-t"], cwd=config["waldiscord"]["path"])
m.wait()
except:
print_status(1, "Discord")
return
print_status(0, "Discord")
# Check to see if it exists somewhere in the path
elif(is_tool("wal-discord")):
try:
n = subprocess.Popen(["wal-discord", "-t"])
n.wait()
except:
print_status(1, "Discord")
return
print_status(0, "Discord")
else:
return
def call_pywal_discord(config):
# Check to see if the user defined a custom path
if("pywaldiscord" in config):
try:
m = subprocess.Popen(["pywal-discord"], cwd=config["pywaldiscord"]["path"])
m.wait()
except:
print_status(1, "Discord")
return
print_status(0, "Discord")
# Check to see if it exists somewhere in the path
elif(is_tool("pywal-discord")):
try:
n = subprocess.Popen(["pywal-discord"])
n.wait()
except:
print_status(1, "Discord")
return
print_status(0, "Discord")
else:
return
def call_xmenu(config):
# Check to see if the user defined a custom path
if("xmenu" in config):
try:
# make xmenu
null = open("/dev/null")
m = subprocess.Popen(["make"], cwd=config["xmenu"]["path"], stdout=subprocess.DEVNULL)
m.wait()
retval = m.returncode
null.close()
# if making failed
if(retval != 0):
print_status(1, "Xmenu")
return
# Install the new files
i = subprocess.Popen(["sudo", "make", "install"], cwd=config["xmenu"]["path"], stdout=subprocess.DEVNULL)
i.wait()
retval = m.returncode
# if installation failed
if(retval != 0):
print_status(1, "Xmenu")
return
# If we found a config but something went wrong
except:
print_status(1, "Xmenu")
return
print_status(0, "Xmenu")
# no config for xmenu, just return
else:
return
def call_cordless(config):
if("cordless" in config):
# the full path to the cordless theme template
templatepath = config['cordless']['path']
try:
with open(home+"/.config/cordless/theme.json", "w") as theme:
commandstring = "go run "+templatepath
commandstring = commandstring.split(' ')
g = subprocess.Popen(commandstring, stdout=theme)
g.wait()
except:
print_status(1, "cordless")
return
print_status(0, "cordless")
def call_razercli(config):
if("razercli" in config):
try:
p = subprocess.Popen([config['razercli']['path']+"razer-cli", '-a'])
p.wait()
except:
print_status(1, "Razer Devices")
return
elif(is_tool("razer-cli")):
try:
p = subprocess.Popen(["razer-cli", "-a"])
p.wait()
except:
print_status(1, "Razer Devices")
return
else:
return
print_status(0, "Razer Devices")
def call_spicetify(config):
if("spicetify" in config):
try:
null = open("/dev/null")
path = config['spicetify']['path']
p = subprocess.Popen([path+"spicetify", 'update'], stdout=null)
p.wait()
null.close()
except:
print_status(1, "Spicetify")
return
elif(is_tool("spicetify")):
try:
null = open("/dev/null")
p = subprocess.Popen(["spicetify", "apply"], stdout=null)
p.wait()
null.close()
except:
print_status(1, "Spicetify")
return
else:
return
print_status(0, "Spicetify")
def call_tellegrampallettegen(config):
if("telegrampalletegen" in config):
print("telegram was found in config")
try:
path = config['telegrampalletegen']['path']
p = subprocess.Popen([path+"telegram-pallete-gen", '--wal'])
p.wait()
except:
print_status(1, "Telegram Pallete")
return
elif(is_tool("telegram-palette-gen")):
print("telegram was found to be tool")
try:
p = subprocess.Popen(["telegram-pallete-gen", "--wal"])
p.wait()
except:
print_status(1, "Telegram Pallete")
return
else:
return
print_status(0, "Telegram Pallete")
def call_oomoxicons(config):
if("oomoxicons" in config):
try:
command = config['oomoxicons']['command']
themepath = config['oomoxicons']['themepath']
fullcommand = command+" "+themepath+" > /dev/null"
os.system(fullcommand)
except:
print_status(1, "Oomox Icons")
return
print_status(0, "Oomox Icons")
def call_oomoxgtk(config):
if("oomoxgtk" in config):
try:
themepath = config['oomoxgtk']['themepath']
fullcommand = "oomox-cli"+" "+themepath+" > /dev/null"
os.system(fullcommand)
except:
print_status(1, "Oomox GTK")
return
print_status(0, "Oomox GTK")
# Spicetify is preferred
def call_oomoxspotify(config):
if("oomoxspotify" in config):
if(config['oomoxspotify']['enabled'] == "True"):
try:
spotifypath = config['oomoxspotify']['spotifypath']
fullcommand = "oomoxify-cli"+" "+home+"/.cache/wal/colors-oomox"+" -s "+spotifypath
os.system(fullcommand)
except:
print_status(1, "Oomox Spotify")
return
print_status(0, "Oomox Spofify")
else:
return
else:
return
def call_pywalfox(config):
if("pywalfox" in config):
try:
path = config['pywalfox']['path']
p = subprocess.Popen([path+"pywalfox", 'update'])
p.wait()
except:
print_status(1, "Pywalfox")
return
elif(is_tool("pywalfox")):
try:
p = subprocess.Popen(["pywalfox", "update"])
p.wait()
except:
print_status(1, "Pywalfox")
return
print_status(0, "Pywalfox")
def call_gnuplot_pywal(config):
if("gnuplotpywal" in config):
try:
path = config['gnuplotpywal']['path']
file = open(home+"/.gnuplot", "w+")
p = subprocess.Popen([path+"gengnuplotconfig"], stdout=file)
p.wait()
file.close()
except:
print_status(1, "Gnuplot")
return
elif(is_tool("gengnuplotconfig")):
try:
file = open(home+"/.gnuplot", "w+")
p = subprocess.Popen(["gengnuplotconfig"], stdout=file)
p.wait()
file.close()
except:
print_status(1, "Gnuplot")
return
print_status(0, "Gnuplot")
def call_starttree(config):
if("starttree" in config):
try:
path = config['starttree']['path']
p = subprocess.Popen([path+"generate.py"])
p.wait()
except:
print_status(1, "StartTree")
return
elif(is_tool("starttree.py")):
try:
p = subprocess.Popen(["starttree.py"], stdout=subprocess.DEVNULL)
p.wait()
except:
print_status(1, "StartTree")
return
else:
return
print_status(0, "StartTree")
def theme(config, args, walargs):
call_wal(args, walargs)
call_slickpywal(config)
call_pywalneopixels(config)
call_wal_discord(config)
call_xmenu(config)
call_cordless(config)
call_razercli(config)
call_spicetify(config)
call_tellegrampallettegen(config)
call_oomoxicons(config)
call_oomoxgtk(config)
call_oomoxspotify(config)
call_pywalfox(config)
call_gnuplot_pywal(config)
call_starttree(config)
user_hooks(config)
def main():
config = parse_yaml()
args, walargs = parse_args()
theme(config, args, walargs)
if __name__ == '__main__':
main()