2020-03-13 17:17:41 +00:00
|
|
|
import logging
|
2016-03-19 20:37:04 +00:00
|
|
|
import os
|
2020-06-05 22:03:37 +00:00
|
|
|
from typing import Dict
|
|
|
|
|
2016-04-06 18:38:45 +00:00
|
|
|
import yaml
|
2016-03-30 19:23:19 +00:00
|
|
|
|
2020-06-05 22:03:37 +00:00
|
|
|
from szurubooru import errors
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2020-03-13 17:17:41 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-04-27 21:50:49 +00:00
|
|
|
def _merge(left: Dict, right: Dict) -> Dict:
|
2016-04-06 18:38:45 +00:00
|
|
|
for key in right:
|
|
|
|
if key in left:
|
|
|
|
if isinstance(left[key], dict) and isinstance(right[key], dict):
|
2019-04-27 21:50:49 +00:00
|
|
|
_merge(left[key], right[key])
|
2016-04-06 18:38:45 +00:00
|
|
|
elif left[key] != right[key]:
|
|
|
|
left[key] = right[key]
|
|
|
|
else:
|
|
|
|
left[key] = right[key]
|
|
|
|
return left
|
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2019-04-27 21:50:49 +00:00
|
|
|
def _docker_config() -> Dict:
|
2020-08-28 18:43:10 +00:00
|
|
|
if "TEST_ENVIRONMENT" not in os.environ:
|
|
|
|
for key in ["POSTGRES_USER", "POSTGRES_PASSWORD", "POSTGRES_HOST"]:
|
|
|
|
if key not in os.environ:
|
|
|
|
raise errors.ConfigError(
|
|
|
|
f'Environment variable "{key}" not set'
|
|
|
|
)
|
2018-07-12 03:30:13 +00:00
|
|
|
return {
|
2020-06-05 22:03:37 +00:00
|
|
|
"debug": True,
|
|
|
|
"show_sql": int(os.getenv("LOG_SQL", 0)),
|
|
|
|
"data_url": os.getenv("DATA_URL", "data/"),
|
|
|
|
"data_dir": "/data/",
|
2022-02-07 21:44:56 +00:00
|
|
|
"database": "postgresql://%(user)s:%(pass)s@%(host)s:%(port)d/%(db)s"
|
2020-06-05 22:03:37 +00:00
|
|
|
% {
|
|
|
|
"user": os.getenv("POSTGRES_USER"),
|
|
|
|
"pass": os.getenv("POSTGRES_PASSWORD"),
|
|
|
|
"host": os.getenv("POSTGRES_HOST"),
|
|
|
|
"port": int(os.getenv("POSTGRES_PORT", 5432)),
|
|
|
|
"db": os.getenv("POSTGRES_DB", os.getenv("POSTGRES_USER")),
|
|
|
|
},
|
2018-07-12 03:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-27 21:50:49 +00:00
|
|
|
def _file_config(filename: str) -> Dict:
|
|
|
|
with open(filename) as handle:
|
2020-03-13 17:17:41 +00:00
|
|
|
return yaml.load(handle.read(), Loader=yaml.SafeLoader) or {}
|
2016-04-16 13:07:33 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2019-04-27 21:50:49 +00:00
|
|
|
def _read_config() -> Dict:
|
2020-06-05 22:03:37 +00:00
|
|
|
ret = _file_config("config.yaml.dist")
|
|
|
|
if os.path.isfile("config.yaml"):
|
|
|
|
ret = _merge(ret, _file_config("config.yaml"))
|
|
|
|
elif os.path.isdir("config.yaml"):
|
2020-03-13 17:17:41 +00:00
|
|
|
logger.warning(
|
2020-06-05 22:03:37 +00:00
|
|
|
"'config.yaml' should be a file, not a directory, skipping"
|
|
|
|
)
|
|
|
|
if os.path.exists("/.dockerenv"):
|
2019-04-27 21:50:49 +00:00
|
|
|
ret = _merge(ret, _docker_config())
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2020-06-05 14:02:18 +00:00
|
|
|
config = _read_config()
|