anyenv 0.3.0__tar.gz → 0.3.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.
- {anyenv-0.3.0 → anyenv-0.3.2}/PKG-INFO +1 -1
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/__init__.py +5 -3
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/download/aiohttp_backend.py +9 -3
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/download/httpx_backend.py +19 -20
- anyenv-0.3.2/src/anyenv/download/httpx_serializer.py +136 -0
- anyenv-0.3.2/src/anyenv/json_tools/dumping.py +64 -0
- anyenv-0.3.2/src/anyenv/json_tools/loading.py +73 -0
- anyenv-0.3.0/src/anyenv/json_tools/dumping.py +0 -34
- anyenv-0.3.0/src/anyenv/json_tools/loading.py +0 -43
- {anyenv-0.3.0 → anyenv-0.3.2}/.copier-answers.yml +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/.github/FUNDING.yml +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/.github/copilot-instructions.md +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/.github/dependabot.yml +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/.github/workflows/build.yml +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/.github/workflows/documentation.yml +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/.gitignore +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/.pre-commit-config.yaml +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/LICENSE +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/README.md +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/docs/.empty +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/duties.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/mkdocs.yml +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/overrides/_dummy.txt +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/pyproject.toml +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/async_run.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/download/__init__.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/download/base.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/download/exceptions.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/download/functional.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/download/pyodide_backend.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/json_tools/__init__.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/package_install/__init__.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/package_install/base.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/package_install/functional.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/package_install/micropip_install.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/package_install/pip_install.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/py.typed +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/testing.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/threadgroup/__init__.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/src/anyenv/threadgroup/threadgroup.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/tests/__init__.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/tests/conftest.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/tests/test_download.py +0 -0
- {anyenv-0.3.0 → anyenv-0.3.2}/tests/test_threadgroup.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: anyenv
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.2
|
|
4
4
|
Summary: Compatibility layer for some basic operations to allow painless operation in PyOdide and Python pre-releases
|
|
5
5
|
Project-URL: Documentation, https://phil65.github.io/anyenv/
|
|
6
6
|
Project-URL: Source, https://github.com/phil65/anyenv
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""AnyEnv: A library for environment management and utility functions."""
|
|
2
2
|
|
|
3
|
-
__version__ = "0.3.
|
|
3
|
+
__version__ = "0.3.2"
|
|
4
4
|
|
|
5
5
|
from anyenv.async_run import run_sync, run_sync_in_thread
|
|
6
6
|
from anyenv.download.functional import (
|
|
@@ -23,10 +23,12 @@ from anyenv.download.functional import (
|
|
|
23
23
|
from anyenv.threadgroup.threadgroup import ThreadGroup
|
|
24
24
|
from anyenv.package_install.functional import install, install_sync
|
|
25
25
|
from anyenv.testing import open_in_playground
|
|
26
|
-
from anyenv.json_tools.loading import load_json
|
|
27
|
-
from anyenv.json_tools.dumping import dump_json
|
|
26
|
+
from anyenv.json_tools.loading import load_json, JsonLoadError
|
|
27
|
+
from anyenv.json_tools.dumping import dump_json, JsonDumpError
|
|
28
28
|
|
|
29
29
|
__all__ = [
|
|
30
|
+
"JsonDumpError",
|
|
31
|
+
"JsonLoadError",
|
|
30
32
|
"ThreadGroup",
|
|
31
33
|
"download",
|
|
32
34
|
"download_sync",
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
import pathlib
|
|
6
5
|
from typing import TYPE_CHECKING, Any
|
|
7
6
|
|
|
8
7
|
import aiohttp
|
|
@@ -17,6 +16,11 @@ from anyenv.download.base import (
|
|
|
17
16
|
)
|
|
18
17
|
|
|
19
18
|
|
|
19
|
+
try:
|
|
20
|
+
from upath import UPath as Path
|
|
21
|
+
except ImportError:
|
|
22
|
+
from pathlib import Path # type: ignore[assignment]
|
|
23
|
+
|
|
20
24
|
if TYPE_CHECKING:
|
|
21
25
|
import os
|
|
22
26
|
|
|
@@ -39,7 +43,9 @@ class AiohttpResponse(HttpResponse):
|
|
|
39
43
|
return await self._response.text()
|
|
40
44
|
|
|
41
45
|
async def json(self) -> Any:
|
|
42
|
-
|
|
46
|
+
from anyenv.json_tools import loading
|
|
47
|
+
|
|
48
|
+
return await self._response.json(loads=loading.load_json)
|
|
43
49
|
|
|
44
50
|
async def bytes(self) -> bytes:
|
|
45
51
|
return await self._response.read()
|
|
@@ -158,7 +164,7 @@ class AiohttpBackend(HttpBackend):
|
|
|
158
164
|
total = int(response.headers.get("content-length", "0"))
|
|
159
165
|
current = 0
|
|
160
166
|
|
|
161
|
-
with
|
|
167
|
+
with Path(path).open("wb") as f:
|
|
162
168
|
async for chunk in response.content.iter_chunked(8192):
|
|
163
169
|
f.write(chunk)
|
|
164
170
|
current += len(chunk)
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
import pathlib
|
|
6
5
|
from typing import TYPE_CHECKING, Any
|
|
7
6
|
|
|
8
7
|
import hishel
|
|
@@ -17,6 +16,11 @@ from anyenv.download.base import (
|
|
|
17
16
|
)
|
|
18
17
|
|
|
19
18
|
|
|
19
|
+
try:
|
|
20
|
+
from upath import UPath as Path
|
|
21
|
+
except ImportError:
|
|
22
|
+
from pathlib import Path # type: ignore[assignment]
|
|
23
|
+
|
|
20
24
|
if TYPE_CHECKING:
|
|
21
25
|
import os
|
|
22
26
|
|
|
@@ -39,7 +43,9 @@ class HttpxResponse(HttpResponse):
|
|
|
39
43
|
return self._response.text
|
|
40
44
|
|
|
41
45
|
async def json(self) -> Any:
|
|
42
|
-
|
|
46
|
+
from anyenv.json_tools import loading
|
|
47
|
+
|
|
48
|
+
return loading.load_json(self._response.content)
|
|
43
49
|
|
|
44
50
|
async def bytes(self) -> bytes:
|
|
45
51
|
return self._response.content
|
|
@@ -95,31 +101,24 @@ class HttpxBackend(HttpBackend):
|
|
|
95
101
|
base_url: str | None = None,
|
|
96
102
|
headers: dict[str, str] | None = None,
|
|
97
103
|
) -> httpx.AsyncClient:
|
|
104
|
+
url = base_url or ""
|
|
98
105
|
if cache:
|
|
106
|
+
from anyenv.download.httpx_serializer import AnyEnvSerializer
|
|
107
|
+
|
|
99
108
|
storage = hishel.AsyncFileStorage(
|
|
109
|
+
serializer=AnyEnvSerializer(),
|
|
100
110
|
base_path=self.cache_dir,
|
|
101
111
|
ttl=self.cache_ttl,
|
|
102
112
|
)
|
|
103
|
-
|
|
113
|
+
ctl = hishel.Controller(
|
|
104
114
|
cacheable_methods=["GET"],
|
|
105
115
|
cacheable_status_codes=[200],
|
|
106
116
|
allow_stale=True,
|
|
107
117
|
)
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
)
|
|
113
|
-
return httpx.AsyncClient(
|
|
114
|
-
transport=transport,
|
|
115
|
-
headers=headers,
|
|
116
|
-
base_url=base_url or "",
|
|
117
|
-
)
|
|
118
|
-
|
|
119
|
-
return httpx.AsyncClient(
|
|
120
|
-
headers=headers,
|
|
121
|
-
base_url=base_url or "",
|
|
122
|
-
)
|
|
118
|
+
tp = httpx.AsyncHTTPTransport()
|
|
119
|
+
transport = hishel.AsyncCacheTransport(tp, storage=storage, controller=ctl)
|
|
120
|
+
return httpx.AsyncClient(transport=transport, headers=headers, base_url=url)
|
|
121
|
+
return httpx.AsyncClient(headers=headers, base_url=url)
|
|
123
122
|
|
|
124
123
|
async def request(
|
|
125
124
|
self,
|
|
@@ -161,7 +160,7 @@ class HttpxBackend(HttpBackend):
|
|
|
161
160
|
total = int(response.headers.get("content-length", "0"))
|
|
162
161
|
current = 0
|
|
163
162
|
|
|
164
|
-
with
|
|
163
|
+
with Path(path).open("wb") as f:
|
|
165
164
|
async for chunk in response.aiter_bytes():
|
|
166
165
|
f.write(chunk)
|
|
167
166
|
current += len(chunk)
|
|
@@ -189,7 +188,7 @@ if __name__ == "__main__":
|
|
|
189
188
|
backend = HttpxBackend()
|
|
190
189
|
await backend.download(
|
|
191
190
|
url="http://speedtest.tele2.net/10MB.zip",
|
|
192
|
-
path=
|
|
191
|
+
path=Path.cwd() / "file.zip",
|
|
193
192
|
headers={"User-Agent": "anyenv"},
|
|
194
193
|
)
|
|
195
194
|
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
from datetime import datetime
|
|
5
|
+
|
|
6
|
+
from hishel._serializers import (
|
|
7
|
+
HEADERS_ENCODING,
|
|
8
|
+
KNOWN_REQUEST_EXTENSIONS,
|
|
9
|
+
KNOWN_RESPONSE_EXTENSIONS,
|
|
10
|
+
BaseSerializer,
|
|
11
|
+
Metadata,
|
|
12
|
+
Request,
|
|
13
|
+
Response,
|
|
14
|
+
normalized_url,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class AnyEnvSerializer(BaseSerializer):
|
|
19
|
+
"""A json-based serializer using AnyEnv."""
|
|
20
|
+
|
|
21
|
+
def dumps(
|
|
22
|
+
self, response: Response, request: Request, metadata: Metadata
|
|
23
|
+
) -> str | bytes:
|
|
24
|
+
"""Dumps the HTTP response and its HTTP request.
|
|
25
|
+
|
|
26
|
+
:param response: An HTTP response
|
|
27
|
+
:type response: Response
|
|
28
|
+
:param request: An HTTP request
|
|
29
|
+
:type request: Request
|
|
30
|
+
:param metadata: Additional information about the stored response
|
|
31
|
+
:type metadata: Metadata
|
|
32
|
+
:return: Serialized response
|
|
33
|
+
:rtype: str | bytes
|
|
34
|
+
"""
|
|
35
|
+
response_dict = {
|
|
36
|
+
"status": response.status,
|
|
37
|
+
"headers": [
|
|
38
|
+
(key.decode(HEADERS_ENCODING), value.decode(HEADERS_ENCODING))
|
|
39
|
+
for key, value in response.headers
|
|
40
|
+
],
|
|
41
|
+
"content": base64.b64encode(response.content).decode("ascii"),
|
|
42
|
+
"extensions": {
|
|
43
|
+
key: value.decode("ascii")
|
|
44
|
+
for key, value in response.extensions.items()
|
|
45
|
+
if key in KNOWN_RESPONSE_EXTENSIONS
|
|
46
|
+
},
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
request_dict = {
|
|
50
|
+
"method": request.method.decode("ascii"),
|
|
51
|
+
"url": normalized_url(request.url),
|
|
52
|
+
"headers": [
|
|
53
|
+
(key.decode(HEADERS_ENCODING), value.decode(HEADERS_ENCODING))
|
|
54
|
+
for key, value in request.headers
|
|
55
|
+
],
|
|
56
|
+
"extensions": {
|
|
57
|
+
key: value
|
|
58
|
+
for key, value in request.extensions.items()
|
|
59
|
+
if key in KNOWN_REQUEST_EXTENSIONS
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
metadata_dict = {
|
|
64
|
+
"cache_key": metadata["cache_key"],
|
|
65
|
+
"number_of_uses": metadata["number_of_uses"],
|
|
66
|
+
"created_at": metadata["created_at"].strftime("%a, %d %b %Y %H:%M:%S GMT"),
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
full_json = {
|
|
70
|
+
"response": response_dict,
|
|
71
|
+
"request": request_dict,
|
|
72
|
+
"metadata": metadata_dict,
|
|
73
|
+
}
|
|
74
|
+
from anyenv.json_tools import dumping
|
|
75
|
+
|
|
76
|
+
return dumping.dump_json(full_json, indent=True)
|
|
77
|
+
|
|
78
|
+
def loads(self, data: str | bytes) -> tuple[Response, Request, Metadata]:
|
|
79
|
+
"""Loads the HTTP response and its HTTP request from serialized data.
|
|
80
|
+
|
|
81
|
+
:param data: Serialized data
|
|
82
|
+
:type data: str | bytes
|
|
83
|
+
:return: HTTP response and its HTTP request
|
|
84
|
+
:rtype: tuple[Response, Request, Metadata]
|
|
85
|
+
"""
|
|
86
|
+
from anyenv.json_tools import loading
|
|
87
|
+
|
|
88
|
+
full_json = loading.load_json(data)
|
|
89
|
+
|
|
90
|
+
response_dict = full_json["response"]
|
|
91
|
+
request_dict = full_json["request"]
|
|
92
|
+
metadata_dict = full_json["metadata"]
|
|
93
|
+
metadata_dict["created_at"] = datetime.strptime(
|
|
94
|
+
metadata_dict["created_at"],
|
|
95
|
+
"%a, %d %b %Y %H:%M:%S GMT",
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
response = Response(
|
|
99
|
+
status=response_dict["status"],
|
|
100
|
+
headers=[
|
|
101
|
+
(key.encode(HEADERS_ENCODING), value.encode(HEADERS_ENCODING))
|
|
102
|
+
for key, value in response_dict["headers"]
|
|
103
|
+
],
|
|
104
|
+
content=base64.b64decode(response_dict["content"].encode("ascii")),
|
|
105
|
+
extensions={
|
|
106
|
+
key: value.encode("ascii")
|
|
107
|
+
for key, value in response_dict["extensions"].items()
|
|
108
|
+
if key in KNOWN_RESPONSE_EXTENSIONS
|
|
109
|
+
},
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
request = Request(
|
|
113
|
+
method=request_dict["method"],
|
|
114
|
+
url=request_dict["url"],
|
|
115
|
+
headers=[
|
|
116
|
+
(key.encode(HEADERS_ENCODING), value.encode(HEADERS_ENCODING))
|
|
117
|
+
for key, value in request_dict["headers"]
|
|
118
|
+
],
|
|
119
|
+
extensions={
|
|
120
|
+
key: value
|
|
121
|
+
for key, value in request_dict["extensions"].items()
|
|
122
|
+
if key in KNOWN_REQUEST_EXTENSIONS
|
|
123
|
+
},
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
metadata = Metadata(
|
|
127
|
+
cache_key=metadata_dict["cache_key"],
|
|
128
|
+
created_at=metadata_dict["created_at"],
|
|
129
|
+
number_of_uses=metadata_dict["number_of_uses"],
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
return response, request, metadata
|
|
133
|
+
|
|
134
|
+
@property
|
|
135
|
+
def is_binary(self) -> bool:
|
|
136
|
+
return False
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""JSON dumping functionality with fallback options."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import importlib.util
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class JsonDumpError(Exception):
|
|
10
|
+
"""Unified exception for all JSON serialization errors."""
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# Find the best available JSON dumper
|
|
14
|
+
if importlib.util.find_spec("orjson") is not None:
|
|
15
|
+
import orjson
|
|
16
|
+
|
|
17
|
+
def dump_json(data: Any, indent: bool = False) -> str:
|
|
18
|
+
"""Dump data to JSON string using orjson."""
|
|
19
|
+
try:
|
|
20
|
+
options = 0
|
|
21
|
+
if indent:
|
|
22
|
+
options = orjson.OPT_INDENT_2
|
|
23
|
+
|
|
24
|
+
result = orjson.dumps(data, option=options)
|
|
25
|
+
return result.decode()
|
|
26
|
+
except (TypeError, ValueError) as exc:
|
|
27
|
+
error_msg = f"Cannot serialize to JSON: {exc}"
|
|
28
|
+
raise JsonDumpError(error_msg) from exc
|
|
29
|
+
|
|
30
|
+
elif importlib.util.find_spec("pydantic_core") is not None:
|
|
31
|
+
from pydantic_core import to_json
|
|
32
|
+
|
|
33
|
+
def dump_json(data: Any, indent: bool = False) -> str:
|
|
34
|
+
"""Dump data to JSON string using pydantic_core."""
|
|
35
|
+
try:
|
|
36
|
+
return to_json(data, indent=2 if indent else None).decode()
|
|
37
|
+
except Exception as exc:
|
|
38
|
+
error_msg = f"Cannot serialize to JSON: {exc}"
|
|
39
|
+
raise JsonDumpError(error_msg) from exc
|
|
40
|
+
|
|
41
|
+
elif importlib.util.find_spec("msgspec") is not None:
|
|
42
|
+
import msgspec.json
|
|
43
|
+
|
|
44
|
+
def dump_json(data: Any, indent: bool = False) -> str:
|
|
45
|
+
"""Dump data to JSON string using msgspec."""
|
|
46
|
+
try:
|
|
47
|
+
result = msgspec.json.encode(data)
|
|
48
|
+
if indent:
|
|
49
|
+
return msgspec.json.format(result, indent=2).decode()
|
|
50
|
+
return result.decode()
|
|
51
|
+
except (TypeError, msgspec.EncodeError) as exc:
|
|
52
|
+
error_msg = f"Cannot serialize to JSON: {exc}"
|
|
53
|
+
raise JsonDumpError(error_msg) from exc
|
|
54
|
+
|
|
55
|
+
else:
|
|
56
|
+
import json
|
|
57
|
+
|
|
58
|
+
def dump_json(data: Any, indent: bool = False) -> str:
|
|
59
|
+
"""Dump data to JSON string using stdlib json."""
|
|
60
|
+
try:
|
|
61
|
+
return json.dumps(data, indent=2 if indent else None)
|
|
62
|
+
except (TypeError, ValueError) as exc:
|
|
63
|
+
error_msg = f"Cannot serialize to JSON: {exc}"
|
|
64
|
+
raise JsonDumpError(error_msg) from exc
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""JSON loading functionality with fallback options."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import importlib.util
|
|
6
|
+
from io import TextIOWrapper
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class JsonLoadError(Exception):
|
|
11
|
+
"""Unified exception for all JSON parsing and serialization errors."""
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# Find the best available JSON parser for loading
|
|
15
|
+
if importlib.util.find_spec("orjson") is not None:
|
|
16
|
+
import orjson
|
|
17
|
+
|
|
18
|
+
def load_json(data: str | bytes | TextIOWrapper) -> Any:
|
|
19
|
+
"""Load JSON using orjson."""
|
|
20
|
+
try:
|
|
21
|
+
match data:
|
|
22
|
+
case TextIOWrapper():
|
|
23
|
+
data = data.read()
|
|
24
|
+
case str():
|
|
25
|
+
data = data.encode()
|
|
26
|
+
return orjson.loads(data)
|
|
27
|
+
except orjson.JSONDecodeError as exc:
|
|
28
|
+
error_msg = f"Invalid JSON: {exc}"
|
|
29
|
+
raise JsonLoadError(error_msg) from exc
|
|
30
|
+
|
|
31
|
+
elif importlib.util.find_spec("pydantic_core") is not None:
|
|
32
|
+
from pydantic_core import from_json
|
|
33
|
+
|
|
34
|
+
def load_json(data: str | bytes | TextIOWrapper) -> Any:
|
|
35
|
+
"""Load JSON using pydantic_core."""
|
|
36
|
+
try:
|
|
37
|
+
match data:
|
|
38
|
+
case TextIOWrapper():
|
|
39
|
+
data = data.read()
|
|
40
|
+
return from_json(data)
|
|
41
|
+
except Exception as exc:
|
|
42
|
+
error_msg = f"Invalid JSON: {exc}"
|
|
43
|
+
raise JsonLoadError(error_msg) from exc
|
|
44
|
+
|
|
45
|
+
elif importlib.util.find_spec("msgspec") is not None:
|
|
46
|
+
import msgspec.json
|
|
47
|
+
|
|
48
|
+
def load_json(data: str | bytes | TextIOWrapper) -> Any:
|
|
49
|
+
"""Load JSON using msgspec."""
|
|
50
|
+
try:
|
|
51
|
+
match data:
|
|
52
|
+
case TextIOWrapper():
|
|
53
|
+
data = data.read()
|
|
54
|
+
return msgspec.json.decode(data)
|
|
55
|
+
except msgspec.DecodeError as exc:
|
|
56
|
+
error_msg = f"Invalid JSON: {exc}"
|
|
57
|
+
raise JsonLoadError(error_msg) from exc
|
|
58
|
+
|
|
59
|
+
else:
|
|
60
|
+
import json
|
|
61
|
+
|
|
62
|
+
def load_json(data: str | bytes | TextIOWrapper) -> Any:
|
|
63
|
+
"""Load JSON using stdlib json."""
|
|
64
|
+
try:
|
|
65
|
+
match data:
|
|
66
|
+
case TextIOWrapper():
|
|
67
|
+
data = data.read()
|
|
68
|
+
case bytes():
|
|
69
|
+
data = data.decode()
|
|
70
|
+
return json.loads(data)
|
|
71
|
+
except json.JSONDecodeError as exc:
|
|
72
|
+
error_msg = f"Invalid JSON: {exc}"
|
|
73
|
+
raise JsonLoadError(error_msg) from exc
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
"""JSON dumping functionality with fallback options."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import importlib.util
|
|
6
|
-
from typing import Any
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
# Find the best available JSON dumper
|
|
10
|
-
if importlib.util.find_spec("orjson") is not None:
|
|
11
|
-
import orjson
|
|
12
|
-
|
|
13
|
-
def dump_json(data: Any, indent: bool = False) -> str:
|
|
14
|
-
"""Dump data to JSON string using orjson."""
|
|
15
|
-
options = 0
|
|
16
|
-
if indent:
|
|
17
|
-
options = orjson.OPT_INDENT_2
|
|
18
|
-
|
|
19
|
-
result = orjson.dumps(data, option=options)
|
|
20
|
-
return result.decode()
|
|
21
|
-
|
|
22
|
-
elif importlib.util.find_spec("pydantic_core") is not None:
|
|
23
|
-
from pydantic_core import to_json
|
|
24
|
-
|
|
25
|
-
def dump_json(data: Any, indent: bool = False) -> str:
|
|
26
|
-
"""Dump data to JSON string using pydantic_core."""
|
|
27
|
-
return to_json(data, indent=2 if indent else None).decode()
|
|
28
|
-
|
|
29
|
-
else:
|
|
30
|
-
import json
|
|
31
|
-
|
|
32
|
-
def dump_json(data: Any, indent: bool = False) -> str:
|
|
33
|
-
"""Dump data to JSON string using stdlib json."""
|
|
34
|
-
return json.dumps(data, indent=2 if indent else None)
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"""JSON loading functionality with fallback options."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import importlib.util
|
|
6
|
-
from io import TextIOWrapper
|
|
7
|
-
from typing import Any
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
# Find the best available JSON parser
|
|
11
|
-
if importlib.util.find_spec("orjson") is not None:
|
|
12
|
-
import orjson
|
|
13
|
-
|
|
14
|
-
def load_json(data: str | bytes | TextIOWrapper) -> Any:
|
|
15
|
-
"""Load JSON using orjson."""
|
|
16
|
-
match data:
|
|
17
|
-
case TextIOWrapper():
|
|
18
|
-
data = data.read()
|
|
19
|
-
case str():
|
|
20
|
-
data = data.encode()
|
|
21
|
-
return orjson.loads(data)
|
|
22
|
-
|
|
23
|
-
elif importlib.util.find_spec("pydantic_core") is not None:
|
|
24
|
-
from pydantic_core import from_json
|
|
25
|
-
|
|
26
|
-
def load_json(data: str | bytes | TextIOWrapper) -> Any:
|
|
27
|
-
"""Load JSON using pydantic_core."""
|
|
28
|
-
match data:
|
|
29
|
-
case TextIOWrapper():
|
|
30
|
-
data = data.read()
|
|
31
|
-
return from_json(data)
|
|
32
|
-
|
|
33
|
-
else:
|
|
34
|
-
import json
|
|
35
|
-
|
|
36
|
-
def load_json(data: str | bytes | TextIOWrapper) -> Any:
|
|
37
|
-
"""Load JSON using stdlib json."""
|
|
38
|
-
match data:
|
|
39
|
-
case TextIOWrapper():
|
|
40
|
-
data = data.read()
|
|
41
|
-
case bytes():
|
|
42
|
-
data = data.decode()
|
|
43
|
-
return json.loads(data)
|
|
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
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|