2015-10-03 20:23:55 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-02-07 21:47:27 +01:00
|
|
|
# Copyright 2015-2018 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 = {}
|
|
|
|
|
|
|
|
if os.name == "nt":
|
|
|
|
_default_configs = [
|
|
|
|
r"%USERPROFILE%\gallery-dl\config.json",
|
|
|
|
r"%USERPROFILE%\gallery-dl.conf",
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
_default_configs = [
|
|
|
|
"/etc/gallery-dl.conf",
|
|
|
|
"${HOME}/.config/gallery/config.json",
|
|
|
|
"${HOME}/.config/gallery-dl/config.json",
|
|
|
|
"${HOME}/.gallery-dl.conf",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2015-10-03 20:23:55 +02:00
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# public interface
|
|
|
|
|
2017-03-08 16:57:42 +01:00
|
|
|
def load(*files, format="json", strict=False):
|
2015-10-03 20:23:55 +02:00
|
|
|
"""Load JSON configuration files"""
|
|
|
|
configfiles = files or _default_configs
|
2017-03-08 16:57:42 +01:00
|
|
|
|
|
|
|
if format == "yaml":
|
|
|
|
try:
|
|
|
|
import yaml
|
|
|
|
parsefunc = yaml.safe_load
|
|
|
|
except ImportError:
|
|
|
|
log.error("Could not import 'yaml' module")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
parsefunc = json.load
|
|
|
|
|
2015-10-03 20:23:55 +02:00
|
|
|
for conf in configfiles:
|
|
|
|
try:
|
2017-10-26 00:04:28 +02:00
|
|
|
path = util.expand_path(conf)
|
2015-10-03 20:23:55 +02:00
|
|
|
with open(path) as file:
|
2017-03-08 16:57:42 +01:00
|
|
|
confdict = parsefunc(file)
|
2017-08-12 20:07:27 +02:00
|
|
|
if not _config:
|
|
|
|
_config.update(confdict)
|
|
|
|
else:
|
|
|
|
util.combine_dict(_config, confdict)
|
2015-10-03 20:23:55 +02:00
|
|
|
except FileNotFoundError:
|
2015-11-14 17:22:56 +01:00
|
|
|
if strict:
|
2017-03-08 16:57:42 +01:00
|
|
|
log.error("Configuration file '%s' not found", path)
|
|
|
|
sys.exit(1)
|
2017-09-30 18:52:23 +02:00
|
|
|
except Exception as exc:
|
|
|
|
log.warning("Could not parse '%s': %s", path, exc)
|
2017-03-08 16:57:42 +01:00
|
|
|
if strict:
|
|
|
|
sys.exit(2)
|
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
|
|
|
|
2017-03-27 11:59:27 +02:00
|
|
|
def get(keys, default=None, conf=_config):
|
|
|
|
"""Get the value of property 'key' or a default value"""
|
2015-10-03 20:23:55 +02:00
|
|
|
try:
|
2015-10-05 12:42:42 +02:00
|
|
|
for k in keys:
|
2015-10-03 20:23:55 +02:00
|
|
|
conf = conf[k]
|
|
|
|
return conf
|
|
|
|
except (KeyError, AttributeError):
|
|
|
|
return default
|
|
|
|
|
2017-01-30 19:40:15 +01:00
|
|
|
|
2017-03-27 11:59:27 +02:00
|
|
|
def interpolate(keys, default=None, conf=_config):
|
2015-10-03 20:23:55 +02:00
|
|
|
"""Interpolate the value of 'key'"""
|
|
|
|
try:
|
2017-03-27 11:59:27 +02:00
|
|
|
lkey = keys[-1]
|
|
|
|
if lkey in conf:
|
|
|
|
return conf[lkey]
|
2015-10-03 20:23:55 +02:00
|
|
|
for k in keys:
|
2017-03-27 11:59:27 +02:00
|
|
|
if lkey in conf:
|
|
|
|
default = conf[lkey]
|
2015-10-03 20:23:55 +02:00
|
|
|
conf = conf[k]
|
|
|
|
return conf
|
|
|
|
except (KeyError, AttributeError):
|
|
|
|
return default
|
|
|
|
|
2017-01-30 19:40:15 +01:00
|
|
|
|
2017-03-27 11:59:27 +02:00
|
|
|
def set(keys, value, conf=_config):
|
2015-10-03 20:23:55 +02:00
|
|
|
"""Set the value of property 'key' for this session"""
|
|
|
|
for k in keys[:-1]:
|
|
|
|
try:
|
|
|
|
conf = conf[k]
|
|
|
|
except KeyError:
|
|
|
|
temp = {}
|
|
|
|
conf[k] = temp
|
|
|
|
conf = temp
|
|
|
|
conf[keys[-1]] = value
|
|
|
|
|
2017-01-30 19:40:15 +01:00
|
|
|
|
2017-03-27 11:59:27 +02:00
|
|
|
def setdefault(keys, value, conf=_config):
|
2015-10-07 00:58:43 +02:00
|
|
|
"""Set the value of property 'key' if it doesn't exist"""
|
|
|
|
for k in keys[:-1]:
|
|
|
|
try:
|
|
|
|
conf = conf[k]
|
|
|
|
except KeyError:
|
|
|
|
temp = {}
|
|
|
|
conf[k] = temp
|
|
|
|
conf = temp
|
|
|
|
return conf.setdefault(keys[-1], value)
|
2018-02-07 21:47:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
class apply():
|
|
|
|
"""Context Manager to apply a dict to global config"""
|
|
|
|
_sentinel = object()
|
|
|
|
|
|
|
|
def __init__(self, config_dict):
|
|
|
|
self.original_values = {}
|
|
|
|
self.config_dict = config_dict
|
|
|
|
for key, value in config_dict.items():
|
|
|
|
self.original_values[key] = _config.get(key, self._sentinel)
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
_config.update(self.config_dict)
|
|
|
|
|
|
|
|
def __exit__(self, etype, value, traceback):
|
|
|
|
for key, value in self.original_values.items():
|
|
|
|
if value is self._sentinel:
|
|
|
|
del _config[key]
|
|
|
|
else:
|
|
|
|
_config[key] = value
|