From d1c2b8023de2585befc5b68e69ed6d0f3ee5d27e Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 23:27:43 +1000 Subject: [PATCH] api: Add function to export individual templates. --- pywal/__init__.py | 6 ++++-- pywal/template.py | 31 ++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/pywal/__init__.py b/pywal/__init__.py index ec61c5c..4a850ca 100644 --- a/pywal/__init__.py +++ b/pywal/__init__.py @@ -15,13 +15,15 @@ from .image import get as get_image from .reload import colors as reload_colors from .reload import env as reload_env from .sequences import send as send_sequences -from .template import export_all as export_all_templates +from .template import export_all +from .template import export from .wallpaper import change as set_wallpaper __all__ = [ "__version__", "create_palette", - "export_all_templates", + "export_all", + "export", "get_image", "reload_colors", "reload_env", diff --git a/pywal/template.py b/pywal/template.py index ca72a12..a7961e3 100644 --- a/pywal/template.py +++ b/pywal/template.py @@ -2,11 +2,15 @@ Export colors in various formats. """ import os +import pathlib from .settings import __cache_dir__ from . import util +TEMPLATE_DIR = pathlib.Path(__file__).parent / "templates" + + def template(colors, input_file, cache_dir): """Read template file, substitute markers and save the file elsewhere.""" @@ -18,15 +22,28 @@ def template(colors, input_file, cache_dir): print(f"export: Exported {template_name}.") -def export_all(colors, cache_dir=__cache_dir__, template_dir=None): - """Export all template files.""" - template_dir = template_dir or \ - os.path.join(os.path.dirname(__file__), "templates") - +def flatten_colors(colors): + """Prepare colors to be exported. (Flatten dicts)""" all_colors = {"wallpaper": colors["wallpaper"], **colors["special"], **colors["colors"]} - all_colors = {k: util.Color(v) for k, v in all_colors.items()} + return {k: util.Color(v) for k, v in all_colors.items()} - for file in os.scandir(template_dir): + +def export_all(colors, cache_dir=__cache_dir__): + """Export all template files.""" + all_colors = flatten_colors(colors) + + for file in os.scandir(TEMPLATE_DIR): template(all_colors, file.path, cache_dir) + + +def export(colors, file, cache_dir=__cache_dir__): + """Export a single template file.""" + all_colors = flatten_colors(colors) + template_file = TEMPLATE_DIR / file + + if template_file.is_file(): + template(all_colors, template_file, cache_dir) + else: + print(f"[!] warning: template '{template_file}' doesn't exist.")