2017-02-04 00:08:12 +00:00
|
|
|
from typing import Dict
|
2016-03-19 20:37:04 +00:00
|
|
|
import os
|
2016-04-06 18:38:45 +00:00
|
|
|
import yaml
|
2018-07-12 03:30:13 +00:00
|
|
|
from szurubooru import errors
|
2016-03-30 19:23:19 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
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:
|
2018-07-12 03:30:13 +00:00
|
|
|
for key in [
|
|
|
|
'POSTGRES_USER',
|
|
|
|
'POSTGRES_PASSWORD',
|
|
|
|
'POSTGRES_HOST',
|
|
|
|
'ESEARCH_HOST'
|
|
|
|
]:
|
|
|
|
if not os.getenv(key, False):
|
|
|
|
raise errors.ConfigError(f'Environment variable "{key}" not set')
|
|
|
|
return {
|
|
|
|
'debug': True,
|
|
|
|
'show_sql': int(os.getenv('LOG_SQL', 0)),
|
2018-08-18 03:09:13 +00:00
|
|
|
'data_url': os.getenv('DATA_URL', 'data/'),
|
2018-07-12 03:30:13 +00:00
|
|
|
'data_dir': '/data/',
|
|
|
|
'database': 'postgres://%(user)s:%(pass)s@%(host)s:%(port)d/%(db)s' % {
|
|
|
|
'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'))
|
|
|
|
},
|
|
|
|
'elasticsearch': {
|
|
|
|
'host': os.getenv('ESEARCH_HOST'),
|
|
|
|
'port': int(os.getenv('ESEARCH_PORT', 9200)),
|
2019-09-15 16:45:17 +00:00
|
|
|
'index': os.getenv('ESEARCH_INDEX', 'szurubooru'),
|
|
|
|
'user': os.getenv('ESEARCH_USER', os.getenv('ESEARCH_INDEX', 'szurubooru')),
|
|
|
|
'pass': os.getenv('ESEARCH_PASSWORD', False)
|
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:
|
|
|
|
return yaml.load(handle.read(), Loader=yaml.SafeLoader)
|
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:
|
|
|
|
ret = _file_config('config.yaml.dist')
|
|
|
|
if os.path.exists('config.yaml'):
|
|
|
|
ret = _merge(ret, _file_config('config.yaml'))
|
|
|
|
if os.path.exists('/.dockerenv'):
|
|
|
|
ret = _merge(ret, _docker_config())
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
config = _read_config() # pylint: disable=invalid-name
|