Skip to content

Commit

Permalink
feat(config): update conf and wrapper
Browse files Browse the repository at this point in the history
- 新增 环境变量自动转写
- 新增 Conf 模块缓存
- 修复 Conf 模块配置加载不全问题
  • Loading branch information
txperl committed Nov 8, 2021
1 parent aac937e commit 96f0345
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ PixivBiu is a nice Pixiv **assistant** tool.

* Install dependencies, run `pip install -r requirements.txt`
+ [Flask](https://github.com/pallets/flask)
+ [requirements](https://github.com/psf/requests)
+ [requests](https://github.com/psf/requests)
+ [PyYAML](https://github.com/yaml/pyyaml)
+ [Pillow](https://github.com/python-pillow/Pillow)
+ [PixivPy](https://github.com/upbit/pixivpy)
Expand Down
58 changes: 36 additions & 22 deletions app/lib/ins/conf/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def __init__(self):
self.rootConfigFolderPath = self.getENV("rootPathFrozen") + "app/config/"
self.customConfigPath = self.getENV("rootPath") + "config.yml"
self._configs = {}
self._dictWrapper = {}
self.load_config()

def load_config(self):
Expand All @@ -18,31 +19,52 @@ def load_config(self):
Then, load or create the user's customized config file(./config.yml).
:return: none
"""
self._configs.update({"config": ConfigWrapper(path=self.customConfigPath, error=False)})
for fileName in os.listdir(self.rootConfigFolderPath):
if ".yml" in fileName or ".yaml" in fileName:
fileName_ = ".".join(fileName.split(".")[:-1])
self._configs.update({fileName_: ConfigWrapper(self.rootConfigFolderPath + fileName)})
self._configs.update({"config": ConfigWrapper(path=self.customConfigPath, error=False)})
self.dict(fileName_, wrapper=True, reload=True)

def dict(self, configName: str, flat=False):
def dict(self, configName: str, flat=False, wrapper=False, reload=False):
"""
Export the final dict data that includes customized and default configs.
Of course, if the customized setting item conflicts with the default, only the customized one is retained.
Environment > Custom > Default.
:param configName: name of config file(the same with "./app/config/xxx.yml"), customized one is called "config"
:param flat: weather to be formatted as a flat-like dict
:param flat: weather to return a flat-like dict
:param wrapper: weather to return a wrapper object
:param reload: weather to reload
:return: final config dict
"""
defaultDic = self.get_wrapper(configName)
if defaultDic is None:
return None
finalDic = ConfigWrapper(config=defaultDic.dict(), error=False)
for key in defaultDic.format2flat():
maybe = self.get("config", key, default=ConfigWrapper.SIGN_EMPTY)
if maybe != ConfigWrapper.SIGN_EMPTY:
finalDic.set(key, maybe)
if self._dictWrapper.get(configName) is None or reload is True:
# load default config
defaultDic = self.get_wrapper(configName)
if defaultDic is None:
return None
# generate final config wrapper
finalDic = ConfigWrapper(config=defaultDic.dict(), error=False)
for key in defaultDic.format2flat():
maybe = ConfigWrapper.SIGN_EMPTY
# load environment variable config
envVar = os.environ.get(key, ConfigWrapper.SIGN_EMPTY)
if envVar != ConfigWrapper.SIGN_EMPTY:
maybe = ConfigWrapper.literal_eval(envVar)
else:
# load customized config
customVar = self.get_wrapper("config").get(key, default=ConfigWrapper.SIGN_EMPTY)
if customVar != ConfigWrapper.SIGN_EMPTY:
maybe = customVar
if maybe != ConfigWrapper.SIGN_EMPTY:
finalDic.set(key, maybe)
self._dictWrapper[configName] = finalDic
else:
# load the cache
finalDic = self._dictWrapper[configName]
if flat is True:
return finalDic.format2flat()
if wrapper is True:
return finalDic
return finalDic.dict()

def get_wrapper(self, configName: str, default=None):
Expand Down Expand Up @@ -81,18 +103,10 @@ def get(self, configName: str, key: str, default=None):
:param default: the default value returned if the key doesn't exist
:return: the value of setting item
"""
envVar = os.environ.get(key, ConfigWrapper.SIGN_EMPTY)
if envVar != ConfigWrapper.SIGN_EMPTY:
if envVar in ("true", "false"):
envVar = True if envVar == "true" else False
return envVar
defaultDic = self.get_wrapper(configName)
if defaultDic is None:
dic = self.dict(configName, wrapper=True)
if dic is None:
return default
customRst = self.get_wrapper("config").get(key)
if customRst is None:
return defaultDic.get(key, default)
return customRst
return dic.get(key, default=default)

def set(self, key: str, value: any):
"""
Expand Down
12 changes: 12 additions & 0 deletions app/lib/ins/conf/wrapper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import errno

from altfe.interface.root import interRoot
Expand Down Expand Up @@ -118,3 +119,14 @@ def format2dict(configFlat):
now.update({subKey: {}})
now = now[subKey]
return r

@staticmethod
def literal_eval(_: str):
if _ == "false":
return False
if _ == "true":
return True
try:
return ast.literal_eval(_)
except:
return _
2 changes: 1 addition & 1 deletion app/lib/ins/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class InsI18n(classRoot):
def __init__(self):
self.rootLangFolderPath = self.getENV("rootPathFrozen") + "app/config/language/"
self.langCode = self.__deter_lang_code()
self._lang = ConfigWrapper(self.rootLangFolderPath + self.langCode + ".yml")
self._lang = ConfigWrapper(self.rootLangFolderPath + self.langCode + ".yml", error=False)

def __deter_lang_code(self, code_=None):
"""
Expand Down

0 comments on commit 96f0345

Please sign in to comment.