mirror of
https://github.com/dylanaraps/pywal.git
synced 2024-11-29 03:13:47 +01:00
api: Remove wal.py
This commit is contained in:
parent
a4ea6a4f7a
commit
de47c1beaa
@ -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__",
|
||||
|
@ -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)
|
||||
|
@ -5,7 +5,8 @@ import re
|
||||
import shutil
|
||||
import subprocess
|
||||
|
||||
from pywal import util
|
||||
from .settings import __cache_dir__, __color_count__
|
||||
from . import util
|
||||
|
||||
|
||||
def imagemagick(color_count, img):
|
||||
@ -47,29 +48,6 @@ def gen_colors(img, color_count):
|
||||
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."""
|
||||
@ -91,3 +69,27 @@ def sort_colors(img, colors):
|
||||
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
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
16
pywal/settings.py
Normal file
16
pywal/settings.py
Normal file
@ -0,0 +1,16 @@
|
||||
"""
|
||||
'||
|
||||
... ... .... ... ... ... ... .... ||
|
||||
||' || '|. | || || | '' .|| ||
|
||||
|| | '|.| ||| ||| .|' || ||
|
||||
||...' '| | | '|..'|' .||.
|
||||
|| .. |
|
||||
'''' ''
|
||||
Created by Dylan Araps.
|
||||
"""
|
||||
|
||||
import pathlib
|
||||
|
||||
__version__ = "0.4.0"
|
||||
__cache_dir__ = pathlib.Path.home() / ".cache/wal/"
|
||||
__color_count__ = 16
|
@ -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")
|
||||
|
55
pywal/wal.py
55
pywal/wal.py
@ -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)
|
@ -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
|
||||
|
@ -1,7 +1,7 @@
|
||||
"""Test imagemagick functions."""
|
||||
import unittest
|
||||
|
||||
from pywal import wal
|
||||
from pywal import colors
|
||||
|
||||
|
||||
class TestGenColors(unittest.TestCase):
|
||||
@ -9,7 +9,7 @@ class TestGenColors(unittest.TestCase):
|
||||
|
||||
def test_gen_colors(self):
|
||||
"""> Generate a colorscheme."""
|
||||
result = wal.create_palette("tests/test_files/test.jpg")
|
||||
result = colors.get("tests/test_files/test.jpg")
|
||||
self.assertEqual(result["colors"]["color0"], "#0F191A")
|
||||
|
||||
|
@ -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")
|
||||
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user