This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
themes_manager.py
213 lines (175 loc) · 9.43 KB
/
themes_manager.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
import sys
import argparse
import os
import json
import shutil
import subprocess
def check_for_git():
try:
output = subprocess.check_output(["git", "--version"])
return True
except FileNotFoundError:
print("ERROR: Git not found! Can't download theme from the internet.", file=sys.stderr)
return False
def check_git_remote(remote_url):
try:
output = subprocess.check_output(["git", "ls-remote", remote_url], env=dict(os.environ, GIT_ASKPASS="/bin/true"), stderr=subprocess.PIPE)
return True
except subprocess.CalledProcessError:
print("ERROR: Invalid remote " + remote_url + "!", file=os.stderr)
return False
def git_pull(folder):
try:
print("Updating " + folder + "...", file=sys.stderr)
output = subprocess.check_output(["git", "-C", folder, "pull"], env=dict(os.environ, GIT_ASKPASS="/bin/true"), stderr=subprocess.PIPE)
return True
except subprocess.CalledProcessError:
return False
def git_clone(url, folder):
try:
print("Cloning " + url + " into " + folder + "...", file=sys.stderr)
output = subprocess.check_output(["git", "clone", url, folder, "--recurse-submodules"], env=dict(os.environ, GIT_ASKPASS="/bin/true"), stderr=subprocess.PIPE)
return True
except subprocess.CalledProcessError:
return False
def get_icons_list():
"""
Load icon list from file
"""
with open(os.path.dirname(os.path.realpath(__file__)) + os.path.sep + "icons.json", "r") as json_file:
data = json.load(json_file)
return data
def get_data(theme, path):
"""
Load theme from file
"""
try:
with open(path + os.path.sep + theme + ".json", "r") as json_file:
data = json.load(json_file)
except FileNotFoundError:
print("ERROR: Theme " + theme + " doesn't exist!", file=sys.stderr)
sys.exit(3)
return data
def write_palette_h(data, file_p):
"""
Write the header to file_p
"""
file_p.write("#ifndef ESCHER_PALETTE_H\n")
file_p.write("#define ESCHER_PALETTE_H\n\n")
file_p.write("#include <kandinsky/color.h>\n")
file_p.write("#include <stddef.h>\n\n")
file_p.write("class Palette {\n")
file_p.write("public:\n")
for key in data["colors"].keys():
if type(data["colors"][key]) is str:
file_p.write(" constexpr static KDColor " + key + " = KDColor::RGB24(0x" + data["colors"][key] + ");\n")
else:
for sub_key in data["colors"][key].keys():
file_p.write(" constexpr static KDColor " + key + sub_key + " = KDColor::RGB24(0x" + data["colors"][key][sub_key] + ");\n")
# Default values - Sometimes never used
file_p.write(" constexpr static KDColor YellowDark = KDColor::RGB24(0xffb734);\n")
file_p.write(" constexpr static KDColor YellowLight = KDColor::RGB24(0xffcc7b);\n")
file_p.write(" constexpr static KDColor PurpleBright = KDColor::RGB24(0x656975);\n")
file_p.write(" constexpr static KDColor PurpleDark = KDColor::RGB24(0x414147);\n")
file_p.write(" constexpr static KDColor GreyWhite = KDColor::RGB24(0xf5f5f5);\n")
file_p.write(" constexpr static KDColor GreyBright = KDColor::RGB24(0xececec);\n")
file_p.write(" constexpr static KDColor GreyMiddle = KDColor::RGB24(0xd9d9d9);\n")
file_p.write(" constexpr static KDColor GreyDark = KDColor::RGB24(0xa7a7a7);\n")
file_p.write(" constexpr static KDColor GreyVeryDark = KDColor::RGB24(0x8c8c8c);\n")
file_p.write(" constexpr static KDColor Select = KDColor::RGB24(0xd4d7e0);\n")
file_p.write(" constexpr static KDColor SelectDark = KDColor::RGB24(0xb0b8d8);\n")
file_p.write(" constexpr static KDColor WallScreen = KDColor::RGB24(0xf7f9fa);\n")
file_p.write(" constexpr static KDColor WallScreenDark = KDColor::RGB24(0xe0e6ed);\n")
file_p.write(" constexpr static KDColor SubTab = KDColor::RGB24(0xb8bbc5);\n")
file_p.write(" constexpr static KDColor LowBattery = KDColor::RGB24(0xf30211);\n")
file_p.write(" constexpr static KDColor Red = KDColor::RGB24(0xff000c);\n")
file_p.write(" constexpr static KDColor RedLight = KDColor::RGB24(0xfe6363);\n")
file_p.write(" constexpr static KDColor Magenta = KDColor::RGB24(0xff0588);\n")
file_p.write(" constexpr static KDColor Turquoise = KDColor::RGB24(0x60c1ec);\n")
file_p.write(" constexpr static KDColor Pink = KDColor::RGB24(0xffabb6);\n")
file_p.write(" constexpr static KDColor Blue = KDColor::RGB24(0x5075f2);\n")
file_p.write(" constexpr static KDColor BlueLight = KDColor::RGB24(0x718fee);\n")
file_p.write(" constexpr static KDColor Orange = KDColor::RGB24(0xfe871f);\n")
file_p.write(" constexpr static KDColor Green = KDColor::RGB24(0x50c102);\n")
file_p.write(" constexpr static KDColor GreenLight = KDColor::RGB24(0x52db8f);\n")
file_p.write(" constexpr static KDColor Brown = KDColor::RGB24(0x8d7350);\n")
file_p.write(" constexpr static KDColor Purple = KDColor::RGB24(0x6e2d79);\n")
file_p.write(" constexpr static KDColor BlueishGrey = KDColor::RGB24(0x919ea4);\n")
file_p.write(" constexpr static KDColor Cyan = KDColorBlue;\n")
# End
file_p.write(" constexpr static KDColor DataColor[] = {Red, Blue, Green, YellowDark, Magenta, Turquoise, Pink, Orange};\n")
file_p.write(" constexpr static KDColor DataColorLight[] = {RedLight, BlueLight, GreenLight, YellowLight};\n")
file_p.write(" constexpr static KDColor AtomColor[] = {\n")
file_p.write(" AtomUnknown, AtomAlkaliMetal, AtomAlkaliEarthMetal, AtomLanthanide, AtomActinide, AtomTransitionMetal,\n")
file_p.write(" AtomPostTransitionMetal, AtomMetalloid, AtomHalogen, AtomReactiveNonmetal, AtomNobleGas\n")
file_p.write(" };\n\n")
file_p.write(" constexpr static size_t numberOfDataColors() { return sizeof(DataColor)/sizeof(KDColor); }\n")
file_p.write(" constexpr static size_t numberOfLightDataColors() { return sizeof(DataColorLight)/sizeof(KDColor); }\n")
file_p.write(" static KDColor nextDataColor(int * colorIndex);\n")
file_p.write("};\n\n")
file_p.write("#endif\n")
def handle_git(args):
output_folder = os.path.basename(args.repo)
output_folder = output_folder[:-4] if output_folder.endswith(".git") else output_folder # Remove .git extension if present.
output_path = os.path.dirname(os.path.realpath(__file__)) + os.path.sep + "themes" + os.path.sep + "git" + os.path.sep + output_folder
if (not args.icon): # Assume .h is called before icons, avoiding lot of pull for nothing.
if (os.path.exists(output_path)): # If directory exists, try to pull
if (not git_pull(output_path)): # If can't pull, delete and re-clone.
shutil.rmtree(output_path)
git_clone(args.repo, output_path)
else: # If directory doesn't exist, clone.
git_clone(args.repo, output_path)
handle_theme(args, output_path)
def handle_theme(args, path):
data = get_data(args.theme, path)
if (args.icon):
# Get the icon in the icon theme folder
icons = get_icons_list()
icon_path = path + os.path.sep + data["icons"] + os.path.sep + icons[args.output.replace(args.build_dir, "")]
# Check if the file exists
if os.path.isfile(icon_path):
# If yes, copy from theme
shutil.copyfile(icon_path, args.output)
else:
# If no, copy from src
print(" (!!) Icon " + icons[args.output.replace(args.build_dir, "")] + " not found in icon theme " + data["icons"] + ". Using default!")
shutil.copyfile(args.output.replace(args.build_dir, ""), args.output)
else:
if (args.stdout):
write_palette_h(data, sys.stdout)
else:
with open(args.output, "w") as palette_file:
write_palette_h(data, palette_file)
def main(args):
if (args.list):
print(" ==== Local themes ====")
for file_info in os.listdir(os.path.dirname(os.path.realpath(__file__)) + os.path.sep + "themes" + os.path.sep + "local"):
if (file_info.endswith(".json")):
filename = os.path.splitext(file_info)[0]
print(filename)
sys.exit(0)
else:
if (args.theme == None or args.repo == None):
print("Please specify repo and theme or use --list!", file=sys.stderr)
sys.exit(2)
if args.repo == "local":
handle_theme(args, os.path.dirname(os.path.realpath(__file__)) + os.path.sep + "themes" + os.path.sep + "local")
else:
if check_for_git():
if (check_git_remote(args.repo)):
handle_git(args)
else:
sys.exit(5)
else:
sys.exit(6)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Process the themes.")
parser.add_argument("repo", nargs="?", help="git remote from wtich to get the themes from. Set to \"local\" for included themes")
parser.add_argument("theme", nargs="?", help="the name of the theme")
parser.add_argument("output", nargs="?", help="path to the output header file")
parser.add_argument("build_dir", nargs="?", help="path to the output folder")
parser.add_argument("-l", "--list", help="list locals themes", action="store_true")
parser.add_argument("-i", "--icon", help="outputs an icon instead of a header", action="store_true")
parser.add_argument("--stdout", help="print palette.h to stdout", action="store_true")
args = parser.parse_args()
main(args)