From 9de88ad3b84d6211df5e8174db5c8b71a3bd3811 Mon Sep 17 00:00:00 2001 From: Paul Bienkowski Date: Thu, 29 Jun 2017 16:47:13 +0200 Subject: [PATCH] template: refactor to allow {color1.rgb} notation and remove putty exception --- pywal/export_colors.py | 34 +++++++++--------------- pywal/templates/colors-putty.reg | 32 +++++++++++----------- pywal/util.py | 11 ++++++++ tests/test_export_colors.py | 9 ++++--- tests/test_files/templates/test_template | 3 +++ tests/test_files/test_template | 2 -- 6 files changed, 48 insertions(+), 43 deletions(-) create mode 100644 tests/test_files/templates/test_template delete mode 100644 tests/test_files/test_template diff --git a/pywal/export_colors.py b/pywal/export_colors.py index 7af9741..036b661 100755 --- a/pywal/export_colors.py +++ b/pywal/export_colors.py @@ -2,7 +2,6 @@ Export colors in various formats. """ import os -import pathlib from pywal.settings import CACHE_DIR from pywal import util @@ -28,29 +27,22 @@ def template(colors, input_file, output_dir): print(f"export: Exported {template_file}.") -def export_all_templates(colors): +def export_all_templates(colors, template_dir=None, output_dir=CACHE_DIR): """Export all template files.""" + # Add the template dir to module path. - template_dir = os.path.join(os.path.dirname(__file__), "templates") + template_dir = template_dir or os.path.join(os.path.dirname(__file__), "templates") - # Exclude these templates from the loop. - # The excluded templates need color - # conversion or other intervention. - exclude = ["colors-putty.reg"] + # Merge all colors (specials and normals) into one dict so we can access + # their values simpler. + all_colors = dict() + for v in colors.values(): + all_colors.update(v) - # Merge both dicts so we can access their - # values simpler. - colors["colors"].update(colors["special"]) - - # Convert colors to other format. - colors_rgb = {k: util.hex_to_rgb(v) for k, v in colors["colors"].items()} + # Turn all those colors into util.Color instances for accessing the + # .hex and .rgb formats + all_colors = {k: util.Color(v) for k, v in all_colors.items()} # pylint: disable=W0106 - [template(colors["colors"], file.path, CACHE_DIR) - for file in os.scandir(template_dir) - if file.name not in exclude] - - # Call 'putty' manually since it needs RGB - # colors. - putty_file = template_dir / pathlib.Path("colors-putty.reg") - template(colors_rgb, putty_file, CACHE_DIR) + for file in os.scandir(template_dir): + template(all_colors, file.path, output_dir) diff --git a/pywal/templates/colors-putty.reg b/pywal/templates/colors-putty.reg index e8a5b6c..f4c06c0 100644 --- a/pywal/templates/colors-putty.reg +++ b/pywal/templates/colors-putty.reg @@ -1,19 +1,19 @@ Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\Default%20Settings]] -"colour0"="{color0}" -"colour1"="{color1}" -"colour2"="{color2}" -"colour3"="{color3}" -"colour4"="{color4}" -"colour5"="{color5}" -"colour6"="{color6}" -"colour7"="{color7}" -"colour8"="{color8}" -"colour9"="{color9}" -"colour10"="{color10}" -"colour11"="{color11}" -"colour12"="{color12}" -"colour13"="{color13}" -"colour14"="{color14}" -"colour15"="{color15}" +"colour0"="{color0.rgb}" +"colour1"="{color1.rgb}" +"colour2"="{color2.rgb}" +"colour3"="{color3.rgb}" +"colour4"="{color4.rgb}" +"colour5"="{color5.rgb}" +"colour6"="{color6.rgb}" +"colour7"="{color7.rgb}" +"colour8"="{color8.rgb}" +"colour9"="{color9.rgb}" +"colour10"="{color10.rgb}" +"colour11"="{color11.rgb}" +"colour12"="{color12.rgb}" +"colour13"="{color13.rgb}" +"colour14"="{color14.rgb}" +"colour15"="{color15.rgb}" diff --git a/pywal/util.py b/pywal/util.py index 69a9d9d..820be10 100755 --- a/pywal/util.py +++ b/pywal/util.py @@ -51,3 +51,14 @@ def disown(*cmd): stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, preexec_fn=os.setpgrp) + +class Color(object): + def __init__(self, hex): + self.hex = hex + + def __str__(self): + return self.hex + + @property + def rgb(self): + return hex_to_rgb(self.hex) diff --git a/tests/test_export_colors.py b/tests/test_export_colors.py index 90af273..b458aa5 100755 --- a/tests/test_export_colors.py +++ b/tests/test_export_colors.py @@ -19,14 +19,15 @@ class TestExportColors(unittest.TestCase): # values simpler. COLORS["colors"].update(COLORS["special"]) - # Dirs to use. - tmp_dir = pathlib.Path("/tmp") - test_template = pathlib.Path("tests/test_files/test_template") - export_colors.template(COLORS["colors"], test_template, tmp_dir) + output_dir = pathlib.Path("/tmp") + export_colors.export_all_templates(COLORS, pathlib.Path("tests/test_files/templates"), output_dir) result = pathlib.Path("/tmp/test_template").is_file() self.assertTrue(result) + content = pathlib.Path("/tmp/test_template").read_text() + self.assertEqual(content, '\n'.join(["test1 #3A5130", "test2 #3A5130", "test3 58,81,48", ""])) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_files/templates/test_template b/tests/test_files/templates/test_template new file mode 100644 index 0000000..af40ce7 --- /dev/null +++ b/tests/test_files/templates/test_template @@ -0,0 +1,3 @@ +test1 {color0} +test2 {background} +test3 {background.rgb} diff --git a/tests/test_files/test_template b/tests/test_files/test_template deleted file mode 100644 index 167e17b..0000000 --- a/tests/test_files/test_template +++ /dev/null @@ -1,2 +0,0 @@ -test {color0} -test {background}