import yaml import sys from .logger import getDebugLogger DEBUG = getDebugLogger() default = { 'AccessTokenLifeTime': 1800, 'Mail': { 'URL': '', 'port': 0, 'user': '', 'passwd': '', 'email': '', 'crypt': 'STARTTLS' } } 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'] 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"') if 'port' not in self.config['LDAP']: DEBUG.info('No Config for port in LDAP found. Set it to default: {}'.format(389)) self.config['LDAP']['port'] = 389 self.ldap = self.config['LDAP'] 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'] 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'] DEBUG.info("No Config for port in Mail found. Set it to default") else: self.config['Mail']['port'] = int(self.config['Mail']['port']) 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'] 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'] 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'] DEBUG.info("No Config for email in Mail found. Set it to default") if 'crypt' not in self.config['Mail']: self.config['Mail']['crypt'] = default['Mail']['crypt'] DEBUG.info("No Config for crypt in Mail found. Set it to default") self.mail = self.config['Mail'] DEBUG.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): DEBUG.error(msg, exc_info=True) sys.exit(-1) if __name__ == '__main__': ConifgParser()