2015-10-03 20:23:55 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-10-25 15:44:36 +02:00
|
|
|
# Copyright 2015, 2016 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
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# public interface
|
|
|
|
|
2015-11-14 17:22:56 +01:00
|
|
|
def load(*files, strict=False):
|
2015-10-03 20:23:55 +02:00
|
|
|
"""Load JSON configuration files"""
|
|
|
|
configfiles = files or _default_configs
|
|
|
|
for conf in configfiles:
|
|
|
|
try:
|
2016-11-10 16:24:08 +01:00
|
|
|
path = os.path.expanduser(os.path.expandvars(conf))
|
2015-10-03 20:23:55 +02:00
|
|
|
with open(path) as file:
|
|
|
|
confdict = json.load(file)
|
|
|
|
_config.update(confdict)
|
|
|
|
except FileNotFoundError:
|
2015-11-14 17:22:56 +01:00
|
|
|
if strict:
|
|
|
|
raise
|
2015-10-03 20:23:55 +02:00
|
|
|
continue
|
2016-09-24 12:05:45 +02:00
|
|
|
except ValueError as exception:
|
2015-10-03 20:23:55 +02:00
|
|
|
print("Error while loading '", path, "':", sep="", file=sys.stderr)
|
|
|
|
print(exception, file=sys.stderr)
|
|
|
|
|
|
|
|
def clear():
|
|
|
|
"""Reset configuration to en empty state"""
|
|
|
|
globals()["_config"] = {}
|
|
|
|
|
2015-10-05 12:42:42 +02:00
|
|
|
def get(keys, default=None):
|
2015-10-03 20:23:55 +02:00
|
|
|
"""Get the value of property 'key' or a default-value if it doenst exist"""
|
|
|
|
conf = _config
|
|
|
|
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
|
|
|
|
|
2015-10-05 12:42:42 +02:00
|
|
|
def interpolate(keys, default=None):
|
2015-10-03 20:23:55 +02:00
|
|
|
"""Interpolate the value of 'key'"""
|
|
|
|
conf = _config
|
|
|
|
try:
|
|
|
|
for k in keys:
|
|
|
|
default = conf.get(keys[-1], default)
|
|
|
|
conf = conf[k]
|
|
|
|
return conf
|
|
|
|
except (KeyError, AttributeError):
|
|
|
|
return default
|
|
|
|
|
2015-10-05 12:42:42 +02:00
|
|
|
def set(keys, value):
|
2015-10-03 20:23:55 +02:00
|
|
|
"""Set the value of property 'key' for this session"""
|
|
|
|
conf = _config
|
|
|
|
for k in keys[:-1]:
|
|
|
|
try:
|
|
|
|
conf = conf[k]
|
|
|
|
except KeyError:
|
|
|
|
temp = {}
|
|
|
|
conf[k] = temp
|
|
|
|
conf = temp
|
|
|
|
conf[keys[-1]] = value
|
|
|
|
|
2015-10-07 00:58:43 +02:00
|
|
|
def setdefault(keys, value):
|
|
|
|
"""Set the value of property 'key' if it doesn't exist"""
|
|
|
|
conf = _config
|
|
|
|
for k in keys[:-1]:
|
|
|
|
try:
|
|
|
|
conf = conf[k]
|
|
|
|
except KeyError:
|
|
|
|
temp = {}
|
|
|
|
conf[k] = temp
|
|
|
|
conf = temp
|
|
|
|
return conf.setdefault(keys[-1], value)
|
|
|
|
|
2015-10-03 20:23:55 +02:00
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# internals
|
|
|
|
|
|
|
|
_config = {}
|
|
|
|
|
2016-10-25 15:44:36 +02:00
|
|
|
if os.name == "nt":
|
2015-10-03 20:23:55 +02:00
|
|
|
_default_configs = [
|
2016-07-25 11:12:52 +02:00
|
|
|
r"~\.config\gallery-dl\config.json",
|
2016-11-10 16:24:08 +01:00
|
|
|
r"%USERPROFILE%\gallery-dl\config.json",
|
2015-10-03 20:23:55 +02:00
|
|
|
r"~\.gallery-dl.conf",
|
2016-11-10 16:24:08 +01:00
|
|
|
r"%USERPROFILE%\gallery-dl.conf",
|
2015-10-03 20:23:55 +02:00
|
|
|
]
|
|
|
|
else:
|
|
|
|
_default_configs = [
|
|
|
|
"/etc/gallery-dl.conf",
|
2016-11-10 16:24:08 +01:00
|
|
|
"${HOME}/.config/gallery/config.json",
|
|
|
|
"${HOME}/.config/gallery-dl/config.json",
|
|
|
|
"${HOME}/.gallery-dl.conf",
|
2015-10-03 20:23:55 +02:00
|
|
|
]
|