2015-10-03 20:23:55 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-05-16 01:12:37 +02:00
|
|
|
# Copyright 2015-2021 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 json
|
|
|
|
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 = {}
|
|
|
|
|
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",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-04-08 21:53:50 +02:00
|
|
|
if getattr(sys, "frozen", False):
|
|
|
|
# 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
|
|
|
|
|
2019-02-27 16:52:15 +01:00
|
|
|
def load(files=None, strict=False, fmt="json"):
|
2015-10-03 20:23:55 +02:00
|
|
|
"""Load JSON configuration files"""
|
2019-02-27 16:52:15 +01:00
|
|
|
if fmt == "yaml":
|
2017-03-08 16:57:42 +01:00
|
|
|
try:
|
|
|
|
import yaml
|
|
|
|
parsefunc = yaml.safe_load
|
|
|
|
except ImportError:
|
|
|
|
log.error("Could not import 'yaml' module")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
parsefunc = json.load
|
|
|
|
|
2019-02-27 16:52:15 +01:00
|
|
|
for path in files or _default_configs:
|
|
|
|
path = util.expand_path(path)
|
2015-10-03 20:23:55 +02:00
|
|
|
try:
|
2018-05-25 16:25:32 +02:00
|
|
|
with open(path, encoding="utf-8") as file:
|
2017-03-08 16:57:42 +01:00
|
|
|
confdict = parsefunc(file)
|
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)
|
2017-03-08 16:57:42 +01:00
|
|
|
sys.exit(1)
|
2017-09-30 18:52:23 +02:00
|
|
|
except Exception as exc:
|
2019-02-27 16:52:15 +01:00
|
|
|
log.warning("Could not parse '%s': %s", path, exc)
|
2017-03-08 16:57:42 +01:00
|
|
|
if strict:
|
|
|
|
sys.exit(2)
|
2019-02-27 16:52:15 +01:00
|
|
|
else:
|
|
|
|
if not _config:
|
|
|
|
_config.update(confdict)
|
|
|
|
else:
|
|
|
|
util.combine_dict(_config, confdict)
|
2015-10-03 20:23:55 +02:00
|
|
|
|
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
|
|
|
|
2019-11-23 23:50:16 +01: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
|
|
|
|
2019-11-23 23:50:16 +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
|
|
|
|
2020-06-20 21:41:59 +02: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
|
|
|
|
|
|
|
|
|
2020-09-14 21:13:08 +02:00
|
|
|
def accumulate(path, key, *, conf=_config):
|
|
|
|
"""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
|
|
|
|
|
|
|
|
|
2019-11-23 23:50:16 +01: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
|
|
|
|
2019-11-23 23:50:16 +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)
|
2018-02-07 21:47:27 +01:00
|
|
|
|
|
|
|
|
2019-11-23 23:50:16 +01: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
|
|
|
|
|
|
|
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)
|
2018-02-07 21:47:27 +01:00
|
|
|
else:
|
2019-11-23 23:50:16 +01:00
|
|
|
set(path, key, value)
|