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