export: Make function more general for the test.

This commit is contained in:
Dylan Araps 2017-06-29 19:52:43 +10:00
parent 4ec8c16d0a
commit 5e5908a013
5 changed files with 41 additions and 62 deletions

View File

@ -8,24 +8,24 @@ from pywal.settings import CACHE_DIR, TEMPLATE_DIR
from pywal import util
def template(colors, input_file):
def template(colors, input_file, output_dir):
"""Read template file, substitute markers and
save the file elsewhere."""
template_file = pathlib.Path(TEMPLATE_DIR).joinpath(input_file)
export_file = pathlib.Path(CACHE_DIR).joinpath(input_file)
# Get the template name.
template_file = os.path.basename(input_file)
# Import the template.
with open(template_file) as file:
with open(input_file) as file:
template_data = file.readlines()
# Format the markers.
template_data = "".join(template_data).format(**colors)
# Export the template.
with open(export_file, "w") as file:
file.write(template_data)
output_file = output_dir / template_file
util.save_file(template_data, output_file)
print(f"export: Exported {input_file}.")
print(f"export: Exported {template_file}.")
def export_all_templates(colors):
@ -43,10 +43,11 @@ def export_all_templates(colors):
colors_rgb = {k: util.hex_to_rgb(v) for k, v in colors["colors"].items()}
# pylint: disable=W0106
[template(colors["colors"], file.name)
[template(colors["colors"], file.path, CACHE_DIR)
for file in os.scandir(TEMPLATE_DIR)
if file not in exclude]
if file.name not in exclude]
# Call 'putty' manually since it needs RGB
# colors.
template(colors_rgb, "colors-putty.reg")
putty_file = TEMPLATE_DIR / pathlib.Path("colors-putty.reg")
template(colors_rgb, putty_file, CACHE_DIR)

16
pywal/templates/colors Normal file
View File

@ -0,0 +1,16 @@
{color0}
{color1}
{color2}
{color3}
{color4}
{color5}
{color6}
{color7}
{color8}
{color9}
{color10}
{color11}
{color12}
{color13}
{color14}
{color15}

View File

@ -13,11 +13,18 @@ COLORS = util.read_file_json("tests/test_files/test_file.json")
class TestExportColors(unittest.TestCase):
"""Test the export_colors functions."""
def test_save_colors(self):
"""> Export colors to a file."""
tmp_file = pathlib.Path("/tmp/test_file.json")
export_colors.save_colors(COLORS, tmp_file, "plain colors")
result = tmp_file.is_file()
def test_template(self):
"""> Test substitutions in template file."""
# Merge both dicts so we can access their
# 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)
result = pathlib.Path("/tmp/test_template").is_file()
self.assertTrue(result)

View File

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

View File

@ -1,47 +0,0 @@
"""Test format functions."""
import unittest
from pywal import format_colors
from pywal import util
# Import colors.
COLORS = util.read_file_json("tests/test_files/test_file.json")
class TestFormatColors(unittest.TestCase):
"""Test the format_colors functions."""
def test_plain(self):
"""> Convert colors to plain."""
result = format_colors.plain(COLORS)
self.assertEqual(result[0], "#3A5130\n")
def test_shell(self):
"""> Convert colors to shell variables."""
result = format_colors.shell(COLORS)
self.assertEqual(result[0], "color0='#3A5130'\n")
def test_css(self):
"""> Convert colors to css variables."""
result = format_colors.css(COLORS)
self.assertEqual(result[1], "\t--color0: #3A5130;\n")
def test_scss(self):
"""> Convert colors to scss variables."""
result = format_colors.scss(COLORS)
self.assertEqual(result[0], "$color0: #3A5130;\n")
def test_putty(self):
"""> Convert colors to putty theme."""
result = format_colors.putty(COLORS)
self.assertEqual(result[2], "\"colour0\"=\"58,81,48\"\n")
def test_xrdb(self):
"""> Convert colors to putty theme."""
result = format_colors.xrdb(COLORS)
self.assertEqual(result[6], "*.color0: #3A5130\n*color0: #3A5130\n")
if __name__ == "__main__":
unittest.main()