python-simpleconf 0.9.1__py3-none-any.whl → 0.9.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-simpleconf
3
- Version: 0.9.1
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.2,<0.4.0)
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
+ ```
@@ -1,8 +1,8 @@
1
- simpleconf/__init__.py,sha256=2oS2zzmxaXtJ5EjB9zFEa3id6iqorLBXZNxijkgzOog,65
1
+ simpleconf/__init__.py,sha256=GJQ8HSbUdkFNwFYiYeUy1hCligKG_H4wcM20K1ozAPI,104
2
2
  simpleconf/caster.py,sha256=qHnegp1FZzbUWm-UbCaGjQKN6d9rN1v_0Wa4I_CMEi4,2379
3
- simpleconf/config.py,sha256=GpQXxehF6p8hPpX1quy0HmIl1X3nnhG3eetPFaKASRY,17567
3
+ simpleconf/config.py,sha256=B6h-mgeiyI7op-R-i1C_z0oyc4pt7Tv7zKWgzsDy4xw,17922
4
4
  simpleconf/exceptions.py,sha256=Tb7w9gMO2XU4mE-Rc2HAnuHWDUpMzdl39oGVjeAv254,171
5
- simpleconf/loaders/__init__.py,sha256=aEWpcrtkpJRuHhobhOZci864vi8f_rIGubgTVOxmvuY,5322
5
+ simpleconf/loaders/__init__.py,sha256=fM4jQd3JoWkp7NdWr1B3MXQ33ytkFI3YS1o59yhE_U4,5362
6
6
  simpleconf/loaders/dict.py,sha256=uGY0ufQFwcEinSUFyNSb96RDgynm6tHW5fcEdc-A0Sc,763
7
7
  simpleconf/loaders/env.py,sha256=7jwh3TCxxdmJNROSBcGyb9muNC6BZ-rEGi0tznhHSnM,3132
8
8
  simpleconf/loaders/ini.py,sha256=PweI85VoTgN5IA4uQUdYZNKP9r0iCna5hb0smsG7_kg,3727
@@ -11,8 +11,8 @@ simpleconf/loaders/osenv.py,sha256=K829KFiHLl3pS8HSCep8JoPVG_d7VRxPAb7SPYFRypg,1
11
11
  simpleconf/loaders/toml.py,sha256=OJCukg_R3zlmas_q_CrdeKUsw8Wj5LoSnkNJ7Flt86w,2525
12
12
  simpleconf/loaders/yaml.py,sha256=IoKMaQF5y5HWEjQsnUwL2UvABVj9A-0AmBhn8s7lE54,2146
13
13
  simpleconf/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
14
- simpleconf/utils.py,sha256=XKE2fi-jKuqbxkgB_3_usdhc6kkUfj37JeJHNSWPuj8,4414
15
- python_simpleconf-0.9.1.dist-info/METADATA,sha256=poHLSqB6pyAFQgDK9buaGibbSo5DR5Sq-0e90bBwX1U,7979
16
- python_simpleconf-0.9.1.dist-info/WHEEL,sha256=kJCRJT_g0adfAJzTx2GUMmS80rTJIVHRCfG0DQgLq3o,88
17
- python_simpleconf-0.9.1.dist-info/licenses/LICENSE,sha256=2j7OLS2vXIEshgbSkiCU9RgYJY1M4OBBtO6ECbo7OB8,1056
18
- python_simpleconf-0.9.1.dist-info/RECORD,,
14
+ simpleconf/utils.py,sha256=prO5mN64Ir_7bmNnn6kEh-xZAAYiXTOxpiuFhiw9HpI,6247
15
+ python_simpleconf-0.9.2.dist-info/METADATA,sha256=jJ6HLpeMlhMHxz9cbIWppK7pA8rLX5QQCwSJlxHZtao,9175
16
+ python_simpleconf-0.9.2.dist-info/WHEEL,sha256=kJCRJT_g0adfAJzTx2GUMmS80rTJIVHRCfG0DQgLq3o,88
17
+ python_simpleconf-0.9.2.dist-info/licenses/LICENSE,sha256=2j7OLS2vXIEshgbSkiCU9RgYJY1M4OBBtO6ECbo7OB8,1056
18
+ python_simpleconf-0.9.2.dist-info/RECORD,,
simpleconf/__init__.py CHANGED
@@ -1,3 +1,5 @@
1
1
  from .config import Config, ProfileConfig
2
2
 
3
- __version__ = "0.9.1"
3
+ __all__ = ["Config", "ProfileConfig"]
4
+
5
+ __version__ = "0.9.2"
simpleconf/config.py CHANGED
@@ -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()
simpleconf/utils.py CHANGED
@@ -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"""