1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-23 03:02:50 +01:00
gallery-dl/gallery_dl/config.py

324 lines
8.0 KiB
Python
Raw Normal View History

2015-10-03 20:23:55 +02:00
# -*- coding: utf-8 -*-
# Copyright 2015-2023 Mike Fährmann
2015-10-03 20:23:55 +02:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Global configuration module"""
import sys
import os.path
2017-03-08 16:57:42 +01:00
import logging
from . import util
2015-10-03 20:23:55 +02:00
2017-03-11 01:47:57 +01:00
log = logging.getLogger("config")
2017-01-30 19:40:15 +01:00
# --------------------------------------------------------------------
# internals
_config = {}
_files = []
2020-05-19 21:42:11 +02:00
if util.WINDOWS:
_default_configs = [
r"%APPDATA%\gallery-dl\config.json",
r"%USERPROFILE%\gallery-dl\config.json",
r"%USERPROFILE%\gallery-dl.conf",
]
else:
_default_configs = [
"/etc/gallery-dl.conf",
"${XDG_CONFIG_HOME}/gallery-dl/config.json"
if os.environ.get("XDG_CONFIG_HOME") else
"${HOME}/.config/gallery-dl/config.json",
"${HOME}/.gallery-dl.conf",
]
2023-02-28 23:10:23 +01:00
if util.EXECUTABLE:
# look for config file in PyInstaller executable directory (#682)
_default_configs.append(os.path.join(
os.path.dirname(sys.executable),
"gallery-dl.conf",
))
2015-10-03 20:23:55 +02:00
# --------------------------------------------------------------------
# public interface
def initialize():
paths = list(map(util.expand_path, _default_configs))
for path in paths:
if os.access(path, os.R_OK | os.W_OK):
log.error("There is already a configuration file at '%s'", path)
return 1
for path in paths:
try:
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "x", encoding="utf-8") as fp:
fp.write("""\
{
"extractor": {
},
"downloader": {
},
"output": {
},
"postprocessor": {
}
}
""")
break
except OSError as exc:
log.debug("%s: %s", exc.__class__.__name__, exc)
else:
log.error("Unable to create a new configuration file "
"at any of the default paths")
return 1
log.info("Created a basic configuration file at '%s'", path)
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))
2023-08-21 21:18:40 +02:00
def load(files=None, strict=False, loads=util.json_loads):
2015-10-03 20:23:55 +02:00
"""Load JSON configuration files"""
for pathfmt in files or _default_configs:
path = util.expand_path(pathfmt)
2015-10-03 20:23:55 +02:00
try:
with open(path, encoding="utf-8") as fp:
conf = loads(fp.read())
except OSError as exc:
2015-11-14 17:22:56 +01:00
if strict:
2019-12-10 21:30:08 +01:00
log.error(exc)
2023-08-21 23:46:39 +02:00
raise SystemExit(1)
2017-09-30 18:52:23 +02:00
except Exception as exc:
log.error("%s when loading '%s': %s",
exc.__class__.__name__, path, exc)
2017-03-08 16:57:42 +01:00
if strict:
2023-08-21 23:46:39 +02:00
raise SystemExit(2)
else:
if not _config:
_config.update(conf)
else:
util.combine_dict(_config, conf)
_files.append(pathfmt)
2015-10-03 20:23:55 +02:00
2023-08-21 21:18:40 +02:00
if "subconfigs" in conf:
subconfigs = conf["subconfigs"]
if subconfigs:
if isinstance(subconfigs, str):
subconfigs = (subconfigs,)
load(subconfigs, strict, loads)
2017-01-30 19:40:15 +01:00
2015-10-03 20:23:55 +02:00
def clear():
"""Reset configuration to an empty state"""
_config.clear()
2015-10-03 20:23:55 +02:00
2017-01-30 19:40:15 +01:00
def get(path, key, default=None, conf=_config):
"""Get the value of property 'key' or a default value"""
2015-10-03 20:23:55 +02:00
try:
2019-11-23 23:50:16 +01:00
for p in path:
conf = conf[p]
return conf[key]
2019-11-25 17:19:14 +01:00
except Exception:
2015-10-03 20:23:55 +02:00
return default
2017-01-30 19:40:15 +01:00
def interpolate(path, key, default=None, conf=_config):
2015-10-03 20:23:55 +02:00
"""Interpolate the value of 'key'"""
2019-11-23 23:50:16 +01:00
if key in conf:
return conf[key]
2015-10-03 20:23:55 +02:00
try:
2019-11-23 23:50:16 +01:00
for p in path:
conf = conf[p]
if key in conf:
default = conf[key]
2019-11-25 17:19:14 +01:00
except Exception:
2019-11-23 23:50:16 +01:00
pass
return default
2015-10-03 20:23:55 +02:00
2017-01-30 19:40:15 +01:00
def interpolate_common(common, paths, key, default=None, conf=_config):
"""Interpolate the value of 'key'
using multiple 'paths' along a 'common' ancestor
"""
if key in conf:
return conf[key]
# follow the common path
try:
for p in common:
conf = conf[p]
if key in conf:
default = conf[key]
except Exception:
return default
# try all paths until a value is found
value = util.SENTINEL
for path in paths:
c = conf
try:
for p in path:
c = c[p]
if key in c:
value = c[key]
except Exception:
pass
if value is not util.SENTINEL:
return value
return default
def accumulate(path, key, conf=_config):
2020-09-14 21:13:08 +02:00
"""Accumulate the values of 'key' along 'path'"""
result = []
try:
if key in conf:
value = conf[key]
if value:
result.extend(value)
for p in path:
conf = conf[p]
if key in conf:
value = conf[key]
if value:
result[:0] = value
except Exception:
pass
return result
def set(path, key, value, conf=_config):
2015-10-03 20:23:55 +02:00
"""Set the value of property 'key' for this session"""
2019-11-23 23:50:16 +01:00
for p in path:
2015-10-03 20:23:55 +02:00
try:
2019-11-23 23:50:16 +01:00
conf = conf[p]
2015-10-03 20:23:55 +02:00
except KeyError:
2019-11-23 23:50:16 +01:00
conf[p] = conf = {}
conf[key] = value
2015-10-03 20:23:55 +02:00
2017-01-30 19:40:15 +01:00
def setdefault(path, key, value, conf=_config):
2015-10-07 00:58:43 +02:00
"""Set the value of property 'key' if it doesn't exist"""
2019-11-23 23:50:16 +01:00
for p in path:
2015-10-07 00:58:43 +02:00
try:
2019-11-23 23:50:16 +01:00
conf = conf[p]
2015-10-07 00:58:43 +02:00
except KeyError:
2019-11-23 23:50:16 +01:00
conf[p] = conf = {}
return conf.setdefault(key, value)
def unset(path, key, conf=_config):
"""Unset the value of property 'key'"""
try:
2019-11-23 23:50:16 +01:00
for p in path:
conf = conf[p]
del conf[key]
2019-11-25 17:19:14 +01:00
except Exception:
pass
class apply():
"""Context Manager: apply a collection of key-value pairs"""
def __init__(self, kvlist):
self.original = []
self.kvlist = kvlist
def __enter__(self):
2019-11-23 23:50:16 +01:00
for path, key, value in self.kvlist:
2020-05-19 21:47:18 +02:00
self.original.append((path, key, get(path, key, util.SENTINEL)))
2019-11-23 23:50:16 +01:00
set(path, key, value)
def __exit__(self, etype, value, traceback):
2019-11-23 23:50:16 +01:00
for path, key, value in self.original:
2020-05-19 21:47:18 +02:00
if value is util.SENTINEL:
2019-11-23 23:50:16 +01:00
unset(path, key)
else:
2019-11-23 23:50:16 +01:00
set(path, key, value)