From 9587854303e66cba93738dac026e438cd3318673 Mon Sep 17 00:00:00 2001 From: dylan araps Date: Thu, 20 Jul 2017 10:42:31 +1000 Subject: general: Start work on a proper api. --- pywal/__init__.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pywal/__init__.py b/pywal/__init__.py index 006f780..853433d 100644 --- a/pywal/__init__.py +++ b/pywal/__init__.py @@ -3,3 +3,34 @@ wal - Generate and change colorschemes on the fly. Created by Dylan Araps. """ from pywal.settings import __version__ # noqa: F401 +from pywal import magic +from pywal import reload +from pywal import sequences +from pywal import wallpaper + + +def create_palette(img): + """Create a palette and return it as a dict.""" + colors = magic.gen_colors(img) + colors = magic.sort_colors(img, colors) + return colors + + +def send_sequences(colors, vte): + """Send the sequences.""" + sequences.send_sequences(colors, vte) + + +def reload_env(): + """Reload the environment.""" + reload.reload_env() + + +def set_wallpaper(img): + """Set the wallpaper.""" + wallpaper.set_wallpaper(img) + + +# def reload_colors(vte): +# """Reload the colors.""" +# sequences.reload_colors(vte) -- cgit v1.2.3 From 6678fa7953fde67d7371a28f6ddfe5b1aae03cdc Mon Sep 17 00:00:00 2001 From: dylan araps Date: Thu, 20 Jul 2017 10:44:21 +1000 Subject: general: Add example script. --- examples/example.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 examples/example.py diff --git a/examples/example.py b/examples/example.py new file mode 100644 index 0000000..9f7bf57 --- /dev/null +++ b/examples/example.py @@ -0,0 +1,22 @@ +"""pywal api example.""" +import pywal + + +def main(): + """Main function.""" + image = "/home/dylan/Pictures/Wallpapers/anVmEaA.jpg" + + # Return a dict with the palette. + colors = pywal.create_palette(image) + + # Apply the palette to all open terminals. + # Second argument is a boolean for VTE terminals. + # Set it to true if the terminal you're using is + # VTE based. (xfce4-terminal, termite, gnome-terminal.) + pywal.send_sequences(colors, False) + + # Reload xrdb, i3 and polybar. + pywal.reload_env() + + # Set the wallpaper. + pywal.set_wallpaper(image) -- cgit v1.2.3 From 93b8c3a6d131a468d3347b061be947c754294260 Mon Sep 17 00:00:00 2001 From: dylan araps Date: Thu, 20 Jul 2017 12:43:23 +1000 Subject: api: Fix example. --- examples/example.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/example.py b/examples/example.py index 9f7bf57..58de3ce 100644 --- a/examples/example.py +++ b/examples/example.py @@ -20,3 +20,6 @@ def main(): # Set the wallpaper. pywal.set_wallpaper(image) + + +main() -- cgit v1.2.3 From b8d6049aa89ae560efd03c2bdd3b372edd5392db Mon Sep 17 00:00:00 2001 From: dylan araps Date: Thu, 20 Jul 2017 12:57:57 +1000 Subject: api: Move api funtions to wal.py --- pywal/__init__.py | 39 +++++++-------------------------------- pywal/wal.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 pywal/wal.py diff --git a/pywal/__init__.py b/pywal/__init__.py index 853433d..02dbe37 100644 --- a/pywal/__init__.py +++ b/pywal/__init__.py @@ -2,35 +2,10 @@ wal - Generate and change colorschemes on the fly. Created by Dylan Araps. """ -from pywal.settings import __version__ # noqa: F401 -from pywal import magic -from pywal import reload -from pywal import sequences -from pywal import wallpaper - - -def create_palette(img): - """Create a palette and return it as a dict.""" - colors = magic.gen_colors(img) - colors = magic.sort_colors(img, colors) - return colors - - -def send_sequences(colors, vte): - """Send the sequences.""" - sequences.send_sequences(colors, vte) - - -def reload_env(): - """Reload the environment.""" - reload.reload_env() - - -def set_wallpaper(img): - """Set the wallpaper.""" - wallpaper.set_wallpaper(img) - - -# def reload_colors(vte): -# """Reload the colors.""" -# sequences.reload_colors(vte) +# flake8: noqa: F401 +from pywal.settings import __version__ +from pywal.wal import create_palette +from pywal.wal import get_image +from pywal.wal import reload_env +from pywal.wal import send_sequences +from pywal.wal import set_wallpaper diff --git a/pywal/wal.py b/pywal/wal.py new file mode 100644 index 0000000..db8dccd --- /dev/null +++ b/pywal/wal.py @@ -0,0 +1,41 @@ +""" +wal - Generate and change colorschemes on the fly. +Created by Dylan Araps. +""" +from pywal import image +from pywal import magic +from pywal import reload +from pywal import sequences +from pywal import wallpaper + + +def get_image(img): + """Validate image input.""" + return image.get_image(img) + + +def create_palette(img): + """Create a palette and return it as a dict.""" + colors = magic.gen_colors(img) + colors = magic.sort_colors(img, colors) + return colors + + +def send_sequences(colors, vte): + """Send the sequences.""" + sequences.send_sequences(colors, vte) + + +def reload_env(): + """Reload the environment.""" + reload.reload_env() + + +def set_wallpaper(img): + """Set the wallpaper.""" + wallpaper.set_wallpaper(img) + + +# def reload_colors(vte): +# """Reload the colors.""" +# sequences.reload_colors(vte) -- cgit v1.2.3 From 8917eef29e0d9279a74c35e0a59b29c9a89fbbc7 Mon Sep 17 00:00:00 2001 From: dylan araps Date: Thu, 20 Jul 2017 12:58:33 +1000 Subject: api: Update example. --- examples/example.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/example.py b/examples/example.py index 58de3ce..5e9cad2 100644 --- a/examples/example.py +++ b/examples/example.py @@ -1,10 +1,11 @@ -"""pywal api example.""" import pywal def main(): """Main function.""" - image = "/home/dylan/Pictures/Wallpapers/anVmEaA.jpg" + # Validate image and pick a random image if a + # directory is given below. + image = pywal.get_image("/home/dylan/Pictures/Wallpapers/") # Return a dict with the palette. colors = pywal.create_palette(image) -- cgit v1.2.3 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 --- 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) -- 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 ++- pywal/wal.py | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 \ diff --git a/pywal/wal.py b/pywal/wal.py index 67109f1..6bf9c0c 100644 --- a/pywal/wal.py +++ b/pywal/wal.py @@ -2,7 +2,6 @@ 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 @@ -31,7 +30,7 @@ def reload_env(): reload.reload_env() -def export_all_templates(colors, template_dir=None, export_dir=CACHE_DIR): +def export_all_templates(colors, template_dir=None, export_dir=None): """Export all templates.""" export.export_all_templates(colors, template_dir, export_dir) -- 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. --- examples/example.py | 7 ++++--- pywal/__init__.py | 2 +- pywal/__main__.py | 11 +++++------ pywal/export.py | 7 +------ pywal/image.py | 9 ++++----- pywal/magic.py | 22 +++++++++++----------- pywal/reload.py | 9 ++++----- pywal/sequences.py | 10 +++++----- pywal/settings.py | 11 ----------- pywal/util.py | 5 ----- pywal/wal.py | 34 ++++++++++++++++++++++------------ 11 files changed, 57 insertions(+), 70 deletions(-) delete mode 100644 pywal/settings.py diff --git a/examples/example.py b/examples/example.py index f81d131..f857b82 100644 --- a/examples/example.py +++ b/examples/example.py @@ -1,3 +1,4 @@ +"""Test script for wal api.""" import pywal @@ -20,9 +21,9 @@ def main(): pywal.reload_env() # Export template files. - pywal.export_all_templates(colors, - "path/to/templates", - "path/to/save/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 32f33af..9de680e 100644 --- a/pywal/__init__.py +++ b/pywal/__init__.py @@ -3,7 +3,7 @@ wal - Generate and change colorschemes on the fly. Created by Dylan Araps. """ # flake8: noqa: F401 -from pywal.settings import __version__ +from pywal.wal import __version__ from pywal.wal import create_palette from pywal.wal import export_all_templates from pywal.wal import get_image diff --git a/pywal/__main__.py b/pywal/__main__.py index c429424..3a47a8a 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -7,7 +7,6 @@ import os import shutil import sys -from pywal.settings import CACHE_DIR, __version__ from pywal import wal from pywal import util @@ -69,8 +68,8 @@ def process_args(args): # -c if args.c: - shutil.rmtree(CACHE_DIR / "schemes") - util.create_dir(CACHE_DIR / "schemes") + shutil.rmtree(wal.CACHE_DIR / "schemes") + util.create_dir(wal.CACHE_DIR / "schemes") # -r if args.r: @@ -78,13 +77,13 @@ def process_args(args): # -v if args.v: - print(f"wal {__version__}") + print(f"wal {wal.__version__}") exit(0) # -i if args.i: image_file = wal.get_image(args.i) - colors_plain = wal.create_palette(image_file, args.q) + colors_plain = wal.create_palette(img=image_file, quiet=args.q) # -f elif args.f: @@ -107,7 +106,7 @@ def process_args(args): def main(): """Main script function.""" - util.create_dir(CACHE_DIR / "schemes") + util.create_dir(wal.CACHE_DIR / "schemes") args = get_args() process_args(args) 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"], diff --git a/pywal/image.py b/pywal/image.py index 394967d..43bc7b5 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -5,13 +5,12 @@ import os import pathlib import random -from pywal.settings import CACHE_DIR from pywal import util -def get_random_image(img_dir): +def get_random_image(img_dir, cache_dir): """Pick a random image file from a directory.""" - current_wall = CACHE_DIR / "wal" + current_wall = cache_dir / "wal" if current_wall.is_file(): current_wall = util.read_file(current_wall) @@ -30,7 +29,7 @@ def get_random_image(img_dir): return img_dir / random.choice(images).name -def get_image(img): +def get_image(img, cache_dir): """Validate image input.""" image = pathlib.Path(img) @@ -38,7 +37,7 @@ def get_image(img): wal_img = image elif image.is_dir(): - wal_img = get_random_image(image) + wal_img = get_random_image(image, cache_dir) else: print("error: No valid image file found.") diff --git a/pywal/magic.py b/pywal/magic.py index cf468e7..3443113 100644 --- a/pywal/magic.py +++ b/pywal/magic.py @@ -5,7 +5,7 @@ import re import shutil import subprocess -from pywal.settings import CACHE_DIR, COLOR_COUNT +# from pywal.settings import color_count from pywal import util @@ -18,7 +18,7 @@ def imagemagick(color_count, img): return colors.stdout.readlines() -def gen_colors(img): +def gen_colors(img, color_count): """Format the output from imagemagick into a list of hex colors.""" # Check if the user has Imagemagick installed. @@ -28,18 +28,18 @@ def gen_colors(img): exit(1) # Generate initial scheme. - raw_colors = imagemagick(COLOR_COUNT, img) + raw_colors = imagemagick(color_count, img) # If imagemagick finds less than 16 colors, use a larger source number # of colors. index = 0 - while len(raw_colors) - 1 < COLOR_COUNT: + while len(raw_colors) - 1 < color_count: index += 1 - raw_colors = imagemagick(COLOR_COUNT + index, img) + raw_colors = imagemagick(color_count + index, img) - print("colors: Imagemagick couldn't generate a", COLOR_COUNT, + print("colors: Imagemagick couldn't generate a", color_count, "color palette, trying a larger palette size", - COLOR_COUNT + index) + color_count + index) if index > 20: print("colors: Imagemagick couldn't generate a suitable scheme", @@ -53,14 +53,14 @@ def gen_colors(img): return [re.search("#.{6}", str(col)).group(0) for col in raw_colors] -def get_colors(img, quiet=False): +def get_colors(img, cache_dir, color_count, quiet): """Get the colorscheme.""" # Cache the wallpaper name. - util.save_file(img, CACHE_DIR / "wal") + util.save_file(img, cache_dir / "wal") # Cache the sequences file. # _home_dylan_img_jpg.json - cache_file = CACHE_DIR / "schemes" / \ + cache_file = cache_dir / "schemes" / \ img.replace("/", "_").replace(".", "_") cache_file = cache_file.with_suffix(".json") @@ -74,7 +74,7 @@ def get_colors(img, quiet=False): util.disown("notify-send", "wal: Generating a colorscheme...") # Generate the colors. - colors = gen_colors(img) + colors = gen_colors(img, color_count) colors = sort_colors(img, colors) # Cache the colorscheme. diff --git a/pywal/reload.py b/pywal/reload.py index 5cc7307..77a496a 100644 --- a/pywal/reload.py +++ b/pywal/reload.py @@ -4,14 +4,13 @@ Reload programs. import shutil import subprocess -from pywal.settings import CACHE_DIR from pywal import util -def reload_xrdb(): +def reload_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"]) + subprocess.call(["xrdb", "-merge", cache_dir / "colors.Xresources"]) def reload_i3(): @@ -26,9 +25,9 @@ def reload_polybar(): util.disown("pkill", "-USR1", "polybar") -def reload_env(): +def reload_env(cache_dir): """Reload environment.""" - reload_xrdb() + reload_xrdb(cache_dir) reload_i3() reload_polybar() print("reload: Reloaded environment.") diff --git a/pywal/sequences.py b/pywal/sequences.py index 2e51bc3..9a90e3e 100644 --- a/pywal/sequences.py +++ b/pywal/sequences.py @@ -4,7 +4,7 @@ Send sequences to all open terminals. import os import re -from pywal.settings import CACHE_DIR +# from pywal.settings import CACHE_DIR from pywal import util @@ -18,7 +18,7 @@ def set_color(index, color): return f"\033]4;{index};{color}\007" -def send_sequences(colors, vte): +def send_sequences(colors, vte, cache_dir): """Send colors to all open terminals.""" # Colors 0-15. sequences = [set_color(num, color) @@ -44,7 +44,7 @@ def send_sequences(colors, vte): # Get the list of terminals. terminals = [f"/dev/pts/{term}" for term in os.listdir("/dev/pts/") if len(term) < 4] - terminals.append(CACHE_DIR / "sequences") + terminals.append(cache_dir / "sequences") # Send the sequences to all open terminals. # pylint: disable=W0106 @@ -53,9 +53,9 @@ def send_sequences(colors, vte): print("colors: Set terminal colors") -def reload_colors(vte, sequence_file=None): +def reload_colors(vte, cache_dir): """Reload the current scheme.""" - sequence_file = sequence_file or CACHE_DIR / "sequences" + sequence_file = cache_dir / "sequences" if sequence_file.is_file(): sequences = "".join(util.read_file(sequence_file)) diff --git a/pywal/settings.py b/pywal/settings.py deleted file mode 100644 index 9037285..0000000 --- a/pywal/settings.py +++ /dev/null @@ -1,11 +0,0 @@ -""" -Global Constants. -""" -import pathlib - - -__version__ = "0.4.0" - - -COLOR_COUNT = 16 -CACHE_DIR = pathlib.Path.home() / ".cache/wal/" diff --git a/pywal/util.py b/pywal/util.py index deab13c..6c5b420 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -98,8 +98,3 @@ 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 6bf9c0c..6d10278 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. """ +import pathlib + from pywal import export from pywal import image from pywal import magic @@ -10,29 +12,37 @@ from pywal import sequences from pywal import wallpaper -def get_image(img): +__version__ = "0.4.0" + + +COLOR_COUNT = 16 +CACHE_DIR = pathlib.Path.home() / ".cache/wal/" + + +def get_image(img, cache_dir=CACHE_DIR): """Validate image input.""" - return image.get_image(img) + return image.get_image(img, cache_dir) -def create_palette(img, quiet=False): +def create_palette(img, cache_dir=CACHE_DIR, + color_count=COLOR_COUNT, quiet=False): """Create a palette and return it as a dict.""" - return magic.get_colors(img, quiet) + return magic.get_colors(img, cache_dir, color_count, quiet) -def send_sequences(colors, vte): +def send_sequences(colors, vte, cache_dir=CACHE_DIR): """Send the sequences.""" - sequences.send_sequences(colors, vte) + sequences.send_sequences(colors, vte, cache_dir) -def reload_env(): +def reload_env(cache_dir=CACHE_DIR): """Reload the environment.""" - reload.reload_env() + reload.reload_env(cache_dir) -def export_all_templates(colors, template_dir=None, export_dir=None): +def export_all_templates(colors, output_dir=CACHE_DIR, template_dir=None): """Export all templates.""" - export.export_all_templates(colors, template_dir, export_dir) + export.export_all_templates(colors, output_dir, template_dir) def set_wallpaper(img): @@ -40,6 +50,6 @@ def set_wallpaper(img): wallpaper.set_wallpaper(img) -def reload_colors(vte, sequence_file=None): +def reload_colors(vte, cache_dir=CACHE_DIR): """Reload the colors.""" - sequences.reload_colors(vte, sequence_file) + sequences.reload_colors(vte, cache_dir) -- cgit v1.2.3 From 2973ea06f6a77233bb4fa8d556ab3d2f014e037c Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 20 Jul 2017 23:32:02 +1000 Subject: api: Update example. --- examples/example.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/examples/example.py b/examples/example.py index f857b82..faf47b6 100644 --- a/examples/example.py +++ b/examples/example.py @@ -1,29 +1,42 @@ """Test script for wal api.""" +import pathlib import pywal +CACHE_DIR = pathlib.Path.home() / "wal-test" +COLOR_COUNT = 16 + + def main(): """Main function.""" + # Create the custom cache directory. + pywal.util.create_dir(CACHE_DIR / "schemes") + # Validate image and pick a random image if a # directory is given below. - image = pywal.get_image("/home/dylan/Pictures/Wallpapers/") + # + # CACHE_DIR is an optional argument and is used to check the current + # wallpaper against the random selection. This prevents shuffling to + # the identical image when a directory is passed as an argument. + image = pywal.get_image("/home/dylan/Pictures/Wallpapers/", CACHE_DIR) # Return a dict with the palette. - colors = pywal.create_palette(image) + # + # The last argument is 'quiet' mode. When set to true, no notifications + # are displayed. + colors = pywal.create_palette(image, CACHE_DIR, COLOR_COUNT, True) # Apply the palette to all open terminals. # Second argument is a boolean for VTE terminals. # Set it to true if the terminal you're using is # VTE based. (xfce4-terminal, termite, gnome-terminal.) - pywal.send_sequences(colors, False) + pywal.send_sequences(colors, False, CACHE_DIR) # Reload xrdb, i3 and polybar. - pywal.reload_env() + pywal.reload_env(CACHE_DIR) # Export template files. - # pywal.export_all_templates(colors, - # "path/to/templates", - # "path/to/save/files/") + pywal.export_all_templates(colors, CACHE_DIR) # Set the wallpaper. pywal.set_wallpaper(image) -- cgit v1.2.3 From ced562e4dfa67ff36d64684dfc0d7335306aeb97 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 20 Jul 2017 23:35:04 +1000 Subject: api: Added a simple example. --- examples/example.py | 27 ++++++----------------- examples/example_custom_cache.py | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 examples/example_custom_cache.py diff --git a/examples/example.py b/examples/example.py index faf47b6..870ba41 100644 --- a/examples/example.py +++ b/examples/example.py @@ -1,42 +1,29 @@ -"""Test script for wal api.""" -import pathlib +"""Simple script for wal api.""" import pywal -CACHE_DIR = pathlib.Path.home() / "wal-test" -COLOR_COUNT = 16 - - def main(): """Main function.""" - # Create the custom cache directory. - pywal.util.create_dir(CACHE_DIR / "schemes") - # Validate image and pick a random image if a # directory is given below. - # - # CACHE_DIR is an optional argument and is used to check the current - # wallpaper against the random selection. This prevents shuffling to - # the identical image when a directory is passed as an argument. - image = pywal.get_image("/home/dylan/Pictures/Wallpapers/", CACHE_DIR) + image = pywal.get_image("/home/dylan/Pictures/Wallpapers/") # Return a dict with the palette. # - # The last argument is 'quiet' mode. When set to true, no notifications - # are displayed. - colors = pywal.create_palette(image, CACHE_DIR, COLOR_COUNT, True) + # Set quiet to 'True' to disable notifications. + colors = pywal.create_palette(image, quiet=False) # Apply the palette to all open terminals. # Second argument is a boolean for VTE terminals. # Set it to true if the terminal you're using is # VTE based. (xfce4-terminal, termite, gnome-terminal.) - pywal.send_sequences(colors, False, CACHE_DIR) + pywal.send_sequences(colors, vte=False) # Reload xrdb, i3 and polybar. - pywal.reload_env(CACHE_DIR) + pywal.reload_env() # Export template files. - pywal.export_all_templates(colors, CACHE_DIR) + pywal.export_all_templates(colors) # Set the wallpaper. pywal.set_wallpaper(image) diff --git a/examples/example_custom_cache.py b/examples/example_custom_cache.py new file mode 100644 index 0000000..c2edb4c --- /dev/null +++ b/examples/example_custom_cache.py @@ -0,0 +1,46 @@ +"""Test script for wal api. + This script uses a custom cache location for the files.""" +import pathlib +import pywal + + +CACHE_DIR = pathlib.Path.home() / "wal-test" +COLOR_COUNT = 16 + + +def main(): + """Main function.""" + # Create the custom cache directory. + pywal.util.create_dir(CACHE_DIR / "schemes") + + # Validate image and pick a random image if a + # directory is given below. + # + # CACHE_DIR is an optional argument and is used to check the current + # wallpaper against the random selection. This prevents shuffling to + # the identical image when a directory is passed as an argument. + image = pywal.get_image("/home/dylan/Pictures/Wallpapers/", CACHE_DIR) + + # Return a dict with the palette. + # + # The last argument is 'quiet' mode. When set to true, no notifications + # are displayed. + colors = pywal.create_palette(image, CACHE_DIR, COLOR_COUNT, True) + + # Apply the palette to all open terminals. + # Second argument is a boolean for VTE terminals. + # Set it to true if the terminal you're using is + # VTE based. (xfce4-terminal, termite, gnome-terminal.) + pywal.send_sequences(colors, False, CACHE_DIR) + + # Reload xrdb, i3 and polybar. + pywal.reload_env(CACHE_DIR) + + # Export template files. + pywal.export_all_templates(colors, CACHE_DIR) + + # Set the wallpaper. + pywal.set_wallpaper(image) + + +main() -- cgit v1.2.3 From 4fd592b7d0a5fcf6674d07f34e6d92051634fc6c Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 20 Jul 2017 23:39:19 +1000 Subject: tests: Fix tests. --- tests/test_colors.py | 21 --------------------- tests/test_image.py | 21 +++++++++++++++++++++ tests/test_magic.py | 4 ++-- 3 files changed, 23 insertions(+), 23 deletions(-) delete mode 100644 tests/test_colors.py create mode 100644 tests/test_image.py diff --git a/tests/test_colors.py b/tests/test_colors.py deleted file mode 100644 index 077600e..0000000 --- a/tests/test_colors.py +++ /dev/null @@ -1,21 +0,0 @@ -"""Test image functions.""" -import unittest - -from pywal import image - - -class TestImage(unittest.TestCase): - """Test image functions.""" - def test_get_img(self): - """> Validate image file.""" - result = image.get_image("tests/test_files/test.jpg") - self.assertEqual(result, "tests/test_files/test.jpg") - - def test_get_img_dir(self): - """> Validate image directory.""" - result = image.get_image("tests/test_files") - self.assertEqual(result, "tests/test_files/test.jpg") - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_image.py b/tests/test_image.py new file mode 100644 index 0000000..7dacecb --- /dev/null +++ b/tests/test_image.py @@ -0,0 +1,21 @@ +"""Test image functions.""" +import unittest + +from pywal import wal + + +class TestImage(unittest.TestCase): + """Test image functions.""" + def test_get_img(self): + """> Validate image file.""" + result = wal.get_image("tests/test_files/test.jpg") + self.assertEqual(result, "tests/test_files/test.jpg") + + def test_get_img_dir(self): + """> Validate image directory.""" + result = wal.get_image("tests/test_files") + self.assertEqual(result, "tests/test_files/test.jpg") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_magic.py b/tests/test_magic.py index 673420c..852f8b3 100755 --- a/tests/test_magic.py +++ b/tests/test_magic.py @@ -1,7 +1,7 @@ """Test imagemagick functions.""" import unittest -from pywal import magic +from pywal import wal class TestGenColors(unittest.TestCase): @@ -9,7 +9,7 @@ class TestGenColors(unittest.TestCase): def test_gen_colors(self): """> Generate a colorscheme.""" - result = magic.gen_colors("tests/test_files/test.jpg") + result = wal.create_palette("tests/test_files/test.jpg") self.assertEqual(result[0], "#0F191A") -- cgit v1.2.3 From 477ca7398d86af76481deb6f6961539486b9a91d Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 20 Jul 2017 23:40:20 +1000 Subject: api: Fix comment. --- examples/example.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/example.py b/examples/example.py index 870ba41..b4b40e1 100644 --- a/examples/example.py +++ b/examples/example.py @@ -9,7 +9,6 @@ def main(): image = pywal.get_image("/home/dylan/Pictures/Wallpapers/") # Return a dict with the palette. - # # Set quiet to 'True' to disable notifications. colors = pywal.create_palette(image, quiet=False) -- cgit v1.2.3 From 950b7e892a98675616653bcabe405d9203691c6e Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 20 Jul 2017 23:42:03 +1000 Subject: general: Remove unused module. --- pywal/sequences.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pywal/sequences.py b/pywal/sequences.py index 9a90e3e..7533f19 100644 --- a/pywal/sequences.py +++ b/pywal/sequences.py @@ -4,7 +4,6 @@ Send sequences to all open terminals. import os import re -# from pywal.settings import CACHE_DIR from pywal import util -- cgit v1.2.3 From 3d1f11b1bd70b17567808849fd9eb3346599bb53 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Fri, 21 Jul 2017 11:19:17 +1000 Subject: util: Add new msg function. --- pywal/magic.py | 9 ++------- pywal/util.py | 9 +++++++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pywal/magic.py b/pywal/magic.py index 3443113..9dc528c 100644 --- a/pywal/magic.py +++ b/pywal/magic.py @@ -69,9 +69,7 @@ def get_colors(img, cache_dir, color_count, quiet): print("colors: Found cached colorscheme.") else: - print("colors: Generating a colorscheme...") - if not quiet: - util.disown("notify-send", "wal: Generating a colorscheme...") + util.msg("wal: Generating a colorscheme...", quiet) # Generate the colors. colors = gen_colors(img, color_count) @@ -79,10 +77,7 @@ def get_colors(img, cache_dir, color_count, quiet): # Cache the colorscheme. util.save_file_json(colors, cache_file) - - print("colors: Generated colorscheme") - if not quiet: - util.disown("notify-send", "wal: Generation complete.") + util.msg("wal: Generation complete.", quiet) return colors diff --git a/pywal/util.py b/pywal/util.py index 6c5b420..1030b60 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -98,3 +98,12 @@ def disown(*cmd): stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, preexec_fn=os.setpgrp) + + +def msg(input_msg, quiet): + """Print to the terminal and a libnotify + notification.""" + if not quiet: + disown("notify-send", input_msg) + + print(input_msg) -- cgit v1.2.3 From 97de3110de378856e04ce17d41a8d75935b28767 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Fri, 21 Jul 2017 11:21:18 +1000 Subject: examples: Update example --- examples/example_custom_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/example_custom_cache.py b/examples/example_custom_cache.py index c2edb4c..5e74350 100644 --- a/examples/example_custom_cache.py +++ b/examples/example_custom_cache.py @@ -25,7 +25,7 @@ def main(): # # The last argument is 'quiet' mode. When set to true, no notifications # are displayed. - colors = pywal.create_palette(image, CACHE_DIR, COLOR_COUNT, True) + colors = pywal.create_palette(image, CACHE_DIR, COLOR_COUNT, quiet=True) # Apply the palette to all open terminals. # Second argument is a boolean for VTE terminals. -- 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/__main__.py | 10 ---------- pywal/export.py | 24 ++++-------------------- pywal/image.py | 2 -- pywal/magic.py | 18 +----------------- pywal/sequences.py | 2 -- pywal/util.py | 15 +++++++++++---- 6 files changed, 16 insertions(+), 55 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 3a47a8a..92e3d4b 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -16,7 +16,6 @@ def get_args(): description = "wal - Generate colorschemes on the fly" arg = argparse.ArgumentParser(description=description) - # Add the args. arg.add_argument("-c", action="store_true", help="Delete all cached colorschemes.") @@ -51,7 +50,6 @@ def get_args(): def process_args(args): """Process args.""" - # If no args were passed. if not len(sys.argv) > 1: print("error: wal needs to be given arguments to run.\n" " Refer to \"wal -h\" for more info.") @@ -62,34 +60,27 @@ def process_args(args): " Refer to \"wal -h\" for more info.") exit(1) - # -q if args.q: sys.stdout = sys.stderr = open(os.devnull, "w") - # -c if args.c: shutil.rmtree(wal.CACHE_DIR / "schemes") util.create_dir(wal.CACHE_DIR / "schemes") - # -r if args.r: wal.reload_colors(args.t) - # -v if args.v: print(f"wal {wal.__version__}") exit(0) - # -i if args.i: image_file = wal.get_image(args.i) colors_plain = wal.create_palette(img=image_file, quiet=args.q) - # -f elif args.f: colors_plain = util.read_file_json(args.f) - # -i or -f if args.i or args.f: wal.send_sequences(colors_plain, args.t) @@ -99,7 +90,6 @@ def process_args(args): wal.export_all_templates(colors_plain) wal.reload_env() - # -o if args.o: util.disown(args.o) 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 diff --git a/pywal/image.py b/pywal/image.py index 43bc7b5..a7a314d 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -16,12 +16,10 @@ def get_random_image(img_dir, cache_dir): current_wall = util.read_file(current_wall) current_wall = os.path.basename(current_wall[0]) - # Add all images to a list excluding the current wallpaper. file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif") images = [img for img in os.scandir(img_dir) if img.name.endswith(file_types) and img.name != current_wall] - # If no images are found, use the current wallpaper. if not images: print("image: No new images found (nothing to do), exiting...") quit(1) diff --git a/pywal/magic.py b/pywal/magic.py index 9dc528c..5768c71 100644 --- a/pywal/magic.py +++ b/pywal/magic.py @@ -5,7 +5,6 @@ import re import shutil import subprocess -# from pywal.settings import color_count from pywal import util @@ -21,17 +20,13 @@ def imagemagick(color_count, img): def gen_colors(img, color_count): """Format the output from imagemagick into a list of hex colors.""" - # Check if the user has Imagemagick installed. if not shutil.which("convert"): print("error: imagemagick not found, exiting...\n" "error: wal requires imagemagick to function.") exit(1) - # Generate initial scheme. raw_colors = imagemagick(color_count, img) - # If imagemagick finds less than 16 colors, use a larger source number - # of colors. index = 0 while len(raw_colors) - 1 < color_count: index += 1 @@ -46,19 +41,16 @@ def gen_colors(img, color_count): "for the image. Exiting...") quit(1) - # Remove the first element, which isn't a color. + # Remove the first element because it isn't a color code. del raw_colors[0] - # Create a list of hex colors. return [re.search("#.{6}", str(col)).group(0) for col in raw_colors] def get_colors(img, cache_dir, color_count, quiet): """Get the colorscheme.""" - # Cache the wallpaper name. util.save_file(img, cache_dir / "wal") - # Cache the sequences file. # _home_dylan_img_jpg.json cache_file = cache_dir / "schemes" / \ img.replace("/", "_").replace(".", "_") @@ -71,11 +63,9 @@ def get_colors(img, cache_dir, color_count, quiet): else: util.msg("wal: Generating a colorscheme...", quiet) - # Generate the colors. colors = gen_colors(img, color_count) colors = sort_colors(img, colors) - # Cache the colorscheme. util.save_file_json(colors, cache_file) util.msg("wal: Generation complete.", quiet) @@ -87,24 +77,18 @@ def sort_colors(img, colors): we will later save in json format.""" raw_colors = colors[:1] + colors[9:] + colors[8:] - # Wallpaper. colors = {"wallpaper": img} - # Special colors. colors_special = {} colors_special.update({"background": raw_colors[0]}) colors_special.update({"foreground": raw_colors[15]}) colors_special.update({"cursor": raw_colors[15]}) - # Colors 0-15. colors_hex = {} [colors_hex.update({f"color{index}": color}) # pylint: disable=W0106 for index, color in enumerate(raw_colors)] - - # Color 8. colors_hex["color8"] = util.set_grey(raw_colors) - # Add the colors to a dict. colors["special"] = colors_special colors["colors"] = colors_hex diff --git a/pywal/sequences.py b/pywal/sequences.py index 7533f19..d7f1fa9 100644 --- a/pywal/sequences.py +++ b/pywal/sequences.py @@ -40,7 +40,6 @@ def send_sequences(colors, vte, cache_dir): if not vte: sequences.append(set_special(708, colors["special"]["background"])) - # Get the list of terminals. terminals = [f"/dev/pts/{term}" for term in os.listdir("/dev/pts/") if len(term) < 4] terminals.append(cache_dir / "sequences") @@ -48,7 +47,6 @@ def send_sequences(colors, vte, cache_dir): # Send the sequences to all open terminals. # pylint: disable=W0106 [util.save_file("".join(sequences), term) for term in terminals] - print("colors: Set terminal colors") diff --git a/pywal/util.py b/pywal/util.py index 1030b60..0891feb 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -44,24 +44,31 @@ def set_grey(colors): def read_file(input_file): - """Read data from a file.""" - with open(input_file) as file: + """Read data from a file and trim newlines.""" + with open(input_file, "r") as file: data = file.read().splitlines() return data def read_file_json(input_file): """Read data from a json file.""" - with open(input_file) as json_file: + with open(input_file, "r") as json_file: data = json.load(json_file) - # If wallpaper is unset, set it to "None" if "wallpaper" not in data: data["wallpaper"] = "None" return data +def read_file_raw(input_file): + """Read data from a file as is, don't strip + newlines or other special characters..""" + with open(input_file, "r") as file: + data = file.readlines() + return data + + def save_file(data, export_file): """Write data to a file.""" with open(export_file, "w") as file: -- cgit v1.2.3 From d8d0297d8968380879718758493fd135b5234591 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 09:50:35 +1000 Subject: general: Fix comment --- pywal/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pywal/util.py b/pywal/util.py index 0891feb..b109744 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -108,7 +108,7 @@ def disown(*cmd): def msg(input_msg, quiet): - """Print to the terminal and a libnotify + """Print to the terminal and display a libnotify notification.""" if not quiet: disown("notify-send", input_msg) -- 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 ------------------------------- pywal/template.py | 31 +++++++++++++++++++++++++++++++ pywal/wal.py | 4 ++-- tests/test_export.py | 36 ------------------------------------ tests/test_files/test2.jpg | 1 + tests/test_image.py | 2 +- tests/test_magic.py | 2 +- tests/test_template.py | 36 ++++++++++++++++++++++++++++++++++++ 8 files changed, 72 insertions(+), 71 deletions(-) delete mode 100644 pywal/export.py create mode 100644 pywal/template.py delete mode 100755 tests/test_export.py create mode 120000 tests/test_files/test2.jpg create mode 100755 tests/test_template.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)] diff --git a/pywal/template.py b/pywal/template.py new file mode 100644 index 0000000..1dc72bf --- /dev/null +++ b/pywal/template.py @@ -0,0 +1,31 @@ +""" +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)] diff --git a/pywal/wal.py b/pywal/wal.py index 6d10278..fa0eb3e 100644 --- a/pywal/wal.py +++ b/pywal/wal.py @@ -4,11 +4,11 @@ Created by Dylan Araps. """ import pathlib -from pywal import export from pywal import image from pywal import magic from pywal import reload from pywal import sequences +from pywal import template from pywal import wallpaper @@ -42,7 +42,7 @@ def reload_env(cache_dir=CACHE_DIR): def export_all_templates(colors, output_dir=CACHE_DIR, template_dir=None): """Export all templates.""" - export.export_all_templates(colors, output_dir, template_dir) + template.export_all_templates(colors, output_dir, template_dir) def set_wallpaper(img): diff --git a/tests/test_export.py b/tests/test_export.py deleted file mode 100755 index 167d3ed..0000000 --- a/tests/test_export.py +++ /dev/null @@ -1,36 +0,0 @@ -"""Test export functions.""" -import unittest -import pathlib - -from pywal import export -from pywal import util - - -# Import colors. -COLORS = util.read_file_json("tests/test_files/test_file.json") - - -class TestExportColors(unittest.TestCase): - """Test the export functions.""" - - def test_template(self): - """> Test substitutions in template file.""" - # Merge both dicts so we can access their - # values simpler. - COLORS["colors"].update(COLORS["special"]) - - output_dir = pathlib.Path("/tmp") - template_dir = pathlib.Path("tests/test_files/templates") - export.export_all_templates(COLORS, template_dir, output_dir) - - result = pathlib.Path("/tmp/test_template").is_file() - self.assertTrue(result) - - content = pathlib.Path("/tmp/test_template").read_text() - self.assertEqual(content, '\n'.join(["test1 #1F211E", - "test2 #1F211E", - "test3 31,33,30", ""])) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_files/test2.jpg b/tests/test_files/test2.jpg new file mode 120000 index 0000000..0e8c5b6 --- /dev/null +++ b/tests/test_files/test2.jpg @@ -0,0 +1 @@ +test.jpg \ No newline at end of file diff --git a/tests/test_image.py b/tests/test_image.py index 7dacecb..bcf3b4d 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -14,7 +14,7 @@ class TestImage(unittest.TestCase): def test_get_img_dir(self): """> Validate image directory.""" result = wal.get_image("tests/test_files") - self.assertEqual(result, "tests/test_files/test.jpg") + self.assertEqual(result, "tests/test_files/test2.jpg") if __name__ == "__main__": diff --git a/tests/test_magic.py b/tests/test_magic.py index 852f8b3..a3434df 100755 --- a/tests/test_magic.py +++ b/tests/test_magic.py @@ -10,7 +10,7 @@ class TestGenColors(unittest.TestCase): def test_gen_colors(self): """> Generate a colorscheme.""" result = wal.create_palette("tests/test_files/test.jpg") - self.assertEqual(result[0], "#0F191A") + self.assertEqual(result["colors"]["color0"], "#0F191A") if __name__ == "__main__": diff --git a/tests/test_template.py b/tests/test_template.py new file mode 100755 index 0000000..9040348 --- /dev/null +++ b/tests/test_template.py @@ -0,0 +1,36 @@ +"""Test export functions.""" +import unittest +import pathlib + +from pywal import template +from pywal import util + + +# Import colors. +COLORS = util.read_file_json("tests/test_files/test_file.json") + + +class TestExportColors(unittest.TestCase): + """Test the export functions.""" + + def test_template(self): + """> Test substitutions in template file.""" + # Merge both dicts so we can access their + # values simpler. + COLORS["colors"].update(COLORS["special"]) + + output_dir = pathlib.Path("/tmp") + template_dir = pathlib.Path("tests/test_files/templates") + template.export_all_templates(COLORS, output_dir, template_dir) + + result = pathlib.Path("/tmp/test_template").is_file() + self.assertTrue(result) + + content = pathlib.Path("/tmp/test_template").read_text() + self.assertEqual(content, '\n'.join(["test1 #1F211E", + "test2 #1F211E", + "test3 31,33,30", ""])) + + +if __name__ == "__main__": + unittest.main() -- cgit v1.2.3 From c6147fd0eb0387c5cc4334dfe9c17f097fdea246 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 10:04:56 +1000 Subject: examples: Move examples out of the repo to the wiki. --- examples/example.py | 31 --------------------------- examples/example_custom_cache.py | 46 ---------------------------------------- 2 files changed, 77 deletions(-) delete mode 100644 examples/example.py delete mode 100644 examples/example_custom_cache.py diff --git a/examples/example.py b/examples/example.py deleted file mode 100644 index b4b40e1..0000000 --- a/examples/example.py +++ /dev/null @@ -1,31 +0,0 @@ -"""Simple script for wal api.""" -import pywal - - -def main(): - """Main function.""" - # Validate image and pick a random image if a - # directory is given below. - image = pywal.get_image("/home/dylan/Pictures/Wallpapers/") - - # Return a dict with the palette. - # Set quiet to 'True' to disable notifications. - colors = pywal.create_palette(image, quiet=False) - - # Apply the palette to all open terminals. - # Second argument is a boolean for VTE terminals. - # Set it to true if the terminal you're using is - # VTE based. (xfce4-terminal, termite, gnome-terminal.) - pywal.send_sequences(colors, vte=False) - - # Reload xrdb, i3 and polybar. - pywal.reload_env() - - # Export template files. - pywal.export_all_templates(colors) - - # Set the wallpaper. - pywal.set_wallpaper(image) - - -main() diff --git a/examples/example_custom_cache.py b/examples/example_custom_cache.py deleted file mode 100644 index 5e74350..0000000 --- a/examples/example_custom_cache.py +++ /dev/null @@ -1,46 +0,0 @@ -"""Test script for wal api. - This script uses a custom cache location for the files.""" -import pathlib -import pywal - - -CACHE_DIR = pathlib.Path.home() / "wal-test" -COLOR_COUNT = 16 - - -def main(): - """Main function.""" - # Create the custom cache directory. - pywal.util.create_dir(CACHE_DIR / "schemes") - - # Validate image and pick a random image if a - # directory is given below. - # - # CACHE_DIR is an optional argument and is used to check the current - # wallpaper against the random selection. This prevents shuffling to - # the identical image when a directory is passed as an argument. - image = pywal.get_image("/home/dylan/Pictures/Wallpapers/", CACHE_DIR) - - # Return a dict with the palette. - # - # The last argument is 'quiet' mode. When set to true, no notifications - # are displayed. - colors = pywal.create_palette(image, CACHE_DIR, COLOR_COUNT, quiet=True) - - # Apply the palette to all open terminals. - # Second argument is a boolean for VTE terminals. - # Set it to true if the terminal you're using is - # VTE based. (xfce4-terminal, termite, gnome-terminal.) - pywal.send_sequences(colors, False, CACHE_DIR) - - # Reload xrdb, i3 and polybar. - pywal.reload_env(CACHE_DIR) - - # Export template files. - pywal.export_all_templates(colors, CACHE_DIR) - - # Set the wallpaper. - pywal.set_wallpaper(image) - - -main() -- cgit v1.2.3 From ccad5150e621067ef637104f9a20371bb4533ce8 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 10:10:05 +1000 Subject: tests: Use wal functions. --- tests/test_template.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_template.py b/tests/test_template.py index 9040348..992c9c0 100755 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -2,7 +2,7 @@ import unittest import pathlib -from pywal import template +from pywal import wal from pywal import util @@ -21,7 +21,7 @@ class TestExportColors(unittest.TestCase): output_dir = pathlib.Path("/tmp") template_dir = pathlib.Path("tests/test_files/templates") - template.export_all_templates(COLORS, output_dir, template_dir) + wal.export_all_templates(COLORS, output_dir, template_dir) result = pathlib.Path("/tmp/test_template").is_file() self.assertTrue(result) -- cgit v1.2.3 From 3dd1fc9039822e4426d5dc8c3f807dd5f6f2d295 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 10:26:36 +1000 Subject: tests: Fix tests --- tests/test_files/test2.jpg | 1 - tests/test_image.py | 10 +++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) delete mode 120000 tests/test_files/test2.jpg diff --git a/tests/test_files/test2.jpg b/tests/test_files/test2.jpg deleted file mode 120000 index 0e8c5b6..0000000 --- a/tests/test_files/test2.jpg +++ /dev/null @@ -1 +0,0 @@ -test.jpg \ No newline at end of file diff --git a/tests/test_image.py b/tests/test_image.py index bcf3b4d..b9099f9 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -1,20 +1,24 @@ """Test image functions.""" +import pathlib import unittest from pywal import wal +DEVNULL = pathlib.Path("/dev/null") + + class TestImage(unittest.TestCase): """Test image functions.""" def test_get_img(self): """> Validate image file.""" - result = wal.get_image("tests/test_files/test.jpg") + result = wal.get_image("tests/test_files/test.jpg", DEVNULL) self.assertEqual(result, "tests/test_files/test.jpg") def test_get_img_dir(self): """> Validate image directory.""" - result = wal.get_image("tests/test_files") - self.assertEqual(result, "tests/test_files/test2.jpg") + result = wal.get_image("tests/test_files", DEVNULL) + self.assertEqual(result, "tests/test_files/test.jpg") if __name__ == "__main__": -- cgit v1.2.3 From 38afea4f2ef5a4a04ef44139aa1ed01f3655a797 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 10:51:52 +1000 Subject: General: move cache call --- pywal/image.py | 9 ++++++--- pywal/magic.py | 2 -- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pywal/image.py b/pywal/image.py index a7a314d..421943e 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -24,7 +24,7 @@ def get_random_image(img_dir, cache_dir): print("image: No new images found (nothing to do), exiting...") quit(1) - return img_dir / random.choice(images).name + return str(img_dir / random.choice(images).name) def get_image(img, cache_dir): @@ -32,7 +32,7 @@ def get_image(img, cache_dir): image = pathlib.Path(img) if image.is_file(): - wal_img = image + wal_img = str(image) elif image.is_dir(): wal_img = get_random_image(image, cache_dir) @@ -41,5 +41,8 @@ def get_image(img, cache_dir): print("error: No valid image file found.") exit(1) + # Cache the image file path. + util.save_file(wal_img, cache_dir / "wal") + print("image: Using image", wal_img) - return str(wal_img) + return wal_img diff --git a/pywal/magic.py b/pywal/magic.py index 5768c71..b384b97 100644 --- a/pywal/magic.py +++ b/pywal/magic.py @@ -49,8 +49,6 @@ def gen_colors(img, color_count): def get_colors(img, cache_dir, color_count, quiet): """Get the colorscheme.""" - util.save_file(img, cache_dir / "wal") - # _home_dylan_img_jpg.json cache_file = cache_dir / "schemes" / \ img.replace("/", "_").replace(".", "_") -- cgit v1.2.3 From 7d1fc8d4d3ea3a182b567a4d54c87574f0506fcd Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 10:58:14 +1000 Subject: general: Disable notifications by default. --- pywal/wal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pywal/wal.py b/pywal/wal.py index fa0eb3e..695645a 100644 --- a/pywal/wal.py +++ b/pywal/wal.py @@ -25,7 +25,7 @@ def get_image(img, cache_dir=CACHE_DIR): def create_palette(img, cache_dir=CACHE_DIR, - color_count=COLOR_COUNT, quiet=False): + color_count=COLOR_COUNT, quiet=True): """Create a palette and return it as a dict.""" return magic.get_colors(img, cache_dir, color_count, quiet) -- cgit v1.2.3 From a34a9c597722056f4eba4b689775f9f202a35822 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 11:19:17 +1000 Subject: api: Cleanup --- pywal/__main__.py | 12 +++++------- pywal/reload.py | 4 +++- pywal/util.py | 8 ++++++-- pywal/wal.py | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 92e3d4b..5adf038 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -60,23 +60,22 @@ def process_args(args): " Refer to \"wal -h\" for more info.") exit(1) + if args.v: + print(f"wal {wal.__version__}") + exit(0) + if args.q: sys.stdout = sys.stderr = open(os.devnull, "w") if args.c: shutil.rmtree(wal.CACHE_DIR / "schemes") - util.create_dir(wal.CACHE_DIR / "schemes") if args.r: wal.reload_colors(args.t) - if args.v: - print(f"wal {wal.__version__}") - exit(0) - if args.i: image_file = wal.get_image(args.i) - colors_plain = wal.create_palette(img=image_file, quiet=args.q) + colors_plain = wal.create_palette(img=image_file, notify=not args.q) elif args.f: colors_plain = util.read_file_json(args.f) @@ -96,7 +95,6 @@ def process_args(args): def main(): """Main script function.""" - util.create_dir(wal.CACHE_DIR / "schemes") args = get_args() process_args(args) diff --git a/pywal/reload.py b/pywal/reload.py index 77a496a..66fdea9 100644 --- a/pywal/reload.py +++ b/pywal/reload.py @@ -10,7 +10,9 @@ from pywal import util def reload_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"]) + subprocess.call(["xrdb", "-merge", cache_dir / "colors.Xresources"], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) def reload_i3(): diff --git a/pywal/util.py b/pywal/util.py index b109744..6338989 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -71,12 +71,16 @@ def read_file_raw(input_file): def save_file(data, export_file): """Write data to a file.""" + create_dir(os.path.dirname(export_file)) + with open(export_file, "w") as file: file.write(data) def save_file_json(data, export_file): """Write data to a json file.""" + create_dir(os.path.dirname(export_file)) + with open(export_file, "w") as file: json.dump(data, file, indent=4) @@ -107,10 +111,10 @@ def disown(*cmd): preexec_fn=os.setpgrp) -def msg(input_msg, quiet): +def msg(input_msg, notify): """Print to the terminal and display a libnotify notification.""" - if not quiet: + if notify: disown("notify-send", input_msg) print(input_msg) diff --git a/pywal/wal.py b/pywal/wal.py index 695645a..e9e3e85 100644 --- a/pywal/wal.py +++ b/pywal/wal.py @@ -25,9 +25,9 @@ def get_image(img, cache_dir=CACHE_DIR): def create_palette(img, cache_dir=CACHE_DIR, - color_count=COLOR_COUNT, quiet=True): + color_count=COLOR_COUNT, notify=False): """Create a palette and return it as a dict.""" - return magic.get_colors(img, cache_dir, color_count, quiet) + return magic.get_colors(img, cache_dir, color_count, notify) def send_sequences(colors, vte, cache_dir=CACHE_DIR): -- cgit v1.2.3 From 09f5d26e78e0a7ee89c778786d03c6120cbd2708 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 11:21:39 +1000 Subject: tests: Cover random image. --- tests/test_files/test2.jpg | 1 + tests/test_image.py | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) create mode 120000 tests/test_files/test2.jpg diff --git a/tests/test_files/test2.jpg b/tests/test_files/test2.jpg new file mode 120000 index 0000000..0e8c5b6 --- /dev/null +++ b/tests/test_files/test2.jpg @@ -0,0 +1 @@ +test.jpg \ No newline at end of file diff --git a/tests/test_image.py b/tests/test_image.py index b9099f9..95a357e 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -5,20 +5,20 @@ import unittest from pywal import wal -DEVNULL = pathlib.Path("/dev/null") +CACHE_DIR = pathlib.Path("/tmp/wal") class TestImage(unittest.TestCase): """Test image functions.""" def test_get_img(self): """> Validate image file.""" - result = wal.get_image("tests/test_files/test.jpg", DEVNULL) + result = wal.get_image("tests/test_files/test.jpg", CACHE_DIR) self.assertEqual(result, "tests/test_files/test.jpg") def test_get_img_dir(self): """> Validate image directory.""" - result = wal.get_image("tests/test_files", DEVNULL) - self.assertEqual(result, "tests/test_files/test.jpg") + result = wal.get_image("tests/test_files", CACHE_DIR) + self.assertEqual(result, "tests/test_files/test2.jpg") if __name__ == "__main__": -- cgit v1.2.3 From 6e39b78dc3a8ae6fc164f2fe26f6c0004d92dc41 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 17:46:40 +1000 Subject: general: Use __all__ in __init__.py --- pywal/__init__.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pywal/__init__.py b/pywal/__init__.py index 9de680e..52cedff 100644 --- a/pywal/__init__.py +++ b/pywal/__init__.py @@ -2,7 +2,6 @@ wal - Generate and change colorschemes on the fly. Created by Dylan Araps. """ -# flake8: noqa: F401 from pywal.wal import __version__ from pywal.wal import create_palette from pywal.wal import export_all_templates @@ -11,3 +10,14 @@ from pywal.wal import reload_colors from pywal.wal import reload_env from pywal.wal import send_sequences from pywal.wal import set_wallpaper + +__all__ = [ + "__version__", + "create_palette", + "export_all_templates", + "get_image", + "reload_colors", + "reload_env", + "send_sequences", + "set_wallpaper", +] -- cgit v1.2.3 From 016e97d6e640ec8f35965f9852f46104c83e982b Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 19:11:14 +1000 Subject: lint: Remove all lint comments. --- pywal/__main__.py | 2 +- pywal/magic.py | 6 +++--- pywal/sequences.py | 7 ++++--- pywal/template.py | 5 ++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 5adf038..bc772ec 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -68,7 +68,7 @@ def process_args(args): sys.stdout = sys.stderr = open(os.devnull, "w") if args.c: - shutil.rmtree(wal.CACHE_DIR / "schemes") + shutil.rmtree(wal.CACHE_DIR / "schemes", ignore_errors=True) if args.r: wal.reload_colors(args.t) diff --git a/pywal/magic.py b/pywal/magic.py index b384b97..757fdce 100644 --- a/pywal/magic.py +++ b/pywal/magic.py @@ -83,10 +83,10 @@ def sort_colors(img, colors): colors_special.update({"cursor": raw_colors[15]}) colors_hex = {} - [colors_hex.update({f"color{index}": color}) # pylint: disable=W0106 - for index, color in enumerate(raw_colors)] - colors_hex["color8"] = util.set_grey(raw_colors) + for index, color in enumerate(raw_colors): + colors_hex.update({f"color{index}": color}) + colors_hex["color8"] = util.set_grey(raw_colors) colors["special"] = colors_special colors["colors"] = colors_hex diff --git a/pywal/sequences.py b/pywal/sequences.py index d7f1fa9..c1a7bdd 100644 --- a/pywal/sequences.py +++ b/pywal/sequences.py @@ -44,9 +44,10 @@ def send_sequences(colors, vte, cache_dir): if len(term) < 4] terminals.append(cache_dir / "sequences") - # Send the sequences to all open terminals. - # pylint: disable=W0106 - [util.save_file("".join(sequences), term) for term in terminals] + # Writing to "/dev/pts/[0-9] lets you send data to open terminals. + for term in terminals: + util.save_file("".join(sequences), term) + print("colors: Set terminal colors") diff --git a/pywal/template.py b/pywal/template.py index 1dc72bf..210e45e 100644 --- a/pywal/template.py +++ b/pywal/template.py @@ -26,6 +26,5 @@ def export_all_templates(colors, output_dir, template_dir=None): **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)] + for file in os.scandir(template_dir): + template(all_colors, file.path, output_dir) -- cgit v1.2.3 From a4ea6a4f7addc3ada3c466a763331d793190a342 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 19:12:45 +1000 Subject: travis: Remove flag from pylint --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f924d60..36c9a18 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,5 +11,5 @@ install: script: - flake8 pywal tests setup.py - - pylint --ignore-imports=yes pywal tests setup.py + - pylint pywal tests setup.py - python setup.py test -- cgit v1.2.3 From de47c1beaaa77ee6f50235daba735ccc98ad608c Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 22:26:49 +1000 Subject: api: Remove wal.py --- pywal/__init__.py | 25 ++++++++----- pywal/__main__.py | 39 ++++++++++++++------- pywal/colors.py | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ pywal/image.py | 5 +-- pywal/magic.py | 93 ------------------------------------------------ pywal/reload.py | 22 ++++++++++-- pywal/sequences.py | 26 +++----------- pywal/settings.py | 16 +++++++++ pywal/template.py | 5 +-- pywal/wal.py | 55 ----------------------------- pywal/wallpaper.py | 2 +- tests/test_colors.py | 17 +++++++++ tests/test_image.py | 10 ++---- tests/test_magic.py | 17 --------- tests/test_template.py | 4 +-- 15 files changed, 207 insertions(+), 224 deletions(-) create mode 100644 pywal/colors.py delete mode 100644 pywal/magic.py create mode 100644 pywal/settings.py delete mode 100644 pywal/wal.py create mode 100755 tests/test_colors.py delete mode 100755 tests/test_magic.py diff --git a/pywal/__init__.py b/pywal/__init__.py index 52cedff..ec61c5c 100644 --- a/pywal/__init__.py +++ b/pywal/__init__.py @@ -1,15 +1,22 @@ """ -wal - Generate and change colorschemes on the fly. + '|| +... ... .... ... ... ... ... .... || + ||' || '|. | || || | '' .|| || + || | '|.| ||| ||| .|' || || + ||...' '| | | '|..'|' .||. + || .. | +'''' '' Created by Dylan Araps. """ -from pywal.wal 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 + +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 as export_all_templates +from .wallpaper import change as set_wallpaper __all__ = [ "__version__", diff --git a/pywal/__main__.py b/pywal/__main__.py index bc772ec..626292f 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -1,14 +1,27 @@ """ -wal - Generate and change colorschemes on the fly. + '|| +... ... .... ... ... ... ... .... || + ||' || '|. | || || | '' .|| || + || | '|.| ||| ||| .|' || || + ||...' '| | | '|..'|' .||. + || .. | +'''' '' Created by Dylan Araps. """ + import argparse import os import shutil import sys -from pywal import wal -from pywal import util +from .settings import __version__, __cache_dir__ +from . import colors +from . import image +from . import reload +from . import sequences +from . import template +from . import util +from . import wallpaper def get_args(): @@ -61,33 +74,33 @@ def process_args(args): exit(1) if args.v: - print(f"wal {wal.__version__}") + print(f"wal {__version__}") exit(0) if args.q: sys.stdout = sys.stderr = open(os.devnull, "w") if args.c: - shutil.rmtree(wal.CACHE_DIR / "schemes", ignore_errors=True) + shutil.rmtree(__cache_dir__ / "schemes", ignore_errors=True) if args.r: - wal.reload_colors(args.t) + reload.colors(args.t) if args.i: - image_file = wal.get_image(args.i) - colors_plain = wal.create_palette(img=image_file, notify=not args.q) + image_file = image.get(args.i) + colors_plain = colors.get(image_file, notify=not args.q) - elif args.f: + if args.f: colors_plain = util.read_file_json(args.f) if args.i or args.f: - wal.send_sequences(colors_plain, args.t) + sequences.send(colors_plain, args.t) if not args.n: - wal.set_wallpaper(colors_plain["wallpaper"]) + wallpaper.change(colors_plain["wallpaper"]) - wal.export_all_templates(colors_plain) - wal.reload_env() + template.export_all(colors_plain) + reload.env() if args.o: util.disown(args.o) diff --git a/pywal/colors.py b/pywal/colors.py new file mode 100644 index 0000000..08490a4 --- /dev/null +++ b/pywal/colors.py @@ -0,0 +1,95 @@ +""" +Generate a colorscheme using imagemagick. +""" +import re +import shutil +import subprocess + +from .settings import __cache_dir__, __color_count__ +from . import util + + +def imagemagick(color_count, img): + """Call Imagemagick to generate a scheme.""" + colors = subprocess.Popen(["convert", img, "+dither", "-colors", + str(color_count), "-unique-colors", "txt:-"], + stdout=subprocess.PIPE) + + return colors.stdout.readlines() + + +def gen_colors(img, color_count): + """Format the output from imagemagick into a list + of hex colors.""" + if not shutil.which("convert"): + print("error: imagemagick not found, exiting...\n" + "error: wal requires imagemagick to function.") + exit(1) + + raw_colors = imagemagick(color_count, img) + + index = 0 + while len(raw_colors) - 1 < color_count: + index += 1 + raw_colors = imagemagick(color_count + index, img) + + print("colors: Imagemagick couldn't generate a", color_count, + "color palette, trying a larger palette size", + color_count + index) + + if index > 20: + print("colors: Imagemagick couldn't generate a suitable scheme", + "for the image. Exiting...") + quit(1) + + # Remove the first element because it isn't a color code. + del raw_colors[0] + + return [re.search("#.{6}", str(col)).group(0) for col in raw_colors] + + +def sort_colors(img, colors): + """Sort the generated colors and store them in a dict that + we will later save in json format.""" + raw_colors = colors[:1] + colors[9:] + colors[8:] + + colors = {"wallpaper": img} + + colors_special = {} + colors_special.update({"background": raw_colors[0]}) + colors_special.update({"foreground": raw_colors[15]}) + colors_special.update({"cursor": raw_colors[15]}) + + colors_hex = {} + for index, color in enumerate(raw_colors): + colors_hex.update({f"color{index}": color}) + + colors_hex["color8"] = util.set_grey(raw_colors) + colors["special"] = colors_special + colors["colors"] = colors_hex + + return colors + + +def get(img, cache_dir=__cache_dir__, + color_count=__color_count__, notify=False): + """Get the colorscheme.""" + # _home_dylan_img_jpg.json + cache_file = cache_dir / "schemes" / \ + img.replace("/", "_").replace(".", "_") + cache_file = cache_file.with_suffix(".json") + + if cache_file.is_file(): + colors = util.read_file_json(cache_file) + print("colors: Found cached colorscheme.") + + else: + util.msg("wal: Generating a colorscheme...", notify) + + colors = gen_colors(img, color_count) + colors = sort_colors(img, colors) + + util.save_file_json(colors, cache_file) + util.msg("wal: Generation complete.", notify) + + return colors diff --git a/pywal/image.py b/pywal/image.py index 421943e..44b82f2 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -5,7 +5,8 @@ import os import pathlib import random -from pywal import util +from .settings import __cache_dir__ +from . import util def get_random_image(img_dir, cache_dir): @@ -27,7 +28,7 @@ def get_random_image(img_dir, cache_dir): return str(img_dir / random.choice(images).name) -def get_image(img, cache_dir): +def get(img, cache_dir=__cache_dir__): """Validate image input.""" image = pathlib.Path(img) diff --git a/pywal/magic.py b/pywal/magic.py deleted file mode 100644 index 757fdce..0000000 --- a/pywal/magic.py +++ /dev/null @@ -1,93 +0,0 @@ -""" -Generate a colorscheme using imagemagick. -""" -import re -import shutil -import subprocess - -from pywal import util - - -def imagemagick(color_count, img): - """Call Imagemagick to generate a scheme.""" - colors = subprocess.Popen(["convert", img, "+dither", "-colors", - str(color_count), "-unique-colors", "txt:-"], - stdout=subprocess.PIPE) - - return colors.stdout.readlines() - - -def gen_colors(img, color_count): - """Format the output from imagemagick into a list - of hex colors.""" - if not shutil.which("convert"): - print("error: imagemagick not found, exiting...\n" - "error: wal requires imagemagick to function.") - exit(1) - - raw_colors = imagemagick(color_count, img) - - index = 0 - while len(raw_colors) - 1 < color_count: - index += 1 - raw_colors = imagemagick(color_count + index, img) - - print("colors: Imagemagick couldn't generate a", color_count, - "color palette, trying a larger palette size", - color_count + index) - - if index > 20: - print("colors: Imagemagick couldn't generate a suitable scheme", - "for the image. Exiting...") - quit(1) - - # Remove the first element because it isn't a color code. - del raw_colors[0] - - return [re.search("#.{6}", str(col)).group(0) for col in raw_colors] - - -def get_colors(img, cache_dir, color_count, quiet): - """Get the colorscheme.""" - # _home_dylan_img_jpg.json - cache_file = cache_dir / "schemes" / \ - img.replace("/", "_").replace(".", "_") - cache_file = cache_file.with_suffix(".json") - - if cache_file.is_file(): - colors = util.read_file_json(cache_file) - print("colors: Found cached colorscheme.") - - else: - util.msg("wal: Generating a colorscheme...", quiet) - - colors = gen_colors(img, color_count) - colors = sort_colors(img, colors) - - util.save_file_json(colors, cache_file) - util.msg("wal: Generation complete.", quiet) - - return colors - - -def sort_colors(img, colors): - """Sort the generated colors and store them in a dict that - we will later save in json format.""" - raw_colors = colors[:1] + colors[9:] + colors[8:] - - colors = {"wallpaper": img} - - colors_special = {} - colors_special.update({"background": raw_colors[0]}) - colors_special.update({"foreground": raw_colors[15]}) - colors_special.update({"cursor": raw_colors[15]}) - - colors_hex = {} - for index, color in enumerate(raw_colors): - colors_hex.update({f"color{index}": color}) - - colors_hex["color8"] = util.set_grey(raw_colors) - colors["special"] = colors_special - colors["colors"] = colors_hex - - return colors diff --git a/pywal/reload.py b/pywal/reload.py index 66fdea9..da09fe0 100644 --- a/pywal/reload.py +++ b/pywal/reload.py @@ -1,10 +1,12 @@ """ Reload programs. """ +import re import shutil import subprocess -from pywal import util +from .settings import __cache_dir__ +from . import util def reload_xrdb(cache_dir): @@ -27,9 +29,25 @@ def reload_polybar(): util.disown("pkill", "-USR1", "polybar") -def reload_env(cache_dir): +def env(cache_dir=__cache_dir__): """Reload environment.""" reload_xrdb(cache_dir) reload_i3() reload_polybar() print("reload: Reloaded environment.") + + +def colors(vte, cache_dir=__cache_dir__): + """Reload the current scheme.""" + sequence_file = cache_dir / "sequences" + + if sequence_file.is_file(): + sequences = "".join(util.read_file(sequence_file)) + + # If vte mode was used, remove the unsupported sequence. + if vte: + sequences = re.sub(r"\]708;\#.{6}", "", sequences) + + print(sequences, end="") + + exit(0) diff --git a/pywal/sequences.py b/pywal/sequences.py index c1a7bdd..77eace3 100644 --- a/pywal/sequences.py +++ b/pywal/sequences.py @@ -2,9 +2,9 @@ Send sequences to all open terminals. """ import os -import re -from pywal import util +from .settings import __cache_dir__ +from . import util def set_special(index, color): @@ -17,14 +17,14 @@ def set_color(index, color): return f"\033]4;{index};{color}\007" -def send_sequences(colors, vte, cache_dir): +def send(colors, vte, cache_dir=__cache_dir__): """Send colors to all open terminals.""" # Colors 0-15. sequences = [set_color(num, color) for num, color in enumerate(colors["colors"].values())] # Special colors. - # http://pod.tst.eu/http://cvs.schmorp.de/rxvt-unicode/doc/rxvt.7.pod#XTerm_Operating_System_Commands + # Source: https://goo.gl/KcoQgP # 10 = foreground, 11 = background, 12 = cursor foregound # 13 = mouse foreground sequences.append(set_special(10, colors["special"]["foreground"])) @@ -32,7 +32,7 @@ def send_sequences(colors, vte, cache_dir): sequences.append(set_special(12, colors["special"]["cursor"])) sequences.append(set_special(13, colors["special"]["cursor"])) - # Set a blank color that isn"t affected by bold highlighting. + # Set a blank color that isn't affected by bold highlighting. # Used in wal.vim's airline theme. sequences.append(set_color(66, colors["special"]["background"])) @@ -49,19 +49,3 @@ def send_sequences(colors, vte, cache_dir): util.save_file("".join(sequences), term) print("colors: Set terminal colors") - - -def reload_colors(vte, cache_dir): - """Reload the current scheme.""" - sequence_file = cache_dir / "sequences" - - if sequence_file.is_file(): - sequences = "".join(util.read_file(sequence_file)) - - # If vte mode was used, remove the unsupported sequence. - if vte: - sequences = re.sub(r"\]708;\#.{6}", "", sequences) - - print(sequences, end="") - - exit(0) diff --git a/pywal/settings.py b/pywal/settings.py new file mode 100644 index 0000000..acfc625 --- /dev/null +++ b/pywal/settings.py @@ -0,0 +1,16 @@ +""" + '|| +... ... .... ... ... ... ... .... || + ||' || '|. | || || | '' .|| || + || | '|.| ||| ||| .|' || || + ||...' '| | | '|..'|' .||. + || .. | +'''' '' +Created by Dylan Araps. +""" + +import pathlib + +__version__ = "0.4.0" +__cache_dir__ = pathlib.Path.home() / ".cache/wal/" +__color_count__ = 16 diff --git a/pywal/template.py b/pywal/template.py index 210e45e..1cb4197 100644 --- a/pywal/template.py +++ b/pywal/template.py @@ -3,7 +3,8 @@ Export colors in various formats. """ import os -from pywal import util +from .settings import __cache_dir__ +from . import util def template(colors, input_file, output_dir): @@ -16,7 +17,7 @@ def template(colors, input_file, output_dir): print(f"export: Exported {template_name}.") -def export_all_templates(colors, output_dir, template_dir=None): +def export_all(colors, output_dir=__cache_dir__, template_dir=None): """Export all template files.""" template_dir = template_dir or \ os.path.join(os.path.dirname(__file__), "templates") diff --git a/pywal/wal.py b/pywal/wal.py deleted file mode 100644 index e9e3e85..0000000 --- a/pywal/wal.py +++ /dev/null @@ -1,55 +0,0 @@ -""" -wal - Generate and change colorschemes on the fly. -Created by Dylan Araps. -""" -import pathlib - -from pywal import image -from pywal import magic -from pywal import reload -from pywal import sequences -from pywal import template -from pywal import wallpaper - - -__version__ = "0.4.0" - - -COLOR_COUNT = 16 -CACHE_DIR = pathlib.Path.home() / ".cache/wal/" - - -def get_image(img, cache_dir=CACHE_DIR): - """Validate image input.""" - return image.get_image(img, cache_dir) - - -def create_palette(img, cache_dir=CACHE_DIR, - color_count=COLOR_COUNT, notify=False): - """Create a palette and return it as a dict.""" - return magic.get_colors(img, cache_dir, color_count, notify) - - -def send_sequences(colors, vte, cache_dir=CACHE_DIR): - """Send the sequences.""" - sequences.send_sequences(colors, vte, cache_dir) - - -def reload_env(cache_dir=CACHE_DIR): - """Reload the environment.""" - reload.reload_env(cache_dir) - - -def export_all_templates(colors, output_dir=CACHE_DIR, template_dir=None): - """Export all templates.""" - template.export_all_templates(colors, output_dir, template_dir) - - -def set_wallpaper(img): - """Set the wallpaper.""" - wallpaper.set_wallpaper(img) - - -def reload_colors(vte, cache_dir=CACHE_DIR): - """Reload the colors.""" - sequences.reload_colors(vte, cache_dir) diff --git a/pywal/wallpaper.py b/pywal/wallpaper.py index 5e524a3..99b978e 100644 --- a/pywal/wallpaper.py +++ b/pywal/wallpaper.py @@ -80,7 +80,7 @@ def set_desktop_wallpaper(desktop, img): set_wm_wallpaper(img) -def set_wallpaper(img): +def change(img): """Set the wallpaper.""" if not os.path.isfile(img): return diff --git a/tests/test_colors.py b/tests/test_colors.py new file mode 100755 index 0000000..e83c54d --- /dev/null +++ b/tests/test_colors.py @@ -0,0 +1,17 @@ +"""Test imagemagick functions.""" +import unittest + +from pywal import colors + + +class TestGenColors(unittest.TestCase): + """Test the gen_colors functions.""" + + def test_gen_colors(self): + """> Generate a colorscheme.""" + result = colors.get("tests/test_files/test.jpg") + self.assertEqual(result["colors"]["color0"], "#0F191A") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_image.py b/tests/test_image.py index 95a357e..da557a7 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -1,23 +1,19 @@ """Test image functions.""" -import pathlib import unittest -from pywal import wal - - -CACHE_DIR = pathlib.Path("/tmp/wal") +from pywal import image class TestImage(unittest.TestCase): """Test image functions.""" def test_get_img(self): """> Validate image file.""" - result = wal.get_image("tests/test_files/test.jpg", CACHE_DIR) + result = image.get("tests/test_files/test.jpg") self.assertEqual(result, "tests/test_files/test.jpg") def test_get_img_dir(self): """> Validate image directory.""" - result = wal.get_image("tests/test_files", CACHE_DIR) + result = image.get("tests/test_files") self.assertEqual(result, "tests/test_files/test2.jpg") diff --git a/tests/test_magic.py b/tests/test_magic.py deleted file mode 100755 index a3434df..0000000 --- a/tests/test_magic.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Test imagemagick functions.""" -import unittest - -from pywal import wal - - -class TestGenColors(unittest.TestCase): - """Test the gen_colors functions.""" - - def test_gen_colors(self): - """> Generate a colorscheme.""" - result = wal.create_palette("tests/test_files/test.jpg") - self.assertEqual(result["colors"]["color0"], "#0F191A") - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_template.py b/tests/test_template.py index 992c9c0..ef71b8b 100755 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -2,7 +2,7 @@ import unittest import pathlib -from pywal import wal +from pywal import template from pywal import util @@ -21,7 +21,7 @@ class TestExportColors(unittest.TestCase): output_dir = pathlib.Path("/tmp") template_dir = pathlib.Path("tests/test_files/templates") - wal.export_all_templates(COLORS, output_dir, template_dir) + template.export_all(COLORS, output_dir, template_dir) result = pathlib.Path("/tmp/test_template").is_file() self.assertTrue(result) -- cgit v1.2.3 From 1e1150bd5612c7afaafd8e7ef65f8e379383f39c Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 22:30:54 +1000 Subject: template: Minor change. --- pywal/template.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pywal/template.py b/pywal/template.py index 1cb4197..de3c110 100644 --- a/pywal/template.py +++ b/pywal/template.py @@ -13,6 +13,7 @@ def template(colors, input_file, output_dir): 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}.") -- cgit v1.2.3 From a84275f1f409e85f873cdbf0caa93c7dc36ff357 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 22:43:35 +1000 Subject: template: Rename arg to make more sense. --- pywal/template.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pywal/template.py b/pywal/template.py index de3c110..ca72a12 100644 --- a/pywal/template.py +++ b/pywal/template.py @@ -7,18 +7,18 @@ from .settings import __cache_dir__ from . import util -def template(colors, input_file, output_dir): +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, output_dir / template_name) + util.save_file(template_data, cache_dir / template_name) print(f"export: Exported {template_name}.") -def export_all(colors, output_dir=__cache_dir__, template_dir=None): +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") @@ -29,4 +29,4 @@ def export_all(colors, output_dir=__cache_dir__, template_dir=None): all_colors = {k: util.Color(v) for k, v in all_colors.items()} for file in os.scandir(template_dir): - template(all_colors, file.path, output_dir) + template(all_colors, file.path, cache_dir) -- cgit v1.2.3 From d1c2b8023de2585befc5b68e69ed6d0f3ee5d27e Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 23:27:43 +1000 Subject: 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.") -- cgit v1.2.3 From 676f5325e11cfc12d6080f6cdb27be2e697a88a8 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 23:48:47 +1000 Subject: tests: Add new template test. --- tests/test_files/templates/test_template | 3 --- tests/test_template.py | 33 +++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 12 deletions(-) delete mode 100644 tests/test_files/templates/test_template diff --git a/tests/test_files/templates/test_template b/tests/test_files/templates/test_template deleted file mode 100644 index af40ce7..0000000 --- a/tests/test_files/templates/test_template +++ /dev/null @@ -1,3 +0,0 @@ -test1 {color0} -test2 {background} -test3 {background.rgb} diff --git a/tests/test_template.py b/tests/test_template.py index ef71b8b..9b9870d 100755 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -8,28 +8,43 @@ from pywal import util # Import colors. COLORS = util.read_file_json("tests/test_files/test_file.json") +OUTPUT_DIR = pathlib.Path("/tmp/wal") + +util.create_dir("/tmp/wal") class TestExportColors(unittest.TestCase): """Test the export functions.""" - def test_template(self): + def test_all_templates(self): """> Test substitutions in template file.""" # Merge both dicts so we can access their # values simpler. COLORS["colors"].update(COLORS["special"]) - output_dir = pathlib.Path("/tmp") - template_dir = pathlib.Path("tests/test_files/templates") - template.export_all(COLORS, output_dir, template_dir) + template.export_all(COLORS, OUTPUT_DIR) + + result = pathlib.Path("/tmp/colors.sh").is_file() + self.assertTrue(result) + + content = pathlib.Path("/tmp/colors.sh").read_text() + content = content.split("\n")[6] + self.assertEqual(content, "foreground='#F5F1F4'") + + def test_css_template(self): + """> Test substitutions in template file (css).""" + # Merge both dicts so we can access their + # values simpler. + COLORS["colors"].update(COLORS["special"]) + + template.export(COLORS, "colors.css", OUTPUT_DIR) - result = pathlib.Path("/tmp/test_template").is_file() + result = pathlib.Path("/tmp/colors.css").is_file() self.assertTrue(result) - content = pathlib.Path("/tmp/test_template").read_text() - self.assertEqual(content, '\n'.join(["test1 #1F211E", - "test2 #1F211E", - "test3 31,33,30", ""])) + content = pathlib.Path("/tmp/colors.css").read_text() + content = content.split("\n")[6] + self.assertEqual(content, " --background: #1F211E;") if __name__ == "__main__": -- cgit v1.2.3 From 7566104c85f0f0f277d917ccb7a14682b5d3630f Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 23:50:40 +1000 Subject: tests: Add new template test. --- tests/test_template.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/test_template.py b/tests/test_template.py index 9b9870d..f1b7dc5 100755 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -8,6 +8,7 @@ from pywal import util # Import colors. COLORS = util.read_file_json("tests/test_files/test_file.json") +COLORS["colors"].update(COLORS["special"]) OUTPUT_DIR = pathlib.Path("/tmp/wal") util.create_dir("/tmp/wal") @@ -18,10 +19,6 @@ class TestExportColors(unittest.TestCase): def test_all_templates(self): """> Test substitutions in template file.""" - # Merge both dicts so we can access their - # values simpler. - COLORS["colors"].update(COLORS["special"]) - template.export_all(COLORS, OUTPUT_DIR) result = pathlib.Path("/tmp/colors.sh").is_file() @@ -33,10 +30,6 @@ class TestExportColors(unittest.TestCase): def test_css_template(self): """> Test substitutions in template file (css).""" - # Merge both dicts so we can access their - # values simpler. - COLORS["colors"].update(COLORS["special"]) - template.export(COLORS, "colors.css", OUTPUT_DIR) result = pathlib.Path("/tmp/colors.css").is_file() -- cgit v1.2.3 From bd5611e35cba2cfa0a57f3f854ed157638da3c95 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 22 Jul 2017 23:57:18 +1000 Subject: tests: Fix tests --- tests/test_template.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_template.py b/tests/test_template.py index f1b7dc5..14b1124 100755 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -21,10 +21,10 @@ class TestExportColors(unittest.TestCase): """> Test substitutions in template file.""" template.export_all(COLORS, OUTPUT_DIR) - result = pathlib.Path("/tmp/colors.sh").is_file() + result = pathlib.Path("/tmp/wal/colors.sh").is_file() self.assertTrue(result) - content = pathlib.Path("/tmp/colors.sh").read_text() + content = pathlib.Path("/tmp/wal/colors.sh").read_text() content = content.split("\n")[6] self.assertEqual(content, "foreground='#F5F1F4'") @@ -32,10 +32,10 @@ class TestExportColors(unittest.TestCase): """> Test substitutions in template file (css).""" template.export(COLORS, "colors.css", OUTPUT_DIR) - result = pathlib.Path("/tmp/colors.css").is_file() + result = pathlib.Path("/tmp/wal/colors.css").is_file() self.assertTrue(result) - content = pathlib.Path("/tmp/colors.css").read_text() + content = pathlib.Path("/tmp/wal/colors.css").read_text() content = content.split("\n")[6] self.assertEqual(content, " --background: #1F211E;") -- 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/__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.") -- cgit v1.2.3 From 1eb16c8f97332bc63103dae6f2ac1fb46a168445 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 23 Jul 2017 11:05:20 +1000 Subject: api: More changes. --- pywal/__main__.py | 4 ++-- pywal/image.py | 8 +++----- pywal/wallpaper.py | 11 ++++++++++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 626292f..36cec51 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -16,10 +16,10 @@ import sys from .settings import __version__, __cache_dir__ from . import colors +from . import export from . import image from . import reload from . import sequences -from . import template from . import util from . import wallpaper @@ -99,7 +99,7 @@ def process_args(args): if not args.n: wallpaper.change(colors_plain["wallpaper"]) - template.export_all(colors_plain) + export.every(colors_plain) reload.env() if args.o: diff --git a/pywal/image.py b/pywal/image.py index 44b82f2..3f28923 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -7,15 +7,13 @@ import random from .settings import __cache_dir__ from . import util +from . import wallpaper def get_random_image(img_dir, cache_dir): """Pick a random image file from a directory.""" - current_wall = cache_dir / "wal" - - if current_wall.is_file(): - current_wall = util.read_file(current_wall) - current_wall = os.path.basename(current_wall[0]) + current_wall = wallpaper.get() + current_wall = os.path.basename(current_wall[0]) file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif") images = [img for img in os.scandir(img_dir) diff --git a/pywal/wallpaper.py b/pywal/wallpaper.py index 99b978e..836a2c6 100644 --- a/pywal/wallpaper.py +++ b/pywal/wallpaper.py @@ -3,7 +3,8 @@ import os import shutil import subprocess -from pywal import util +from .settings import __cache_dir__ +from . import util def get_desktop_env(): @@ -94,3 +95,11 @@ def change(img): set_wm_wallpaper(img) print("wallpaper: Set the new wallpaper") + + +def get(cache_dir=__cache_dir__): + """Get the current wallpaper.""" + current_wall = cache_dir / "wal" + + if current_wall.is_file(): + return util.read_file(current_wall)[0] -- cgit v1.2.3 From 7e5723d7c1104f984642059e7e514a58924afd49 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 23 Jul 2017 11:32:03 +1000 Subject: reload: Add missing arg. --- .pylintrc | 2 ++ pywal/reload.py | 10 ++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 .pylintrc diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..636c5a3 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,2 @@ +[BASIC] +good-names=i3 diff --git a/pywal/reload.py b/pywal/reload.py index 2648a70..12769bf 100644 --- a/pywal/reload.py +++ b/pywal/reload.py @@ -9,10 +9,12 @@ from .settings import __cache_dir__ from . import util -def xrdb(cache_dir): +def xrdb(xrdb_file=None): """Merge the colors into the X db so new terminals use them.""" + xrdb_file = xrdb_file or __cache_dir__ / "colors.Xresources" + if shutil.which("xrdb"): - subprocess.call(["xrdb", "-merge", cache_dir / "colors.Xresources"], + subprocess.call(["xrdb", "-merge", xrdb_file], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) @@ -29,9 +31,9 @@ def polybar(): util.disown("pkill", "-USR1", "polybar") -def env(cache_dir=__cache_dir__): +def env(xrdb_file=None): """Reload environment.""" - xrdb(cache_dir) + xrdb(xrdb_file) i3() polybar() print("reload: Reloaded environment.") -- cgit v1.2.3 From d8376eecc90b659f7b087ddd2ec0f29200ae6141 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 23 Jul 2017 11:50:46 +1000 Subject: tests: Update tests. --- tests/test_export.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ tests/test_image.py | 2 +- tests/test_template.py | 44 -------------------------------------------- 3 files changed, 45 insertions(+), 45 deletions(-) create mode 100755 tests/test_export.py delete mode 100755 tests/test_template.py diff --git a/tests/test_export.py b/tests/test_export.py new file mode 100755 index 0000000..ebf5740 --- /dev/null +++ b/tests/test_export.py @@ -0,0 +1,44 @@ +"""Test export functions.""" +import unittest +import pathlib + +from pywal import export +from pywal import util + + +# Import colors. +COLORS = util.read_file_json("tests/test_files/test_file.json") +COLORS["colors"].update(COLORS["special"]) +OUTPUT_DIR = pathlib.Path("/tmp/wal") + +util.create_dir("/tmp/wal") + + +class TestExportColors(unittest.TestCase): + """Test the export functions.""" + + def test_all_templates(self): + """> Test substitutions in template file.""" + export.every(COLORS, OUTPUT_DIR) + + result = pathlib.Path("/tmp/wal/colors.sh").is_file() + self.assertTrue(result) + + content = pathlib.Path("/tmp/wal/colors.sh").read_text() + content = content.split("\n")[6] + self.assertEqual(content, "foreground='#F5F1F4'") + + def test_css_template(self): + """> Test substitutions in template file (css).""" + export.color(COLORS, "css", OUTPUT_DIR / "test.css") + + result = pathlib.Path("/tmp/wal/test.css").is_file() + self.assertTrue(result) + + content = pathlib.Path("/tmp/wal/test.css").read_text() + content = content.split("\n")[6] + self.assertEqual(content, " --background: #1F211E;") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_image.py b/tests/test_image.py index da557a7..e0b7902 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -14,7 +14,7 @@ class TestImage(unittest.TestCase): def test_get_img_dir(self): """> Validate image directory.""" result = image.get("tests/test_files") - self.assertEqual(result, "tests/test_files/test2.jpg") + self.assertEqual(result.endswith(".jpg"), True) if __name__ == "__main__": diff --git a/tests/test_template.py b/tests/test_template.py deleted file mode 100755 index 14b1124..0000000 --- a/tests/test_template.py +++ /dev/null @@ -1,44 +0,0 @@ -"""Test export functions.""" -import unittest -import pathlib - -from pywal import template -from pywal import util - - -# Import colors. -COLORS = util.read_file_json("tests/test_files/test_file.json") -COLORS["colors"].update(COLORS["special"]) -OUTPUT_DIR = pathlib.Path("/tmp/wal") - -util.create_dir("/tmp/wal") - - -class TestExportColors(unittest.TestCase): - """Test the export functions.""" - - def test_all_templates(self): - """> Test substitutions in template file.""" - template.export_all(COLORS, OUTPUT_DIR) - - result = pathlib.Path("/tmp/wal/colors.sh").is_file() - self.assertTrue(result) - - content = pathlib.Path("/tmp/wal/colors.sh").read_text() - content = content.split("\n")[6] - self.assertEqual(content, "foreground='#F5F1F4'") - - def test_css_template(self): - """> Test substitutions in template file (css).""" - template.export(COLORS, "colors.css", OUTPUT_DIR) - - result = pathlib.Path("/tmp/wal/colors.css").is_file() - self.assertTrue(result) - - content = pathlib.Path("/tmp/wal/colors.css").read_text() - content = content.split("\n")[6] - self.assertEqual(content, " --background: #1F211E;") - - -if __name__ == "__main__": - unittest.main() -- cgit v1.2.3 From a0c130cbdaad332473d8c7ef573059e46fa189f9 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 23 Jul 2017 12:03:23 +1000 Subject: tests: Fix tests --- .pylintrc | 3 +++ pywal/image.py | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.pylintrc b/.pylintrc index 636c5a3..10e9185 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,2 +1,5 @@ [BASIC] good-names=i3 + +[SIMILARITIES] +ignore-imports=yes diff --git a/pywal/image.py b/pywal/image.py index 3f28923..eec05f6 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -10,7 +10,7 @@ from . import util from . import wallpaper -def get_random_image(img_dir, cache_dir): +def get_random_image(img_dir): """Pick a random image file from a directory.""" current_wall = wallpaper.get() current_wall = os.path.basename(current_wall[0]) @@ -34,7 +34,7 @@ def get(img, cache_dir=__cache_dir__): wal_img = str(image) elif image.is_dir(): - wal_img = get_random_image(image, cache_dir) + wal_img = get_random_image(image) else: print("error: No valid image file found.") -- cgit v1.2.3 From 38fcae33a09fae218fff7211725e3781f287f5a6 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 23 Jul 2017 14:51:42 +1000 Subject: api: Added function to load colors from file. --- pywal/__main__.py | 2 +- pywal/colors.py | 10 ++++++++++ pywal/util.py | 3 --- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 36cec51..44663df 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -91,7 +91,7 @@ def process_args(args): colors_plain = colors.get(image_file, notify=not args.q) if args.f: - colors_plain = util.read_file_json(args.f) + colors_plain = colors.load(args.f) if args.i or args.f: sequences.send(colors_plain, args.t) diff --git a/pywal/colors.py b/pywal/colors.py index 08490a4..0ede3e1 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -93,3 +93,13 @@ def get(img, cache_dir=__cache_dir__, util.msg("wal: Generation complete.", notify) return colors + + +def file(input_file): + """Import colorscheme from json file.""" + data = util.read_file_json(input_file) + + if "wallpaper" not in data: + data["wallpaper"] = "None" + + return data diff --git a/pywal/util.py b/pywal/util.py index 6338989..1df3c9f 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -55,9 +55,6 @@ def read_file_json(input_file): with open(input_file, "r") as json_file: data = json.load(json_file) - if "wallpaper" not in data: - data["wallpaper"] = "None" - return data -- cgit v1.2.3 From 07b8ad8e0d97f54277c2e744f745ba3e47d0e53c Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 23 Jul 2017 14:59:20 +1000 Subject: tests: Fix tests --- pywal/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 44663df..0af4df6 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -91,7 +91,7 @@ def process_args(args): colors_plain = colors.get(image_file, notify=not args.q) if args.f: - colors_plain = colors.load(args.f) + colors_plain = colors.file(args.f) if args.i or args.f: sequences.send(colors_plain, args.t) -- cgit v1.2.3