improve config module
- speed improvements, especially in the 'interpolate' function - 'interpolate' now prioritizes base-level values if they exist - "username" is chosen before "extractor.<category>.username" - -u/--username & co can now override config-file values
This commit is contained in:
@@ -23,7 +23,7 @@ if sys.hexversion < 0x3030000:
|
|||||||
import logging
|
import logging
|
||||||
from . import version, config, option, extractor, job, exception
|
from . import version, config, option, extractor, job, exception
|
||||||
|
|
||||||
__version = version.__version__
|
__version__ = version.__version__
|
||||||
log = logging.getLogger("gallery-dl")
|
log = logging.getLogger("gallery-dl")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,27 @@ import logging
|
|||||||
log = logging.getLogger("config")
|
log = logging.getLogger("config")
|
||||||
|
|
||||||
|
|
||||||
|
# --------------------------------------------------------------------
|
||||||
|
# internals
|
||||||
|
|
||||||
|
_config = {}
|
||||||
|
|
||||||
|
if os.name == "nt":
|
||||||
|
_default_configs = [
|
||||||
|
r"~\.config\gallery-dl\config.json",
|
||||||
|
r"%USERPROFILE%\gallery-dl\config.json",
|
||||||
|
r"~\.gallery-dl.conf",
|
||||||
|
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",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# public interface
|
# public interface
|
||||||
|
|
||||||
@@ -52,12 +73,11 @@ def load(*files, format="json", strict=False):
|
|||||||
|
|
||||||
def clear():
|
def clear():
|
||||||
"""Reset configuration to en empty state"""
|
"""Reset configuration to en empty state"""
|
||||||
globals()["_config"] = {}
|
globals()["_config"].clear()
|
||||||
|
|
||||||
|
|
||||||
def get(keys, default=None):
|
def get(keys, default=None, conf=_config):
|
||||||
"""Get the value of property 'key' or a default-value if it doenst exist"""
|
"""Get the value of property 'key' or a default value"""
|
||||||
conf = _config
|
|
||||||
try:
|
try:
|
||||||
for k in keys:
|
for k in keys:
|
||||||
conf = conf[k]
|
conf = conf[k]
|
||||||
@@ -66,21 +86,23 @@ def get(keys, default=None):
|
|||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
||||||
def interpolate(keys, default=None):
|
def interpolate(keys, default=None, conf=_config):
|
||||||
"""Interpolate the value of 'key'"""
|
"""Interpolate the value of 'key'"""
|
||||||
conf = _config
|
|
||||||
try:
|
try:
|
||||||
|
lkey = keys[-1]
|
||||||
|
if lkey in conf:
|
||||||
|
return conf[lkey]
|
||||||
for k in keys:
|
for k in keys:
|
||||||
default = conf.get(keys[-1], default)
|
if lkey in conf:
|
||||||
|
default = conf[lkey]
|
||||||
conf = conf[k]
|
conf = conf[k]
|
||||||
return conf
|
return conf
|
||||||
except (KeyError, AttributeError):
|
except (KeyError, AttributeError):
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
||||||
def set(keys, value):
|
def set(keys, value, conf=_config):
|
||||||
"""Set the value of property 'key' for this session"""
|
"""Set the value of property 'key' for this session"""
|
||||||
conf = _config
|
|
||||||
for k in keys[:-1]:
|
for k in keys[:-1]:
|
||||||
try:
|
try:
|
||||||
conf = conf[k]
|
conf = conf[k]
|
||||||
@@ -91,9 +113,8 @@ def set(keys, value):
|
|||||||
conf[keys[-1]] = value
|
conf[keys[-1]] = value
|
||||||
|
|
||||||
|
|
||||||
def setdefault(keys, value):
|
def setdefault(keys, value, conf=_config):
|
||||||
"""Set the value of property 'key' if it doesn't exist"""
|
"""Set the value of property 'key' if it doesn't exist"""
|
||||||
conf = _config
|
|
||||||
for k in keys[:-1]:
|
for k in keys[:-1]:
|
||||||
try:
|
try:
|
||||||
conf = conf[k]
|
conf = conf[k]
|
||||||
@@ -102,24 +123,3 @@ def setdefault(keys, value):
|
|||||||
conf[k] = temp
|
conf[k] = temp
|
||||||
conf = temp
|
conf = temp
|
||||||
return conf.setdefault(keys[-1], value)
|
return conf.setdefault(keys[-1], value)
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
|
||||||
# internals
|
|
||||||
|
|
||||||
_config = {}
|
|
||||||
|
|
||||||
if os.name == "nt":
|
|
||||||
_default_configs = [
|
|
||||||
r"~\.config\gallery-dl\config.json",
|
|
||||||
r"%USERPROFILE%\gallery-dl\config.json",
|
|
||||||
r"~\.gallery-dl.conf",
|
|
||||||
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",
|
|
||||||
]
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# Copyright 2015 Mike Fährmann
|
# Copyright 2015-2017 Mike Fährmann
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify
|
# 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
|
# it under the terms of the GNU General Public License version 2 as
|
||||||
@@ -18,7 +18,7 @@ class TestConfig(unittest.TestCase):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
fd, self._configfile = tempfile.mkstemp()
|
fd, self._configfile = tempfile.mkstemp()
|
||||||
with os.fdopen(fd, "w") as file:
|
with os.fdopen(fd, "w") as file:
|
||||||
file.write('{"a": "1", "b": {"c": "text"}}')
|
file.write('{"a": "1", "b": {"a": 2, "c": "text"}}')
|
||||||
config.load(self._configfile)
|
config.load(self._configfile)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user