python-simpleconf 0.9.1__tar.gz → 0.9.2__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.
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/PKG-INFO +55 -2
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/README.md +54 -0
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/pyproject.toml +2 -2
- python_simpleconf-0.9.2/simpleconf/__init__.py +5 -0
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/simpleconf/config.py +7 -1
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/simpleconf/loaders/__init__.py +2 -2
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/simpleconf/utils.py +62 -0
- python_simpleconf-0.9.1/simpleconf/__init__.py +0 -3
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/LICENSE +0 -0
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/simpleconf/caster.py +0 -0
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/simpleconf/exceptions.py +0 -0
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/simpleconf/loaders/dict.py +0 -0
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/simpleconf/loaders/env.py +0 -0
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/simpleconf/loaders/ini.py +0 -0
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/simpleconf/loaders/json.py +0 -0
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/simpleconf/loaders/osenv.py +0 -0
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/simpleconf/loaders/toml.py +0 -0
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/simpleconf/loaders/yaml.py +0 -0
- {python_simpleconf-0.9.1 → python_simpleconf-0.9.2}/simpleconf/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: python-simpleconf
|
|
3
|
-
Version: 0.9.
|
|
3
|
+
Version: 0.9.2
|
|
4
4
|
Summary: Simple configuration management with python.
|
|
5
5
|
License: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -25,7 +25,7 @@ Provides-Extra: liquidpy
|
|
|
25
25
|
Provides-Extra: toml
|
|
26
26
|
Provides-Extra: yaml
|
|
27
27
|
Requires-Dist: aiofiles (>=23.0.0) ; extra == "async"
|
|
28
|
-
Requires-Dist: diot (>=0.3.
|
|
28
|
+
Requires-Dist: diot (>=0.3.3,<0.4.0)
|
|
29
29
|
Requires-Dist: iniconfig (>=2.0,<3.0) ; extra == "ini" or extra == "all"
|
|
30
30
|
Requires-Dist: jinja2 (>=3.0,<4.0) ; extra == "jinja2"
|
|
31
31
|
Requires-Dist: liquidpy (>=0.8,<0.9) ; extra == "liquid" or extra == "liquidpy"
|
|
@@ -350,4 +350,57 @@ conf = Config.load('config.liq.toml')
|
|
|
350
350
|
# conf.section1.a == 1
|
|
351
351
|
# conf.section2.a == 2
|
|
352
352
|
```
|
|
353
|
+
#### Loader directive in the first line
|
|
353
354
|
|
|
355
|
+
Instead of using a special file extension, you can embed a loader directive in the **first line** of a configuration file as a comment. This allows a plain `config.toml` to be processed by the Liquid engine without renaming the file.
|
|
356
|
+
|
|
357
|
+
The directive format is:
|
|
358
|
+
|
|
359
|
+
```
|
|
360
|
+
# simpleconf-loader: <loader>
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
Supported comment prefixes: `#`, `;`, `//` (case-insensitive).
|
|
364
|
+
|
|
365
|
+
The `<loader>` value can be:
|
|
366
|
+
|
|
367
|
+
| Value | Meaning |
|
|
368
|
+
|---|---|
|
|
369
|
+
| `liq` or `liquid` | Use the Liquid loader for this file's format |
|
|
370
|
+
| `j2`, `jinja`, or `jinja2` | Use the Jinja2 loader for this file's format |
|
|
371
|
+
| A full extension (e.g. `toml.liq`) | Use the exact loader specified |
|
|
372
|
+
|
|
373
|
+
**Example — Liquid in a plain `.toml` file:**
|
|
374
|
+
|
|
375
|
+
`config.toml`
|
|
376
|
+
```toml
|
|
377
|
+
# simpleconf-loader: liq
|
|
378
|
+
{% set x = 10 %}
|
|
379
|
+
[default]
|
|
380
|
+
a = {{ 1 + 1 }}
|
|
381
|
+
b = {{ x + 2 }}
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
```python
|
|
385
|
+
from simpleconf import Config
|
|
386
|
+
|
|
387
|
+
conf = Config.load('config.toml')
|
|
388
|
+
# conf.default.a == 2
|
|
389
|
+
# conf.default.b == 12
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
**Example — Jinja2 in a plain `.yaml` file:**
|
|
393
|
+
|
|
394
|
+
`config.yaml`
|
|
395
|
+
```yaml
|
|
396
|
+
# simpleconf-loader: j2
|
|
397
|
+
default:
|
|
398
|
+
a: {{ 1 + 1 }}
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
```python
|
|
402
|
+
from simpleconf import Config
|
|
403
|
+
|
|
404
|
+
conf = Config.load('config.yaml')
|
|
405
|
+
# conf.default.a == 2
|
|
406
|
+
```
|
|
@@ -310,3 +310,57 @@ conf = Config.load('config.liq.toml')
|
|
|
310
310
|
# conf.section1.a == 1
|
|
311
311
|
# conf.section2.a == 2
|
|
312
312
|
```
|
|
313
|
+
#### Loader directive in the first line
|
|
314
|
+
|
|
315
|
+
Instead of using a special file extension, you can embed a loader directive in the **first line** of a configuration file as a comment. This allows a plain `config.toml` to be processed by the Liquid engine without renaming the file.
|
|
316
|
+
|
|
317
|
+
The directive format is:
|
|
318
|
+
|
|
319
|
+
```
|
|
320
|
+
# simpleconf-loader: <loader>
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
Supported comment prefixes: `#`, `;`, `//` (case-insensitive).
|
|
324
|
+
|
|
325
|
+
The `<loader>` value can be:
|
|
326
|
+
|
|
327
|
+
| Value | Meaning |
|
|
328
|
+
|---|---|
|
|
329
|
+
| `liq` or `liquid` | Use the Liquid loader for this file's format |
|
|
330
|
+
| `j2`, `jinja`, or `jinja2` | Use the Jinja2 loader for this file's format |
|
|
331
|
+
| A full extension (e.g. `toml.liq`) | Use the exact loader specified |
|
|
332
|
+
|
|
333
|
+
**Example — Liquid in a plain `.toml` file:**
|
|
334
|
+
|
|
335
|
+
`config.toml`
|
|
336
|
+
```toml
|
|
337
|
+
# simpleconf-loader: liq
|
|
338
|
+
{% set x = 10 %}
|
|
339
|
+
[default]
|
|
340
|
+
a = {{ 1 + 1 }}
|
|
341
|
+
b = {{ x + 2 }}
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
```python
|
|
345
|
+
from simpleconf import Config
|
|
346
|
+
|
|
347
|
+
conf = Config.load('config.toml')
|
|
348
|
+
# conf.default.a == 2
|
|
349
|
+
# conf.default.b == 12
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
**Example — Jinja2 in a plain `.yaml` file:**
|
|
353
|
+
|
|
354
|
+
`config.yaml`
|
|
355
|
+
```yaml
|
|
356
|
+
# simpleconf-loader: j2
|
|
357
|
+
default:
|
|
358
|
+
a: {{ 1 + 1 }}
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
```python
|
|
362
|
+
from simpleconf import Config
|
|
363
|
+
|
|
364
|
+
conf = Config.load('config.yaml')
|
|
365
|
+
# conf.default.a == 2
|
|
366
|
+
```
|
|
@@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"
|
|
|
4
4
|
|
|
5
5
|
[tool.poetry]
|
|
6
6
|
name = "python-simpleconf"
|
|
7
|
-
version = "0.9.
|
|
7
|
+
version = "0.9.2"
|
|
8
8
|
description = "Simple configuration management with python."
|
|
9
9
|
authors = [ "pwwang <pwwang@pwwang.com>",]
|
|
10
10
|
license = "MIT"
|
|
@@ -17,7 +17,7 @@ packages = [
|
|
|
17
17
|
|
|
18
18
|
[tool.poetry.dependencies]
|
|
19
19
|
python = "^3.9"
|
|
20
|
-
diot = "^0.3.
|
|
20
|
+
diot = "^0.3.3"
|
|
21
21
|
python-dotenv = { version="^1.1", optional = true}
|
|
22
22
|
pyyaml = { version="^6", optional = true}
|
|
23
23
|
panpath = "^0.4.8"
|
|
@@ -5,7 +5,7 @@ from typing import Any, List, Generator, Union, Sequence
|
|
|
5
5
|
|
|
6
6
|
from diot import Diot
|
|
7
7
|
|
|
8
|
-
from .utils import config_to_ext, get_loader, POOL_KEY, META_KEY
|
|
8
|
+
from .utils import config_to_ext, detect_loader_directive, get_loader, POOL_KEY, META_KEY
|
|
9
9
|
from .loaders import Loader
|
|
10
10
|
|
|
11
11
|
LoaderType = Union[str, Loader, None]
|
|
@@ -116,6 +116,7 @@ class Config:
|
|
|
116
116
|
raise ValueError("'loader' must be specified for stream")
|
|
117
117
|
|
|
118
118
|
ext = config_to_ext(config)
|
|
119
|
+
ext = detect_loader_directive(config, ext)
|
|
119
120
|
loader = get_loader(ext)
|
|
120
121
|
else:
|
|
121
122
|
loader = get_loader(loader)
|
|
@@ -145,6 +146,7 @@ class Config:
|
|
|
145
146
|
raise ValueError("'loader' must be specified for stream")
|
|
146
147
|
|
|
147
148
|
ext = config_to_ext(config)
|
|
149
|
+
ext = detect_loader_directive(config, ext)
|
|
148
150
|
loader = get_loader(ext)
|
|
149
151
|
else:
|
|
150
152
|
loader = get_loader(loader)
|
|
@@ -202,6 +204,7 @@ class ProfileConfig:
|
|
|
202
204
|
|
|
203
205
|
if lder is None:
|
|
204
206
|
ext = config_to_ext(conf)
|
|
207
|
+
ext = detect_loader_directive(conf, ext)
|
|
205
208
|
lder = get_loader(ext)
|
|
206
209
|
else:
|
|
207
210
|
lder = get_loader(lder)
|
|
@@ -271,6 +274,7 @@ class ProfileConfig:
|
|
|
271
274
|
|
|
272
275
|
if lder is None:
|
|
273
276
|
ext = config_to_ext(conf)
|
|
277
|
+
ext = detect_loader_directive(conf, ext)
|
|
274
278
|
lder = get_loader(ext)
|
|
275
279
|
else:
|
|
276
280
|
lder = get_loader(lder)
|
|
@@ -323,6 +327,7 @@ class ProfileConfig:
|
|
|
323
327
|
raise ValueError("'loader' must be specified for stream")
|
|
324
328
|
|
|
325
329
|
ext = config_to_ext(conf)
|
|
330
|
+
ext = detect_loader_directive(conf, ext)
|
|
326
331
|
loader = get_loader(ext)
|
|
327
332
|
else:
|
|
328
333
|
loader = get_loader(loader)
|
|
@@ -378,6 +383,7 @@ class ProfileConfig:
|
|
|
378
383
|
raise ValueError("'loader' must be specified for stream")
|
|
379
384
|
|
|
380
385
|
ext = config_to_ext(conf)
|
|
386
|
+
ext = detect_loader_directive(conf, ext)
|
|
381
387
|
loader = get_loader(ext)
|
|
382
388
|
else:
|
|
383
389
|
loader = get_loader(loader)
|
|
@@ -148,7 +148,7 @@ class J2ModifierMixin(LoaderModifierMixin):
|
|
|
148
148
|
def _modifier(self, content: str | bytes) -> str | bytes:
|
|
149
149
|
"""Modify the content of the configuration file before loading"""
|
|
150
150
|
from jinja2 import Template
|
|
151
|
-
return Template(content).render()
|
|
151
|
+
return Template(content).render() # type: ignore
|
|
152
152
|
|
|
153
153
|
|
|
154
154
|
class LiqModifierMixin(LoaderModifierMixin):
|
|
@@ -156,5 +156,5 @@ class LiqModifierMixin(LoaderModifierMixin):
|
|
|
156
156
|
|
|
157
157
|
def _modifier(self, content: str | bytes) -> str | bytes:
|
|
158
158
|
"""Modify the content of the configuration file before loading"""
|
|
159
|
-
from liquid import Liquid
|
|
159
|
+
from liquid import Liquid # type: ignore[import]
|
|
160
160
|
return Liquid(content, from_file=False, mode="wild").render()
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import re
|
|
3
4
|
from pathlib import Path
|
|
4
5
|
from importlib import import_module
|
|
5
6
|
from types import ModuleType
|
|
@@ -11,6 +12,67 @@ from .loaders import Loader
|
|
|
11
12
|
POOL_KEY = "_SIMPLECONF_POOL"
|
|
12
13
|
META_KEY = "_SIMPLECONF_META"
|
|
13
14
|
|
|
15
|
+
_LOADER_DIRECTIVE_RE = re.compile(
|
|
16
|
+
r"^\s*(?:#|;|//)\s*simpleconf-loader:\s*(\S+)",
|
|
17
|
+
re.IGNORECASE,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def detect_loader_directive(conf: Any, current_ext: str) -> str:
|
|
22
|
+
"""Detect if the first line of a config file contains a loader directive.
|
|
23
|
+
|
|
24
|
+
Supports comment styles::
|
|
25
|
+
|
|
26
|
+
# simpleconf-loader: toml.liq
|
|
27
|
+
# simpleconf-loader: liq
|
|
28
|
+
# simpleconf-loader: liquid
|
|
29
|
+
; simpleconf-loader: ini.liq
|
|
30
|
+
// simpleconf-loader: json.j2
|
|
31
|
+
|
|
32
|
+
Short aliases ``liq``/``liquid`` and ``j2``/``jinja``/``jinja2`` are
|
|
33
|
+
expanded relative to the base format of *current_ext*.
|
|
34
|
+
Any other value is used verbatim as the loader extension.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
conf: The configuration source. Dicts and stream objects are
|
|
38
|
+
ignored (returns *current_ext* unchanged).
|
|
39
|
+
current_ext: The extension string already derived from the filename.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
The overriding extension string, or *current_ext* when no directive
|
|
43
|
+
is present.
|
|
44
|
+
"""
|
|
45
|
+
if isinstance(conf, dict) or hasattr(conf, "read"):
|
|
46
|
+
return current_ext
|
|
47
|
+
|
|
48
|
+
path = Path(conf)
|
|
49
|
+
if not path.exists():
|
|
50
|
+
return current_ext
|
|
51
|
+
|
|
52
|
+
try:
|
|
53
|
+
first_line = path.read_text(errors="replace").split("\n", 1)[0]
|
|
54
|
+
except Exception:
|
|
55
|
+
return current_ext
|
|
56
|
+
|
|
57
|
+
match = _LOADER_DIRECTIVE_RE.match(first_line)
|
|
58
|
+
if not match:
|
|
59
|
+
return current_ext
|
|
60
|
+
|
|
61
|
+
directive = match.group(1).lower()
|
|
62
|
+
|
|
63
|
+
# Derive the base format name (strip any existing template suffix)
|
|
64
|
+
parts = current_ext.split(".")
|
|
65
|
+
base_ext = parts[0] if parts[-1] in ("j2", "liq") else current_ext
|
|
66
|
+
|
|
67
|
+
if directive in ("liq", "liquid"):
|
|
68
|
+
return base_ext + ".liq"
|
|
69
|
+
|
|
70
|
+
if directive in ("j2", "jinja", "jinja2"):
|
|
71
|
+
return base_ext + ".j2"
|
|
72
|
+
|
|
73
|
+
# Treat as an explicit loader extension name
|
|
74
|
+
return directive
|
|
75
|
+
|
|
14
76
|
|
|
15
77
|
def config_to_ext(conf: Any, secondary: bool = True) -> str:
|
|
16
78
|
"""Find the extension(flag) of the configuration"""
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|