Skip to content

Commit 7518bf4

Browse files
committed
Merge branch 'dev'
2 parents 9932c99 + ee877e8 commit 7518bf4

File tree

7 files changed

+61
-12
lines changed

7 files changed

+61
-12
lines changed

Changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 0.08
2+
3+
## 新特性
4+
5+
+ 可以通过设置`load_all_config_file = True`来按设定顺序读取全部预设的配置文件位置
6+
17
# 0.0.7
28

39
## 新特性

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ class Test_A(EntryPoint):
349349
我们也可以通过字段`config_file_only_get_need`定义从配置文件中读取配置的行为(默认为`True`),
350350
当置为`True`时我们只会在配置文件中读取schema中定义的字段,否则则会加载全部字段.
351351

352+
也可以通过设置`load_all_config_file = True`来按设定顺序读取全部预设的配置文件位置
353+
352354
默认配置文件地址是一个列表,会按顺序查找读取,只要找到了满足条件的配置文件就会读取.
353355

354356
```python

schema_entry/entrypoint.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class EntryPoint(EntryPointABC):
3838

3939
default_config_file_paths: List[str] = []
4040
config_file_only_get_need = True
41+
load_all_config_file = False
4142
env_prefix = None
4243
parse_env = True
4344

@@ -250,18 +251,31 @@ def parse_yaml_configfile_args(self, p: Path) -> Dict[str, Any]:
250251
def parse_configfile_args(self) -> Dict[str, Any]:
251252
if not self.default_config_file_paths:
252253
return {}
253-
for p_str in self.default_config_file_paths:
254-
p = Path(p_str)
255-
if p.is_file():
256-
if p.suffix == ".json":
257-
return self.parse_json_configfile_args(p)
258-
elif p.suffix == ".yml":
259-
return self.parse_yaml_configfile_args(p)
260-
else:
261-
warnings.warn(f"跳过不支持的配置格式的文件{str(p)}")
254+
if not self.load_all_config_file:
255+
for p_str in self.default_config_file_paths:
256+
p = Path(p_str)
257+
if p.is_file():
258+
if p.suffix == ".json":
259+
return self.parse_json_configfile_args(p)
260+
elif p.suffix == ".yml":
261+
return self.parse_yaml_configfile_args(p)
262+
else:
263+
warnings.warn(f"跳过不支持的配置格式的文件{str(p)}")
264+
else:
265+
warnings.warn("配置文件的指定路径都不可用.")
266+
return {}
262267
else:
263-
warnings.warn("配置文件的指定路径都不可用.")
264-
return {}
268+
result = {}
269+
for p_str in self.default_config_file_paths:
270+
p = Path(p_str)
271+
if p.is_file():
272+
if p.suffix == ".json":
273+
result.update(self.parse_json_configfile_args(p))
274+
elif p.suffix == ".yml":
275+
result.update(self.parse_yaml_configfile_args(p))
276+
else:
277+
warnings.warn(f"跳过不支持的配置格式的文件{str(p)}")
278+
return result
265279

266280
def validat_config(self) -> bool:
267281
if self.verify_schema:

schema_entry/entrypoint_base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class EntryPointABC(abc.ABC):
1414
schema (Optional[Dict[str, Any]]): 入口节点的设置需要满足的json schema对应字典.Default None
1515
verify_schema (bool): 获得设置后节点是否校验设置是否满足定义的json schema模式
1616
default_config_file_paths (Sequence[str]): 设置默认的配置文件位置.
17+
config_file_only_get_need (bool): 设置是否只从配置文件中获取schema中定义的配置项
18+
load_all_config_file (bool): 设置的默认配置文件全部加载.
1719
env_prefix (str): 设置环境变量的前缀
1820
parse_env (bool): 展示是否解析环境变量
1921
argparse_check_required (bool): 命令行参数是否解析必填项为必填项
@@ -30,6 +32,7 @@ class EntryPointABC(abc.ABC):
3032

3133
default_config_file_paths: Sequence[str]
3234
config_file_only_get_need: bool
35+
load_all_config_file: bool
3336
env_prefix: Optional[str]
3437
parse_env: bool
3538
argparse_check_required: bool

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = schema_entry
3-
version = 0.0.7
3+
version = 0.0.8
44
url = https://github.com/Python-Tools/schema_entry
55
author = hsz
66
author_email = hsz1273327@gmail.com

test_config2.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"a": 1,
3+
"d": 43,
4+
"c": 13
5+
}

tests/test_entrypoint.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,25 @@ def _(a: int) -> None:
224224
"a": 1
225225
})
226226

227+
def test_load_all_configfile(self) -> None:
228+
class Test_AC(EntryPoint):
229+
load_all_config_file = True
230+
default_config_file_paths = [
231+
"./test_config.json",
232+
"./test_config1.json",
233+
"./test_config2.json"
234+
]
235+
root = Test_AC()
236+
237+
@root.as_main
238+
def _(a: int, b: int, c: int, d: int) -> None:
239+
assert a == 1
240+
assert b == 2
241+
assert c == 13
242+
assert d == 43
243+
244+
root([])
245+
227246
def test_load_ENV_config(self) -> None:
228247
class Test_A(EntryPoint):
229248
env_prefix = "app"

0 commit comments

Comments
 (0)