urban-json-files 0.1.0__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.
- urban_json_files-0.1.0/LICENSE +21 -0
- urban_json_files-0.1.0/PKG-INFO +51 -0
- urban_json_files-0.1.0/README.md +24 -0
- urban_json_files-0.1.0/pyproject.toml +57 -0
- urban_json_files-0.1.0/setup.cfg +4 -0
- urban_json_files-0.1.0/src/urban_json_files/__init__.py +87 -0
- urban_json_files-0.1.0/src/urban_json_files/py.typed +0 -0
- urban_json_files-0.1.0/src/urban_json_files.egg-info/PKG-INFO +51 -0
- urban_json_files-0.1.0/src/urban_json_files.egg-info/SOURCES.txt +12 -0
- urban_json_files-0.1.0/src/urban_json_files.egg-info/dependency_links.txt +1 -0
- urban_json_files-0.1.0/src/urban_json_files.egg-info/entry_points.txt +2 -0
- urban_json_files-0.1.0/src/urban_json_files.egg-info/requires.txt +1 -0
- urban_json_files-0.1.0/src/urban_json_files.egg-info/top_level.txt +1 -0
- urban_json_files-0.1.0/tests/test_files.py +109 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 black beard
|
|
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,51 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: urban-json-files
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Plugin urban-json : fichiers JSON (loadjson, iteratejsonfile) et chemins (concatpath).
|
|
5
|
+
Author-email: Gael Beard <gaelgael5@gmail.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/gaelgael5/Jslt
|
|
8
|
+
Project-URL: Repository, https://github.com/gaelgael5/Jslt
|
|
9
|
+
Keywords: json,transform,template,jslt,files,plugin
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Topic :: Text Processing :: Markup
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: urban-json>=0.2.0
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# urban-json-files
|
|
29
|
+
|
|
30
|
+
Plugin [urban-json](https://github.com/gaelgael5/Jslt) : **fichiers JSON et chemins**
|
|
31
|
+
dans les templates (port des fonctions .NET `loadjson`, `IterateJsonFile`, `concatpath`).
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install urban-json-files
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
| Fonction | Rôle |
|
|
38
|
+
|---|---|
|
|
39
|
+
| `loadjson(chemin)` | Contenu d'un fichier JSON (`null` si le fichier n'existe pas). |
|
|
40
|
+
| `iteratejsonfile(chemin, "$.chemin")` | Éléments d'un fichier JSON sélectionnés par un JSONPath (compilé ou en chaîne). |
|
|
41
|
+
| `concatpath(["dossier", "fichier.json"])` | Chemin de fichier assemblé (`null` ignorés ; un segment absolu repart de la racine). |
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from urban_json import JsltTemplate
|
|
45
|
+
|
|
46
|
+
tpl = JsltTemplate.parse('{ "ids": iteratejsonfile("data.json", "$.items[*].id") }')
|
|
47
|
+
tpl.transform({})
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Les chemins relatifs le sont au répertoire courant. Ces fonctions lisent le disque :
|
|
51
|
+
elles ne sont pas disponibles dans le playground (bac à sable du cœur).
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# urban-json-files
|
|
2
|
+
|
|
3
|
+
Plugin [urban-json](https://github.com/gaelgael5/Jslt) : **fichiers JSON et chemins**
|
|
4
|
+
dans les templates (port des fonctions .NET `loadjson`, `IterateJsonFile`, `concatpath`).
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
pip install urban-json-files
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
| Fonction | Rôle |
|
|
11
|
+
|---|---|
|
|
12
|
+
| `loadjson(chemin)` | Contenu d'un fichier JSON (`null` si le fichier n'existe pas). |
|
|
13
|
+
| `iteratejsonfile(chemin, "$.chemin")` | Éléments d'un fichier JSON sélectionnés par un JSONPath (compilé ou en chaîne). |
|
|
14
|
+
| `concatpath(["dossier", "fichier.json"])` | Chemin de fichier assemblé (`null` ignorés ; un segment absolu repart de la racine). |
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
from urban_json import JsltTemplate
|
|
18
|
+
|
|
19
|
+
tpl = JsltTemplate.parse('{ "ids": iteratejsonfile("data.json", "$.items[*].id") }')
|
|
20
|
+
tpl.transform({})
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Les chemins relatifs le sont au répertoire courant. Ces fonctions lisent le disque :
|
|
24
|
+
elles ne sont pas disponibles dans le playground (bac à sable du cœur).
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "urban-json-files"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Plugin urban-json : fichiers JSON (loadjson, iteratejsonfile) et chemins (concatpath)."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "Gael Beard", email = "gaelgael5@gmail.com" }]
|
|
14
|
+
keywords = ["json", "transform", "template", "jslt", "files", "plugin"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.9",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
26
|
+
"Topic :: Text Processing :: Markup",
|
|
27
|
+
"Typing :: Typed",
|
|
28
|
+
]
|
|
29
|
+
dependencies = [
|
|
30
|
+
# 0.2.0 : chemins JSONPath acceptés en chaîne (as_json_path), utilisés ici.
|
|
31
|
+
"urban-json>=0.2.0",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Homepage = "https://github.com/gaelgael5/Jslt"
|
|
36
|
+
Repository = "https://github.com/gaelgael5/Jslt"
|
|
37
|
+
|
|
38
|
+
# Découverte automatique par urban-json : installer ce paquet suffit à exposer ses fonctions.
|
|
39
|
+
[project.entry-points."urban_json.functions"]
|
|
40
|
+
files = "urban_json_files:register"
|
|
41
|
+
|
|
42
|
+
[tool.setuptools.dynamic]
|
|
43
|
+
version = { attr = "urban_json_files.__version__" }
|
|
44
|
+
|
|
45
|
+
[tool.setuptools.packages.find]
|
|
46
|
+
where = ["src"]
|
|
47
|
+
|
|
48
|
+
[tool.setuptools.package-data]
|
|
49
|
+
urban_json_files = ["py.typed"]
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
testpaths = ["tests"]
|
|
53
|
+
pythonpath = ["src"]
|
|
54
|
+
|
|
55
|
+
[tool.ruff]
|
|
56
|
+
line-length = 100
|
|
57
|
+
src = ["src", "tests"]
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Plugin urban-json : fichiers JSON et chemins (``loadjson``, ``iteratejsonfile``,
|
|
2
|
+
``concatpath``).
|
|
3
|
+
|
|
4
|
+
Port de ``Services.cs`` (``loadjson``, ``IterateJsonFile``) et
|
|
5
|
+
``Services.paths.cs`` (``concatpath``) du .NET. Ces fonctions lisent le disque :
|
|
6
|
+
elles restent hors du cœur, que le playground exécute en bac à sable.
|
|
7
|
+
|
|
8
|
+
Découvert automatiquement via l'entry point ``urban_json.functions`` ; aussi
|
|
9
|
+
enregistrable à la main : ``from urban_json_files import register``.
|
|
10
|
+
|
|
11
|
+
Écarts volontaires vs .NET :
|
|
12
|
+
|
|
13
|
+
* La résolution de chemin du .NET (``ctx.Configuration.ResolveFile``) n'existe pas
|
|
14
|
+
dans le portage : un chemin relatif l'est au répertoire courant (comme
|
|
15
|
+
``loadcsv``). Le BOM éventuel est ignoré.
|
|
16
|
+
* ``iteratejsonfile`` lit le fichier entier puis applique le chemin, au lieu du
|
|
17
|
+
lecteur en flux ``BigJsonReader`` : le .NET matérialisait de toute façon le
|
|
18
|
+
résultat complet (``new JArray(enumerator)``). Le chemin est accepté compilé
|
|
19
|
+
(``$.root.*``) ou en chaîne (``"$.root.*"``, forme du .NET).
|
|
20
|
+
* Erreurs (JSON invalide, fichier manquant pour ``iteratejsonfile``) : ``JsltError``
|
|
21
|
+
explicite.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
import json
|
|
27
|
+
import os
|
|
28
|
+
from typing import Any
|
|
29
|
+
|
|
30
|
+
from urban_json import JsltError
|
|
31
|
+
from urban_json.functions._helpers import as_json_path, to_text
|
|
32
|
+
|
|
33
|
+
__version__ = "0.1.0"
|
|
34
|
+
|
|
35
|
+
__all__ = ["execute_concatpath", "execute_iteratejsonfile", "execute_loadjson", "register"]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _read_json(path: str, function: str) -> Any:
|
|
39
|
+
try:
|
|
40
|
+
with open(path, encoding="utf-8-sig") as handle:
|
|
41
|
+
return json.load(handle)
|
|
42
|
+
except json.JSONDecodeError as exc:
|
|
43
|
+
raise JsltError(f"{function} : JSON invalide dans {path!r} ({exc})") from exc
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def execute_loadjson(ctx: Any, source_path: Any = None) -> Any:
|
|
47
|
+
if not source_path:
|
|
48
|
+
raise JsltError("loadjson : le chemin du fichier est requis")
|
|
49
|
+
path = to_text(source_path)
|
|
50
|
+
if not os.path.isfile(path):
|
|
51
|
+
return None # le .NET émettait un avertissement et rendait null
|
|
52
|
+
return _read_json(path, "loadjson")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def execute_iteratejsonfile(ctx: Any, file_source: Any = None, slicer: Any = None) -> list:
|
|
56
|
+
if not file_source:
|
|
57
|
+
raise JsltError("iteratejsonfile : le chemin du fichier est requis")
|
|
58
|
+
if slicer is None:
|
|
59
|
+
raise JsltError("iteratejsonfile : le chemin JSONPath de découpe est requis")
|
|
60
|
+
path = to_text(file_source)
|
|
61
|
+
if not os.path.isfile(path):
|
|
62
|
+
raise JsltError(f"iteratejsonfile : fichier introuvable {path!r}")
|
|
63
|
+
return as_json_path(slicer).select_values(_read_json(path, "iteratejsonfile"))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def execute_concatpath(ctx: Any, path: Any = None) -> Any:
|
|
67
|
+
if not isinstance(path, list):
|
|
68
|
+
return None
|
|
69
|
+
parts = [to_text(item) for item in path if item is not None]
|
|
70
|
+
# Path.Combine .NET = os.path.join : un segment absolu repart de la racine.
|
|
71
|
+
return os.path.join(*parts) if parts else ""
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def register(registry: Any) -> None:
|
|
75
|
+
"""Enregistre ``loadjson``, ``iteratejsonfile`` et ``concatpath``."""
|
|
76
|
+
registry.register(
|
|
77
|
+
"loadjson", execute_loadjson,
|
|
78
|
+
description="Lit un fichier JSON et renvoie son contenu (null si le fichier n'existe pas)",
|
|
79
|
+
)
|
|
80
|
+
registry.register(
|
|
81
|
+
"iteratejsonfile", execute_iteratejsonfile, path_args=(1,),
|
|
82
|
+
description="Lit un fichier JSON et renvoie le tableau des éléments sélectionnés par un chemin JSONPath",
|
|
83
|
+
)
|
|
84
|
+
registry.register(
|
|
85
|
+
"concatpath", execute_concatpath,
|
|
86
|
+
description="Assemble un chemin de fichier à partir d'un tableau de segments (null ignorés)",
|
|
87
|
+
)
|
|
File without changes
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: urban-json-files
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Plugin urban-json : fichiers JSON (loadjson, iteratejsonfile) et chemins (concatpath).
|
|
5
|
+
Author-email: Gael Beard <gaelgael5@gmail.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/gaelgael5/Jslt
|
|
8
|
+
Project-URL: Repository, https://github.com/gaelgael5/Jslt
|
|
9
|
+
Keywords: json,transform,template,jslt,files,plugin
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Topic :: Text Processing :: Markup
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: urban-json>=0.2.0
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# urban-json-files
|
|
29
|
+
|
|
30
|
+
Plugin [urban-json](https://github.com/gaelgael5/Jslt) : **fichiers JSON et chemins**
|
|
31
|
+
dans les templates (port des fonctions .NET `loadjson`, `IterateJsonFile`, `concatpath`).
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install urban-json-files
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
| Fonction | Rôle |
|
|
38
|
+
|---|---|
|
|
39
|
+
| `loadjson(chemin)` | Contenu d'un fichier JSON (`null` si le fichier n'existe pas). |
|
|
40
|
+
| `iteratejsonfile(chemin, "$.chemin")` | Éléments d'un fichier JSON sélectionnés par un JSONPath (compilé ou en chaîne). |
|
|
41
|
+
| `concatpath(["dossier", "fichier.json"])` | Chemin de fichier assemblé (`null` ignorés ; un segment absolu repart de la racine). |
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from urban_json import JsltTemplate
|
|
45
|
+
|
|
46
|
+
tpl = JsltTemplate.parse('{ "ids": iteratejsonfile("data.json", "$.items[*].id") }')
|
|
47
|
+
tpl.transform({})
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Les chemins relatifs le sont au répertoire courant. Ces fonctions lisent le disque :
|
|
51
|
+
elles ne sont pas disponibles dans le playground (bac à sable du cœur).
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/urban_json_files/__init__.py
|
|
5
|
+
src/urban_json_files/py.typed
|
|
6
|
+
src/urban_json_files.egg-info/PKG-INFO
|
|
7
|
+
src/urban_json_files.egg-info/SOURCES.txt
|
|
8
|
+
src/urban_json_files.egg-info/dependency_links.txt
|
|
9
|
+
src/urban_json_files.egg-info/entry_points.txt
|
|
10
|
+
src/urban_json_files.egg-info/requires.txt
|
|
11
|
+
src/urban_json_files.egg-info/top_level.txt
|
|
12
|
+
tests/test_files.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
urban-json>=0.2.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
urban_json_files
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""loadjson, iteratejsonfile, concatpath — port de Services.cs / Services.paths.cs."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
from urban_json import FunctionRegistry, JsltError, JsltTemplate
|
|
11
|
+
|
|
12
|
+
from urban_json_files import register
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@pytest.fixture(scope="module")
|
|
16
|
+
def registry() -> FunctionRegistry:
|
|
17
|
+
reg = FunctionRegistry()
|
|
18
|
+
register(reg)
|
|
19
|
+
return reg
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def run(registry, expression: str, source=None):
|
|
23
|
+
return JsltTemplate.parse(f'{{ "r": {expression} }}').transform(source or {}, functions=registry)["r"]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _write(tmp_path: Path, name: str, data, raw: str | None = None) -> str:
|
|
27
|
+
path = tmp_path / name
|
|
28
|
+
path.write_text(raw if raw is not None else json.dumps(data), encoding="utf-8")
|
|
29
|
+
return str(path)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_loadjson(registry, tmp_path) -> None:
|
|
33
|
+
path = _write(tmp_path, "a.json", {"k": [1, 2], "s": "é"})
|
|
34
|
+
assert run(registry, "loadjson($.p)", {"p": path}) == {"k": [1, 2], "s": "é"}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_loadjson_bom(registry, tmp_path) -> None:
|
|
38
|
+
path = _write(tmp_path, "bom.json", None, raw="[1]")
|
|
39
|
+
assert run(registry, "loadjson($.p)", {"p": path}) == [1]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_loadjson_fichier_absent_donne_null(registry, tmp_path) -> None:
|
|
43
|
+
assert run(registry, "loadjson($.p)", {"p": str(tmp_path / "absent.json")}) is None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_loadjson_json_invalide(registry, tmp_path) -> None:
|
|
47
|
+
path = _write(tmp_path, "bad.json", None, raw="{oops")
|
|
48
|
+
with pytest.raises(JsltError, match="JSON invalide"):
|
|
49
|
+
run(registry, "loadjson($.p)", {"p": path})
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_loadjson_chemin_manquant(registry) -> None:
|
|
53
|
+
with pytest.raises(JsltError, match="loadjson"):
|
|
54
|
+
run(registry, "loadjson($.p)", {"p": None})
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def test_iteratejsonfile(registry, tmp_path) -> None:
|
|
58
|
+
path = _write(tmp_path, "big.json", {"root": {"a": {"id": 1}, "b": {"id": 2}}})
|
|
59
|
+
assert run(registry, 'iteratejsonfile($.p, "$.root.*")', {"p": path}) == [{"id": 1}, {"id": 2}]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_iteratejsonfile_chemin_compile(registry, tmp_path) -> None:
|
|
63
|
+
path = _write(tmp_path, "big.json", {"items": [{"id": 1}, {"id": 2}]})
|
|
64
|
+
assert run(registry, "iteratejsonfile($.p, $.items[*].id)", {"p": path}) == [1, 2]
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def test_iteratejsonfile_dans_une_boucle(registry, tmp_path) -> None:
|
|
68
|
+
# Usage du test .NET : la fonction alimente une projection par élément.
|
|
69
|
+
path = _write(tmp_path, "big.json", {"root": [{"n": "a"}, {"n": "b"}]})
|
|
70
|
+
template = JsltTemplate.parse('{ "r": [ { "$": iteratejsonfile($.p, "$.root[*]"), "nom": $.n } ] }')
|
|
71
|
+
assert template.transform({"p": path}, functions=registry)["r"] == [{"nom": "a"}, {"nom": "b"}]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@pytest.mark.parametrize("expression", ['iteratejsonfile($.p, "$.x")', "iteratejsonfile(null, null)"])
|
|
75
|
+
def test_iteratejsonfile_erreurs(registry, tmp_path, expression) -> None:
|
|
76
|
+
with pytest.raises(JsltError, match="iteratejsonfile"):
|
|
77
|
+
run(registry, expression, {"p": str(tmp_path / "absent.json")})
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def test_concatpath(registry) -> None:
|
|
81
|
+
assert run(registry, 'concatpath(["data", "2026", "file.json"])') == os.path.join("data", "2026", "file.json")
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def test_concatpath_ignore_null_et_convertit(registry) -> None:
|
|
85
|
+
assert run(registry, 'concatpath(["a", null, 3])') == os.path.join("a", "3")
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def test_concatpath_segment_absolu_repart_de_la_racine(registry) -> None:
|
|
89
|
+
root = os.path.abspath(os.sep)
|
|
90
|
+
out = run(registry, "concatpath($.p)", {"p": ["a", os.path.join(root, "etc"), "x"]})
|
|
91
|
+
assert out == os.path.join(root, "etc", "x")
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@pytest.mark.parametrize("value", [None, "abc", []])
|
|
95
|
+
def test_concatpath_entrees_vides(registry, value) -> None:
|
|
96
|
+
assert run(registry, "concatpath($.p)", {"p": value}) in (None, "")
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def test_catalogue_documente(registry) -> None:
|
|
100
|
+
names = {e["name"]: e for e in registry.catalog()}
|
|
101
|
+
assert set(names) == {"loadjson", "iteratejsonfile", "concatpath"}
|
|
102
|
+
assert all(len(e["description"]) >= 15 for e in names.values())
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_decouverte_par_entry_point() -> None:
|
|
106
|
+
from urban_json import default_registry, load_plugins
|
|
107
|
+
|
|
108
|
+
load_plugins()
|
|
109
|
+
assert "loadjson" in default_registry
|