i3-colors/src/config.py

73 lines
2 KiB
Python
Raw Normal View History

2019-10-06 21:57:32 -04:00
#!/usr/bin/python
import re,tempfile,shutil
2019-10-07 13:38:30 -04:00
config_keys=["client.focused",
2019-10-06 21:57:32 -04:00
"client.focused_inactive",
"client.unfocused",
"client.urgent",
"separator",
"background",
"statusline",
"focused_workspace",
"active_workspace",
"inactive_workspace",
"urgent_workspace"]
def contains(r,line):
return(re.match(r,line)!=None)
def extract_config(config_file):
f=open(config_file,"r")
tmp=tempfile.NamedTemporaryFile(mode="w",delete=False)
for line in f:
is_theme_line=False
2019-10-07 13:38:30 -04:00
for key in config_keys:
2019-10-06 21:57:32 -04:00
if contains(".*"+key+"\s",line):
is_theme_line=True
if not(is_theme_line):
tmp.write(line)
f.close()
tmp.close()
return(tmp.name)
2019-10-06 23:05:44 -04:00
def safe_get(theme,key):
if key in theme:
return(theme[key])
return("")
2019-10-06 21:57:32 -04:00
def apply_to_config(tmp_config,theme):
f=open(tmp_config,mode="r")
tmp=tempfile.NamedTemporaryFile(mode="w",delete=False)
##### Apply bar theme #####
2019-10-06 23:05:44 -04:00
bar_theme=theme["bar_colors"]
2019-10-06 21:57:32 -04:00
for line in f:
if contains(".*colors\s{",line):
tmp.write(line)
for key,value in bar_theme.items():
if not(isinstance(value,dict)):
tmp.write("\t"+key+" "+value+"\n")
else:
tmp.write("\t"+key+" "+value["border"]+" "+value["background"]+" "+value["text"]+"\n")
else:
tmp.write(line)
tmp.close()
f.close()
shutil.move(tmp.name,tmp_config)
##### Apply client theme #####
2019-10-06 23:05:44 -04:00
client_theme=theme["window_colors"]
2019-10-06 21:57:32 -04:00
f=open(tmp_config,mode="a")
for key,value in client_theme.items():
2019-10-06 23:05:44 -04:00
f.write("client."+key+" "+value["border"]+" "+value["background"]+" "+value["text"]+" "+value["indicator"]+" "+safe_get(value,"child_border")+"\n")
2019-10-06 21:57:32 -04:00
f.close()
2019-10-07 13:38:30 -04:00
2019-10-06 21:57:32 -04:00
def apply_theme(config_file,theme):
tmp=extract_config(config_file)
apply_to_config(tmp,theme)
2019-10-06 23:05:44 -04:00
shutil.move(tmp,config_file)
2019-10-06 21:57:32 -04:00