anyenv 0.3.1__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.
Files changed (42) hide show
  1. {anyenv-0.3.1 → anyenv-0.3.2}/PKG-INFO +1 -1
  2. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/__init__.py +1 -1
  3. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/download/aiohttp_backend.py +9 -3
  4. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/download/httpx_backend.py +19 -20
  5. anyenv-0.3.2/src/anyenv/download/httpx_serializer.py +136 -0
  6. {anyenv-0.3.1 → anyenv-0.3.2}/.copier-answers.yml +0 -0
  7. {anyenv-0.3.1 → anyenv-0.3.2}/.github/FUNDING.yml +0 -0
  8. {anyenv-0.3.1 → anyenv-0.3.2}/.github/copilot-instructions.md +0 -0
  9. {anyenv-0.3.1 → anyenv-0.3.2}/.github/dependabot.yml +0 -0
  10. {anyenv-0.3.1 → anyenv-0.3.2}/.github/workflows/build.yml +0 -0
  11. {anyenv-0.3.1 → anyenv-0.3.2}/.github/workflows/documentation.yml +0 -0
  12. {anyenv-0.3.1 → anyenv-0.3.2}/.gitignore +0 -0
  13. {anyenv-0.3.1 → anyenv-0.3.2}/.pre-commit-config.yaml +0 -0
  14. {anyenv-0.3.1 → anyenv-0.3.2}/LICENSE +0 -0
  15. {anyenv-0.3.1 → anyenv-0.3.2}/README.md +0 -0
  16. {anyenv-0.3.1 → anyenv-0.3.2}/docs/.empty +0 -0
  17. {anyenv-0.3.1 → anyenv-0.3.2}/duties.py +0 -0
  18. {anyenv-0.3.1 → anyenv-0.3.2}/mkdocs.yml +0 -0
  19. {anyenv-0.3.1 → anyenv-0.3.2}/overrides/_dummy.txt +0 -0
  20. {anyenv-0.3.1 → anyenv-0.3.2}/pyproject.toml +0 -0
  21. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/async_run.py +0 -0
  22. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/download/__init__.py +0 -0
  23. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/download/base.py +0 -0
  24. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/download/exceptions.py +0 -0
  25. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/download/functional.py +0 -0
  26. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/download/pyodide_backend.py +0 -0
  27. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/json_tools/__init__.py +0 -0
  28. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/json_tools/dumping.py +0 -0
  29. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/json_tools/loading.py +0 -0
  30. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/package_install/__init__.py +0 -0
  31. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/package_install/base.py +0 -0
  32. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/package_install/functional.py +0 -0
  33. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/package_install/micropip_install.py +0 -0
  34. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/package_install/pip_install.py +0 -0
  35. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/py.typed +0 -0
  36. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/testing.py +0 -0
  37. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/threadgroup/__init__.py +0 -0
  38. {anyenv-0.3.1 → anyenv-0.3.2}/src/anyenv/threadgroup/threadgroup.py +0 -0
  39. {anyenv-0.3.1 → anyenv-0.3.2}/tests/__init__.py +0 -0
  40. {anyenv-0.3.1 → anyenv-0.3.2}/tests/conftest.py +0 -0
  41. {anyenv-0.3.1 → anyenv-0.3.2}/tests/test_download.py +0 -0
  42. {anyenv-0.3.1 → 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.1
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.1"
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 (
@@ -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
- return await self._response.json()
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 pathlib.Path(path).open("wb") as f:
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
- return self._response.json()
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
- controller = hishel.Controller(
113
+ ctl = hishel.Controller(
104
114
  cacheable_methods=["GET"],
105
115
  cacheable_status_codes=[200],
106
116
  allow_stale=True,
107
117
  )
108
- transport = hishel.AsyncCacheTransport(
109
- transport=httpx.AsyncHTTPTransport(),
110
- storage=storage,
111
- controller=controller,
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 pathlib.Path(path).open("wb") as f:
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=pathlib.Path.cwd() / "file.zip",
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
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