config: Proper config handling with args

This commit is contained in:
Dylan Araps 2018-04-03 08:56:33 +10:00
parent 33b063cdc7
commit 658a2b26a3
9 changed files with 161 additions and 73 deletions

View File

@ -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__":

View File

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

15
pywal/config.py Normal file
View 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
View 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

View File

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

View File

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

View File

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

View File

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

View File

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