mirror of
https://gitlab.com/manzerbredes/i3-colors.git
synced 2025-04-07 09:06:25 +02:00
Debug/refactoring
This commit is contained in:
parent
3e8c84e135
commit
9adb0c6b4b
3 changed files with 61 additions and 23 deletions
11
TODO.org
11
TODO.org
|
@ -1,18 +1,19 @@
|
||||||
#+TITLE: "i3-colors Project Todo List"
|
#+TITLE: "i3-colors Project Todo List"
|
||||||
#+AUTHOR: "Loic Guegan"
|
#+AUTHOR: "Loic Guegan"
|
||||||
|
|
||||||
- Refactoring [1/3]:
|
- Refactoring [1/2]:
|
||||||
- [X] Change src/parser.py name => now config.py
|
- [X] Change src/parser.py name => now config.py
|
||||||
- [ ] Improve function/variables names
|
- [ ] Improve function/variables names
|
||||||
|
|
||||||
- YAML Theme Parser [0/2]:
|
- YAML Theme Parser [1/2]:
|
||||||
- [ ] Improve yaml theme loader reliability
|
- [ ] Improve yaml theme loader reliability
|
||||||
- [ ] Allow several metadata
|
- [X] Allow several metadata
|
||||||
|
|
||||||
- Theme applying algorithm [0/3]:
|
- Theme applying algorithm [1/3]:
|
||||||
- [ ] Improve efficiency
|
- [ ] Improve efficiency
|
||||||
|
- [ ] Bugs may appear here! Found them...
|
||||||
- [ ] Assume that theme to apply is well formated (checked in YAML Theme Parser)
|
- [ ] Assume that theme to apply is well formated (checked in YAML Theme Parser)
|
||||||
- [ ] If there is no "colors" field in "bar" section, we still need to apply theme
|
- [X] If there is no "colors" field in "bar" section, we still need to apply theme
|
||||||
|
|
||||||
- General [1/2]:
|
- General [1/2]:
|
||||||
- [ ] Improve user API
|
- [ ] Improve user API
|
||||||
|
|
|
@ -17,42 +17,80 @@ config_keys=["client.focused",
|
||||||
def contains(r,line):
|
def contains(r,line):
|
||||||
return(re.match(r,line)!=None)
|
return(re.match(r,line)!=None)
|
||||||
|
|
||||||
def extract_config(config_file):
|
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.
|
||||||
|
"""
|
||||||
f=open(config_file,"r")
|
f=open(config_file,"r")
|
||||||
tmp=tempfile.NamedTemporaryFile(mode="w",delete=False)
|
tmp=tempfile.NamedTemporaryFile(mode="w",delete=False)
|
||||||
for line in f:
|
|
||||||
|
in_colors=False
|
||||||
|
for line_orig in f:
|
||||||
|
line=no_comment(line_orig)
|
||||||
is_theme_line=False
|
is_theme_line=False
|
||||||
for key in config_keys:
|
for key in config_keys:
|
||||||
if contains(".*"+key+"\s",line):
|
if contains(".*"+key+"\s",line):
|
||||||
is_theme_line=True
|
is_theme_line=True
|
||||||
if not(is_theme_line):
|
if contains(".*colors",line):
|
||||||
tmp.write(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
|
||||||
f.close()
|
f.close()
|
||||||
tmp.close()
|
tmp.close()
|
||||||
return(tmp.name)
|
return(tmp.name)
|
||||||
|
|
||||||
def safe_get(theme,key):
|
def safe_get(theme,key):
|
||||||
|
"""
|
||||||
|
TODO: To remove (useless now)
|
||||||
|
"""
|
||||||
if key in theme:
|
if key in theme:
|
||||||
return(theme[key])
|
return(theme[key])
|
||||||
return("")
|
return("")
|
||||||
|
|
||||||
def apply_to_config(tmp_config,theme):
|
def write_theme(tmp_config,theme):
|
||||||
|
"""
|
||||||
|
Write the theme in a temporary file
|
||||||
|
based on tmp_config file.
|
||||||
|
"""
|
||||||
f=open(tmp_config,mode="r")
|
f=open(tmp_config,mode="r")
|
||||||
tmp=tempfile.NamedTemporaryFile(mode="w",delete=False)
|
tmp=tempfile.NamedTemporaryFile(mode="w",delete=False)
|
||||||
|
|
||||||
##### Apply bar theme #####
|
##### Apply bar theme #####
|
||||||
bar_theme=theme["bar_colors"]
|
bar_theme=theme["bar_colors"]
|
||||||
for line in f:
|
in_bar=False
|
||||||
if contains(".*colors\s{",line):
|
for line_orig in f:
|
||||||
tmp.write(line)
|
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")
|
||||||
for key,value in bar_theme.items():
|
for key,value in bar_theme.items():
|
||||||
if not(isinstance(value,dict)):
|
if not(isinstance(value,dict)):
|
||||||
tmp.write("\t"+key+" "+value+"\n")
|
tmp.write(" "+key+" "+value+"\n")
|
||||||
else:
|
else:
|
||||||
tmp.write("\t"+key+" "+value["border"]+" "+value["background"]+" "+value["text"]+"\n")
|
tmp.write(" "+key+" "+value["border"]+" "+value["background"]+" "+value["text"]+"\n")
|
||||||
|
tmp.write(" }\n}\n")
|
||||||
|
in_bar=False
|
||||||
else:
|
else:
|
||||||
tmp.write(line)
|
tmp.write(line_orig)
|
||||||
tmp.close()
|
tmp.close()
|
||||||
f.close()
|
f.close()
|
||||||
shutil.move(tmp.name,tmp_config)
|
shutil.move(tmp.name,tmp_config)
|
||||||
|
@ -65,8 +103,8 @@ def apply_to_config(tmp_config,theme):
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
def apply_theme(config_file,theme):
|
def apply(config_file,theme):
|
||||||
tmp=extract_config(config_file)
|
tmp=extract(config_file)
|
||||||
apply_to_config(tmp,theme)
|
write_theme(tmp,theme)
|
||||||
shutil.move(tmp,config_file)
|
shutil.move(tmp,config_file)
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
import config, theme, os, argparse, subprocess
|
import config, theme, os, argparse, subprocess
|
||||||
|
|
||||||
|
|
||||||
##### Utils Functions #####
|
##### Utils Functions #####
|
||||||
def log(msg,title=""):
|
def log(msg,title=""):
|
||||||
if len(title)>0:
|
if len(title)>0:
|
||||||
|
@ -21,7 +20,7 @@ args = args_parser.parse_args()
|
||||||
|
|
||||||
##### Apply Theme #####
|
##### Apply Theme #####
|
||||||
loaded_theme=theme.load(args.theme_path)
|
loaded_theme=theme.load(args.theme_path)
|
||||||
config.apply_theme(os.environ["HOME"]+"/.config/i3/config",loaded_theme)
|
config.apply(os.environ["HOME"]+"/.config/i3/config",loaded_theme)
|
||||||
for meta_key,meta_value in loaded_theme["meta"].items():
|
for meta_key,meta_value in loaded_theme["meta"].items():
|
||||||
log(meta_value,title=meta_key.title())
|
log(meta_value,title=meta_key.title())
|
||||||
if args.restart:
|
if args.restart:
|
||||||
|
|
Loading…
Add table
Reference in a new issue