2016-03-19 20:37:04 +00:00
|
|
|
import os
|
2016-04-06 18:38:45 +00:00
|
|
|
import yaml
|
2016-04-03 20:03:58 +00:00
|
|
|
from szurubooru import errors
|
2016-03-30 19:23:19 +00:00
|
|
|
|
2016-04-06 18:38:45 +00:00
|
|
|
def merge(left, right):
|
|
|
|
for key in right:
|
|
|
|
if key in left:
|
|
|
|
if isinstance(left[key], dict) and isinstance(right[key], dict):
|
|
|
|
merge(left[key], right[key])
|
|
|
|
elif left[key] != right[key]:
|
|
|
|
left[key] = right[key]
|
|
|
|
else:
|
|
|
|
left[key] = right[key]
|
|
|
|
return left
|
|
|
|
|
2016-04-16 13:07:33 +00:00
|
|
|
def read_config():
|
|
|
|
with open('../config.yaml.dist') as handle:
|
|
|
|
ret = yaml.load(handle.read())
|
2016-04-06 18:38:45 +00:00
|
|
|
if os.path.exists('../config.yaml'):
|
|
|
|
with open('../config.yaml') as handle:
|
2016-04-16 13:07:33 +00:00
|
|
|
ret = merge(ret, yaml.load(handle.read()))
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def validate_config(src):
|
|
|
|
'''
|
|
|
|
Check whether config doesn't contain errors that might prove
|
|
|
|
lethal at runtime.
|
|
|
|
'''
|
|
|
|
all_ranks = src['ranks']
|
|
|
|
for privilege, rank in src['privileges'].items():
|
|
|
|
if rank not in all_ranks:
|
2016-04-03 20:03:58 +00:00
|
|
|
raise errors.ConfigError(
|
2016-04-16 13:07:33 +00:00
|
|
|
'Rank %r for privilege %r is missing' % (rank, privilege))
|
|
|
|
for rank in ['anonymous', 'admin', 'nobody']:
|
|
|
|
if rank not in all_ranks:
|
|
|
|
raise errors.ConfigError('Protected rank %r is missing' % rank)
|
|
|
|
if src['default_rank'] not in all_ranks:
|
|
|
|
raise errors.ConfigError(
|
|
|
|
'Default rank %r is not on the list of known ranks' % (
|
|
|
|
src['default_rank']))
|
|
|
|
|
|
|
|
for key in ['base_url', 'api_url', 'data_url', 'data_dir']:
|
|
|
|
if not src[key]:
|
|
|
|
raise errors.ConfigError(
|
|
|
|
'Service is not configured: %r is missing' % key)
|
2016-04-06 18:38:45 +00:00
|
|
|
|
2016-04-16 13:07:33 +00:00
|
|
|
if not os.path.isabs(src['data_dir']):
|
|
|
|
raise errors.ConfigError(
|
|
|
|
'data_dir must be an absolute path')
|
2016-04-09 19:41:10 +00:00
|
|
|
|
2016-04-16 13:07:33 +00:00
|
|
|
for key in ['schema', 'host', 'port', 'user', 'pass', 'name']:
|
|
|
|
if not src['database'][key]:
|
2016-04-09 19:41:10 +00:00
|
|
|
raise errors.ConfigError(
|
2016-04-16 13:07:33 +00:00
|
|
|
'Database is not configured: %r is missing' % key)
|
2016-04-03 20:03:58 +00:00
|
|
|
|
2016-04-16 13:07:33 +00:00
|
|
|
config = read_config() # pylint: disable=invalid-name
|
|
|
|
validate_config(config)
|