aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xsrc/config.py28
-rwxr-xr-xsrc/i3-colors.py41
-rw-r--r--src/theme.py38
3 files changed, 91 insertions, 16 deletions
diff --git a/src/config.py b/src/config.py
index c8fa8bd..80e264f 100755
--- a/src/config.py
+++ b/src/config.py
@@ -1,4 +1,4 @@
-import re,tempfile,shutil
+import re,tempfile,shutil,theme
config_keys=["client.focused",
"client.focused_inactive",
@@ -66,7 +66,31 @@ def extract(config_file):
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 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)
+
+
def write_theme(tmp_config,theme):
"""
Write the theme in a temporary file
diff --git a/src/i3-colors.py b/src/i3-colors.py
index 925ff3a..0afafcd 100755
--- a/src/i3-colors.py
+++ b/src/i3-colors.py
@@ -9,21 +9,36 @@ def log(msg,title=""):
print(msg)
###########################
+##### Apply Theme #####
+def apply(args):
+ loaded_theme=theme.load(args.theme_path)
+ config.apply(os.environ["HOME"]+"/.config/i3/config",loaded_theme)
+ for meta_key,meta_value in loaded_theme["meta"].items():
+ log(meta_value,title=meta_key.title())
+ if args.restart:
+ subprocess.Popen("i3-msg restart".split(),stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+#######################
+
+##### Extract Theme #####
+def extract(args):
+ theme=config.extract_theme(args.config_path)
+ theme.dump()
+#######################
##### Parse Arguments #####
-args_parser = argparse.ArgumentParser(description='I3 Window Manager Colors Themer.')
-args_parser.add_argument('theme_path', type=str, nargs='?',
+argsMainParser = argparse.ArgumentParser(description='I3 Window Manager Colors Themer.')
+argsSubParsers = argsMainParser.add_subparsers()
+argsApplyParser = argsSubParsers.add_parser("apply")
+argsApplyParser.add_argument('theme_path', type=str, nargs='?',
help='I3 YAML theme path.')
-args_parser.add_argument('-r', '--restart' ,action='store_true', help='Restart i3 after applying theme.')
-args = args_parser.parse_args()
-###########################
+argsApplyParser.add_argument('-r', '--restart' ,action='store_true', help='Restart i3 after applying theme.')
+argsApplyParser.set_defaults(func=apply)
-##### Apply Theme #####
-loaded_theme=theme.load(args.theme_path)
-config.apply(os.environ["HOME"]+"/.config/i3/config",loaded_theme)
-for meta_key,meta_value in loaded_theme["meta"].items():
- log(meta_value,title=meta_key.title())
-if args.restart:
- subprocess.Popen("i3-msg restart".split(),stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-#######################
+argsExtractParser = argsSubParsers.add_parser("extract")
+argsExtractParser.add_argument('config_path', type=str, nargs='?',
+ help='Extract theme from config file.')
+argsExtractParser.set_defaults(func=extract)
+args = argsMainParser.parse_args()
+args.func(args)
+###########################
diff --git a/src/theme.py b/src/theme.py
index 50a7ee0..fd1461a 100644
--- a/src/theme.py
+++ b/src/theme.py
@@ -56,7 +56,7 @@ def validate(theme):
for key,value in value.items():
if not(key in ["focused","focused_inactive","unfocused","urgent","child_border"]):
inv_key(key)
-
+
def load(theme_file):
"""
Load a theme as a dict():
@@ -71,3 +71,39 @@ def load(theme_file):
theme=configure(theme)
validate(theme)
return(theme)
+
+class ThemeBuilder:
+ def __init__(self):
+ self.theme={"meta": {"description": "Generated From i3-colors"},
+ "window_colors":dict(),
+ "bar_colors":dict()}
+ def dump(self):
+ print(yaml.dump(self.theme))
+
+ def parse(self,line):
+ if re.match("client.*",line):
+ tokens=line.split()
+ key=tokens[0].replace("client.","")
+ tokens.pop(0)
+ subkeys=["border","background","text","indicator","child_border"]
+ self.theme["window_colors"][key]=dict()
+ for token in tokens:
+ self.theme["window_colors"][key][subkeys[0]]=token
+ subkeys.pop(0)
+ elif re.match(".*background.*",line):
+ self.theme["bar_colors"]["background"]=line.split()[1]
+ elif re.match(".*statusline.*",line):
+ self.theme["bar_colors"]["statusline"]=line.split()[1]
+ elif re.match(".*separator.*",line):
+ self.theme["bar_colors"]["separator"]=line.split()[1]
+ elif re.match(".*_workspace.*",line):
+ tokens=line.split()
+ key=tokens[0]
+ tokens.pop(0)
+ subkeys=["border","background","text"]
+ self.theme["bar_colors"][key]=dict()
+ for token in tokens:
+ self.theme["bar_colors"][key][subkeys[0]]=token
+ subkeys.pop(0)
+
+