diff --git a/app/config.py b/app/config.py index 29e4a83..4374528 100644 --- a/app/config.py +++ b/app/config.py @@ -1,48 +1,33 @@ -from typing import Union -from os import path as osp import os -import importlib -import time +import json5 +import app.env as env class Config: - def __init__(self,conf:Union[dict,str]): - - if type(conf) == dict: - # 杈撳叆鏄瓧鍏告椂 - - self._conf_Dict = conf - self.__name__ = '' - else: - ConfList = conf.split('://') - FileFormat = ConfList[0] - FilePath = ConfList[1] - - if FileFormat not in ['json','yaml','py','env']: - # 鍒ゆ柇鏂囦欢鏍煎紡 - raise NameError(f'Unsupported file format:{FileFormat}') - - if not osp.exists(FilePath) and FileFormat not in ['env','py']: - # 鍒ゆ柇鏂囦欢鏄惁瀛樺湪 - raise FileExistsError('The configuration file path does not exist.') - - self.__name__ = osp.splitext(osp.basename(FilePath))[0] - - self._conf_Dict = self._readConf(FilePath,FileFormat) - + def __init__(self): + self._conf_Dict = json5.load(os.path.join(env.dirs.user_config_path, "config.json"), encoding="utf-8") + self.__name__ = "" self.update() - + def __getattr__(self, name): if name in self._conf_Dict: return self._conf_Dict[name] else: - raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'") - - def update(self,conf:Union[dict, None] = None): - if conf: - self._conf_Dict = conf - # 鏇存柊鍒楄〃 - for k,v in self._conf_Dict.items(): - setattr(self,k,v) - self.__updateTime__ = time.time() + raise AttributeError( + f"'{self.__class__.__name__}' object has no attribute '{name}'" + ) -c = Config('env://MY:dotenv') \ No newline at end of file + def update(self): + for k, v in self._conf_Dict.items(): + if isinstance(v, dict): + setattr(self, k, Config(v)) + elif isinstance(v, list): + setattr( + self, + k, + [Config(item) if isinstance(item, dict) else item for item in v], + ) + else: + setattr(self, k, v) + + def __str__(self): + return str(self._conf_Dict) diff --git a/app/endponit/HuTao.py b/app/endponit/HuTao.py new file mode 100644 index 0000000..e69de29 diff --git a/app/endponit/__init__.py b/app/endponit/__init__.py new file mode 100644 index 0000000..69a5e8e --- /dev/null +++ b/app/endponit/__init__.py @@ -0,0 +1 @@ +from .HuTao import * \ No newline at end of file diff --git a/app/env.py b/app/env.py index 2a51bac..6d17acd 100644 --- a/app/env.py +++ b/app/env.py @@ -8,6 +8,10 @@ def init_dirs(): os.makedirs(dirs.user_log_path, exist_ok=True) os.makedirs(dirs.user_data_path, exist_ok=True) + if not os.path.exists(os.path.join(dirs.user_config_path, "config.json")): + with open(os.path.join(dirs.user_config_path, "config.json"), "w") as f: + f.write("{}") + def init(): init_dirs() diff --git a/requirements.txt b/requirements.txt index a26654b..4d298bd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pyqt5==5.15.11 PyQt-Fluent-Widgets[full]==1.6.3 -platformdirs==4.3.1 \ No newline at end of file +platformdirs==4.3.1 +json5==0.9.25 \ No newline at end of file