General: Unhardcode all CACHE_DIR and COLOR_COUNT usage.

This commit is contained in:
Dylan Araps 2017-07-20 23:19:13 +10:00
parent a3d4b3d9f3
commit 52cd5e5f1a
11 changed files with 57 additions and 70 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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"],

View File

@ -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.")

View File

@ -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.

View File

@ -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.")

View File

@ -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))

View File

@ -1,11 +0,0 @@
"""
Global Constants.
"""
import pathlib
__version__ = "0.4.0"
COLOR_COUNT = 16
CACHE_DIR = pathlib.Path.home() / ".cache/wal/"

View File

@ -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)

View File

@ -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)