-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
127 lines (114 loc) · 5.1 KB
/
server.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import io, os, glob, re, ntpath, yaml
import base64
from bottle import route, run, template, request
from subprocess import call
from PIL import Image, ImageOps
# ██ ██ ██
# ░██ ░░ ░██
# ███ ██░██ ██ ██████░██ ██ █████ ██████ ██████
# ░░██ █ ░██░██████ ░██░░░░██ ░██ ██ ██░░░██░░██░░█ ██░░░░ █████
# ░██ ███░██░██░░░██░██ ██ ░████ ░███████ ░██ ░ ░░█████ ░░░░░
# ░████░████░██ ░██░██ ██ ░██░██ ░██░░░░ ░██ ░░░░░██
# ███░ ░░░██░██ ░██░██ ██████░██░░██░░██████░███ ██████
# ░░░ ░░░ ░░ ░░ ░░ ░░░░░░ ░░ ░░ ░░░░░░ ░░░ ░░░░░░
#
# ██████ █████ ██████ ██ ██ █████ ██████
# ██░░░░ ██░░░██░░██░░█░██ ░██ ██░░░██░░██░░█
# ░░█████ ░███████ ░██ ░ ░░██ ░██ ░███████ ░██ ░
# ░░░░░██░██░░░░ ░██ ░░████ ░██░░░░ ░██
# ██████ ░░██████░███ ░░██ ░░██████░███
# ░░░░░░ ░░░░░░ ░░░ ░░ ░░░░░░ ░░░
#
# https://github.com/97-109-107/whizkers-server
# Depends on whizkers (https://github.com/metakirby5/whizkers) by metakirby5
# The command in your PATH to load the them, will be executed with the theme name as the last element
command_load = os.getenv('WHIZ_EXE_CMD', "whizkers")
# The command in your PATH to restart the programs/wm (by default fullsalvo's wz-utils)
command_reload = os.getenv('WHIZ_RELOAD_CMD', "reload-desktop")
# Port for the server
port = os.getenv('WHIZ_SERV_PORT', 9696)
# location of your yaml variable sets
variable_sets_path = os.getenv('WHIZ_SERV_VPATH',os.path.expanduser("~/.config/zenbu/variable_sets/"))
@route('/static/:path#.+#', name='static')
def static(path):
return static_file(path, root='static')
def renderThemes():
output = []
for (dir, _, files) in os.walk(variable_sets_path):
for f in files:
path = os.path.join(dir, f)
if os.path.exists(path):
filename = ntpath.basename(path)
theme_name = os.path.splitext(filename)[0]
with open(path, 'r') as f:
try:
content = yaml.load(f)
colors, wallpapers, fg, bg = parse(content)
if wallpapers or colors:
output.append({
'theme_name': theme_name,
'filename': filename,
'fullpath': path,
'colors': colors,
'fg': fg,
'bg': bg,
'wallpapers': make_thumb(wallpapers)
})
except yaml.YAMLError as exc:
print("Skipping. There's a yaml parsing error:", exc)
output = sorted(output, key=lambda k: k['filename'].lower())
return (template('index', e=output), output)
def parse(d, path=[], colors=None, wallpapers=None, bg=None, fg=None):
colors = colors or {}
wallpapers = wallpapers or ""
fg = fg or ""
bg = bg or ""
for k,v in d.items():
if isinstance(v, (int, float, list, type(None))):
pass
elif isinstance(v, str):
path.append(k)
if looks_like_color(v):
# Check if any of the colors are called background or foreground, expose them for easy handling
if(path[-1] == "background"):
bg = v
if(path[-1] == "foreground"):
fg = v
colors[".".join(path)] = v
elif looks_like_wallpaper(v):
wallpapers = os.path.expanduser(v)
path.pop()
elif isinstance(v, dict):
path.append(k)
colors, wallpapers, fg, bg = parse(v, path, colors, wallpapers, fg, bg)
path.pop()
else:
print("###Type {} not recognized: {}.{}={}".format(type(v), ".".join(path),k, v))
return colors, wallpapers, fg, bg
def looks_like_wallpaper(s):
if re.findall(r'\.(jpe?g|png|gif|bmp)', s, re.IGNORECASE or re.DOTALL):
return True
return False
def looks_like_color(s):
if re.findall(r'#(?:[a-fA-F0-9]{3}|[a-fA-F0-9]{6})', s, re.DOTALL):
return True
return False
def make_thumb(wallpapers):
if len(wallpapers) > 0:
with open(wallpapers, "rb") as imageFile:
str = base64.b64encode(imageFile.read())
return str
@route('/')
def index():
theme = request.query.get( "theme" )
template, output = renderThemes()
if theme:
#filter through the whole list of themes for the one just requested, get the full path
theme_path = list(filter(lambda i: i['theme_name'] == theme, output))[0]['fullpath']
print("located ")
call([command_load, theme_path])
call([command_reload])
return template
run(host='0.0.0.0', port=port, reloader=True)