feat: configuration

This commit is contained in:
superobk
2024-04-26 10:51:10 +08:00
parent f8f75e11ff
commit d493bd221f
4 changed files with 99 additions and 3 deletions

47
src/configuration.py Normal file
View File

@ -0,0 +1,47 @@
from dataclasses import dataclass
from injector import Injector, inject
import yaml
import sys
class Configuration():
@inject
def __init__(self) -> None:
config_file_path = ""
try:
config_file_path = sys.argv[1]
except:
config_file_path = ".env.yaml"
with open(config_file_path) as f:
cfg = yaml.load(f, Loader=yaml.FullLoader)
self.cfg = cfg
def getDict(self):
return self.cfg
"""
# yaml 檔中的路徑 get("aaa.bbb.ccc")
aaa:
bbb:
ccc: "hello world"
"""
def get(self, path: str | list[str], cfg: dict = None):
if isinstance(path, str):
if cfg is None:
cfg = self.cfg
return self.get(path.split("."), cfg)
lenght = len(path)
if lenght == 0 or not isinstance(cfg, dict):
return None
if lenght == 1:
return cfg.get(path[0])
return self.get(path[1:], cfg.get(path[0]))
class TesouConf():
url: str
@inject
def __init__(self,config: Configuration) -> None:
self.url = config.get("tesou.url")