| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- # -*- coding: utf-8 -*-
- #
- import os, re
- import yaml
- regex = re.compile(r'^\$\{(?P<ENV>[A-Z_\-]+\:)?(?P<VAL>[\w\.]+)\}$')
- class YamlConfig:
- def __init__(self, config):
- self.config = config
- def get(self, key:str):
- return YamlConfig(self.config.get(key))
-
- def getValueAsString(self, key: str):
- try:
- match = regex.match(self.config[key])
- group = match.groupdict()
- if group['ENV'] != None:
- env = group['ENV'][:-1]
- return os.getenv(env, group['VAL'])
- return None
- except:
- return self.config[key]
-
- def getValueAsInt(self, key: str):
- try:
- match = regex.match(self.config[key])
- group = match.groupdict()
- if group['ENV'] != None:
- env = group['ENV'][:-1]
- return int(os.getenv(env, group['VAL']))
- return 0
- except:
- return int(self.config[key])
-
- def getValueAsBool(self, key: str, env: str = None):
- try:
- match = regex.match(self.config[key])
- group = match.groupdict()
- if group['ENV'] != None:
- env = group['ENV'][:-1]
- return bool(os.getenv(env, group['VAL']))
- return False
- except:
- return bool(self.config[key])
-
- def readYaml(path:str = 'application.yml', profile:str = None) -> YamlConfig:
- if os.path.exists(path):
- with open(path) as fd:
- conf = yaml.load(fd, Loader=yaml.FullLoader)
- if profile != None:
- result = path.split('.')
- profiledYaml = f'{result[0]}-{profile}.{result[1]}'
- if os.path.exists(profiledYaml):
- with open(profiledYaml) as fd:
- conf.update(yaml.load(fd, Loader=yaml.FullLoader))
- return YamlConfig(conf)
- # res = readYaml()
- # mysqlConf = res.get('mysql')
- # print(mysqlConf)
- # print(res.getValueAsString("host"))
- # mysqlYaml = mysqlConf.getValueAsString("host")
- # print(mysqlYaml)
- # host = mysqlYaml.get("host").split(':')[-1][:-1]
- # port = mysqlYaml.get("port").split(':')[-1][:-1]
- # username = mysqlYaml.get("username").split(':')[-1][:-1]
- # password = mysqlYaml.get("password").split(':')[-1][:-1]
- # mysql_db = mysqlYaml.get("db").split(':')[-1][:-1]
- # print(host,port,username,password)
|