From d98be353ecd5deff97804312ec798fb227adfbc1 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Wed, 9 Aug 2017 11:22:51 +1000 Subject: [PATCH 01/19] general: Make pywal compatible with python 3.5 --- pywal/__main__.py | 4 ++-- pywal/colors.py | 2 +- pywal/export.py | 6 +++--- pywal/image.py | 2 +- pywal/reload.py | 6 +++--- pywal/sequences.py | 20 ++++++++++---------- pywal/util.py | 26 +++++++++++++------------- pywal/wallpaper.py | 2 +- tests/test_export.py | 2 +- tests/test_main.py | 4 +++- tests/test_sequences.py | 3 ++- tests/test_util.py | 5 ++--- 12 files changed, 42 insertions(+), 40 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index dbca515..412a6d4 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -84,14 +84,14 @@ def process_args(args): sys.exit(1) if args.v: - print(f"wal {__version__}") + print("wal %s" % __version__) sys.exit(0) if args.q: sys.stdout = sys.stderr = open(os.devnull, "w") if args.c: - shutil.rmtree(CACHE_DIR / "schemes", ignore_errors=True) + shutil.rmtree(str(CACHE_DIR / "schemes"), ignore_errors=True) if args.r: reload.colors(args.t) diff --git a/pywal/colors.py b/pywal/colors.py index 2483acd..7a1a991 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -73,7 +73,7 @@ def sort_colors(img, colors): colors_special.update({"cursor": raw_colors[15]}) for index, color in enumerate(raw_colors): - colors_hex.update({f"color{index}": color}) + colors_hex.update({"color%s" % index: color}) colors["special"] = colors_special colors["colors"] = colors_hex diff --git a/pywal/export.py b/pywal/export.py index 748e3a9..d621672 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -45,7 +45,7 @@ def every(colors, output_dir=CACHE_DIR): all_colors = flatten_colors(colors) output_dir = pathlib.Path(output_dir) - for file in os.scandir(MODULE_DIR / "templates"): + for file in os.scandir(str(MODULE_DIR / "templates")): template(all_colors, file.path, output_dir / file.name) print("export: Exported all files.") @@ -61,6 +61,6 @@ def color(colors, export_type, output_file=None): if template_file.is_file(): template(all_colors, template_file, output_file) - print(f"export: Exported {export_type}.") + print("export: Exported %s." % export_type) else: - print(f"[!] warning: template '{export_type}' doesn't exist.") + print("warning: template '%s' doesn't exist." % export_type) diff --git a/pywal/image.py b/pywal/image.py index 787bb80..e55a0b1 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -17,7 +17,7 @@ def get_random_image(img_dir): current_wall = os.path.basename(current_wall) file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif") - images = [img for img in os.scandir(img_dir) + images = [img for img in os.scandir(str(img_dir)) if img.name.endswith(file_types) and img.name != current_wall] if not images: diff --git a/pywal/reload.py b/pywal/reload.py index f284966..11d048f 100644 --- a/pywal/reload.py +++ b/pywal/reload.py @@ -15,7 +15,7 @@ def xrdb(xrdb_file=None): xrdb_file = xrdb_file or CACHE_DIR / "colors.Xresources" if shutil.which("xrdb"): - subprocess.Popen(["xrdb", "-merge", xrdb_file], + subprocess.Popen(["xrdb", "-merge", str(xrdb_file)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).wait() @@ -27,13 +27,13 @@ def gtk(): if theme_path.is_dir(): if gtk2_file.is_file(): - shutil.copy(gtk2_file, theme_path / "gtk-2.0") + shutil.copy(str(gtk2_file), str(theme_path / "gtk-2.0")) # Here we call a Python 2 script to reload the GTK themes. # This is done because the Python 3 GTK/Gdk libraries don't # provide a way of doing this. if shutil.which("python2"): - util.disown(["python2", MODULE_DIR / "scripts" / "gtk_reload.py"]) + util.disown(["python2", str(MODULE_DIR / "scripts" / "gtk_reload.py")]) else: print("warning: GTK2 reload support requires Python 2.") diff --git a/pywal/sequences.py b/pywal/sequences.py index b94bd93..87339d9 100644 --- a/pywal/sequences.py +++ b/pywal/sequences.py @@ -12,30 +12,30 @@ def set_special(index, color, iterm_name="h"): alpha = util.Color.alpha_num if OS == "Darwin": - return f"\033]P{iterm_name}{color.strip('#')}\033\\" + return "\033[P%s%s\033\\" % (iterm_name, color.strip("#")) if index in [11, 708] and alpha != 100: - return f"\033]{index};[{alpha}]{color}\007" + return "\033]%s;[%s]%s\007" % (index, alpha, color) - return f"\033]{index};{color}\007" + return "\033]%s;%s\007" % (index, color) def set_color(index, color): """Convert a hex color to a text color sequence.""" if OS == "Darwin": - return f"\033]P{index:x}{color.strip('#')}\033\\" + return "\033]P%x%s\033\\" % (index, color.strip("#")) - return f"\033]4;{index};{color}\007" + return "\033]4;%s;%s\007" % (index, color) def set_iterm_tab_color(color): """Set iTerm2 tab/window color""" red, green, blue = util.hex_to_rgb(color) - return [ - f"\033]6;1;bg;red;brightness;{red}\a", - f"\033]6;1;bg;green;brightness;{green}\a", - f"\033]6;1;bg;blue;brightness;{blue}\a", - ] + return """ + \033]6;1;bg;red;brightness;%s\a + \033]6;1;bg;green;brightness;%s\a + \033]6;1;bg;blue;brightness;%s\a + """ % (red, green, blue) def create_sequences(colors, vte): diff --git a/pywal/util.py b/pywal/util.py index 9931958..411d459 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -21,7 +21,7 @@ class Color: def rgb(self): """Convert a hex color to rgb.""" red, green, blue = hex_to_rgb(self.hex_color) - return f"{red},{green},{blue}" + return "%s,%s,%s" % (red, green, blue) @property def xrgba(self): @@ -31,19 +31,19 @@ class Color: @property def alpha(self): """Add URxvt alpha value to color.""" - return f"[{self.alpha_num}]{self.hex_color}" + return "[%s]%s" % (self.alpha_num, self.hex_color) def read_file(input_file): """Read data from a file and trim newlines.""" - with open(input_file, "r") as file: + with open(str(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, "r") as json_file: + with open(str(input_file), "r") as json_file: data = json.load(json_file) return data @@ -52,27 +52,27 @@ def read_file_json(input_file): 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: + with open(str(input_file), "r") as file: data = file.readlines() return data def save_file(data, export_file): """Write data to a file.""" - create_dir(os.path.dirname(export_file)) + create_dir(export_file.parent) try: - with open(export_file, "w") as file: + with open(str(export_file), "w") as file: file.write(data) except PermissionError: - print(f"[!] warning: Couldn't write to {export_file}.") + print("warning: Couldn't write to %s." % export_file) def save_file_json(data, export_file): """Write data to a json file.""" - create_dir(os.path.dirname(export_file)) + create_dir(export_file.parent) - with open(export_file, "w") as file: + with open(str(export_file), "w") as file: json.dump(data, file, indent=4) @@ -88,13 +88,13 @@ def hex_to_rgb(color): def hex_to_xrgba(color): """Convert a hex color to xrdb rgba.""" - col = color.lower() - return f"{col[1]}{col[2]}/{col[3]}{col[4]}/{col[5]}{col[6]}/ff" + col = color.lower().strip("#") + return "%s%s/%s%s/%s%s/ff" % (*col,) def rgb_to_hex(color): """Convert an rgb color to hex.""" - return f"#{color[0]:02x}{color[1]:02x}{color[2]:02x}" + return "#%02x%02x%02x" % (*color,) def darken_color(color, amount): diff --git a/pywal/wallpaper.py b/pywal/wallpaper.py index f1ce1dd..79234be 100644 --- a/pywal/wallpaper.py +++ b/pywal/wallpaper.py @@ -84,7 +84,7 @@ def set_desktop_wallpaper(desktop, img): def set_mac_wallpaper(img): """Set the wallpaper on macOS.""" db_file = HOME / "Library/Application Support/Dock/desktoppicture.db" - subprocess.call(["sqlite3", db_file, f"update data set value = '{img}'"]) + subprocess.call(["sqlite3", db_file, "update data set value = '%s'" % img]) # Kill the dock to fix issues with cached wallpapers. # macOS caches wallpapers and if a wallpaper is set that shares diff --git a/tests/test_export.py b/tests/test_export.py index 2323c69..e2838fe 100755 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -43,7 +43,7 @@ class TestExportColors(unittest.TestCase): def test_invalid_template(self): """> Test template validation.""" - error_msg = "[!] warning: template 'dummy' doesn't exist." + error_msg = "warning: template 'dummy' doesn't exist." # Since this function prints a message on fail we redirect # it's output so that we can read it. diff --git a/tests/test_main.py b/tests/test_main.py index 9262c8f..7f1a1bb 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -2,6 +2,8 @@ import unittest from unittest.mock import MagicMock +import os + from pywal import __main__ from pywal import reload from pywal.settings import CACHE_DIR @@ -14,7 +16,7 @@ class TestMain(unittest.TestCase): """> Test arg parsing (-c)""" args = __main__.get_args(["-c"]) __main__.process_args(args) - self.assertFalse((CACHE_DIR / "schemes").is_dir()) + self.assertFalse(os.path.isdir(str(CACHE_DIR / "schemes"))) def test_args_e(self): """> Test arg parsing (-e)""" diff --git a/tests/test_sequences.py b/tests/test_sequences.py index 66f4084..392abc7 100755 --- a/tests/test_sequences.py +++ b/tests/test_sequences.py @@ -32,7 +32,8 @@ class Testsequences(unittest.TestCase): def test_set_iterm_tab_color(self): """> Create iterm tab color sequences""" result = sequences.set_iterm_tab_color(COLORS["special"]["background"]) - self.assertEqual(len(result), 3) + print(result) + self.assertEqual(len(result), 104) if __name__ == "__main__": diff --git a/tests/test_util.py b/tests/test_util.py index bc551ef..aae0356 100755 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -49,10 +49,9 @@ class TestUtil(unittest.TestCase): def test_create_dir(self): """> Create a directory.""" - tmp_dir = pathlib.Path("/tmp/test_dir") + tmp_dir = "/tmp/test_dir" util.create_dir(tmp_dir) - result = tmp_dir.is_dir() - self.assertTrue(result) + self.assertTrue(os.path.isdir(tmp_dir)) os.rmdir(tmp_dir) def test_hex_to_rgb_black(self): From 0b8e6749f3bb7d351f62ddd2c19f01118a4d3d5c Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Wed, 9 Aug 2017 11:26:57 +1000 Subject: [PATCH 02/19] util: Fix error on 3.6 --- pywal/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pywal/util.py b/pywal/util.py index 411d459..707b3d2 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -59,7 +59,7 @@ def read_file_raw(input_file): def save_file(data, export_file): """Write data to a file.""" - create_dir(export_file.parent) + create_dir(os.path.dirname(str(export_file))) try: with open(str(export_file), "w") as file: @@ -70,7 +70,7 @@ def save_file(data, export_file): def save_file_json(data, export_file): """Write data to a json file.""" - create_dir(export_file.parent) + create_dir(os.path.dirname(str(export_file))) with open(str(export_file), "w") as file: json.dump(data, file, indent=4) From c9f0ad292130e440faca7e83bf0adfef0362af9f Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 00:40:19 +1000 Subject: [PATCH 03/19] colors: Use an ordered dict to store the colors. --- pywal/colors.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/pywal/colors.py b/pywal/colors.py index 7a1a991..a501344 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -1,6 +1,7 @@ """ Generate a colorscheme using imagemagick. """ +import collections import re import shutil import subprocess @@ -64,19 +65,17 @@ def sort_colors(img, colors): # Create a comment color from the background. raw_colors[8] = util.lighten_color(raw_colors[0], 0.40) - colors = {"wallpaper": img} - colors_special = {} - colors_hex = {} + colors = {} + colors["wallpaper"] = img + colors["special"] = {} + colors["colors"] = collections.OrderedDict() - colors_special.update({"background": raw_colors[0]}) - colors_special.update({"foreground": raw_colors[15]}) - colors_special.update({"cursor": raw_colors[15]}) + colors["special"]["background"] = raw_colors[0] + colors["special"]["foreground"] = raw_colors[15] + colors["special"]["cursor"] = raw_colors[15] for index, color in enumerate(raw_colors): - colors_hex.update({"color%s" % index: color}) - - colors["special"] = colors_special - colors["colors"] = colors_hex + colors["colors"]["color%s" % index] = color return colors From db88dd2d25b0c851b43122c17eaceb0f5fe37706 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 00:41:02 +1000 Subject: [PATCH 04/19] travis: Test on Python 3.5 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 36c9a18..1c9b3d8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: python python: + - "3.5" - "3.6" before_install: From 80b19ccc43f9aed7b523cc3015a8c706580bf8e7 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 00:44:43 +1000 Subject: [PATCH 05/19] setup: Change message to 3.5 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index e8e4143..ba498a4 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ import setuptools try: import pywal except (ImportError, SyntaxError): - print("error: pywal requires Python 3.6 or greater.") + print("error: pywal requires Python 3.5 or greater.") quit(1) From c43b52b5d88d39ad9f88c035d5952e29e441175b Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 01:02:48 +1000 Subject: [PATCH 06/19] general: Remove pointless format --- pywal/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 412a6d4..6ad0555 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -84,7 +84,7 @@ def process_args(args): sys.exit(1) if args.v: - print("wal %s" % __version__) + print("wal", __version__) sys.exit(0) if args.q: From cbbb5a5b6a317dd2d8790afb8aa6b26d1d61a179 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 01:08:28 +1000 Subject: [PATCH 07/19] tests: Fix print --- tests/test_sequences.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_sequences.py b/tests/test_sequences.py index 392abc7..7a21c11 100755 --- a/tests/test_sequences.py +++ b/tests/test_sequences.py @@ -32,7 +32,6 @@ class Testsequences(unittest.TestCase): def test_set_iterm_tab_color(self): """> Create iterm tab color sequences""" result = sequences.set_iterm_tab_color(COLORS["special"]["background"]) - print(result) self.assertEqual(len(result), 104) From eb027f6e435d571d99352b6b642475c72690b50c Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 01:16:28 +1000 Subject: [PATCH 08/19] travis: Add even older python versions to test. --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 1c9b3d8..2bc2732 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: python python: + - "3.3" + - "3.4" - "3.5" - "3.6" From 979ec3ee1025e90c536ec013aa8b8fa29bed3a21 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 01:18:54 +1000 Subject: [PATCH 09/19] travis: nope --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2bc2732..1c9b3d8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,5 @@ language: python python: - - "3.3" - - "3.4" - "3.5" - "3.6" From 05c271c3a7faf0e31c28df4a7743c8ce4369c670 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 09:17:11 +1000 Subject: [PATCH 10/19] general: Remove all pathlib usage --- pywal/__main__.py | 3 ++- pywal/colors.py | 8 ++++---- pywal/export.py | 13 ++++++------- pywal/image.py | 15 ++++++--------- pywal/reload.py | 19 ++++++++++--------- pywal/sequences.py | 3 ++- pywal/settings.py | 8 ++++---- pywal/util.py | 3 +-- pywal/wallpaper.py | 4 ++-- tests/test_export.py | 27 ++++++++++++++------------- tests/test_main.py | 3 ++- 11 files changed, 53 insertions(+), 53 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 6ad0555..b062f09 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -91,7 +91,8 @@ def process_args(args): sys.stdout = sys.stderr = open(os.devnull, "w") if args.c: - shutil.rmtree(str(CACHE_DIR / "schemes"), ignore_errors=True) + scheme_dir = os.path.join(CACHE_DIR, "schemes") + shutil.rmtree(scheme_dir, ignore_errors=True) if args.r: reload.colors(args.t) diff --git a/pywal/colors.py b/pywal/colors.py index a501344..cb1df05 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -2,6 +2,7 @@ Generate a colorscheme using imagemagick. """ import collections +import os import re import shutil import subprocess @@ -84,11 +85,10 @@ 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") + cache_file = os.path.join(cache_dir, "schemes", + img.replace("/", "_"), ".json") - if cache_file.is_file(): + if os.path.isfile(cache_file): colors = util.read_file_json(cache_file) print("colors: Found cached colorscheme.") diff --git a/pywal/export.py b/pywal/export.py index d621672..d2db143 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -2,7 +2,6 @@ Export colors in various formats. """ import os -import pathlib from .settings import CACHE_DIR, MODULE_DIR from . import util @@ -43,10 +42,10 @@ def 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) + template_dir = os.path.join(MODULE_DIR, "templates") - for file in os.scandir(str(MODULE_DIR / "templates")): - template(all_colors, file.path, output_dir / file.name) + for file in os.scandir(template_dir): + template(all_colors, file.path, os.path.join(output_dir, file.name)) print("export: Exported all files.") @@ -56,10 +55,10 @@ def color(colors, export_type, output_file=None): all_colors = flatten_colors(colors) template_name = get_export_type(export_type) - template_file = MODULE_DIR / "templates" / template_name - output_file = output_file or CACHE_DIR / template_name + template_file = os.path.join(MODULE_DIR, "templates", template_name) + output_file = output_file or os.path.join(CACHE_DIR, template_name) - if template_file.is_file(): + if os.path.isfile(template_file): template(all_colors, template_file, output_file) print("export: Exported %s." % export_type) else: diff --git a/pywal/image.py b/pywal/image.py index e55a0b1..e01c3d1 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -2,7 +2,6 @@ Get the image file. """ import os -import pathlib import random import sys @@ -24,25 +23,23 @@ def get_random_image(img_dir): print("image: No new images found (nothing to do), exiting...") sys.exit(1) - return str(img_dir / random.choice(images).name) + return os.path.join(img_dir, random.choice(images).name) def get(img, cache_dir=CACHE_DIR): """Validate image input.""" - image = pathlib.Path(img) + if os.path.isfile(img): + wal_img = img - if image.is_file(): - wal_img = str(image) - - elif image.is_dir(): - wal_img = get_random_image(image) + elif os.path.isdir(img): + wal_img = get_random_image(img) else: print("error: No valid image file found.") sys.exit(1) # Cache the image file path. - util.save_file(wal_img, cache_dir / "wal") + util.save_file(wal_img, os.path.join(cache_dir, "wal")) print("image: Using image", wal_img) return wal_img diff --git a/pywal/reload.py b/pywal/reload.py index 11d048f..76c072c 100644 --- a/pywal/reload.py +++ b/pywal/reload.py @@ -1,6 +1,7 @@ """ Reload programs. """ +import os import re import shutil import subprocess @@ -12,28 +13,28 @@ from . import util 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" + xrdb_file = xrdb_file or os.path.join(CACHE_DIR, "colors.Xresources") if shutil.which("xrdb"): - subprocess.Popen(["xrdb", "-merge", str(xrdb_file)], + subprocess.Popen(["xrdb", "-merge", xrdb_file], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).wait() def gtk(): """Move gtkrc files to the correct location.""" - theme_path = HOME / ".themes" / "Flatabulous-wal" - gtk2_file = CACHE_DIR / "colors-gtk2.rc" + theme_path = os.path.join(HOME, ".themes", "Flatabulous-wal") + gtk2_file = os.path.join(CACHE_DIR, "colors-gtk2.rc") - if theme_path.is_dir(): - if gtk2_file.is_file(): - shutil.copy(str(gtk2_file), str(theme_path / "gtk-2.0")) + if os.path.isdir(theme_path): + shutil.copy(gtk2_file, os.path.join(theme_path, "gtk-2.0")) # Here we call a Python 2 script to reload the GTK themes. # This is done because the Python 3 GTK/Gdk libraries don't # provide a way of doing this. if shutil.which("python2"): - util.disown(["python2", str(MODULE_DIR / "scripts" / "gtk_reload.py")]) + gtk_reload = os.path.join(MODULE_DIR, "scripts", "gtk_reload.py") + util.disown(["python2", gtk_reload]) else: print("warning: GTK2 reload support requires Python 2.") @@ -62,7 +63,7 @@ def env(xrdb_file=None): def colors(vte, cache_dir=CACHE_DIR): """Reload the current scheme.""" - sequence_file = cache_dir / "sequences" + sequence_file = os.path.join(cache_dir, "sequences") if sequence_file.is_file(): sequences = "".join(util.read_file(sequence_file)) diff --git a/pywal/sequences.py b/pywal/sequences.py index 87339d9..4844837 100644 --- a/pywal/sequences.py +++ b/pywal/sequences.py @@ -2,6 +2,7 @@ Send sequences to all open terminals. """ import glob +import os from .settings import CACHE_DIR, OS from . import util @@ -81,5 +82,5 @@ def send(colors, vte, cache_dir=CACHE_DIR): for term in glob.glob(tty_pattern): util.save_file(sequences, term) - util.save_file(sequences, cache_dir / "sequences") + util.save_file(sequences, os.path.join(cache_dir, "sequences")) print("colors: Set terminal colors.") diff --git a/pywal/settings.py b/pywal/settings.py index c500243..c2cd7bd 100644 --- a/pywal/settings.py +++ b/pywal/settings.py @@ -9,15 +9,15 @@ Created by Dylan Araps. """ -import pathlib +import os import platform __version__ = "0.5.12" -HOME = pathlib.Path.home() -CACHE_DIR = HOME / ".cache/wal/" -MODULE_DIR = pathlib.Path(__file__).parent +HOME = os.environ["HOME"] +CACHE_DIR = os.path.join(HOME, ".cache/wal/") +MODULE_DIR = os.path.dirname(__file__) COLOR_COUNT = 16 OS = platform.uname()[0] diff --git a/pywal/util.py b/pywal/util.py index 707b3d2..832ac5f 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -3,7 +3,6 @@ Misc helper functions. """ import json import os -import pathlib import subprocess @@ -78,7 +77,7 @@ def save_file_json(data, export_file): def create_dir(directory): """Alias to create the cache dir.""" - pathlib.Path(directory).mkdir(parents=True, exist_ok=True) + os.makedirs(directory, exist_ok=True) def hex_to_rgb(color): diff --git a/pywal/wallpaper.py b/pywal/wallpaper.py index 79234be..7f09684 100644 --- a/pywal/wallpaper.py +++ b/pywal/wallpaper.py @@ -114,9 +114,9 @@ def change(img): def get(cache_dir=CACHE_DIR): """Get the current wallpaper.""" - current_wall = cache_dir / "wal" + current_wall = os.path.join(cache_dir, "wal") - if current_wall.is_file(): + if os.path.isfile(current_wall): return util.read_file(current_wall)[0] return "None" diff --git a/tests/test_export.py b/tests/test_export.py index e2838fe..7cb0bb4 100755 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -2,7 +2,7 @@ import unittest import unittest.mock import io -import pathlib +import os from pywal import export from pywal import util @@ -11,7 +11,6 @@ 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") @@ -21,25 +20,27 @@ class TestExportColors(unittest.TestCase): def test_all_templates(self): """> Test substitutions in template file.""" - export.every(COLORS, OUTPUT_DIR) + export.every(COLORS, "/tmp/wal") - result = pathlib.Path("/tmp/wal/colors.sh").is_file() + result = os.path.isfile("/tmp/wal/colors.sh") self.assertTrue(result) - content = pathlib.Path("/tmp/wal/colors.sh").read_text() - content = content.split("\n")[6] - self.assertEqual(content, "foreground='#F5F1F4'") + with open("/tmp/wal/colors.sh") as file: + content = file.read().splitlines() + + self.assertEqual(content[6], "foreground='#F5F1F4'") def test_css_template(self): """> Test substitutions in template file (css).""" - export.color(COLORS, "css", OUTPUT_DIR / "test.css") + export.color(COLORS, "css", "/tmp/wal/test.css") - result = pathlib.Path("/tmp/wal/test.css").is_file() + result = os.path.isfile("/tmp/wal/test.css") self.assertTrue(result) - content = pathlib.Path("/tmp/wal/test.css").read_text() - content = content.split("\n")[6] - self.assertEqual(content, " --background: #1F211E;") + with open("/tmp/wal/test.css") as file: + content = file.read().splitlines() + + self.assertEqual(content[6], " --background: #1F211E;") def test_invalid_template(self): """> Test template validation.""" @@ -48,7 +49,7 @@ class TestExportColors(unittest.TestCase): # Since this function prints a message on fail we redirect # it's output so that we can read it. with unittest.mock.patch('sys.stdout', new=io.StringIO()) as fake_out: - export.color(COLORS, "dummy", OUTPUT_DIR / "test.css") + export.color(COLORS, "dummy", "/tmp/wal/test.css") self.assertEqual(fake_out.getvalue().strip(), error_msg) diff --git a/tests/test_main.py b/tests/test_main.py index 7f1a1bb..daf9cab 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -16,7 +16,8 @@ class TestMain(unittest.TestCase): """> Test arg parsing (-c)""" args = __main__.get_args(["-c"]) __main__.process_args(args) - self.assertFalse(os.path.isdir(str(CACHE_DIR / "schemes"))) + scheme_dir = os.path.join(CACHE_DIR, "schemes") + self.assertFalse(os.path.isdir(scheme_dir)) def test_args_e(self): """> Test arg parsing (-e)""" From b1abb37f8c38e6bc6cbd89842717608e93694abf Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 09:17:47 +1000 Subject: [PATCH 11/19] general: Remove all pathlib usage --- pywal/reload.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pywal/reload.py b/pywal/reload.py index 76c072c..3f46004 100644 --- a/pywal/reload.py +++ b/pywal/reload.py @@ -65,7 +65,7 @@ def colors(vte, cache_dir=CACHE_DIR): """Reload the current scheme.""" sequence_file = os.path.join(cache_dir, "sequences") - if sequence_file.is_file(): + if os.path.isfile(sequence_file): sequences = "".join(util.read_file(sequence_file)) # If vte mode was used, remove the unsupported sequence. From c61960722d28373a4668814d996a248819e220c0 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 09:23:50 +1000 Subject: [PATCH 12/19] general: Remove all pathlib usage --- pywal/image.py | 2 +- pywal/util.py | 14 +++++++------- tests/test_util.py | 9 ++++----- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/pywal/image.py b/pywal/image.py index e01c3d1..bb495b1 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -16,7 +16,7 @@ def get_random_image(img_dir): current_wall = os.path.basename(current_wall) file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif") - images = [img for img in os.scandir(str(img_dir)) + images = [img for img in os.scandir(img_dir) if img.name.endswith(file_types) and img.name != current_wall] if not images: diff --git a/pywal/util.py b/pywal/util.py index 832ac5f..55be6c2 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -35,14 +35,14 @@ class Color: def read_file(input_file): """Read data from a file and trim newlines.""" - with open(str(input_file), "r") as file: + 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(str(input_file), "r") as json_file: + with open(input_file, "r") as json_file: data = json.load(json_file) return data @@ -51,17 +51,17 @@ def read_file_json(input_file): def read_file_raw(input_file): """Read data from a file as is, don't strip newlines or other special characters..""" - with open(str(input_file), "r") as file: + with open(input_file, "r") as file: data = file.readlines() return data def save_file(data, export_file): """Write data to a file.""" - create_dir(os.path.dirname(str(export_file))) + create_dir(os.path.dirname(export_file)) try: - with open(str(export_file), "w") as file: + with open(export_file, "w") as file: file.write(data) except PermissionError: print("warning: Couldn't write to %s." % export_file) @@ -69,9 +69,9 @@ def save_file(data, export_file): def save_file_json(data, export_file): """Write data to a json file.""" - create_dir(os.path.dirname(str(export_file))) + create_dir(os.path.dirname(export_file)) - with open(str(export_file), "w") as file: + with open(export_file, "w") as file: json.dump(data, file, indent=4) diff --git a/tests/test_util.py b/tests/test_util.py index aae0356..79b6b96 100755 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -1,7 +1,6 @@ """Test util functions.""" import unittest import os -import pathlib from pywal import util @@ -35,16 +34,16 @@ class TestUtil(unittest.TestCase): def test_save_file(self): """> Save colors to a file.""" - tmp_file = pathlib.Path("/tmp/test_file") + tmp_file = "/tmp/test_file" util.save_file("Hello, world", tmp_file) - result = tmp_file.is_file() + result = os.path.isfile(tmp_file) self.assertTrue(result) def test_save_file_json(self): """> Save colors to a file.""" - tmp_file = pathlib.Path("/tmp/test_file.json") + tmp_file = "/tmp/test_file.json" util.save_file_json(COLORS, tmp_file) - result = tmp_file.is_file() + result = os.path.isfile(tmp_file) self.assertTrue(result) def test_create_dir(self): From b142ea0814ea1846451769b05a061e988f57a769 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 09:35:33 +1000 Subject: [PATCH 13/19] general: Remove need for OrderedDict --- pywal/colors.py | 3 +-- pywal/sequences.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pywal/colors.py b/pywal/colors.py index cb1df05..e20c27d 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -1,7 +1,6 @@ """ Generate a colorscheme using imagemagick. """ -import collections import os import re import shutil @@ -69,7 +68,7 @@ def sort_colors(img, colors): colors = {} colors["wallpaper"] = img colors["special"] = {} - colors["colors"] = collections.OrderedDict() + colors["colors"] = {} colors["special"]["background"] = raw_colors[0] colors["special"]["foreground"] = raw_colors[15] diff --git a/pywal/sequences.py b/pywal/sequences.py index 4844837..67c3efd 100644 --- a/pywal/sequences.py +++ b/pywal/sequences.py @@ -42,8 +42,8 @@ def set_iterm_tab_color(color): def create_sequences(colors, vte): """Create the escape sequences.""" # Colors 0-15. - sequences = [set_color(num, col) for num, col in - enumerate(colors["colors"].values())] + sequences = [set_color(index, colors["colors"]["color%s" % index]) + for index in range(16)] # Set a blank color that isn't affected by bold highlighting. # Used in wal.vim's airline theme. From 9f2e80b376be2fe66d7962cd9b672fe81b4d892b Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 09:37:31 +1000 Subject: [PATCH 14/19] travis: Test 3.4 again --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 1c9b3d8..d4b63f7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: python python: + - "3.4" - "3.5" - "3.6" From 2782d97acf502c6e0aa62df66fc2b80db5ffeeb0 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 09:43:46 +1000 Subject: [PATCH 15/19] travis: 3.4 (almost there) --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d4b63f7..1c9b3d8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: python python: - - "3.4" - "3.5" - "3.6" From 236558365aae9bdfb5e8114de80ab1a8d0c3f668 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 09:52:28 +1000 Subject: [PATCH 16/19] travis: Test on 3.7 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 1c9b3d8..d132f1e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: python python: - "3.5" - "3.6" + - "nightly" before_install: - sudo apt-get -qq update From 61cb5724e0f18cef7a5693f498cd774f8447adff Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 09:57:41 +1000 Subject: [PATCH 17/19] travis: revert --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d132f1e..1c9b3d8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: python python: - "3.5" - "3.6" - - "nightly" before_install: - sudo apt-get -qq update From 02325547f4e82c7fec2404b024a57b3e7efc99f0 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 12 Aug 2017 12:03:18 +1000 Subject: [PATCH 18/19] colors: Fix cache file name. --- pywal/colors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pywal/colors.py b/pywal/colors.py index e20c27d..fd233c5 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -84,8 +84,8 @@ def get(img, cache_dir=CACHE_DIR, color_count=COLOR_COUNT, notify=False): """Get the colorscheme.""" # _home_dylan_img_jpg.json - cache_file = os.path.join(cache_dir, "schemes", - img.replace("/", "_"), ".json") + cache_file = img.replace("/", "_").replace(".", "_") + cache_file = os.path.join(cache_dir, "schemes", cache_file + ".json") if os.path.isfile(cache_file): colors = util.read_file_json(cache_file) From c743cab4f0b74e928496c2a5052906da52acc8f7 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sat, 12 Aug 2017 12:21:22 +1000 Subject: [PATCH 19/19] tests: Add a test for sequence order. --- tests/test_sequences.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_sequences.py b/tests/test_sequences.py index 7a21c11..8034da7 100755 --- a/tests/test_sequences.py +++ b/tests/test_sequences.py @@ -34,6 +34,13 @@ class Testsequences(unittest.TestCase): result = sequences.set_iterm_tab_color(COLORS["special"]["background"]) self.assertEqual(len(result), 104) + def test_sequence_order(self): + """> Test that the sequences are in order.""" + result = sequences.create_sequences(COLORS, vte=False).split("\007") + self.assertEqual(result[2], "\x1b]4;2;#CC6A93") + self.assertEqual(result[15], "\x1b]4;15;#F5F1F4") + self.assertEqual(result[8], "\x1b]4;8;#666666") + if __name__ == "__main__": unittest.main()