i3-colors/src/config.py

111 lines
3.1 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)
2019-10-07 19:08:16 -04:00
def no_comment(line):
newline=""
for ch in line:
if ch=='#':
break
else:
newline+=ch
return(newline)
def extract(config_file):
"""
Return a temporary file path containing the current user configuration
without any related theme/colors lines.
"""
2019-10-06 21:57:32 -04:00
f=open(config_file,"r")
tmp=tempfile.NamedTemporaryFile(mode="w",delete=False)
2019-10-07 19:08:16 -04:00
in_colors=False
for line_orig in f:
line=no_comment(line_orig)
2019-10-06 21:57:32 -04:00
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
2019-10-07 19:08:16 -04:00
if contains(".*colors",line):
in_colors=True
beforeColor=re.search(".*colors",line).group(0)[:-6]
if len(beforeColor)>0:
tmp.write(beforeColor+"\n")
if not(is_theme_line) and not(in_colors):
tmp.write(line_orig)
if contains(".*}",line) and in_colors:
in_colors=False
2019-10-06 21:57:32 -04:00
f.close()
tmp.close()
return(tmp.name)
2019-10-07 19:08:16 -04:00
2019-10-06 23:05:44 -04:00
def safe_get(theme,key):
2019-10-07 19:08:16 -04:00
"""
TODO: To remove (useless now)
"""
2019-10-06 23:05:44 -04:00
if key in theme:
return(theme[key])
return("")
2019-10-07 19:08:16 -04:00
def write_theme(tmp_config,theme):
"""
Write the theme in a temporary file
based on tmp_config file.
"""
2019-10-06 21:57:32 -04:00
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-07 19:08:16 -04:00
in_bar=False
for line_orig in f:
line=no_comment(line_orig)
if contains("(\s)*bar",line):
in_bar=True
if contains(".*}",line) and in_bar:
beforeBrace=re.search(".*}",line).group(0)[:-1]
if len(beforeBrace)>0:
tmp.write(beforeBrace+"\n")
tmp.write(" colors {\n")
2019-10-06 21:57:32 -04:00
for key,value in bar_theme.items():
if not(isinstance(value,dict)):
2019-10-07 19:08:16 -04:00
tmp.write(" "+key+" "+value+"\n")
2019-10-06 21:57:32 -04:00
else:
2019-10-07 19:08:16 -04:00
tmp.write(" "+key+" "+value["border"]+" "+value["background"]+" "+value["text"]+"\n")
tmp.write(" }\n}\n")
in_bar=False
2019-10-06 21:57:32 -04:00
else:
2019-10-07 19:08:16 -04:00
tmp.write(line_orig)
2019-10-06 21:57:32 -04:00
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-07 19:08:16 -04:00
def apply(config_file,theme):
tmp=extract(config_file)
write_theme(tmp,theme)
2019-10-06 23:05:44 -04:00
shutil.move(tmp,config_file)
2019-10-06 21:57:32 -04:00