Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
hsz1273327 committed Jan 14, 2021
2 parents 9932c99 + ee877e8 commit 7518bf4
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 12 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.08

## 新特性

+ 可以通过设置`load_all_config_file = True`来按设定顺序读取全部预设的配置文件位置

# 0.0.7

## 新特性
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ class Test_A(EntryPoint):
我们也可以通过字段`config_file_only_get_need`定义从配置文件中读取配置的行为(默认为`True`),
当置为`True`时我们只会在配置文件中读取schema中定义的字段,否则则会加载全部字段.

也可以通过设置`load_all_config_file = True`来按设定顺序读取全部预设的配置文件位置

默认配置文件地址是一个列表,会按顺序查找读取,只要找到了满足条件的配置文件就会读取.

```python
Expand Down
36 changes: 25 additions & 11 deletions schema_entry/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions schema_entry/entrypoint_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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): 命令行参数是否解析必填项为必填项
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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 = [email protected]
Expand Down
5 changes: 5 additions & 0 deletions test_config2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"a": 1,
"d": 43,
"c": 13
}
19 changes: 19 additions & 0 deletions tests/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 7518bf4

Please sign in to comment.