mirror of
https://github.com/BoardWare-Genius/jarvis-models.git
synced 2025-12-13 16:53:24 +00:00
feat: configuration
This commit is contained in:
47
src/configuration.py
Normal file
47
src/configuration.py
Normal 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")
|
||||
Reference in New Issue
Block a user