From 88bdd9ab013bad324e1f3f4ad8516fd6d55ca312 Mon Sep 17 00:00:00 2001 From: dylan araps Date: Thu, 20 Jul 2017 13:40:31 +1000 Subject: [PATCH] api: Use wal file in __main__.py --- examples/example.py | 5 +++++ pywal/__init__.py | 2 ++ pywal/__main__.py | 21 ++++++++------------- pywal/export.py | 7 +++++-- pywal/magic.py | 2 +- pywal/sequences.py | 4 ++-- pywal/util.py | 5 +++++ pywal/wal.py | 19 ++++++++++++------- 8 files changed, 40 insertions(+), 25 deletions(-) diff --git a/examples/example.py b/examples/example.py index 5e9cad2..f81d131 100644 --- a/examples/example.py +++ b/examples/example.py @@ -19,6 +19,11 @@ def main(): # Reload xrdb, i3 and polybar. pywal.reload_env() + # Export template files. + pywal.export_all_templates(colors, + "path/to/templates", + "path/to/save/files/") + # Set the wallpaper. pywal.set_wallpaper(image) diff --git a/pywal/__init__.py b/pywal/__init__.py index 02dbe37..32f33af 100644 --- a/pywal/__init__.py +++ b/pywal/__init__.py @@ -5,7 +5,9 @@ Created by Dylan Araps. # flake8: noqa: F401 from pywal.settings import __version__ from pywal.wal import create_palette +from pywal.wal import export_all_templates from pywal.wal import get_image +from pywal.wal import reload_colors from pywal.wal import reload_env from pywal.wal import send_sequences from pywal.wal import set_wallpaper diff --git a/pywal/__main__.py b/pywal/__main__.py index cdd5001..c429424 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -8,13 +8,8 @@ import shutil import sys from pywal.settings import CACHE_DIR, __version__ -from pywal import export -from pywal import image -from pywal import magic -from pywal import reload -from pywal import sequences +from pywal import wal from pywal import util -from pywal import wallpaper def get_args(): @@ -79,7 +74,7 @@ def process_args(args): # -r if args.r: - sequences.reload_colors(args.t) + wal.reload_colors(args.t) # -v if args.v: @@ -88,8 +83,8 @@ def process_args(args): # -i if args.i: - image_file = image.get_image(args.i) - colors_plain = magic.get_colors(image_file, args.q) + image_file = wal.get_image(args.i) + colors_plain = wal.create_palette(image_file, args.q) # -f elif args.f: @@ -97,13 +92,13 @@ def process_args(args): # -i or -f if args.i or args.f: - sequences.send_sequences(colors_plain, args.t) + wal.send_sequences(colors_plain, args.t) if not args.n: - wallpaper.set_wallpaper(colors_plain["wallpaper"]) + wal.set_wallpaper(colors_plain["wallpaper"]) - export.export_all_templates(colors_plain) - reload.reload_env() + wal.export_all_templates(colors_plain) + wal.reload_env() # -o if args.o: 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"], diff --git a/pywal/magic.py b/pywal/magic.py index 0cc868a..cf468e7 100644 --- a/pywal/magic.py +++ b/pywal/magic.py @@ -53,7 +53,7 @@ def gen_colors(img): return [re.search("#.{6}", str(col)).group(0) for col in raw_colors] -def get_colors(img, quiet): +def get_colors(img, quiet=False): """Get the colorscheme.""" # Cache the wallpaper name. util.save_file(img, CACHE_DIR / "wal") diff --git a/pywal/sequences.py b/pywal/sequences.py index 858c381..2e51bc3 100644 --- a/pywal/sequences.py +++ b/pywal/sequences.py @@ -53,9 +53,9 @@ def send_sequences(colors, vte): print("colors: Set terminal colors") -def reload_colors(vte): +def reload_colors(vte, sequence_file=None): """Reload the current scheme.""" - sequence_file = CACHE_DIR / "sequences" + sequence_file = sequence_file or CACHE_DIR / "sequences" if sequence_file.is_file(): sequences = "".join(util.read_file(sequence_file)) diff --git a/pywal/util.py b/pywal/util.py index 6c5b420..deab13c 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -98,3 +98,8 @@ def disown(*cmd): stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, preexec_fn=os.setpgrp) + + +def str_to_path(str_path): + """Convert a string to a Path type.""" + return pathlib.Path(str_path) diff --git a/pywal/wal.py b/pywal/wal.py index db8dccd..67109f1 100644 --- a/pywal/wal.py +++ b/pywal/wal.py @@ -2,6 +2,8 @@ wal - Generate and change colorschemes on the fly. Created by Dylan Araps. """ +from pywal.settings import CACHE_DIR +from pywal import export from pywal import image from pywal import magic from pywal import reload @@ -14,11 +16,9 @@ def get_image(img): return image.get_image(img) -def create_palette(img): +def create_palette(img, quiet=False): """Create a palette and return it as a dict.""" - colors = magic.gen_colors(img) - colors = magic.sort_colors(img, colors) - return colors + return magic.get_colors(img, quiet) def send_sequences(colors, vte): @@ -31,11 +31,16 @@ def reload_env(): reload.reload_env() +def export_all_templates(colors, template_dir=None, export_dir=CACHE_DIR): + """Export all templates.""" + export.export_all_templates(colors, template_dir, export_dir) + + def set_wallpaper(img): """Set the wallpaper.""" wallpaper.set_wallpaper(img) -# def reload_colors(vte): -# """Reload the colors.""" -# sequences.reload_colors(vte) +def reload_colors(vte, sequence_file=None): + """Reload the colors.""" + sequences.reload_colors(vte, sequence_file)