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.
"""
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)

View File

@ -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}"

View File

@ -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)

View File

@ -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()

View File

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

View File

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