2015-10-03 20:23:55 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2023-02-07 23:14:53 +01:00
|
|
|
# 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
|
2017-08-12 20:07:27 +02:00
|
|
|
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
|
|
|
|
2017-03-27 11:59:27 +02:00
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# internals
|
|
|
|
|
|
|
|
_config = {}
|
2022-11-18 17:15:32 +01:00
|
|
|
_files = []
|
2017-03-27 11:59:27 +02:00
|
|
|
|
2020-05-19 21:42:11 +02:00
|
|
|
if util.WINDOWS:
|
2017-03-27 11:59:27 +02:00
|
|
|
_default_configs = [
|
2020-05-28 02:56:38 +02:00
|
|
|
r"%APPDATA%\gallery-dl\config.json",
|
2017-03-27 11:59:27 +02:00
|
|
|
r"%USERPROFILE%\gallery-dl\config.json",
|
|
|
|
r"%USERPROFILE%\gallery-dl.conf",
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
_default_configs = [
|
|
|
|
"/etc/gallery-dl.conf",
|
2021-05-16 01:12:37 +02:00
|
|
|
"${XDG_CONFIG_HOME}/gallery-dl/config.json"
|
|
|
|
if os.environ.get("XDG_CONFIG_HOME") else
|
2017-03-27 11:59:27 +02:00
|
|
|
"${HOME}/.config/gallery-dl/config.json",
|
|
|
|
"${HOME}/.gallery-dl.conf",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-02-28 23:10:23 +01:00
|
|
|
if util.EXECUTABLE:
|
2020-04-08 21:53:50 +02:00
|
|
|
# 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
|
|
|
|
|
2023-03-01 14:49:40 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2024-06-13 23:08:11 +02:00
|
|
|
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"""
|
2022-11-18 17:15:32 +01:00
|
|
|
for pathfmt in files or _default_configs:
|
|
|
|
path = util.expand_path(pathfmt)
|
2015-10-03 20:23:55 +02:00
|
|
|
try:
|
2024-06-14 01:22:00 +02:00
|
|
|
with open(path, encoding="utf-8") as fp:
|
|
|
|
conf = loads(fp.read())
|
2019-02-27 16:52:15 +01:00
|
|
|
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:
|
2023-05-02 17:49:02 +02:00
|
|
|
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)
|
2019-02-27 16:52:15 +01:00
|
|
|
else:
|
|
|
|
if not _config:
|
2023-02-07 23:14:53 +01:00
|
|
|
_config.update(conf)
|
2019-02-27 16:52:15 +01:00
|
|
|
else:
|
2023-02-07 23:14:53 +01:00
|
|
|
util.combine_dict(_config, conf)
|
2022-11-18 17:15:32 +01:00
|
|
|
_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():
|
2018-02-07 21:47:27 +01:00
|
|
|
"""Reset configuration to an empty state"""
|
|
|
|
_config.clear()
|
2015-10-03 20:23:55 +02:00
|
|
|
|
2017-01-30 19:40:15 +01:00
|
|
|
|
2023-05-02 22:16:58 +02:00
|
|
|
def get(path, key, default=None, conf=_config):
|
2017-03-27 11:59:27 +02:00
|
|
|
"""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
|
|
|
|
2023-05-02 22:16:58 +02: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
|
|
|
|
2023-05-02 22:16:58 +02:00
|
|
|
def interpolate_common(common, paths, key, default=None, conf=_config):
|
2020-06-20 21:41:59 +02:00
|
|
|
"""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
|
|
|
|
|
|
|
|
|
2023-05-02 22:16:58 +02:00
|
|
|
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:
|
2024-11-16 21:13:57 +01:00
|
|
|
if isinstance(value, list):
|
|
|
|
result.extend(value)
|
|
|
|
else:
|
|
|
|
result.append(value)
|
2020-09-14 21:13:08 +02:00
|
|
|
for p in path:
|
|
|
|
conf = conf[p]
|
|
|
|
if key in conf:
|
|
|
|
value = conf[key]
|
|
|
|
if value:
|
2024-11-16 21:13:57 +01:00
|
|
|
if isinstance(value, list):
|
|
|
|
result[:0] = value
|
|
|
|
else:
|
|
|
|
result.insert(0, value)
|
2020-09-14 21:13:08 +02:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-05-02 22:16:58 +02:00
|
|
|
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
|
|
|
|
2023-05-02 22:16:58 +02: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)
|
2018-02-07 21:47:27 +01:00
|
|
|
|
|
|
|
|
2023-05-02 22:16:58 +02:00
|
|
|
def unset(path, key, conf=_config):
|
2018-02-15 21:15:33 +01:00
|
|
|
"""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:
|
2018-02-15 21:15:33 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-02-07 21:47:27 +01:00
|
|
|
class apply():
|
2019-02-27 16:52:15 +01:00
|
|
|
"""Context Manager: apply a collection of key-value pairs"""
|
2018-02-07 21:47:27 +01:00
|
|
|
|
2018-02-15 21:15:33 +01:00
|
|
|
def __init__(self, kvlist):
|
|
|
|
self.original = []
|
|
|
|
self.kvlist = kvlist
|
2018-02-07 21:47:27 +01:00
|
|
|
|
|
|
|
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)
|
2018-02-07 21:47:27 +01:00
|
|
|
|
2024-10-11 16:43:28 +02:00
|
|
|
def __exit__(self, exc_type, exc_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)
|
2018-02-07 21:47:27 +01:00
|
|
|
else:
|
2019-11-23 23:50:16 +01:00
|
|
|
set(path, key, value)
|