i3-colors/src/theme.py

74 lines
2.7 KiB
Python
Raw Normal View History

2019-10-07 13:38:30 -04:00
import yaml,re, sys
2019-10-06 23:05:44 -04:00
def configure(theme):
2019-10-07 21:15:06 -04:00
"""
Apply user define colors and apply some correction factors.
"""
2019-10-06 23:05:44 -04:00
if "colors" in theme:
colors=theme["colors"]
window_colors=theme["window_colors"]
2019-10-07 21:00:48 -04:00
##### Apply colors to window #####
2019-10-06 23:05:44 -04:00
for key1,value1 in window_colors.items():
for key2,value2 in value1.items():
if re.match("#.*",value2) == None:
window_colors[key1][key2]=colors[value2]
theme["window_colors"]=window_colors
2019-10-07 21:00:48 -04:00
##################################
##### Apply color to bar #####
2019-10-06 23:05:44 -04:00
bar_colors=theme["bar_colors"]
for key1,value1 in bar_colors.items():
if isinstance(value1,dict):
for key2,value2 in value1.items():
if re.match("#.*",value2) == None:
bar_colors[key1][key2]=colors[value2]
else:
if re.match("#.*",value1) == None:
bar_colors[key1]=colors[value1]
theme["bar_colors"]=bar_colors
2019-10-07 21:00:48 -04:00
##############################
##### I3-style theme do not include child_border by default #####
window_colors=theme["window_colors"]
for key,value in window_colors.items():
if not("child_border" in value):
newvalue=value
theme["window_colors"][key]["child_border"]=newvalue["border"] # Set it to border by default
#################################################################
2019-10-06 23:05:44 -04:00
return(theme)
2019-10-07 13:38:30 -04:00
def validate(theme):
2019-10-07 21:15:06 -04:00
"""
Abort if theme is in a wrong format.
"""
2019-10-07 13:38:30 -04:00
abort=lambda msg: sys.exit("Error while loading theme: "+msg)
inv_key=lambda key: abort("invalid key \""+key+"\"")
for key,value in theme.items():
if not(key in ["meta","colors","window_colors","bar_colors"]):
inv_key(key)
if key=="bar_colors":
for key,value in value.items():
if not(key in ["separator","background","statusline",
"focused_workspace","active_workspace","inactive_workspace","urgent_workspace"]):
inv_key(key)
if key=="window_colors":
for key,value in value.items():
if not(key in ["focused","focused_inactive","unfocused","urgent","child_border"]):
inv_key(key)
2019-10-06 23:05:44 -04:00
def load(theme_file):
2019-10-07 21:15:06 -04:00
"""
Load a theme as a dict():
- Open YAML file
- Parse it as a dict
- Configure the parsed dict
- Validate the configured dict
"""
2019-10-06 23:05:44 -04:00
f=open(theme_file,mode="r")
theme=yaml.load(f,Loader=yaml.FullLoader)
f.close()
2019-10-07 13:38:30 -04:00
theme=configure(theme)
validate(theme)
return(theme)