mirror of
https://github.com/dylanaraps/pywal.git
synced 2025-06-25 12:01:48 +02:00
config: Proper config handling with args
This commit is contained in:
parent
33b063cdc7
commit
658a2b26a3
@ -17,6 +17,7 @@ import sys
|
|||||||
|
|
||||||
from .settings import __version__, CACHE_DIR
|
from .settings import __version__, CACHE_DIR
|
||||||
from . import colors
|
from . import colors
|
||||||
|
from . import config
|
||||||
from . import export
|
from . import export
|
||||||
from . import image
|
from . import image
|
||||||
from . import reload
|
from . import reload
|
||||||
@ -127,63 +128,117 @@ def parse_args_exit(parser):
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
def parse_args(parser):
|
def parse_args(parser, conf):
|
||||||
"""Process args."""
|
"""Process args."""
|
||||||
args = parser.parse_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:
|
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
|
logging.getLogger().disabled = True
|
||||||
sys.stdout = sys.stderr = open(os.devnull, "w")
|
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")
|
scheme_dir = os.path.join(CACHE_DIR, "schemes")
|
||||||
shutil.rmtree(scheme_dir, ignore_errors=True)
|
shutil.rmtree(scheme_dir, ignore_errors=True)
|
||||||
|
|
||||||
if args.i:
|
if conf.get("image"):
|
||||||
image_file = image.get(args.i)
|
image_file = image.get(conf.get("image"))
|
||||||
colors_plain = colors.get(image_file, args.l, args.backend)
|
cols = colors.get(image_file, conf.get("type"), conf.get("backend"))
|
||||||
|
|
||||||
if args.theme:
|
if conf.get("theme"):
|
||||||
colors_plain = theme.file(args.theme)
|
cols = theme.file(conf.get("theme"))
|
||||||
|
|
||||||
if args.R:
|
if conf.get("restore"):
|
||||||
colors_plain = theme.file(os.path.join(CACHE_DIR, "colors.json"))
|
cols = theme.file(os.path.join(CACHE_DIR, "colors.json"))
|
||||||
|
|
||||||
if args.a:
|
if conf.get("alpha"):
|
||||||
util.Color.alpha_num = args.a
|
util.Color.alpha_num = conf.get("alpha", "100")
|
||||||
|
|
||||||
if args.b:
|
if conf.get("background"):
|
||||||
args.b = "#%s" % (args.b.strip("#"))
|
cols["special"]["background"] = conf.get("background")
|
||||||
colors_plain["special"]["background"] = args.b
|
cols["colors"]["color0"] = conf.get("background")
|
||||||
colors_plain["colors"]["color0"] = args.b
|
|
||||||
|
|
||||||
if not args.n:
|
if conf.get("wallpaper"):
|
||||||
wallpaper.change(colors_plain["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():
|
if sys.stdout.isatty():
|
||||||
colors.palette()
|
colors.palette()
|
||||||
|
|
||||||
export.every(colors_plain)
|
export.every(cols)
|
||||||
|
|
||||||
if not args.e:
|
if conf.get("reload"):
|
||||||
reload.env(tty_reload=not args.t)
|
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:
|
if conf.get("reload"):
|
||||||
reload.oomox(args.g)
|
reload.oomox(conf.get("oomox"))
|
||||||
reload.gtk()
|
reload.gtk()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main script function."""
|
"""Main script function."""
|
||||||
util.setup_logging()
|
util.setup_logging()
|
||||||
parser = get_args()
|
|
||||||
|
|
||||||
|
parser = get_args()
|
||||||
parse_args_exit(parser)
|
parse_args_exit(parser)
|
||||||
parse_args(parser)
|
|
||||||
|
conf = config.load()
|
||||||
|
conf = parse_args(parser, conf)
|
||||||
|
|
||||||
|
wal(conf)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -9,7 +9,7 @@ import sys
|
|||||||
|
|
||||||
from . import theme
|
from . import theme
|
||||||
from . import util
|
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():
|
def list_backends():
|
||||||
@ -84,11 +84,11 @@ def cache_fname(img, backend, light, cache_dir):
|
|||||||
|
|
||||||
def get_backend(backend):
|
def get_backend(backend):
|
||||||
"""Figure out which backend to use."""
|
"""Figure out which backend to use."""
|
||||||
if CONFIG.get("colors", "backend") and backend == "default":
|
# if CONFIG.get("colors", "backend") and backend == "default":
|
||||||
return CONFIG.get("colors", "backend")
|
# return CONFIG.get("colors", "backend")
|
||||||
|
|
||||||
elif backend == "default":
|
# elif backend == "default":
|
||||||
return "wal"
|
# return "wal"
|
||||||
|
|
||||||
if backend == "random":
|
if backend == "random":
|
||||||
backends = list_backends()
|
backends = list_backends()
|
||||||
|
15
pywal/config.py
Normal file
15
pywal/config.py
Normal file
@ -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
|
45
pywal/config/config.conf
Normal file
45
pywal/config/config.conf
Normal file
@ -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
|
@ -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 =
|
|
@ -7,7 +7,7 @@ import shutil
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from .settings import CACHE_DIR, MODULE_DIR, OS, CONFIG
|
from .settings import CACHE_DIR, MODULE_DIR, OS
|
||||||
from . import util
|
from . import util
|
||||||
|
|
||||||
|
|
||||||
@ -88,15 +88,6 @@ def colors(cache_dir=CACHE_DIR):
|
|||||||
print("".join(util.read_file(sequences)), end="")
|
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):
|
def env(xrdb_file=None, tty_reload=True):
|
||||||
"""Reload environment."""
|
"""Reload environment."""
|
||||||
xrdb(xrdb_file)
|
xrdb(xrdb_file)
|
||||||
|
@ -9,13 +9,8 @@
|
|||||||
Created by Dylan Araps.
|
Created by Dylan Araps.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import configparser
|
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
import shutil
|
|
||||||
|
|
||||||
from . import util
|
|
||||||
|
|
||||||
|
|
||||||
__version__ = "2.0.3"
|
__version__ = "2.0.3"
|
||||||
__cache_version__ = "1.1.0"
|
__cache_version__ = "1.1.0"
|
||||||
@ -25,14 +20,6 @@ HOME = os.getenv("HOME", os.getenv("USERPROFILE"))
|
|||||||
CACHE_DIR = os.path.join(HOME, ".cache", "wal")
|
CACHE_DIR = os.path.join(HOME, ".cache", "wal")
|
||||||
MODULE_DIR = os.path.dirname(__file__)
|
MODULE_DIR = os.path.dirname(__file__)
|
||||||
CONF_DIR = os.path.join(HOME, ".config", "wal")
|
CONF_DIR = os.path.join(HOME, ".config", "wal")
|
||||||
CONF_FILE = os.path.join(CONF_DIR, "config.ini")
|
CONF_FILE = os.path.join(CONF_DIR, "config.conf")
|
||||||
DEFAULT_CONF_FILE = os.path.join(MODULE_DIR, "config", "config.ini")
|
DEFAULT_CONF_FILE = os.path.join(MODULE_DIR, "config", "config.conf")
|
||||||
OS = platform.uname()[0]
|
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)
|
|
||||||
|
@ -5,6 +5,7 @@ import colorsys
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@ -93,6 +94,13 @@ def create_dir(directory):
|
|||||||
os.makedirs(directory, exist_ok=True)
|
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():
|
def setup_logging():
|
||||||
"""Logging config."""
|
"""Logging config."""
|
||||||
logging.basicConfig(format=("[%(levelname)s\033[0m] "
|
logging.basicConfig(format=("[%(levelname)s\033[0m] "
|
||||||
|
@ -6,7 +6,7 @@ import shutil
|
|||||||
import subprocess
|
import subprocess
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
from .settings import CACHE_DIR, HOME, OS, CONFIG
|
from .settings import CACHE_DIR, HOME, OS
|
||||||
from . import util
|
from . import util
|
||||||
|
|
||||||
|
|
||||||
@ -125,9 +125,9 @@ def change(img):
|
|||||||
if not os.path.isfile(img):
|
if not os.path.isfile(img):
|
||||||
return
|
return
|
||||||
|
|
||||||
if CONFIG.get("wallpaper", "setter"):
|
# if CONFIG.get("wallpaper", "setter"):
|
||||||
util.disown([*CONFIG.get("wallpaper", "setter").split(), img])
|
# util.disown([*CONFIG.get("wallpaper", "setter").split(), img])
|
||||||
return
|
# return
|
||||||
|
|
||||||
desktop = get_desktop_env()
|
desktop = get_desktop_env()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user