import yaml import sys from .logger import getLogger LOGGER = getLogger(__name__) default = { 'AccessTokenLifeTime': 1800, 'Mail': { 'URL': '', 'port': 0, 'user': '', 'passwd': '', 'email': '' } } 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"') self.db = self.config['Database'] 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"') self.ldap = self.config['LDAP'] LOGGER.info("Set LDAPconfig: {}".format(self.ldap)) if 'AccessTokenLifeTime' in self.config: self.accessTokenLifeTime = int(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)) 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): LOGGER.error(msg) sys.exit(-1) if __name__ == '__main__': ConifgParser()