template: refactor to allow {color1.rgb} notation and remove putty exception

This commit is contained in:
Paul Bienkowski 2017-06-29 16:47:13 +02:00
parent fce5036554
commit 9de88ad3b8
6 changed files with 48 additions and 43 deletions

View File

@ -2,7 +2,6 @@
Export colors in various formats. Export colors in various formats.
""" """
import os import os
import pathlib
from pywal.settings import CACHE_DIR from pywal.settings import CACHE_DIR
from pywal import util from pywal import util
@ -28,29 +27,22 @@ def template(colors, input_file, output_dir):
print(f"export: Exported {template_file}.") 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.""" """Export all template files."""
# Add the template dir to module path. # 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. # Merge all colors (specials and normals) into one dict so we can access
# The excluded templates need color # their values simpler.
# conversion or other intervention. all_colors = dict()
exclude = ["colors-putty.reg"] for v in colors.values():
all_colors.update(v)
# Merge both dicts so we can access their # Turn all those colors into util.Color instances for accessing the
# values simpler. # .hex and .rgb formats
colors["colors"].update(colors["special"]) all_colors = {k: util.Color(v) for k, v in all_colors.items()}
# Convert colors to other format.
colors_rgb = {k: util.hex_to_rgb(v) for k, v in colors["colors"].items()}
# pylint: disable=W0106 # pylint: disable=W0106
[template(colors["colors"], file.path, CACHE_DIR) for file in os.scandir(template_dir):
for file in os.scandir(template_dir) template(all_colors, file.path, output_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)

View File

@ -1,19 +1,19 @@
Windows Registry Editor Version 5.00 Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\Default%20Settings]] [HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\Default%20Settings]]
"colour0"="{color0}" "colour0"="{color0.rgb}"
"colour1"="{color1}" "colour1"="{color1.rgb}"
"colour2"="{color2}" "colour2"="{color2.rgb}"
"colour3"="{color3}" "colour3"="{color3.rgb}"
"colour4"="{color4}" "colour4"="{color4.rgb}"
"colour5"="{color5}" "colour5"="{color5.rgb}"
"colour6"="{color6}" "colour6"="{color6.rgb}"
"colour7"="{color7}" "colour7"="{color7.rgb}"
"colour8"="{color8}" "colour8"="{color8.rgb}"
"colour9"="{color9}" "colour9"="{color9.rgb}"
"colour10"="{color10}" "colour10"="{color10.rgb}"
"colour11"="{color11}" "colour11"="{color11.rgb}"
"colour12"="{color12}" "colour12"="{color12.rgb}"
"colour13"="{color13}" "colour13"="{color13.rgb}"
"colour14"="{color14}" "colour14"="{color14.rgb}"
"colour15"="{color15}" "colour15"="{color15.rgb}"

View File

@ -51,3 +51,14 @@ def disown(*cmd):
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
preexec_fn=os.setpgrp) 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)

View File

@ -19,14 +19,15 @@ class TestExportColors(unittest.TestCase):
# values simpler. # values simpler.
COLORS["colors"].update(COLORS["special"]) COLORS["colors"].update(COLORS["special"])
# Dirs to use. output_dir = pathlib.Path("/tmp")
tmp_dir = pathlib.Path("/tmp") export_colors.export_all_templates(COLORS, pathlib.Path("tests/test_files/templates"), output_dir)
test_template = pathlib.Path("tests/test_files/test_template")
export_colors.template(COLORS["colors"], test_template, tmp_dir)
result = pathlib.Path("/tmp/test_template").is_file() result = pathlib.Path("/tmp/test_template").is_file()
self.assertTrue(result) 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__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -0,0 +1,3 @@
test1 {color0}
test2 {background}
test3 {background.rgb}

View File

@ -1,2 +0,0 @@
test {color0}
test {background}