From ee877e8c97281eb6b69075663d00c4293fd33d9b Mon Sep 17 00:00:00 2001 From: hsz1273327 Date: Wed, 13 Jan 2021 23:47:24 +0800 Subject: [PATCH] update --- Changelog.md | 6 ++++++ README.md | 2 ++ schema_entry/entrypoint.py | 36 +++++++++++++++++++++++---------- schema_entry/entrypoint_base.py | 3 +++ setup.cfg | 2 +- test_config2.json | 5 +++++ tests/test_entrypoint.py | 19 +++++++++++++++++ 7 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 test_config2.json diff --git a/Changelog.md b/Changelog.md index b19300c..79841cc 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,9 @@ +# 0.08 + +## 新特性 + ++ 可以通过设置`load_all_config_file = True`来按设定顺序读取全部预设的配置文件位置 + # 0.0.7 ## 新特性 diff --git a/README.md b/README.md index 462dc30..f4a8c60 100644 --- a/README.md +++ b/README.md @@ -349,6 +349,8 @@ class Test_A(EntryPoint): 我们也可以通过字段`config_file_only_get_need`定义从配置文件中读取配置的行为(默认为`True`), 当置为`True`时我们只会在配置文件中读取schema中定义的字段,否则则会加载全部字段. +也可以通过设置`load_all_config_file = True`来按设定顺序读取全部预设的配置文件位置 + 默认配置文件地址是一个列表,会按顺序查找读取,只要找到了满足条件的配置文件就会读取. ```python diff --git a/schema_entry/entrypoint.py b/schema_entry/entrypoint.py index 6a169da..4ddcfb1 100644 --- a/schema_entry/entrypoint.py +++ b/schema_entry/entrypoint.py @@ -38,6 +38,7 @@ class EntryPoint(EntryPointABC): default_config_file_paths: List[str] = [] config_file_only_get_need = True + load_all_config_file = False env_prefix = None parse_env = True @@ -250,18 +251,31 @@ def parse_yaml_configfile_args(self, p: Path) -> Dict[str, Any]: def parse_configfile_args(self) -> Dict[str, Any]: if not self.default_config_file_paths: return {} - for p_str in self.default_config_file_paths: - p = Path(p_str) - if p.is_file(): - if p.suffix == ".json": - return self.parse_json_configfile_args(p) - elif p.suffix == ".yml": - return self.parse_yaml_configfile_args(p) - else: - warnings.warn(f"跳过不支持的配置格式的文件{str(p)}") + if not self.load_all_config_file: + for p_str in self.default_config_file_paths: + p = Path(p_str) + if p.is_file(): + if p.suffix == ".json": + return self.parse_json_configfile_args(p) + elif p.suffix == ".yml": + return self.parse_yaml_configfile_args(p) + else: + warnings.warn(f"跳过不支持的配置格式的文件{str(p)}") + else: + warnings.warn("配置文件的指定路径都不可用.") + return {} else: - warnings.warn("配置文件的指定路径都不可用.") - return {} + result = {} + for p_str in self.default_config_file_paths: + p = Path(p_str) + if p.is_file(): + if p.suffix == ".json": + result.update(self.parse_json_configfile_args(p)) + elif p.suffix == ".yml": + result.update(self.parse_yaml_configfile_args(p)) + else: + warnings.warn(f"跳过不支持的配置格式的文件{str(p)}") + return result def validat_config(self) -> bool: if self.verify_schema: diff --git a/schema_entry/entrypoint_base.py b/schema_entry/entrypoint_base.py index 3d002f2..aeb3c64 100644 --- a/schema_entry/entrypoint_base.py +++ b/schema_entry/entrypoint_base.py @@ -14,6 +14,8 @@ class EntryPointABC(abc.ABC): schema (Optional[Dict[str, Any]]): 入口节点的设置需要满足的json schema对应字典.Default None verify_schema (bool): 获得设置后节点是否校验设置是否满足定义的json schema模式 default_config_file_paths (Sequence[str]): 设置默认的配置文件位置. + config_file_only_get_need (bool): 设置是否只从配置文件中获取schema中定义的配置项 + load_all_config_file (bool): 设置的默认配置文件全部加载. env_prefix (str): 设置环境变量的前缀 parse_env (bool): 展示是否解析环境变量 argparse_check_required (bool): 命令行参数是否解析必填项为必填项 @@ -30,6 +32,7 @@ class EntryPointABC(abc.ABC): default_config_file_paths: Sequence[str] config_file_only_get_need: bool + load_all_config_file: bool env_prefix: Optional[str] parse_env: bool argparse_check_required: bool diff --git a/setup.cfg b/setup.cfg index cb95ee5..5b8c1e2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = schema_entry -version = 0.0.7 +version = 0.0.8 url = https://github.com/Python-Tools/schema_entry author = hsz author_email = hsz1273327@gmail.com diff --git a/test_config2.json b/test_config2.json new file mode 100644 index 0000000..6fe83be --- /dev/null +++ b/test_config2.json @@ -0,0 +1,5 @@ +{ + "a": 1, + "d": 43, + "c": 13 +} \ No newline at end of file diff --git a/tests/test_entrypoint.py b/tests/test_entrypoint.py index e552568..379ad5b 100644 --- a/tests/test_entrypoint.py +++ b/tests/test_entrypoint.py @@ -224,6 +224,25 @@ def _(a: int) -> None: "a": 1 }) + def test_load_all_configfile(self) -> None: + class Test_AC(EntryPoint): + load_all_config_file = True + default_config_file_paths = [ + "./test_config.json", + "./test_config1.json", + "./test_config2.json" + ] + root = Test_AC() + + @root.as_main + def _(a: int, b: int, c: int, d: int) -> None: + assert a == 1 + assert b == 2 + assert c == 13 + assert d == 43 + + root([]) + def test_load_ENV_config(self) -> None: class Test_A(EntryPoint): env_prefix = "app"