| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- # -*- coding: utf-8 -*-
- # Author : Charley
- # Python : 3.10.8
- # Date : 2025/12/22 10:44
- 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'] is not 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'] is not 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):
- try:
- match = regex.match(self.config[key])
- group = match.groupdict()
- if group['ENV'] is not 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 is not 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)
|