2016-03-19 20:37:04 +00:00
|
|
|
import os
|
|
|
|
import configobj
|
2016-04-03 17:00:47 +00:00
|
|
|
import szurubooru.errors
|
2016-03-30 19:23:19 +00:00
|
|
|
|
2016-03-19 20:37:04 +00:00
|
|
|
class Config(object):
|
2016-03-28 12:14:50 +00:00
|
|
|
''' INI config parser and container. '''
|
2016-03-19 20:37:04 +00:00
|
|
|
def __init__(self):
|
2016-04-01 16:45:25 +00:00
|
|
|
self.config = configobj.ConfigObj('../config.ini.dist')
|
|
|
|
if os.path.exists('../config.ini'):
|
2016-04-03 15:01:48 +00:00
|
|
|
self.config.merge(configobj.ConfigObj('../config.ini'))
|
2016-03-30 19:23:19 +00:00
|
|
|
self._validate()
|
2016-03-19 20:37:04 +00:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.config[key]
|
2016-03-30 19:23:19 +00:00
|
|
|
|
|
|
|
def _validate(self):
|
|
|
|
'''
|
|
|
|
Checks whether config.ini doesn't contain errors that might prove
|
|
|
|
lethal at runtime.
|
|
|
|
'''
|
|
|
|
all_ranks = self['service']['user_ranks']
|
|
|
|
for privilege, rank in self['privileges'].items():
|
|
|
|
if rank not in all_ranks:
|
2016-04-03 17:00:47 +00:00
|
|
|
raise szurubooru.errors.ConfigError(
|
2016-03-30 19:23:19 +00:00
|
|
|
'Rank %r for privilege %r is missing from user_ranks' % (
|
|
|
|
rank, privilege))
|
|
|
|
for rank in ['anonymous', 'admin', 'nobody']:
|
|
|
|
if rank not in all_ranks:
|
2016-04-03 17:00:47 +00:00
|
|
|
raise szurubooru.errors.ConfigError(
|
|
|
|
'Fixed rank %r is missing from user_ranks' % rank)
|
2016-03-30 19:23:19 +00:00
|
|
|
if self['service']['default_user_rank'] not in all_ranks:
|
2016-04-03 17:00:47 +00:00
|
|
|
raise szurubooru.errors.ConfigError(
|
2016-03-30 19:23:19 +00:00
|
|
|
'Default rank %r is missing from user_ranks' % (
|
|
|
|
self['service']['default_user_rank']))
|