2019-12-30 08:22:43 +00:00
|
|
|
import yaml
|
2020-01-05 13:15:02 +00:00
|
|
|
import sys
|
|
|
|
from . import LOGGER
|
|
|
|
|
|
|
|
default = {
|
|
|
|
'AccessTokenLifeTime': 1800,
|
|
|
|
'Mail': {
|
|
|
|
'URL': '',
|
|
|
|
'port': 0,
|
|
|
|
'user': '',
|
|
|
|
'passwd': '',
|
|
|
|
'email': ''
|
|
|
|
}
|
|
|
|
}
|
2019-12-30 08:22:43 +00:00
|
|
|
|
|
|
|
class ConifgParser():
|
|
|
|
def __init__(self, file='config.yml'):
|
|
|
|
self.file = file
|
|
|
|
with open(file, 'r') as f:
|
2020-01-05 13:15:02 +00:00
|
|
|
self.config = yaml.safe_load(f)
|
|
|
|
|
|
|
|
if 'Database' not in self.config:
|
|
|
|
self.__error__('Wrong Configuration for Database. You should configure databaseconfig with "URL", "user", "passwd", "database"')
|
|
|
|
if 'URL' not in self.config['Database'] or 'user' not in self.config['Database'] or 'passwd' not in self.config['Database'] or 'database' not in self.config['Database']:
|
|
|
|
self.__error__('Wrong Configuration for Database. You should configure databaseconfig with "URL", "user", "passwd", "database"')
|
2019-12-30 08:22:43 +00:00
|
|
|
|
|
|
|
self.db = self.config['Database']
|
2020-01-05 13:15:02 +00:00
|
|
|
LOGGER.debug("Set Databaseconfig: {}".format(self.db))
|
|
|
|
|
|
|
|
if 'LDAP' not in self.config:
|
|
|
|
self.__error__('Wrong Configuration for LDAP. You should configure ldapconfig with "URL" and "dn"')
|
|
|
|
if 'URL' not in self.config['LDAP'] or 'dn' not in self.config['LDAP']:
|
|
|
|
self.__error__('Wrong Configuration for LDAP. You should configure ldapconfig with "URL" and "dn"')
|
2019-12-30 08:22:43 +00:00
|
|
|
self.ldap = self.config['LDAP']
|
2020-01-05 13:15:02 +00:00
|
|
|
LOGGER.info("Set LDAPconfig: {}".format(self.ldap))
|
|
|
|
if 'AccessTokenLifeTime' in self.config:
|
|
|
|
self.accessTokenLifeTime = self.config['AccessTokenLifeTime']
|
|
|
|
LOGGER.info("Set AccessTokenLifeTime: {}".format(self.accessTokenLifeTime))
|
|
|
|
else:
|
|
|
|
self.accessTokenLifeTime = default['AccessTokenLifeTime']
|
|
|
|
LOGGER.info("No Config for AccessTokenLifetime found. Set it to default: {}".format(self.accessTokenLifeTime))
|
|
|
|
|
|
|
|
if 'Mail' not in self.config:
|
|
|
|
self.config['Mail'] = default['Mail']
|
|
|
|
LOGGER.info('No Conifg for Mail found. Set it to defaul: {}'.format(self.config['Mail']))
|
|
|
|
if 'URL' not in self.config['Mail']:
|
|
|
|
self.config['Mail']['URL'] = default['Mail']['URL']
|
|
|
|
LOGGER.info("No Config for URL in Mail found. Set it to default")
|
|
|
|
if 'port' not in self.config['Mail']:
|
|
|
|
self.config['Mail']['port'] = default['Mail']['port']
|
|
|
|
LOGGER.info("No Config for port in Mail found. Set it to default")
|
|
|
|
else:
|
|
|
|
self.config['Mail']['port'] = int(self.config['Mail']['port'])
|
|
|
|
if 'user' not in self.config['Mail']:
|
|
|
|
self.config['Mail']['user'] = default['Mail']['user']
|
|
|
|
LOGGER.info("No Config for user in Mail found. Set it to default")
|
|
|
|
if 'passwd' not in self.config['Mail']:
|
|
|
|
self.config['Mail']['passwd'] = default['Mail']['passwd']
|
|
|
|
LOGGER.info("No Config for passwd in Mail found. Set it to default")
|
|
|
|
if 'email' not in self.config['Mail']:
|
|
|
|
self.config['Mail']['email'] = default['Mail']['email']
|
|
|
|
LOGGER.info("No Config for email in Mail found. Set it to default")
|
|
|
|
self.mail = self.config['Mail']
|
|
|
|
LOGGER.info('Set Mailconfig: {}'.format(self.mail))
|
|
|
|
|
2019-12-30 08:22:43 +00:00
|
|
|
|
|
|
|
def getLDAP(self):
|
|
|
|
return self.ldap
|
|
|
|
|
|
|
|
def getDatabase(self):
|
|
|
|
return self.db
|
|
|
|
|
|
|
|
def getAccessToken(self):
|
|
|
|
return self.accessTokenLifeTime
|
|
|
|
|
2020-01-05 13:15:02 +00:00
|
|
|
def getMail(self):
|
|
|
|
return self.mail
|
|
|
|
|
|
|
|
def __error__(self, msg):
|
|
|
|
LOGGER.error(msg)
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2019-12-30 08:22:43 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
ConifgParser()
|