1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 18:53:21 +01:00
gallery-dl/gallery_dl/config.py

159 lines
3.9 KiB
Python
Raw Normal View History

2015-10-03 20:23:55 +02:00
# -*- coding: utf-8 -*-
# Copyright 2015-2020 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
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 = {}
2020-05-19 21:42:11 +02:00
if util.WINDOWS:
_default_configs = [
r"%USERPROFILE%\gallery-dl\config.json",
r"%USERPROFILE%\gallery-dl.conf",
]
else:
_default_configs = [
"/etc/gallery-dl.conf",
"${HOME}/.config/gallery-dl/config.json",
"${HOME}/.gallery-dl.conf",
]
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
def load(files=None, strict=False, fmt="json"):
2015-10-03 20:23:55 +02:00
"""Load JSON configuration files"""
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
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)
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:
log.warning("Could not parse '%s': %s", path, exc)
2017-03-08 16:57:42 +01:00
if strict:
sys.exit(2)
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():
"""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):
"""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
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)
2019-11-23 23:50:16 +01:00
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"""
_sentinel = object()
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:
self.original.append((path, key, get(path, key, self._sentinel)))
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:
if value is self._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)