From 8159cebf203d48e9cf2f3ca27e8d76ce6127d7ef Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 7 Oct 2019 09:29:21 -0400 Subject: Improve log+refactoring --- TODO.org | 5 ++- src/config.py | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/i3-theme.py | 12 ++++--- src/parser.py | 107 -------------------------------------------------------- src/theme.py | 1 - 5 files changed, 117 insertions(+), 115 deletions(-) create mode 100755 src/config.py delete mode 100755 src/parser.py diff --git a/TODO.org b/TODO.org index 7af7a04..a54c291 100644 --- a/TODO.org +++ b/TODO.org @@ -1,9 +1,8 @@ #+TITLE: "i3-colors Project Todo List" #+AUTHOR: "Loic Guegan" -- Refactoring [0/3]: - - [ ] Change src/parser.py name - - [ ] Change src/theme.py name +- Refactoring [1/3]: + - [X] Change src/parser.py name => now config.py - [ ] Improve function/variables names - YAML Theme Parser [0/2]: diff --git a/src/config.py b/src/config.py new file mode 100755 index 0000000..fdee9fc --- /dev/null +++ b/src/config.py @@ -0,0 +1,107 @@ +#!/usr/bin/python + +import re,tempfile,shutil + +theme_keys=["client.focused", + "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 + for key in theme_keys: + if contains(".*"+key+"\s",line): + is_theme_line=True + if not(is_theme_line): + tmp.write(line) + + f.close() + tmp.close() + return(tmp.name) + +def safe_get(theme,key): + if key in theme: + return(theme[key]) + return("") + +def apply_to_config(tmp_config,theme): + f=open(tmp_config,mode="r") + tmp=tempfile.NamedTemporaryFile(mode="w",delete=False) + + ##### Apply bar theme ##### + bar_theme=theme["bar_colors"] + 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 ##### + client_theme=theme["window_colors"] + f=open(tmp_config,mode="a") + for key,value in client_theme.items(): + f.write("client."+key+" "+value["border"]+" "+value["background"]+" "+value["text"]+" "+value["indicator"]+" "+safe_get(value,"child_border")+"\n") + f.close() + +def apply_theme(config_file,theme): + tmp=extract_config(config_file) + apply_to_config(tmp,theme) + shutil.move(tmp,config_file) + +theme={ + "bar":{ + "separator": "#666666", + "background": "#333333", + "statusline": "#bbbbbb", + "focused_workspace": { "border":"#888888", + "background": "#dddddd", + "text": "#222222"}, + "active_workspace": { "border": "#333333", + "background": "#555555", + "text": "#bbbbbb"}, + "inactive_workspace": { "border": "#333333", + "background": "#555555", + "text": "#bbbbbb"}, + "urgent_workspace": { "border": "#2f343a", + "background": "#900000", + "text": "#ffffff"}}, + "client": { + "client.focused": { "background": "#888888", + "text": "#dddddd", + "indicator": "#222222", + "child_border": "#2e9ef4"}, + "client.focused_inactive": { "background": "#333333", + "text": "#555555", + "indicator": "#bbbbbb", + "child_border": "#484e50"}, + "client.unfocused": { "background": "#333333", + "text": "#333333", + "indicator": "#888888", + "child_border": "#292d2e"}, + "client.urgent": { "background": "#2f343a", + "text": "#900000", + "indicator": "#ffffff", + "child_border": "#900000"}} +} + diff --git a/src/i3-theme.py b/src/i3-theme.py index fa31b1d..1e367b5 100755 --- a/src/i3-theme.py +++ b/src/i3-theme.py @@ -1,9 +1,13 @@ #!/usr/bin/python -import parser, theme, os, argparse, subprocess +import config, theme, os, argparse, subprocess ##### Utils Functions ##### -def log(title, content): print("\033[92m{}\033[00m: {}" .format(title,content)) +def log(msg,title=""): + if len(title)>0: + print("\033[92m{}\033[00m: {}" .format(title,msg)) + else: + print(msg) ########################### @@ -18,8 +22,8 @@ args = args_parser.parse_args() ##### Apply Theme ##### loaded_theme=theme.load(args.theme_path) for meta_key,meta_value in loaded_theme["meta"].items(): - log(meta_key.title(),meta_value) -parser.apply_theme(os.environ["HOME"]+"/.config/i3/config",loaded_theme) + log(meta_value,title=meta_key.title()) +config.apply_theme(os.environ["HOME"]+"/.config/i3/config",loaded_theme) if args.restart: subprocess.Popen("i3-msg restart".split()) ####################### diff --git a/src/parser.py b/src/parser.py deleted file mode 100755 index fdee9fc..0000000 --- a/src/parser.py +++ /dev/null @@ -1,107 +0,0 @@ -#!/usr/bin/python - -import re,tempfile,shutil - -theme_keys=["client.focused", - "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 - for key in theme_keys: - if contains(".*"+key+"\s",line): - is_theme_line=True - if not(is_theme_line): - tmp.write(line) - - f.close() - tmp.close() - return(tmp.name) - -def safe_get(theme,key): - if key in theme: - return(theme[key]) - return("") - -def apply_to_config(tmp_config,theme): - f=open(tmp_config,mode="r") - tmp=tempfile.NamedTemporaryFile(mode="w",delete=False) - - ##### Apply bar theme ##### - bar_theme=theme["bar_colors"] - 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 ##### - client_theme=theme["window_colors"] - f=open(tmp_config,mode="a") - for key,value in client_theme.items(): - f.write("client."+key+" "+value["border"]+" "+value["background"]+" "+value["text"]+" "+value["indicator"]+" "+safe_get(value,"child_border")+"\n") - f.close() - -def apply_theme(config_file,theme): - tmp=extract_config(config_file) - apply_to_config(tmp,theme) - shutil.move(tmp,config_file) - -theme={ - "bar":{ - "separator": "#666666", - "background": "#333333", - "statusline": "#bbbbbb", - "focused_workspace": { "border":"#888888", - "background": "#dddddd", - "text": "#222222"}, - "active_workspace": { "border": "#333333", - "background": "#555555", - "text": "#bbbbbb"}, - "inactive_workspace": { "border": "#333333", - "background": "#555555", - "text": "#bbbbbb"}, - "urgent_workspace": { "border": "#2f343a", - "background": "#900000", - "text": "#ffffff"}}, - "client": { - "client.focused": { "background": "#888888", - "text": "#dddddd", - "indicator": "#222222", - "child_border": "#2e9ef4"}, - "client.focused_inactive": { "background": "#333333", - "text": "#555555", - "indicator": "#bbbbbb", - "child_border": "#484e50"}, - "client.unfocused": { "background": "#333333", - "text": "#333333", - "indicator": "#888888", - "child_border": "#292d2e"}, - "client.urgent": { "background": "#2f343a", - "text": "#900000", - "indicator": "#ffffff", - "child_border": "#900000"}} -} - diff --git a/src/theme.py b/src/theme.py index 187b8d2..d83b6b5 100644 --- a/src/theme.py +++ b/src/theme.py @@ -1,6 +1,5 @@ import yaml,re - def configure(theme): if "colors" in theme: colors=theme["colors"] -- cgit v1.2.3