From 377cc7e68ce2576ad928fdd6efb40cabdf0f19dc Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 23 Jul 2017 10:57:33 +1000 Subject: [PATCH] api: Changed export arguments. --- pywal/__init__.py | 26 ++++++++---------- pywal/export.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++ pywal/reload.py | 12 ++++----- pywal/template.py | 49 ---------------------------------- 4 files changed, 85 insertions(+), 70 deletions(-) create mode 100644 pywal/export.py delete mode 100644 pywal/template.py diff --git a/pywal/__init__.py b/pywal/__init__.py index 4a850ca..e9c0603 100644 --- a/pywal/__init__.py +++ b/pywal/__init__.py @@ -10,23 +10,19 @@ Created by Dylan Araps. """ from .settings import __version__ -from .colors import get as create_palette -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 -from .template import export -from .wallpaper import change as set_wallpaper +from . import colors +from . import export +from . import image +from . import reload +from . import sequences +from . import wallpaper __all__ = [ "__version__", - "create_palette", - "export_all", + "colors", "export", - "get_image", - "reload_colors", - "reload_env", - "send_sequences", - "set_wallpaper", + "image", + "reload", + "sequences", + "wallpaper", ] diff --git a/pywal/export.py b/pywal/export.py new file mode 100644 index 0000000..316de7b --- /dev/null +++ b/pywal/export.py @@ -0,0 +1,68 @@ +""" +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, output_file=None): + """Read template file, substitute markers and + save the file elsewhere.""" + template_data = util.read_file_raw(input_file) + template_data = "".join(template_data).format(**colors) + + util.save_file(template_data, output_file) + + +def flatten_colors(colors): + """Prepare colors to be exported. + Flatten dicts and convert colors to util.Color()""" + all_colors = {"wallpaper": colors["wallpaper"], + **colors["special"], + **colors["colors"]} + return {k: util.Color(v) for k, v in all_colors.items()} + + +def get_export_type(export_type): + """Convert template type to the right filename.""" + return { + "css": "colors.css", + "json": "colors.json", + "konsole": "colors-konsole.colorscheme", + "putty": "colors-putty.reg", + "scss": "colors.scss", + "shell": "colors.sh", + "xresources": "colors.Xresources", + }.get(export_type, export_type) + + +def every(colors, output_dir=__cache_dir__): + """Export all template files.""" + all_colors = flatten_colors(colors) + output_dir = pathlib.Path(output_dir) + + for file in os.scandir(TEMPLATE_DIR): + template(all_colors, file.path, output_dir / file.name) + + print(f"export: Exported all files.") + + +def color(colors, export_type, output_file=None): + """Export a single template file.""" + all_colors = flatten_colors(colors) + + template_name = get_export_type(export_type) + template_file = TEMPLATE_DIR / template_name + output_file = output_file or __cache_dir__ / template_name + + if template_file.is_file(): + template(all_colors, template_file, output_file) + print(f"export: Exported {export_type}.") + else: + print(f"[!] warning: template '{export_type}' doesn't exist.") diff --git a/pywal/reload.py b/pywal/reload.py index da09fe0..2648a70 100644 --- a/pywal/reload.py +++ b/pywal/reload.py @@ -9,7 +9,7 @@ from .settings import __cache_dir__ from . import util -def reload_xrdb(cache_dir): +def xrdb(cache_dir): """Merge the colors into the X db so new terminals use them.""" if shutil.which("xrdb"): subprocess.call(["xrdb", "-merge", cache_dir / "colors.Xresources"], @@ -17,13 +17,13 @@ def reload_xrdb(cache_dir): stderr=subprocess.DEVNULL) -def reload_i3(): +def i3(): """Reload i3 colors.""" if shutil.which("i3-msg"): util.disown("i3-msg", "reload") -def reload_polybar(): +def polybar(): """Reload polybar colors.""" if shutil.which("polybar"): util.disown("pkill", "-USR1", "polybar") @@ -31,9 +31,9 @@ def reload_polybar(): def env(cache_dir=__cache_dir__): """Reload environment.""" - reload_xrdb(cache_dir) - reload_i3() - reload_polybar() + xrdb(cache_dir) + i3() + polybar() print("reload: Reloaded environment.") diff --git a/pywal/template.py b/pywal/template.py deleted file mode 100644 index a7961e3..0000000 --- a/pywal/template.py +++ /dev/null @@ -1,49 +0,0 @@ -""" -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.""" - template_data = util.read_file_raw(input_file) - template_data = "".join(template_data).format(**colors) - template_name = os.path.basename(input_file) - - util.save_file(template_data, cache_dir / template_name) - print(f"export: Exported {template_name}.") - - -def flatten_colors(colors): - """Prepare colors to be exported. (Flatten dicts)""" - all_colors = {"wallpaper": colors["wallpaper"], - **colors["special"], - **colors["colors"]} - return {k: util.Color(v) for k, v in all_colors.items()} - - -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.")