charley преди 2 седмици
родител
ревизия
5d153c52e9
променени са 1 файла, в които са добавени 19 реда и са изтрити 15 реда
  1. 19 15
      cgc_spider/YamlLoader.py

+ 19 - 15
cgc_spider/YamlLoader.py

@@ -1,22 +1,25 @@
 # -*- 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\.]+)\}$')
+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):
+    
+    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:
+            if group['ENV'] is not None:
                 env = group['ENV'][:-1]
                 return os.getenv(env, group['VAL'])
             return None
@@ -27,36 +30,37 @@ class YamlConfig:
         try:
             match = regex.match(self.config[key])
             group = match.groupdict()
-            if group['ENV'] != None:
+            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, env: str = None):
+    
+    def getValueAsBool(self, key: str):
         try:
             match = regex.match(self.config[key])
             group = match.groupdict()
-            if group['ENV'] != None:
+            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:
+
+
+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:
+    
+    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()
@@ -71,4 +75,4 @@ def readYaml(path:str = 'application.yml', profile:str = None) -> YamlConfig:
 # 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)
+# print(host,port,username,password)