summaryrefslogtreecommitdiff
path: root/pywal
diff options
context:
space:
mode:
authorDylan Araps <dylan.araps@gmail.com>2017-07-20 23:19:13 +1000
committerDylan Araps <dylan.araps@gmail.com>2017-07-20 23:19:13 +1000
commit52cd5e5f1a7c972a593fe55a853afd43111531e9 (patch)
tree86a972bee26b93db9f8f8bd3fd5c8d05553ec4c0 /pywal
parenta3d4b3d9f348fd308ad8a55f5ef71263e11b6875 (diff)
General: Unhardcode all CACHE_DIR and COLOR_COUNT usage.
Diffstat (limited to 'pywal')
-rw-r--r--pywal/__init__.py2
-rw-r--r--pywal/__main__.py11
-rw-r--r--pywal/export.py7
-rw-r--r--pywal/image.py9
-rw-r--r--pywal/magic.py22
-rw-r--r--pywal/reload.py9
-rw-r--r--pywal/sequences.py10
-rw-r--r--pywal/settings.py11
-rw-r--r--pywal/util.py5
-rw-r--r--pywal/wal.py34
10 files changed, 53 insertions, 67 deletions
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)