|
@@ -1,22 +1,25 @@
|
|
|
# -*- coding: utf-8 -*-
|
|
# -*- coding: utf-8 -*-
|
|
|
-#
|
|
|
|
|
|
|
+# Author : Charley
|
|
|
|
|
+# Python : 3.10.8
|
|
|
|
|
+# Date : 2025/12/22 10:44
|
|
|
import os, re
|
|
import os, re
|
|
|
import yaml
|
|
import yaml
|
|
|
|
|
|
|
|
-regex = re.compile(r'^\$\{(?P<ENV>[A-Z_\-]+\:)?(?P<VAL>[\w\.]+)\}$')
|
|
|
|
|
|
|
+regex = re.compile(r'^\$\{(?P<ENV>[A-Z_\-]+:)?(?P<VAL>[\w.]+)}$')
|
|
|
|
|
+
|
|
|
|
|
|
|
|
class YamlConfig:
|
|
class YamlConfig:
|
|
|
def __init__(self, config):
|
|
def __init__(self, config):
|
|
|
self.config = config
|
|
self.config = config
|
|
|
-
|
|
|
|
|
- def get(self, key:str):
|
|
|
|
|
|
|
+
|
|
|
|
|
+ def get(self, key: str):
|
|
|
return YamlConfig(self.config.get(key))
|
|
return YamlConfig(self.config.get(key))
|
|
|
|
|
|
|
|
def getValueAsString(self, key: str):
|
|
def getValueAsString(self, key: str):
|
|
|
try:
|
|
try:
|
|
|
match = regex.match(self.config[key])
|
|
match = regex.match(self.config[key])
|
|
|
group = match.groupdict()
|
|
group = match.groupdict()
|
|
|
- if group['ENV'] != None:
|
|
|
|
|
|
|
+ if group['ENV'] is not None:
|
|
|
env = group['ENV'][:-1]
|
|
env = group['ENV'][:-1]
|
|
|
return os.getenv(env, group['VAL'])
|
|
return os.getenv(env, group['VAL'])
|
|
|
return None
|
|
return None
|
|
@@ -27,36 +30,37 @@ class YamlConfig:
|
|
|
try:
|
|
try:
|
|
|
match = regex.match(self.config[key])
|
|
match = regex.match(self.config[key])
|
|
|
group = match.groupdict()
|
|
group = match.groupdict()
|
|
|
- if group['ENV'] != None:
|
|
|
|
|
|
|
+ if group['ENV'] is not None:
|
|
|
env = group['ENV'][:-1]
|
|
env = group['ENV'][:-1]
|
|
|
return int(os.getenv(env, group['VAL']))
|
|
return int(os.getenv(env, group['VAL']))
|
|
|
return 0
|
|
return 0
|
|
|
except:
|
|
except:
|
|
|
return int(self.config[key])
|
|
return int(self.config[key])
|
|
|
-
|
|
|
|
|
- def getValueAsBool(self, key: str, env: str = None):
|
|
|
|
|
|
|
+
|
|
|
|
|
+ def getValueAsBool(self, key: str):
|
|
|
try:
|
|
try:
|
|
|
match = regex.match(self.config[key])
|
|
match = regex.match(self.config[key])
|
|
|
group = match.groupdict()
|
|
group = match.groupdict()
|
|
|
- if group['ENV'] != None:
|
|
|
|
|
|
|
+ if group['ENV'] is not None:
|
|
|
env = group['ENV'][:-1]
|
|
env = group['ENV'][:-1]
|
|
|
return bool(os.getenv(env, group['VAL']))
|
|
return bool(os.getenv(env, group['VAL']))
|
|
|
return False
|
|
return False
|
|
|
except:
|
|
except:
|
|
|
return bool(self.config[key])
|
|
return bool(self.config[key])
|
|
|
-
|
|
|
|
|
-def readYaml(path:str = 'application.yml', profile:str = None) -> YamlConfig:
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def readYaml(path: str = 'application.yml', profile: str = None) -> YamlConfig:
|
|
|
if os.path.exists(path):
|
|
if os.path.exists(path):
|
|
|
with open(path) as fd:
|
|
with open(path) as fd:
|
|
|
conf = yaml.load(fd, Loader=yaml.FullLoader)
|
|
conf = yaml.load(fd, Loader=yaml.FullLoader)
|
|
|
-
|
|
|
|
|
- if profile != None:
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if profile is not None:
|
|
|
result = path.split('.')
|
|
result = path.split('.')
|
|
|
profiledYaml = f'{result[0]}-{profile}.{result[1]}'
|
|
profiledYaml = f'{result[0]}-{profile}.{result[1]}'
|
|
|
if os.path.exists(profiledYaml):
|
|
if os.path.exists(profiledYaml):
|
|
|
with open(profiledYaml) as fd:
|
|
with open(profiledYaml) as fd:
|
|
|
conf.update(yaml.load(fd, Loader=yaml.FullLoader))
|
|
conf.update(yaml.load(fd, Loader=yaml.FullLoader))
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return YamlConfig(conf)
|
|
return YamlConfig(conf)
|
|
|
|
|
|
|
|
# res = readYaml()
|
|
# res = readYaml()
|
|
@@ -71,4 +75,4 @@ def readYaml(path:str = 'application.yml', profile:str = None) -> YamlConfig:
|
|
|
# username = mysqlYaml.get("username").split(':')[-1][:-1]
|
|
# username = mysqlYaml.get("username").split(':')[-1][:-1]
|
|
|
# password = mysqlYaml.get("password").split(':')[-1][:-1]
|
|
# password = mysqlYaml.get("password").split(':')[-1][:-1]
|
|
|
# mysql_db = mysqlYaml.get("db").split(':')[-1][:-1]
|
|
# mysql_db = mysqlYaml.get("db").split(':')[-1][:-1]
|
|
|
-# print(host,port,username,password)
|
|
|
|
|
|
|
+# print(host,port,username,password)
|