FovesConfig 0.1.1__tar.gz
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.
- fovesconfig-0.1.1/.gitignore +222 -0
- fovesconfig-0.1.1/FovesConfig/__init__.py +1 -0
- fovesconfig-0.1.1/FovesConfig/loader.py +29 -0
- fovesconfig-0.1.1/LICENSE +21 -0
- fovesconfig-0.1.1/PKG-INFO +94 -0
- fovesconfig-0.1.1/README.md +62 -0
- fovesconfig-0.1.1/pyproject.toml +23 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
.ilina
|
|
2
|
+
mygo.py
|
|
3
|
+
temp
|
|
4
|
+
|
|
5
|
+
# Byte-compiled / optimized / DLL files
|
|
6
|
+
__pycache__/
|
|
7
|
+
*.py[codz]
|
|
8
|
+
*$py.class
|
|
9
|
+
|
|
10
|
+
# C extensions
|
|
11
|
+
*.so
|
|
12
|
+
|
|
13
|
+
# Distribution / packaging
|
|
14
|
+
.Python
|
|
15
|
+
build/
|
|
16
|
+
develop-eggs/
|
|
17
|
+
dist/
|
|
18
|
+
downloads/
|
|
19
|
+
eggs/
|
|
20
|
+
.eggs/
|
|
21
|
+
lib/
|
|
22
|
+
lib64/
|
|
23
|
+
parts/
|
|
24
|
+
sdist/
|
|
25
|
+
var/
|
|
26
|
+
wheels/
|
|
27
|
+
share/python-wheels/
|
|
28
|
+
*.egg-info/
|
|
29
|
+
.installed.cfg
|
|
30
|
+
*.egg
|
|
31
|
+
MANIFEST
|
|
32
|
+
|
|
33
|
+
# PyInstaller
|
|
34
|
+
# Usually these files are written by a python script from a template
|
|
35
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
36
|
+
*.manifest
|
|
37
|
+
*.spec
|
|
38
|
+
|
|
39
|
+
# Installer logs
|
|
40
|
+
pip-log.txt
|
|
41
|
+
pip-delete-this-directory.txt
|
|
42
|
+
|
|
43
|
+
# Unit test / coverage reports
|
|
44
|
+
htmlcov/
|
|
45
|
+
.tox/
|
|
46
|
+
.nox/
|
|
47
|
+
.coverage
|
|
48
|
+
.coverage.*
|
|
49
|
+
.cache
|
|
50
|
+
nosetests.xml
|
|
51
|
+
coverage.xml
|
|
52
|
+
*.cover
|
|
53
|
+
*.py.cover
|
|
54
|
+
.hypothesis/
|
|
55
|
+
.pytest_cache/
|
|
56
|
+
cover/
|
|
57
|
+
|
|
58
|
+
# Translations
|
|
59
|
+
*.mo
|
|
60
|
+
*.pot
|
|
61
|
+
|
|
62
|
+
# Django stuff:
|
|
63
|
+
*.log
|
|
64
|
+
local_settings.py
|
|
65
|
+
db.sqlite3
|
|
66
|
+
db.sqlite3-journal
|
|
67
|
+
|
|
68
|
+
# Flask stuff:
|
|
69
|
+
instance/
|
|
70
|
+
.webassets-cache
|
|
71
|
+
|
|
72
|
+
# Scrapy stuff:
|
|
73
|
+
.scrapy
|
|
74
|
+
|
|
75
|
+
# Sphinx documentation
|
|
76
|
+
docs/_build/
|
|
77
|
+
|
|
78
|
+
# PyBuilder
|
|
79
|
+
.pybuilder/
|
|
80
|
+
target/
|
|
81
|
+
|
|
82
|
+
# Jupyter Notebook
|
|
83
|
+
.ipynb_checkpoints
|
|
84
|
+
|
|
85
|
+
# IPython
|
|
86
|
+
profile_default/
|
|
87
|
+
ipython_config.py
|
|
88
|
+
|
|
89
|
+
# pyenv
|
|
90
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
91
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
92
|
+
# .python-version
|
|
93
|
+
|
|
94
|
+
# pipenv
|
|
95
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
96
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
97
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
98
|
+
# install all needed dependencies.
|
|
99
|
+
# Pipfile.lock
|
|
100
|
+
|
|
101
|
+
# UV
|
|
102
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
103
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
104
|
+
# commonly ignored for libraries.
|
|
105
|
+
# uv.lock
|
|
106
|
+
|
|
107
|
+
# poetry
|
|
108
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
109
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
110
|
+
# commonly ignored for libraries.
|
|
111
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
112
|
+
# poetry.lock
|
|
113
|
+
# poetry.toml
|
|
114
|
+
|
|
115
|
+
# pdm
|
|
116
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
117
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
118
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
119
|
+
# pdm.lock
|
|
120
|
+
# pdm.toml
|
|
121
|
+
.pdm-python
|
|
122
|
+
.pdm-build/
|
|
123
|
+
|
|
124
|
+
# pixi
|
|
125
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
126
|
+
# pixi.lock
|
|
127
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
128
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
129
|
+
.pixi
|
|
130
|
+
|
|
131
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
132
|
+
__pypackages__/
|
|
133
|
+
|
|
134
|
+
# Celery stuff
|
|
135
|
+
celerybeat-schedule
|
|
136
|
+
celerybeat.pid
|
|
137
|
+
|
|
138
|
+
# Redis
|
|
139
|
+
*.rdb
|
|
140
|
+
*.aof
|
|
141
|
+
*.pid
|
|
142
|
+
|
|
143
|
+
# RabbitMQ
|
|
144
|
+
mnesia/
|
|
145
|
+
rabbitmq/
|
|
146
|
+
rabbitmq-data/
|
|
147
|
+
|
|
148
|
+
# ActiveMQ
|
|
149
|
+
activemq-data/
|
|
150
|
+
|
|
151
|
+
# SageMath parsed files
|
|
152
|
+
*.sage.py
|
|
153
|
+
|
|
154
|
+
# Environments
|
|
155
|
+
.env
|
|
156
|
+
.envrc
|
|
157
|
+
.venv
|
|
158
|
+
env/
|
|
159
|
+
venv/
|
|
160
|
+
ENV/
|
|
161
|
+
env.bak/
|
|
162
|
+
venv.bak/
|
|
163
|
+
|
|
164
|
+
# Spyder project settings
|
|
165
|
+
.spyderproject
|
|
166
|
+
.spyproject
|
|
167
|
+
|
|
168
|
+
# Rope project settings
|
|
169
|
+
.ropeproject
|
|
170
|
+
|
|
171
|
+
# mkdocs documentation
|
|
172
|
+
/site
|
|
173
|
+
|
|
174
|
+
# mypy
|
|
175
|
+
.mypy_cache/
|
|
176
|
+
.dmypy.json
|
|
177
|
+
dmypy.json
|
|
178
|
+
|
|
179
|
+
# Pyre type checker
|
|
180
|
+
.pyre/
|
|
181
|
+
|
|
182
|
+
# pytype static type analyzer
|
|
183
|
+
.pytype/
|
|
184
|
+
|
|
185
|
+
# Cython debug symbols
|
|
186
|
+
cython_debug/
|
|
187
|
+
|
|
188
|
+
# PyCharm
|
|
189
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
190
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
191
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
192
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
193
|
+
# .idea/
|
|
194
|
+
|
|
195
|
+
# Abstra
|
|
196
|
+
# Abstra is an AI-powered process automation framework.
|
|
197
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
198
|
+
# Learn more at https://abstra.io/docs
|
|
199
|
+
.abstra/
|
|
200
|
+
|
|
201
|
+
# Visual Studio Code
|
|
202
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
203
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
204
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
205
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
206
|
+
# .vscode/
|
|
207
|
+
# Temporary file for partial code execution
|
|
208
|
+
tempCodeRunnerFile.py
|
|
209
|
+
|
|
210
|
+
# Ruff stuff:
|
|
211
|
+
.ruff_cache/
|
|
212
|
+
|
|
213
|
+
# PyPI configuration file
|
|
214
|
+
.pypirc
|
|
215
|
+
|
|
216
|
+
# Marimo
|
|
217
|
+
marimo/_static/
|
|
218
|
+
marimo/_lsp/
|
|
219
|
+
__marimo__/
|
|
220
|
+
|
|
221
|
+
# Streamlit
|
|
222
|
+
.streamlit/secrets.toml
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .loader import ConfigLoader
|
|
@@ -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,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.
|
|
@@ -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,62 @@
|
|
|
1
|
+
# FovesConfig
|
|
2
|
+
|
|
3
|
+
基于 Pydantic 的配置管理工具,上下文管理器自动读写 JSON 配置文件。
|
|
4
|
+
|
|
5
|
+
可使用 pip 安装或下载源码使用:
|
|
6
|
+
```BASH
|
|
7
|
+
pip install FovesConfig
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
项目主页:
|
|
11
|
+
https://github.com/Foves7017/FovesLib
|
|
12
|
+
|
|
13
|
+
## ConfigLoader
|
|
14
|
+
|
|
15
|
+
泛型配置加载器。把你的配置定义成一个 Pydantic 模型,然后用 `ConfigLoader` 包裹它——进入上下文自动读取 JSON,退出时自动写回,省去所有手工序列化的麻烦。
|
|
16
|
+
|
|
17
|
+
### 基本用法
|
|
18
|
+
|
|
19
|
+
```Python
|
|
20
|
+
from pydantic import BaseModel
|
|
21
|
+
from FovesConfig import ConfigLoader
|
|
22
|
+
|
|
23
|
+
class AppConfig(BaseModel):
|
|
24
|
+
host: str = "localhost"
|
|
25
|
+
port: int = 8080
|
|
26
|
+
debug: bool = False
|
|
27
|
+
|
|
28
|
+
with ConfigLoader("config.json", AppConfig) as cfg:
|
|
29
|
+
print(cfg.host) # 首次运行会使用默认值
|
|
30
|
+
cfg.port = 9090 # 修改配置
|
|
31
|
+
# 退出上下文时自动写入 config.json
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### 参数
|
|
35
|
+
|
|
36
|
+
`path: str | Path` — 配置文件路径。文件存在则读取,不存在则使用模型默认值。
|
|
37
|
+
|
|
38
|
+
`model: Type[T]` — 一个 Pydantic BaseModel 子类,定义了配置的结构和默认值。
|
|
39
|
+
|
|
40
|
+
### 只读
|
|
41
|
+
你可以使用如下的方法只读取磁盘上的配置,在此例中,就算修改 cfg 的值也不会同步到磁盘中。
|
|
42
|
+
|
|
43
|
+
```Python
|
|
44
|
+
loader = ConfigLoader("config.json", AppConfig)
|
|
45
|
+
cfg = loader.readonly()
|
|
46
|
+
print(cfg.host)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### 读写行为
|
|
50
|
+
|
|
51
|
+
- **读取**:进入上下文管理器时,若文件存在则 `model_validate_json` 反序列化,否则用 `model_cls()` 构造默认实例。
|
|
52
|
+
- **写入**:退出上下文管理器时自动创建父目录,以 `indent=2`、`ensure_ascii=False` 写出 JSON。
|
|
53
|
+
|
|
54
|
+
### 其他
|
|
55
|
+
如果你发现自己需要手动调用 save() 或 exit(),请先考虑是否应该使用上下文管理器。大多数修改配置的场景都更适合:
|
|
56
|
+
|
|
57
|
+
```Python
|
|
58
|
+
with ConfigLoader(...) as config:
|
|
59
|
+
...
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
而不是手动管理配置对象的生命周期。
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "FovesConfig"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "四叶的配置管理工具 — 基于 Pydantic 的配置加载器,上下文管理器自动读写"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = {file = "LICENSE"}
|
|
11
|
+
authors = [
|
|
12
|
+
{name = "Foves7017"}
|
|
13
|
+
]
|
|
14
|
+
requires-python = ">=3.12"
|
|
15
|
+
dependencies = [
|
|
16
|
+
"pydantic>=2.0.0",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.urls]
|
|
20
|
+
Repository = "https://github.com/Foves7017/FovesLib"
|
|
21
|
+
|
|
22
|
+
[tool.hatch.build.targets.wheel]
|
|
23
|
+
packages = ["FovesConfig"]
|