i3-colors/src/config.py

149 lines
4.2 KiB
Python
Raw Normal View History

import re,tempfile,shutil,theme
2019-10-06 21:57:32 -04:00
2019-10-07 13:38:30 -04:00
config_keys=["client.focused",
2019-10-07 21:15:06 -04:00
"client.focused_inactive",
"client.unfocused",
"client.urgent",
"separator",
"background",
"statusline",
"focused_workspace",
"active_workspace",
"inactive_workspace",
"urgent_workspace"]
2019-10-06 21:57:32 -04:00
2019-10-07 21:00:48 -04:00
##### Parsing Utils #####
2019-10-06 21:57:32 -04:00
def contains(r,line):
2019-10-07 21:00:48 -04:00
"""
Return true if line contains regex r.
"""
2019-10-06 21:57:32 -04:00
return(re.match(r,line)!=None)
2019-10-07 21:00:48 -04:00
def before_token(token, line):
"""
Return what is before token in line.
"""
found=re.search(".*"+token,line)
if found:
return(found.group(0)[:-len(token)])
return("")
def sorted_items(d):
return(sorted(d.items()))
2019-10-07 19:08:16 -04:00
def no_comment(line):
2019-10-07 21:00:48 -04:00
"""
Remove comment from a line.
"""
2019-10-07 19:08:16 -04:00
newline=""
for ch in line:
if ch=='#':
break
else:
newline+=ch
return(newline)
2019-10-07 21:00:48 -04:00
#########################
2019-10-07 19:08:16 -04:00
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
2019-10-07 21:15:06 -04:00
beforeColor=before_token("colors",line).strip()
2019-10-07 19:08:16 -04:00
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)
def extract_theme(config_file):
"""
Return a ThemeBuilder object of the config_file file.
"""
f=open(config_file,"r")
build=theme.ThemeBuilder()
in_colors=False
for line_orig in f:
line=no_comment(line_orig)
is_theme_line=False
for key in config_keys:
if contains(".*"+key+"\s",line):
is_theme_line=True
if contains(".*colors",line):
in_colors=True
if contains("(\s)*set",line): # If var definition
build.parse(line_orig)
elif is_theme_line or in_colors:
build.parse(line_orig) # Seems to by strange to have comment here
if contains(".*}",line) and in_colors:
in_colors=False
f.close()
return(build)
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:
2019-10-07 21:15:06 -04:00
beforeBrace=before_token("}",line).strip()
2019-10-07 19:08:16 -04:00
if len(beforeBrace)>0:
tmp.write(beforeBrace+"\n")
tmp.write(" colors {\n")
for key,value in sorted_items(bar_theme):
2019-10-06 21:57:32 -04:00
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 sorted_items(client_theme):
2019-10-07 21:00:48 -04:00
f.write("client."+key+" "+value["border"]+" "+value["background"]+" "+value["text"]+" "+value["indicator"]+" "+value["child_border"]+"\n")
2019-10-06 21:57:32 -04:00
f.close()
2019-10-07 13:38:30 -04:00
2019-10-09 08:00:34 -04:00
def apply(config_file,theme,dry=False):
2019-10-07 19:08:16 -04:00
tmp=extract(config_file)
write_theme(tmp,theme)
2019-10-09 08:00:34 -04:00
f=open(tmp,mode="r")
new_config=f.read()
f.close()
if not(dry):
shutil.move(tmp,config_file)
return(new_config)
2019-10-06 21:57:32 -04:00