add '--config-open' and '--config-status' (#5713)

adapted from 119a25a59c
pull/5748/head
Mike Fährmann 3 months ago
parent a92c0bef96
commit 3fc2e61818
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -85,6 +85,8 @@
--config-yaml FILE Additional configuration files in YAML format
--config-toml FILE Additional configuration files in TOML format
--config-create Create a basic configuration file
--config-status Show configuration file status
--config-open Open configuration file in external application
--config-ignore Do not read default configuration files
## Authentication Options:

@ -220,8 +220,13 @@ def main():
cnt, "entry" if cnt == 1 else "entries", cache._path(),
)
elif args.config_init:
return config.initialize()
elif args.config:
if args.config == "init":
return config.initialize()
elif args.config == "status":
return config.status()
else:
return config.open_extern()
else:
if not args.urls and not args.input_files:

@ -90,6 +90,78 @@ def initialize():
return 0
def open_extern():
for path in _default_configs:
path = util.expand_path(path)
if os.access(path, os.R_OK | os.W_OK):
break
else:
log.warning("Unable to find any writable configuration file")
return 1
if util.WINDOWS:
openers = ("explorer", "notepad")
else:
openers = ("xdg-open", "open")
editor = os.environ.get("EDITOR")
if editor:
openers = (editor,) + openers
import shutil
for opener in openers:
opener = shutil.which(opener)
if opener:
break
else:
log.warning("Unable to find a program to open '%s' with", path)
return 1
log.info("Running '%s %s'", opener, path)
retcode = util.Popen((opener, path)).wait()
if not retcode:
try:
with open(path, encoding="utf-8") as fp:
util.json_loads(fp.read())
except Exception as exc:
log.warning("%s when parsing '%s': %s",
exc.__class__.__name__, path, exc)
return 2
return retcode
def status():
from .output import stdout_write
paths = []
for path in _default_configs:
path = util.expand_path(path)
try:
with open(path, encoding="utf-8") as fp:
util.json_loads(fp.read())
except FileNotFoundError:
status = "Not Present"
except OSError:
status = "Inaccessible"
except ValueError:
status = "Invalid JSON"
except Exception as exc:
log.debug(exc)
status = "Unknown"
else:
status = "OK"
paths.append((path, status))
fmt = "{{:<{}}} : {{}}\n".format(
max(len(p[0]) for p in paths)).format
for path, status in paths:
stdout_write(fmt(path, status))
def load(files=None, strict=False, loads=util.json_loads):
"""Load JSON configuration files"""
for pathfmt in files or _default_configs:

@ -461,9 +461,19 @@ def build_parser():
)
configuration.add_argument(
"--config-create",
dest="config_init", action="store_true",
dest="config", action="store_const", const="init",
help="Create a basic configuration file",
)
configuration.add_argument(
"--config-status",
dest="config", action="store_const", const="status",
help="Show configuration file status",
)
configuration.add_argument(
"--config-open",
dest="config", action="store_const", const="open",
help="Open configuration file in external application",
)
configuration.add_argument(
"--config-ignore",
dest="config_load", action="store_false",

Loading…
Cancel
Save