flaschengeist/geruecht/configparser.py

118 lines
4.9 KiB
Python
Raw Normal View History

2019-12-30 08:22:43 +00:00
import yaml
import sys
from .logger import getDebugLogger
DEBUG = getDebugLogger()
default = {
'AccessTokenLifeTime': 1800,
'Mail': {
'URL': '',
'port': 0,
'user': '',
'passwd': '',
2020-02-26 21:13:44 +00:00
'email': '',
'crypt': 'STARTTLS'
}
}
2019-12-30 08:22:43 +00:00
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:
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-03-09 18:54:51 +00:00
DEBUG.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"')
2020-02-26 22:03:02 +00:00
if 'port' not in self.config['LDAP']:
DEBUG.info(
'No Config for port in LDAP found. Set it to default: {}'.format(389))
2020-02-26 22:03:02 +00:00
self.config['LDAP']['port'] = 389
if 'ADMIN_DN' not in self.config['LDAP']:
DEBUG.info(
'No Config for ADMIN_DN in LDAP found. Set it to default {}. (Maybe Password reset not working)'.format(None)
)
self.config['LDAP']['ADMIN_DN'] = None
if 'ADMIN_SECRET' not in self.config['LDAP']:
DEBUG.info(
'No Config for ADMIN_SECRET in LDAP found. Set it to default {}. (Maybe Password reset not working)'.format(None)
)
self.config['LDAP']['ADMIN_SECRET'] = None
if 'USER_DN' not in self.config['LDAP']:
DEBUG.info(
'No Config for USER_DN in LDAP found. Set it to default {}. (Maybe Password reset not working)'.format(None)
)
self.config['LDAP']['USER_DN'] = None
2019-12-30 08:22:43 +00:00
self.ldap = self.config['LDAP']
2020-03-09 18:54:51 +00:00
DEBUG.info("Set LDAPconfig: {}".format(self.ldap))
if 'AccessTokenLifeTime' in self.config:
self.accessTokenLifeTime = int(self.config['AccessTokenLifeTime'])
DEBUG.info("Set AccessTokenLifeTime: {}".format(
self.accessTokenLifeTime))
else:
self.accessTokenLifeTime = default['AccessTokenLifeTime']
DEBUG.info("No Config for AccessTokenLifetime found. Set it to default: {}".format(
self.accessTokenLifeTime))
if 'Mail' not in self.config:
self.config['Mail'] = default['Mail']
DEBUG.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']
2020-03-09 18:54:51 +00:00
DEBUG.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']
2020-03-09 18:54:51 +00:00
DEBUG.info("No Config for port in Mail found. Set it to default")
else:
self.config['Mail']['port'] = int(self.config['Mail']['port'])
2020-03-09 18:54:51 +00:00
DEBUG.info("No Conifg for port in Mail found. Set it to default")
if 'user' not in self.config['Mail']:
self.config['Mail']['user'] = default['Mail']['user']
2020-03-09 18:54:51 +00:00
DEBUG.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']
2020-03-09 18:54:51 +00:00
DEBUG.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']
2020-03-09 18:54:51 +00:00
DEBUG.info("No Config for email in Mail found. Set it to default")
2020-02-26 21:13:44 +00:00
if 'crypt' not in self.config['Mail']:
self.config['Mail']['crypt'] = default['Mail']['crypt']
2020-03-09 18:54:51 +00:00
DEBUG.info("No Config for crypt in Mail found. Set it to default")
self.mail = self.config['Mail']
2020-03-09 18:54:51 +00:00
DEBUG.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
def getMail(self):
return self.mail
def __error__(self, msg):
DEBUG.error(msg, exc_info=True)
sys.exit(-1)
2019-12-30 08:22:43 +00:00
if __name__ == '__main__':
ConifgParser()