From 05c271c3a7faf0e31c28df4a7743c8ce4369c670 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 10 Aug 2017 09:17:11 +1000 Subject: [PATCH] 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)"""