From 88bdd9ab013bad324e1f3f4ad8516fd6d55ca312 Mon Sep 17 00:00:00 2001 From: dylan araps Date: Thu, 20 Jul 2017 13:40:31 +1000 Subject: api: Use wal file in __main__.py --- pywal/export.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'pywal/export.py') diff --git a/pywal/export.py b/pywal/export.py index 9eea198..40024b0 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -3,7 +3,6 @@ Export colors in various formats. """ import os -from pywal.settings import CACHE_DIR from pywal import util @@ -27,12 +26,16 @@ def template(colors, input_file, output_dir): print(f"export: Exported {template_file}.") -def export_all_templates(colors, template_dir=None, output_dir=CACHE_DIR): +def export_all_templates(colors, template_dir=None, output_dir=None): """Export all template files.""" # Add the template dir to module path. template_dir = template_dir or \ os.path.join(os.path.dirname(__file__), "templates") + # Convert path strings into Path types. + template_dir = util.str_to_path(template_dir) + output_dir = util.str_to_path(output_dir) + # Merge all colors (specials and normals) into one dict so we can access # their values simpler. all_colors = {"wallpaper": colors["wallpaper"], -- cgit v1.2.3 From a3d4b3d9f348fd308ad8a55f5ef71263e11b6875 Mon Sep 17 00:00:00 2001 From: dylan araps Date: Thu, 20 Jul 2017 13:44:47 +1000 Subject: api: Move CACHE_DIR --- pywal/export.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'pywal/export.py') diff --git a/pywal/export.py b/pywal/export.py index 40024b0..68ae0eb 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -3,6 +3,7 @@ Export colors in various formats. """ import os +from pywal.settings import CACHE_DIR from pywal import util @@ -26,7 +27,7 @@ def template(colors, input_file, output_dir): print(f"export: Exported {template_file}.") -def export_all_templates(colors, template_dir=None, output_dir=None): +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 = template_dir or \ -- cgit v1.2.3 From 52cd5e5f1a7c972a593fe55a853afd43111531e9 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 20 Jul 2017 23:19:13 +1000 Subject: General: Unhardcode all CACHE_DIR and COLOR_COUNT usage. --- pywal/export.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'pywal/export.py') diff --git a/pywal/export.py b/pywal/export.py index 68ae0eb..06ad215 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -3,7 +3,6 @@ Export colors in various formats. """ import os -from pywal.settings import CACHE_DIR from pywal import util @@ -27,16 +26,12 @@ def template(colors, input_file, output_dir): print(f"export: Exported {template_file}.") -def export_all_templates(colors, template_dir=None, output_dir=CACHE_DIR): +def export_all_templates(colors, output_dir, template_dir=None): """Export all template files.""" # Add the template dir to module path. template_dir = template_dir or \ os.path.join(os.path.dirname(__file__), "templates") - # Convert path strings into Path types. - template_dir = util.str_to_path(template_dir) - output_dir = util.str_to_path(output_dir) - # Merge all colors (specials and normals) into one dict so we can access # their values simpler. all_colors = {"wallpaper": colors["wallpaper"], -- cgit v1.2.3 From 12f9211cd4a255226d151545e0532c8507af7361 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 09:46:45 +1000 Subject: general: Remove comments that just repeat what the code does. --- pywal/export.py | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) (limited to 'pywal/export.py') diff --git a/pywal/export.py b/pywal/export.py index 06ad215..1dc72bf 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -9,37 +9,21 @@ from pywal import util def template(colors, input_file, output_dir): """Read template file, substitute markers and save the file elsewhere.""" - # Import the template. - with open(input_file) as file: - template_data = file.readlines() - - # Format the markers. + template_data = util.read_file_raw(input_file) template_data = "".join(template_data).format(**colors) - - # Get the template name. - template_file = os.path.basename(input_file) - - # Export the template. - output_file = output_dir / template_file - util.save_file(template_data, output_file) - - print(f"export: Exported {template_file}.") + template_name = os.path.basename(input_file) + util.save_file(template_data, output_dir / template_name) + print(f"export: Exported {template_name}.") def export_all_templates(colors, output_dir, template_dir=None): """Export all template files.""" - # Add the template dir to module path. template_dir = template_dir or \ os.path.join(os.path.dirname(__file__), "templates") - # Merge all colors (specials and normals) into one dict so we can access - # their values simpler. all_colors = {"wallpaper": colors["wallpaper"], **colors["special"], **colors["colors"]} - - # 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 -- cgit v1.2.3 From c8dd8f4d03fc79b5594e8d873e0aba77e360796a Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 10:01:14 +1000 Subject: general: Fixes. --- pywal/export.py | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 pywal/export.py (limited to 'pywal/export.py') diff --git a/pywal/export.py b/pywal/export.py deleted file mode 100644 index 1dc72bf..0000000 --- a/pywal/export.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -Export colors in various formats. -""" -import os - -from pywal import util - - -def template(colors, input_file, output_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, output_dir / template_name) - print(f"export: Exported {template_name}.") - - -def export_all_templates(colors, output_dir, template_dir=None): - """Export all template files.""" - template_dir = template_dir or \ - os.path.join(os.path.dirname(__file__), "templates") - - all_colors = {"wallpaper": colors["wallpaper"], - **colors["special"], - **colors["colors"]} - all_colors = {k: util.Color(v) for k, v in all_colors.items()} - - # pylint: disable=W0106 - [template(all_colors, file.path, output_dir) - for file in os.scandir(template_dir)] -- cgit v1.2.3 From 377cc7e68ce2576ad928fdd6efb40cabdf0f19dc Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 23 Jul 2017 10:57:33 +1000 Subject: api: Changed export arguments. --- pywal/export.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 pywal/export.py (limited to 'pywal/export.py') 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.") -- cgit v1.2.3