diff --git a/pywal/__main__.py b/pywal/__main__.py index 7ca31bd..44f5b55 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -17,6 +17,7 @@ import sys from .settings import __version__, CACHE_DIR from . import colors +from . import config from . import export from . import image from . import reload @@ -127,63 +128,117 @@ def parse_args_exit(parser): sys.exit(0) -def parse_args(parser): +def parse_args(parser, conf): """Process args.""" args = parser.parse_args() + if args.a: + conf["alpha"] = args.a + + if args.backend: + conf["backend"] = args.backend + + if args.b: + conf["background"] = "#%s" % (args.b.strip("#")) + + if args.c: + conf["cache"] = False + + if args.e: + conf["reload"] = False + + if args.g: + conf["oomox"] = True + + if args.i: + conf["image"] = args.i + + if args.theme: + conf["theme"] = args.theme + + if args.l: + conf["type"] = "light" if args.l else "dark" + + if args.n: + conf["wallpaper"] = False + + if args.o: + conf["cmd_hook"] = args.o + if args.q: + conf["quiet"] = True + + if args.s: + conf["sequences"] = False + + if args.t: + conf["tty"] = False + + if args.R: + conf["restore"] = True + + return conf + + +def wal(conf): + """Start the show.""" + if conf.get("quiet", False): logging.getLogger().disabled = True sys.stdout = sys.stderr = open(os.devnull, "w") - if args.c: + if not conf.get("cache", True): scheme_dir = os.path.join(CACHE_DIR, "schemes") shutil.rmtree(scheme_dir, ignore_errors=True) - if args.i: - image_file = image.get(args.i) - colors_plain = colors.get(image_file, args.l, args.backend) + if conf.get("image"): + image_file = image.get(conf.get("image")) + cols = colors.get(image_file, conf.get("type"), conf.get("backend")) - if args.theme: - colors_plain = theme.file(args.theme) + if conf.get("theme"): + cols = theme.file(conf.get("theme")) - if args.R: - colors_plain = theme.file(os.path.join(CACHE_DIR, "colors.json")) + if conf.get("restore"): + cols = theme.file(os.path.join(CACHE_DIR, "colors.json")) - if args.a: - util.Color.alpha_num = args.a + if conf.get("alpha"): + util.Color.alpha_num = conf.get("alpha", "100") - if args.b: - args.b = "#%s" % (args.b.strip("#")) - colors_plain["special"]["background"] = args.b - colors_plain["colors"]["color0"] = args.b + if conf.get("background"): + cols["special"]["background"] = conf.get("background") + cols["colors"]["color0"] = conf.get("background") - if not args.n: - wallpaper.change(colors_plain["wallpaper"]) + if conf.get("wallpaper"): + wallpaper.change(cols["wallpaper"]) - sequences.send(colors_plain, to_send=not args.s) + sequences.send(cols, to_send=conf.get("sequences")) if sys.stdout.isatty(): colors.palette() - export.every(colors_plain) + export.every(cols) - if not args.e: - reload.env(tty_reload=not args.t) + if conf.get("reload"): + reload.env(tty_reload=conf.get("tty")) - reload.external_script(args.o) + if conf.get("cmd_hook"): + util.disown(conf.get("cmd_hook")) - if not args.e: - reload.oomox(args.g) + if conf.get("reload"): + reload.oomox(conf.get("oomox")) reload.gtk() def main(): """Main script function.""" util.setup_logging() - parser = get_args() + parser = get_args() parse_args_exit(parser) - parse_args(parser) + + conf = config.load() + conf = parse_args(parser, conf) + + wal(conf) if __name__ == "__main__": diff --git a/pywal/colors.py b/pywal/colors.py index 600bc21..591e441 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -9,7 +9,7 @@ import sys from . import theme from . import util -from .settings import CACHE_DIR, MODULE_DIR, CONFIG, __cache_version__ +from .settings import CACHE_DIR, MODULE_DIR, __cache_version__ def list_backends(): @@ -84,11 +84,11 @@ def cache_fname(img, backend, light, cache_dir): def get_backend(backend): """Figure out which backend to use.""" - if CONFIG.get("colors", "backend") and backend == "default": - return CONFIG.get("colors", "backend") + # if CONFIG.get("colors", "backend") and backend == "default": + # return CONFIG.get("colors", "backend") - elif backend == "default": - return "wal" + # elif backend == "default": + # return "wal" if backend == "random": backends = list_backends() diff --git a/pywal/config.py b/pywal/config.py new file mode 100644 index 0000000..b147680 --- /dev/null +++ b/pywal/config.py @@ -0,0 +1,15 @@ +""" +Setup and handling of pywal config file. +""" +from . import util +from .settings import DEFAULT_CONF_FILE, CONF_FILE + + +def load(): + """Setup config file.""" + util.copy_file_if(DEFAULT_CONF_FILE, CONF_FILE) + + config = {} + exec(open(CONF_FILE).read(), config) + + return config diff --git a/pywal/config/config.conf b/pywal/config/config.conf new file mode 100644 index 0000000..d7f4bbf --- /dev/null +++ b/pywal/config/config.conf @@ -0,0 +1,45 @@ +# Alpha level (URxvt only) (0-100) +# Example: +# alpha = 50 +alpha = 100 + +# Background override. +# Example: +# background = "#000000" +background = "" + +# Reload environment. +# Example: +# reload = True +reload = True + +# Send sequences to terminals. +# Example: +# sequences = True +sequences = True + +# External command to run after pywal. +# Example: +# cmd_hook = ["wal-set", "-p"] +cmd_hook = [] + +# Color backend to use. +# Possible values, see: wal --backend +# Example: +# backend = "colorz" +backend = "wal" + +# Set the wallpaper. +# Example: +# wallpaper = True +wallpaper = True + +# Which wallpaper setter to use. +# Example: +# setter = ["feh", "--bg-fill"] +setter = [] + +# Show terminal output. +# Example: +# quiet = False +quiet = False diff --git a/pywal/config/config.ini b/pywal/config/config.ini deleted file mode 100644 index dcfc20f..0000000 --- a/pywal/config/config.ini +++ /dev/null @@ -1,13 +0,0 @@ -[general] -# External command to run after pywal. -cmd_hook = - -[colors] -# Color backend to use. -# Possible values, see: wal --backend -backend = - -[wallpaper] -# Which wallpaper setter to use. -# Example: feh --bg-fill -setter = diff --git a/pywal/reload.py b/pywal/reload.py index a4a087c..02d1073 100644 --- a/pywal/reload.py +++ b/pywal/reload.py @@ -7,7 +7,7 @@ import shutil import subprocess import sys -from .settings import CACHE_DIR, MODULE_DIR, OS, CONFIG +from .settings import CACHE_DIR, MODULE_DIR, OS from . import util @@ -88,15 +88,6 @@ def colors(cache_dir=CACHE_DIR): print("".join(util.read_file(sequences)), end="") -def external_script(cmd_hook): - """Launch an external process after pywal.""" - if CONFIG.get("general", "cmd_hook"): - util.disown(CONFIG.get("general", "cmd_hook").split()) - - if cmd_hook: - util.disown([cmd_hook]) - - def env(xrdb_file=None, tty_reload=True): """Reload environment.""" xrdb(xrdb_file) diff --git a/pywal/settings.py b/pywal/settings.py index b838525..c035644 100644 --- a/pywal/settings.py +++ b/pywal/settings.py @@ -9,13 +9,8 @@ Created by Dylan Araps. """ -import configparser import os import platform -import shutil - -from . import util - __version__ = "2.0.3" __cache_version__ = "1.1.0" @@ -25,14 +20,6 @@ HOME = os.getenv("HOME", os.getenv("USERPROFILE")) CACHE_DIR = os.path.join(HOME, ".cache", "wal") MODULE_DIR = os.path.dirname(__file__) CONF_DIR = os.path.join(HOME, ".config", "wal") -CONF_FILE = os.path.join(CONF_DIR, "config.ini") -DEFAULT_CONF_FILE = os.path.join(MODULE_DIR, "config", "config.ini") +CONF_FILE = os.path.join(CONF_DIR, "config.conf") +DEFAULT_CONF_FILE = os.path.join(MODULE_DIR, "config", "config.conf") OS = platform.uname()[0] - - -if not os.path.isfile(CONF_FILE): - util.create_dir(CONF_DIR) - shutil.copy2(DEFAULT_CONF_FILE, CONF_DIR) - -CONFIG = configparser.ConfigParser() -CONFIG.read(CONF_FILE) diff --git a/pywal/util.py b/pywal/util.py index 4495dbe..e53052a 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -5,6 +5,7 @@ import colorsys import json import logging import os +import shutil import subprocess import sys @@ -93,6 +94,13 @@ def create_dir(directory): os.makedirs(directory, exist_ok=True) +def copy_file_if(input_file, export_file): + """Copy file to dir if it doesn't exist.""" + if not os.path.isfile(export_file): + create_dir(os.path.dirname(export_file)) + shutil.copy2(input_file, export_file) + + def setup_logging(): """Logging config.""" logging.basicConfig(format=("[%(levelname)s\033[0m] " diff --git a/pywal/wallpaper.py b/pywal/wallpaper.py index b1a825f..52d83b4 100644 --- a/pywal/wallpaper.py +++ b/pywal/wallpaper.py @@ -6,7 +6,7 @@ import shutil import subprocess import urllib.parse -from .settings import CACHE_DIR, HOME, OS, CONFIG +from .settings import CACHE_DIR, HOME, OS from . import util @@ -125,9 +125,9 @@ def change(img): if not os.path.isfile(img): return - if CONFIG.get("wallpaper", "setter"): - util.disown([*CONFIG.get("wallpaper", "setter").split(), img]) - return + # if CONFIG.get("wallpaper", "setter"): + # util.disown([*CONFIG.get("wallpaper", "setter").split(), img]) + # return desktop = get_desktop_env()