FovesConfig 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ from .loader import ConfigLoader
FovesConfig/loader.py ADDED
@@ -0,0 +1,29 @@
1
+ from typing import Type
2
+ from pathlib import Path
3
+ from pydantic import BaseModel
4
+
5
+ class ConfigLoader[T: BaseModel]:
6
+ def __init__(self, path: str|Path, model: Type[T]):
7
+ self.path = Path(path)
8
+ self.model_cls = model
9
+
10
+ def _read(self):
11
+ if self.path.exists():
12
+ self.model = self.model_cls.model_validate_json(self.path.read_text("UTF8"))
13
+ else:
14
+ self.model = self.model_cls()
15
+
16
+ def readonly(self) -> T:
17
+ self._read()
18
+ return self.model
19
+
20
+ def __enter__(self) -> T:
21
+ self._read()
22
+ return self.model
23
+
24
+ def __exit__(self, exc_type, exc, tb):
25
+ self.path.parent.mkdir(parents=True, exist_ok=True)
26
+ self.path.write_text(
27
+ self.model.model_dump_json(indent=2, ensure_ascii=False),
28
+ encoding="utf-8",
29
+ )
@@ -0,0 +1,94 @@
1
+ Metadata-Version: 2.4
2
+ Name: FovesConfig
3
+ Version: 0.1.1
4
+ Summary: 四叶的配置管理工具 — 基于 Pydantic 的配置加载器,上下文管理器自动读写
5
+ Project-URL: Repository, https://github.com/Foves7017/FovesLib
6
+ Author: Foves7017
7
+ License: MIT License
8
+
9
+ Copyright (c) 2026 Foves7017
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+ License-File: LICENSE
29
+ Requires-Python: >=3.12
30
+ Requires-Dist: pydantic>=2.0.0
31
+ Description-Content-Type: text/markdown
32
+
33
+ # FovesConfig
34
+
35
+ 基于 Pydantic 的配置管理工具,上下文管理器自动读写 JSON 配置文件。
36
+
37
+ 可使用 pip 安装或下载源码使用:
38
+ ```BASH
39
+ pip install FovesConfig
40
+ ```
41
+
42
+ 项目主页:
43
+ https://github.com/Foves7017/FovesLib
44
+
45
+ ## ConfigLoader
46
+
47
+ 泛型配置加载器。把你的配置定义成一个 Pydantic 模型,然后用 `ConfigLoader` 包裹它——进入上下文自动读取 JSON,退出时自动写回,省去所有手工序列化的麻烦。
48
+
49
+ ### 基本用法
50
+
51
+ ```Python
52
+ from pydantic import BaseModel
53
+ from FovesConfig import ConfigLoader
54
+
55
+ class AppConfig(BaseModel):
56
+ host: str = "localhost"
57
+ port: int = 8080
58
+ debug: bool = False
59
+
60
+ with ConfigLoader("config.json", AppConfig) as cfg:
61
+ print(cfg.host) # 首次运行会使用默认值
62
+ cfg.port = 9090 # 修改配置
63
+ # 退出上下文时自动写入 config.json
64
+ ```
65
+
66
+ ### 参数
67
+
68
+ `path: str | Path` — 配置文件路径。文件存在则读取,不存在则使用模型默认值。
69
+
70
+ `model: Type[T]` — 一个 Pydantic BaseModel 子类,定义了配置的结构和默认值。
71
+
72
+ ### 只读
73
+ 你可以使用如下的方法只读取磁盘上的配置,在此例中,就算修改 cfg 的值也不会同步到磁盘中。
74
+
75
+ ```Python
76
+ loader = ConfigLoader("config.json", AppConfig)
77
+ cfg = loader.readonly()
78
+ print(cfg.host)
79
+ ```
80
+
81
+ ### 读写行为
82
+
83
+ - **读取**:进入上下文管理器时,若文件存在则 `model_validate_json` 反序列化,否则用 `model_cls()` 构造默认实例。
84
+ - **写入**:退出上下文管理器时自动创建父目录,以 `indent=2`、`ensure_ascii=False` 写出 JSON。
85
+
86
+ ### 其他
87
+ 如果你发现自己需要手动调用 save() 或 exit(),请先考虑是否应该使用上下文管理器。大多数修改配置的场景都更适合:
88
+
89
+ ```Python
90
+ with ConfigLoader(...) as config:
91
+ ...
92
+ ```
93
+
94
+ 而不是手动管理配置对象的生命周期。
@@ -0,0 +1,6 @@
1
+ FovesConfig/__init__.py,sha256=zE5ODSOZMCdZHMsHZ7JcxLxlm7XgaXadpfQyv2JwP54,34
2
+ FovesConfig/loader.py,sha256=uTjfq2WyNBdvShWVwbrSAONvq8Ih52JP-kfpCc7sL3U,861
3
+ fovesconfig-0.1.1.dist-info/METADATA,sha256=tDaHFjtvqB21UM5_RmkJ5w0cwMDWKWjp8bHdWSd5cNY,3487
4
+ fovesconfig-0.1.1.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
5
+ fovesconfig-0.1.1.dist-info/licenses/LICENSE,sha256=lXz0pZeICx_-HWjVeatkJhfuZp5Wd4jOPYzVs57mLfM,1087
6
+ fovesconfig-0.1.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.30.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Foves7017
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.