imgproxykit 0.6.0__tar.gz → 0.7.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.
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/PKG-INFO +14 -14
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/README.md +7 -7
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/client.py +11 -4
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/django/settings.py +15 -14
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/drf/serializers.py +2 -2
- imgproxykit-0.7.0/imgproxykit/internal/typing.py +75 -0
- imgproxykit-0.7.0/imgproxykit/serialization.py +297 -0
- imgproxykit-0.7.0/imgproxykit/transform.py +175 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/variants.py +4 -4
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/pyproject.toml +22 -20
- imgproxykit-0.6.0/imgproxykit/keyword_options.py +0 -92
- imgproxykit-0.6.0/imgproxykit/serialization.py +0 -149
- imgproxykit-0.6.0/imgproxykit/transform.py +0 -81
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/LICENSE +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/__init__.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/__init__.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/django/__init__.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/django/apps.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/django/client.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/django/shortcuts.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/django/sources.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/django/templatetags/__init__.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/django/templatetags/imgproxykit.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/drf/__init__.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/fastapi/__init__.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/fastapi/dependencies.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/litestar/__init__.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/integrations/litestar/plugin.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/internal/__init__.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/internal/encoding.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/internal/types.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/__init__.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/auto_rotate.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/background.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/base.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/blur.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/cache_buster.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/crop.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/dpr.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/enforce_thumbnail.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/enlarge.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/expires.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/extend.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/extend_aspect_ratio.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/filename.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/flip.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/format.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/format_quality.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/gravity.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/height.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/keep_copyright.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/max_animation_frame_resolution.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/max_animation_frames.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/max_bytes.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/max_result_dimension.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/max_src_file_size.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/max_src_resolution.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/min_height.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/min_width.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/padding.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/pixelate.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/preserve_hdr.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/preset.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/quality.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/raw.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/resize.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/resizing_type.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/return_attachment.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/rotate.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/sharpen.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/size.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/skip_processing.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/strip_color_profile.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/strip_metadata.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/trim.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/watermark.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/width.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/options/zoom.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/py.typed +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/signing.py +0 -0
- {imgproxykit-0.6.0 → imgproxykit-0.7.0}/imgproxykit/types.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: imgproxykit
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.0
|
|
4
4
|
Summary: A typed Python toolkit for building imgproxy URLs
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -24,16 +24,16 @@ Provides-Extra: django
|
|
|
24
24
|
Provides-Extra: drf
|
|
25
25
|
Provides-Extra: fastapi
|
|
26
26
|
Provides-Extra: litestar
|
|
27
|
-
Requires-Dist: django (>=
|
|
28
|
-
Requires-Dist: django (>=
|
|
27
|
+
Requires-Dist: django (>=6.1.3,<7) ; extra == "django"
|
|
28
|
+
Requires-Dist: django (>=6.1.3,<7) ; extra == "drf"
|
|
29
29
|
Requires-Dist: djangorestframework (>=3.16,<4) ; extra == "drf"
|
|
30
|
-
Requires-Dist: fastapi (>=0.
|
|
30
|
+
Requires-Dist: fastapi (>=0.165.0,<1) ; extra == "fastapi"
|
|
31
31
|
Requires-Dist: litestar (>=2.24,<3) ; extra == "litestar"
|
|
32
32
|
Requires-Dist: typing-extensions (>=4.16,<5)
|
|
33
|
-
Project-URL: Changelog, https://github.com/
|
|
33
|
+
Project-URL: Changelog, https://github.com/wemake-services/imgproxykit/releases
|
|
34
34
|
Project-URL: Documentation, https://imgproxykit.readthedocs.io/en/latest/
|
|
35
|
-
Project-URL: GitHub, https://github.com/
|
|
36
|
-
Project-URL: Issues, https://github.com/
|
|
35
|
+
Project-URL: GitHub, https://github.com/wemake-services/imgproxykit
|
|
36
|
+
Project-URL: Issues, https://github.com/wemake-services/imgproxykit/issues
|
|
37
37
|
Description-Content-Type: text/markdown
|
|
38
38
|
|
|
39
39
|
<h1 align="center">imgproxykit</h1>
|
|
@@ -44,11 +44,12 @@ Description-Content-Type: text/markdown
|
|
|
44
44
|
|
|
45
45
|
<div align="center">
|
|
46
46
|
|
|
47
|
-
[](https://github.com/wemake-services/imgproxykit/actions/workflows/test.yml)
|
|
48
48
|
[](https://pypi.org/project/imgproxykit/)
|
|
49
49
|
[](https://pepy.tech/projects/imgproxykit)
|
|
50
50
|
[](https://pypi.org/project/imgproxykit/)
|
|
51
51
|
[](LICENSE)
|
|
52
|
+
[](https://deepwiki.com/wemake-services/imgproxykit)
|
|
52
53
|
|
|
53
54
|
</div>
|
|
54
55
|
|
|
@@ -78,8 +79,9 @@ pip install imgproxykit
|
|
|
78
79
|
|
|
79
80
|
There are several included extras:
|
|
80
81
|
|
|
81
|
-
- `'imgproxykit[django]'` provides [Django
|
|
82
|
-
|
|
82
|
+
- `'imgproxykit[django]'` provides [Django](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/django.html)
|
|
83
|
+
and [Django-Modern-REST](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/dmr.html)
|
|
84
|
+
integrations with shortcuts, template tags, and client configuration through Django settings
|
|
83
85
|
- `'imgproxykit[drf]'` provides [Django REST Framework integration](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/drf.html)
|
|
84
86
|
with serializer fields for image URLs and named variants
|
|
85
87
|
- `'imgproxykit[fastapi]'` provides [FastAPI integration](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/fastapi.html)
|
|
@@ -139,12 +141,10 @@ Build variants by extending a shared transform with a width for each size:
|
|
|
139
141
|
>>> source_url = 'https://originals.example.com/products/backpack.jpg'
|
|
140
142
|
>>> # Build one URL per variant for the same source image.
|
|
141
143
|
>>> product_urls = client.variants(source=source_url, variants=products)
|
|
142
|
-
>>> small_url = product_urls['small'] # Access a URL by its variant name.
|
|
143
144
|
|
|
144
145
|
```
|
|
145
146
|
|
|
146
|
-
`product_urls` maps each variant name to its generated URL
|
|
147
|
-
(shown here with the encoded source abbreviated):
|
|
147
|
+
`product_urls` maps each variant name to its generated URL:
|
|
148
148
|
|
|
149
149
|
```text
|
|
150
150
|
{
|
|
@@ -166,7 +166,7 @@ Build variants by extending a shared transform with a width for each size:
|
|
|
166
166
|
|
|
167
167
|
See the [contributing guide](CONTRIBUTING.md) for development setup and checks.
|
|
168
168
|
Bug reports and feature requests are welcome on the
|
|
169
|
-
[issue tracker](https://github.com/
|
|
169
|
+
[issue tracker](https://github.com/wemake-services/imgproxykit/issues).
|
|
170
170
|
|
|
171
171
|
## License
|
|
172
172
|
|
|
@@ -6,11 +6,12 @@
|
|
|
6
6
|
|
|
7
7
|
<div align="center">
|
|
8
8
|
|
|
9
|
-
[](https://github.com/wemake-services/imgproxykit/actions/workflows/test.yml)
|
|
10
10
|
[](https://pypi.org/project/imgproxykit/)
|
|
11
11
|
[](https://pepy.tech/projects/imgproxykit)
|
|
12
12
|
[](https://pypi.org/project/imgproxykit/)
|
|
13
13
|
[](LICENSE)
|
|
14
|
+
[](https://deepwiki.com/wemake-services/imgproxykit)
|
|
14
15
|
|
|
15
16
|
</div>
|
|
16
17
|
|
|
@@ -40,8 +41,9 @@ pip install imgproxykit
|
|
|
40
41
|
|
|
41
42
|
There are several included extras:
|
|
42
43
|
|
|
43
|
-
- `'imgproxykit[django]'` provides [Django
|
|
44
|
-
|
|
44
|
+
- `'imgproxykit[django]'` provides [Django](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/django.html)
|
|
45
|
+
and [Django-Modern-REST](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/dmr.html)
|
|
46
|
+
integrations with shortcuts, template tags, and client configuration through Django settings
|
|
45
47
|
- `'imgproxykit[drf]'` provides [Django REST Framework integration](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/drf.html)
|
|
46
48
|
with serializer fields for image URLs and named variants
|
|
47
49
|
- `'imgproxykit[fastapi]'` provides [FastAPI integration](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/fastapi.html)
|
|
@@ -101,12 +103,10 @@ Build variants by extending a shared transform with a width for each size:
|
|
|
101
103
|
>>> source_url = 'https://originals.example.com/products/backpack.jpg'
|
|
102
104
|
>>> # Build one URL per variant for the same source image.
|
|
103
105
|
>>> product_urls = client.variants(source=source_url, variants=products)
|
|
104
|
-
>>> small_url = product_urls['small'] # Access a URL by its variant name.
|
|
105
106
|
|
|
106
107
|
```
|
|
107
108
|
|
|
108
|
-
`product_urls` maps each variant name to its generated URL
|
|
109
|
-
(shown here with the encoded source abbreviated):
|
|
109
|
+
`product_urls` maps each variant name to its generated URL:
|
|
110
110
|
|
|
111
111
|
```text
|
|
112
112
|
{
|
|
@@ -128,7 +128,7 @@ Build variants by extending a shared transform with a width for each size:
|
|
|
128
128
|
|
|
129
129
|
See the [contributing guide](CONTRIBUTING.md) for development setup and checks.
|
|
130
130
|
Bug reports and feature requests are welcome on the
|
|
131
|
-
[issue tracker](https://github.com/
|
|
131
|
+
[issue tracker](https://github.com/wemake-services/imgproxykit/issues).
|
|
132
132
|
|
|
133
133
|
## License
|
|
134
134
|
|
|
@@ -31,11 +31,8 @@ class Client:
|
|
|
31
31
|
'_url_signer',
|
|
32
32
|
)
|
|
33
33
|
|
|
34
|
-
#: Component that resolves processing parameters and builds unsigned paths.
|
|
35
34
|
path_serializer_cls: ClassVar[type[PathSerializer]] = PathSerializer
|
|
36
|
-
#: Component that signs paths or returns the unsigned URL marker.
|
|
37
35
|
url_signer_cls: ClassVar[type[UrlSigner]] = UrlSigner
|
|
38
|
-
#: Component that validates dataclass variant declarations.
|
|
39
36
|
variants_resolver_cls: ClassVar[type[DataclassVariantsResolver]] = (
|
|
40
37
|
DataclassVariantsResolver
|
|
41
38
|
)
|
|
@@ -52,6 +49,7 @@ class Client:
|
|
|
52
49
|
unsigned_marker: str = 'unsafe',
|
|
53
50
|
argument_separator: str = ':',
|
|
54
51
|
use_short_options: bool = False,
|
|
52
|
+
only_presets: bool = False,
|
|
55
53
|
) -> None: ...
|
|
56
54
|
|
|
57
55
|
@overload
|
|
@@ -66,6 +64,7 @@ class Client:
|
|
|
66
64
|
signature_size: int = 32,
|
|
67
65
|
argument_separator: str = ':',
|
|
68
66
|
use_short_options: bool = False,
|
|
67
|
+
only_presets: bool = False,
|
|
69
68
|
) -> None: ...
|
|
70
69
|
|
|
71
70
|
def __init__( # noqa: WPS211
|
|
@@ -80,6 +79,7 @@ class Client:
|
|
|
80
79
|
unsigned_marker: str = 'unsafe',
|
|
81
80
|
argument_separator: str = ':',
|
|
82
81
|
use_short_options: bool = False,
|
|
82
|
+
only_presets: bool = False,
|
|
83
83
|
) -> None:
|
|
84
84
|
"""
|
|
85
85
|
Configure the imgproxy address and create the client components.
|
|
@@ -117,6 +117,12 @@ class Client:
|
|
|
117
117
|
<https://docs.imgproxy.net/usage/processing#source-url>`__.
|
|
118
118
|
use_short_options: Use short processing option aliases when
|
|
119
119
|
available, falling back to full names. Defaults to ``False``.
|
|
120
|
+
only_presets: Match ``IMGPROXY_ONLY_PRESETS`` on the server.
|
|
121
|
+
Require nonempty Preset options, optionally combined with Format
|
|
122
|
+
options. The last format becomes a source suffix. Other
|
|
123
|
+
processing options are rejected. Defaults to ``False``.
|
|
124
|
+
See `IMGPROXY_ONLY_PRESETS
|
|
125
|
+
<https://docs.imgproxy.net/usage/presets#only-presets>`__.
|
|
120
126
|
|
|
121
127
|
Raises:
|
|
122
128
|
ValueError: With the default signer, if only one signing
|
|
@@ -144,6 +150,7 @@ class Client:
|
|
|
144
150
|
argument_separator=argument_separator,
|
|
145
151
|
use_short_options=use_short_options,
|
|
146
152
|
source_encoding=source_encoding,
|
|
153
|
+
only_presets=only_presets,
|
|
147
154
|
)
|
|
148
155
|
self._url_signer = self.url_signer_cls(
|
|
149
156
|
key=key,
|
|
@@ -233,4 +240,4 @@ class Client:
|
|
|
233
240
|
name: self.url(source, transform=transform) # ty: ignore[invalid-argument-type]
|
|
234
241
|
for name, transform in transforms.items()
|
|
235
242
|
}
|
|
236
|
-
return urls if isinstance(variants, Mapping) else variants(**urls) # ty: ignore[invalid-return
|
|
243
|
+
return urls if isinstance(variants, Mapping) else variants(**urls) # ty: ignore[invalid-explicit-return, invalid-argument-type]
|
|
@@ -12,8 +12,6 @@ from imgproxykit.types import SourceEncoding
|
|
|
12
12
|
if TYPE_CHECKING:
|
|
13
13
|
from _typeshed import DataclassInstance
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
#: Django setting containing the imgproxy client configuration.
|
|
17
15
|
IMGPROXYKIT_SETTINGS: Final = 'IMGPROXYKIT'
|
|
18
16
|
|
|
19
17
|
|
|
@@ -29,6 +27,7 @@ class ClientSettings(enum.StrEnum):
|
|
|
29
27
|
unsigned_marker = 'unsigned_marker'
|
|
30
28
|
argument_separator = 'argument_separator'
|
|
31
29
|
use_short_options = 'use_short_options'
|
|
30
|
+
only_presets = 'only_presets'
|
|
32
31
|
source_encoding = 'source_encoding'
|
|
33
32
|
|
|
34
33
|
|
|
@@ -46,35 +45,37 @@ class Settings(enum.StrEnum):
|
|
|
46
45
|
class ClientSettingsDict(TypedDict, total=False, closed=True):
|
|
47
46
|
"""Describe client constructor options for static type checking."""
|
|
48
47
|
|
|
49
|
-
#: Public imgproxy address, including any deployment path prefix.
|
|
50
48
|
base_url: Required[str]
|
|
51
|
-
|
|
49
|
+
"""Public imgproxy address, including any deployment path prefix."""
|
|
52
50
|
key: str | None
|
|
53
|
-
|
|
51
|
+
"""Hex-encoded signing key matching the imgproxy server."""
|
|
54
52
|
salt: str | None
|
|
55
|
-
|
|
53
|
+
"""Hex-encoded signing salt matching the imgproxy server."""
|
|
56
54
|
signature_size: int
|
|
57
|
-
|
|
55
|
+
"""Positive signature digest byte count; must match the server."""
|
|
58
56
|
unsigned_marker: str
|
|
59
|
-
|
|
57
|
+
"""Signature marker used when signing credentials are omitted."""
|
|
60
58
|
argument_separator: str
|
|
61
|
-
|
|
59
|
+
"""Processing argument separator; must match the server."""
|
|
62
60
|
use_short_options: bool
|
|
63
|
-
|
|
61
|
+
"""Use short processing option aliases when available."""
|
|
62
|
+
only_presets: bool
|
|
63
|
+
"""Serialize only preset options, ignoring other processing options."""
|
|
64
64
|
source_encoding: SourceEncoding
|
|
65
|
+
"""Source URL representation using Base64 or percent-encoded plain mode."""
|
|
65
66
|
|
|
66
67
|
|
|
67
68
|
class SettingsDict(TypedDict, total=False, closed=True):
|
|
68
69
|
"""Describe the IMGPROXYKIT dictionary for static type checking."""
|
|
69
70
|
|
|
70
|
-
#: Client constructor options; defaults remain in the core Client.
|
|
71
71
|
client: Required[ClientSettingsDict]
|
|
72
|
-
|
|
72
|
+
"""Client constructor options; defaults remain in the core Client."""
|
|
73
73
|
transforms: Mapping[str, Transform]
|
|
74
|
-
|
|
74
|
+
"""Named Python transformations, distinct from imgproxy server presets."""
|
|
75
75
|
variants: Mapping[str, Mapping[str, Transform] | type['DataclassInstance']]
|
|
76
|
-
|
|
76
|
+
"""Named sets of transformations; objects can be shared with transforms."""
|
|
77
77
|
source_resolver: Callable[[str | FieldFile], str] | str
|
|
78
|
+
"""Convert the original source to a URI, including empty file fields."""
|
|
78
79
|
|
|
79
80
|
|
|
80
81
|
assert set(SettingsDict.__annotations__) == set(Settings), ( # noqa: S101
|
|
@@ -51,7 +51,7 @@ class ImgproxyURLField(serializers.URLField):
|
|
|
51
51
|
|
|
52
52
|
# Empty sources produce None, which URLField's return type excludes.
|
|
53
53
|
@override
|
|
54
|
-
def to_representation( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] # ty: ignore[
|
|
54
|
+
def to_representation( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] # ty: ignore[incompatible-method-override]
|
|
55
55
|
self,
|
|
56
56
|
value: str | FieldFile | None,
|
|
57
57
|
) -> str | None:
|
|
@@ -111,7 +111,7 @@ class ImgproxyVariantsField(serializers.DictField):
|
|
|
111
111
|
|
|
112
112
|
# DictField's types exclude source strings, model files, and None output.
|
|
113
113
|
@override
|
|
114
|
-
def to_representation( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] # ty: ignore[
|
|
114
|
+
def to_representation( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] # ty: ignore[incompatible-method-override]
|
|
115
115
|
self,
|
|
116
116
|
value: str | FieldFile | None, # type: ignore[override]
|
|
117
117
|
) -> dict[str, str] | None:
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Parts of the code are adapted from
|
|
2
|
+
# https://github.com/wemake-services/django-modern-rest/blob/4aec07961229cddbdfa1b67d415e552def144bc8/dmr/internal/types.py
|
|
3
|
+
# under MIT license.
|
|
4
|
+
|
|
5
|
+
# Original license:
|
|
6
|
+
# https://github.com/wemake-services/django-modern-rest/blob/4aec07961229cddbdfa1b67d415e552def144bc8/LICENSE
|
|
7
|
+
|
|
8
|
+
# The MIT License (MIT)
|
|
9
|
+
# Copyright (c) 2025 wemake-services
|
|
10
|
+
#
|
|
11
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
12
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
13
|
+
# in the Software without restriction, including without limitation the rights
|
|
14
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
15
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
16
|
+
# furnished to do so, subject to the following conditions:
|
|
17
|
+
#
|
|
18
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
19
|
+
# copies or substantial portions of the Software.
|
|
20
|
+
#
|
|
21
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
22
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
23
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
24
|
+
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
25
|
+
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
26
|
+
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
|
27
|
+
# OR OTHER DEALINGS IN THE SOFTWARE.
|
|
28
|
+
|
|
29
|
+
from typing import Any, Final, TypeAliasType, get_args, get_origin
|
|
30
|
+
|
|
31
|
+
import typing_extensions
|
|
32
|
+
|
|
33
|
+
# `typing` and `typing_extensions` define distinct alias classes.
|
|
34
|
+
_TYPE_ALIAS_TYPES: Final = (TypeAliasType, typing_extensions.TypeAliasType)
|
|
35
|
+
|
|
36
|
+
# Bound alias chains to avoid hanging on mutually recursive aliases.
|
|
37
|
+
_MAX_ALIAS_DEPTH: Final = 15
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def unwrap_type_alias(
|
|
41
|
+
annotation: typing_extensions.TypeForm[Any],
|
|
42
|
+
) -> typing_extensions.TypeForm[Any]:
|
|
43
|
+
"""
|
|
44
|
+
Replaces a type alias with the type it points to.
|
|
45
|
+
|
|
46
|
+
Type aliases created with the ``type X = Y`` syntax
|
|
47
|
+
or with ``TypeAliasType`` directly are lazy objects that hide
|
|
48
|
+
the real type behind them, so things like ``Annotated``
|
|
49
|
+
metadata are not visible without unwrapping.
|
|
50
|
+
|
|
51
|
+
Aliases of aliases and subscripted generic aliases
|
|
52
|
+
are unwrapped as well. Anything that is not a type alias
|
|
53
|
+
is returned unchanged.
|
|
54
|
+
|
|
55
|
+
Raises:
|
|
56
|
+
TypeError: when there are too many nested aliases,
|
|
57
|
+
which also happens for mutually recursive ones.
|
|
58
|
+
|
|
59
|
+
"""
|
|
60
|
+
# Type checkers do not know about `__value__` of type aliases:
|
|
61
|
+
current: Any = annotation
|
|
62
|
+
for _ in range(_MAX_ALIAS_DEPTH):
|
|
63
|
+
origin = get_origin(current)
|
|
64
|
+
if isinstance(current, _TYPE_ALIAS_TYPES):
|
|
65
|
+
current = current.__value__
|
|
66
|
+
elif isinstance(origin, _TYPE_ALIAS_TYPES):
|
|
67
|
+
# Generic aliases keep their args outside of `__value__`,
|
|
68
|
+
# we apply them back to get the real type:
|
|
69
|
+
current = origin.__value__[get_args(current)]
|
|
70
|
+
else:
|
|
71
|
+
unwrapped: typing_extensions.TypeForm[Any] = current
|
|
72
|
+
return unwrapped
|
|
73
|
+
raise TypeError(
|
|
74
|
+
f'Cannot unwrap {current!r}, too many nested type aliases',
|
|
75
|
+
)
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
from collections.abc import Callable, Mapping
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import ClassVar
|
|
4
|
+
|
|
5
|
+
from imgproxykit.internal.encoding import encode_base64, encode_plain
|
|
6
|
+
from imgproxykit.options import Format, Option, Preset
|
|
7
|
+
from imgproxykit.transform import Transform
|
|
8
|
+
from imgproxykit.types import Argument, FormatType, SourceEncoding
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(frozen=True, slots=True)
|
|
12
|
+
class _SourceCodec:
|
|
13
|
+
"""Source encoder, path prefix, and output format separator."""
|
|
14
|
+
|
|
15
|
+
encoder: Callable[[str], str]
|
|
16
|
+
prefix: str
|
|
17
|
+
format_separator: str
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True, slots=True)
|
|
21
|
+
class SerializedTransform:
|
|
22
|
+
"""Ordered option segments and an optional source format suffix."""
|
|
23
|
+
|
|
24
|
+
segments: tuple[str, ...]
|
|
25
|
+
format_type: FormatType | None = None
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class SourceSerializer:
|
|
29
|
+
"""Encode a source URI into a source path, including its marker."""
|
|
30
|
+
|
|
31
|
+
__slots__ = ('_codec',)
|
|
32
|
+
|
|
33
|
+
# Source representations defined by imgproxy:
|
|
34
|
+
# https://docs.imgproxy.net/usage/processing#source-url
|
|
35
|
+
_codecs: ClassVar[Mapping[SourceEncoding, _SourceCodec]] = {
|
|
36
|
+
'base64': _SourceCodec(
|
|
37
|
+
encoder=encode_base64,
|
|
38
|
+
prefix='',
|
|
39
|
+
format_separator='.',
|
|
40
|
+
),
|
|
41
|
+
'plain': _SourceCodec(
|
|
42
|
+
encoder=encode_plain,
|
|
43
|
+
prefix='plain/',
|
|
44
|
+
format_separator='@',
|
|
45
|
+
),
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
def __init__(self, *, source_encoding: SourceEncoding) -> None:
|
|
49
|
+
"""Select the codec for the configured source encoding."""
|
|
50
|
+
self._codec = self._resolve_codec(source_encoding)
|
|
51
|
+
|
|
52
|
+
def __call__(
|
|
53
|
+
self,
|
|
54
|
+
source: str,
|
|
55
|
+
*,
|
|
56
|
+
format_type: FormatType | None = None,
|
|
57
|
+
) -> str:
|
|
58
|
+
"""
|
|
59
|
+
Encode the source URL and append an optional output format.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
source: Complete source URI understood by the imgproxy server.
|
|
63
|
+
format_type: Output format suffix. Omit to add no suffix.
|
|
64
|
+
"""
|
|
65
|
+
codec = self._codec
|
|
66
|
+
encoded = codec.encoder(source)
|
|
67
|
+
source_path = f'{codec.prefix}{encoded}'
|
|
68
|
+
|
|
69
|
+
if format_type is None:
|
|
70
|
+
return source_path
|
|
71
|
+
|
|
72
|
+
return f'{source_path}{codec.format_separator}{format_type}'
|
|
73
|
+
|
|
74
|
+
@classmethod
|
|
75
|
+
def _resolve_codec(cls, source_encoding: SourceEncoding) -> _SourceCodec:
|
|
76
|
+
"""Resolve the source codec or reject an unknown encoding."""
|
|
77
|
+
codec = cls._codecs.get(source_encoding)
|
|
78
|
+
if codec is None:
|
|
79
|
+
raise ValueError(
|
|
80
|
+
f'Unknown source encoding {source_encoding!r}. '
|
|
81
|
+
f'Supported encodings: {list(cls._codecs)!r}.',
|
|
82
|
+
)
|
|
83
|
+
return codec
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class OptionSerializer:
|
|
87
|
+
"""Serialize an option name and arguments into a processing segment."""
|
|
88
|
+
|
|
89
|
+
__slots__ = ('_argument_separator',)
|
|
90
|
+
|
|
91
|
+
def __init__(self, *, argument_separator: str) -> None:
|
|
92
|
+
"""Configure the separator between the name and positional arguments."""
|
|
93
|
+
self._argument_separator = argument_separator
|
|
94
|
+
|
|
95
|
+
def __call__(
|
|
96
|
+
self,
|
|
97
|
+
name: str,
|
|
98
|
+
arguments: tuple[Argument, ...],
|
|
99
|
+
) -> str:
|
|
100
|
+
"""
|
|
101
|
+
Serialize an option name and positional arguments into a path segment.
|
|
102
|
+
|
|
103
|
+
Internal ``None`` values become empty positions; trailing ``None``
|
|
104
|
+
values are omitted. Explicit zeroes and empty strings retain their
|
|
105
|
+
positions. Booleans are serialized as ``1`` and ``0``.
|
|
106
|
+
"""
|
|
107
|
+
end = len(arguments)
|
|
108
|
+
while end and arguments[end - 1] is None:
|
|
109
|
+
end -= 1
|
|
110
|
+
|
|
111
|
+
return self._argument_separator.join(
|
|
112
|
+
(
|
|
113
|
+
name,
|
|
114
|
+
*(
|
|
115
|
+
self._serialize_argument(argument)
|
|
116
|
+
for argument in arguments[:end]
|
|
117
|
+
),
|
|
118
|
+
),
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
@classmethod
|
|
122
|
+
def _serialize_argument(cls, value: Argument) -> str:
|
|
123
|
+
"""Serialize None as empty, booleans as integers, and others as text."""
|
|
124
|
+
if value is None:
|
|
125
|
+
return ''
|
|
126
|
+
return str(int(value) if isinstance(value, bool) else value)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class TransformSerializer:
|
|
130
|
+
"""Serialize transformation options in order, preserving repetitions."""
|
|
131
|
+
|
|
132
|
+
__slots__ = ('_option_serializer', '_use_short_options')
|
|
133
|
+
|
|
134
|
+
option_serializer_cls: ClassVar[type[OptionSerializer]] = OptionSerializer
|
|
135
|
+
|
|
136
|
+
def __init__(
|
|
137
|
+
self,
|
|
138
|
+
*,
|
|
139
|
+
argument_separator: str,
|
|
140
|
+
use_short_options: bool,
|
|
141
|
+
) -> None:
|
|
142
|
+
"""Configure argument separation and processing option aliases."""
|
|
143
|
+
self._option_serializer = self.option_serializer_cls(
|
|
144
|
+
argument_separator=argument_separator,
|
|
145
|
+
)
|
|
146
|
+
self._use_short_options = use_short_options
|
|
147
|
+
|
|
148
|
+
def __call__(self, transform: Transform) -> SerializedTransform:
|
|
149
|
+
"""Build option segments; empty transforms add none."""
|
|
150
|
+
segments = tuple(
|
|
151
|
+
self._option_serializer(
|
|
152
|
+
(option.short_name or option.name)
|
|
153
|
+
if self._use_short_options
|
|
154
|
+
else option.name,
|
|
155
|
+
option.arguments,
|
|
156
|
+
)
|
|
157
|
+
for option in transform.options
|
|
158
|
+
)
|
|
159
|
+
return SerializedTransform(segments=segments)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
class PresetsSerializer:
|
|
163
|
+
"""Serialize server presets and an output format in presets-only mode."""
|
|
164
|
+
|
|
165
|
+
__slots__ = ('_argument_separator',)
|
|
166
|
+
|
|
167
|
+
def __init__(self, *, argument_separator: str) -> None:
|
|
168
|
+
"""Configure the separator between preset names."""
|
|
169
|
+
self._argument_separator = argument_separator
|
|
170
|
+
|
|
171
|
+
def __call__(self, transform: Transform) -> SerializedTransform:
|
|
172
|
+
"""Collect ordered preset names and an optional source format suffix."""
|
|
173
|
+
names, output_format = self._collect_presets(transform.options)
|
|
174
|
+
if not names or not all(names):
|
|
175
|
+
raise ValueError(
|
|
176
|
+
'Presets-only mode requires at least one preset. '
|
|
177
|
+
'All preset names must be nonempty strings.',
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
return SerializedTransform(
|
|
181
|
+
segments=(self._argument_separator.join(names),),
|
|
182
|
+
format_type=output_format,
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
@classmethod
|
|
186
|
+
def _collect_presets(
|
|
187
|
+
cls,
|
|
188
|
+
options: tuple[Option, ...],
|
|
189
|
+
) -> tuple[list[str], FormatType | None]:
|
|
190
|
+
"""Collect ordered preset names and the last format."""
|
|
191
|
+
names: list[str] = []
|
|
192
|
+
output_format = None
|
|
193
|
+
for option in options:
|
|
194
|
+
if isinstance(option, Preset):
|
|
195
|
+
names.extend(option.names)
|
|
196
|
+
elif isinstance(option, Format):
|
|
197
|
+
output_format = option.value
|
|
198
|
+
else:
|
|
199
|
+
supported = [Preset.name, Format.name]
|
|
200
|
+
raise TypeError(
|
|
201
|
+
f'Option {option.name!r} is not allowed '
|
|
202
|
+
'in presets-only mode. '
|
|
203
|
+
f'Supported options: {supported!r}.',
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
return names, output_format
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
class PathSerializer:
|
|
210
|
+
"""
|
|
211
|
+
Serialize processing options and a source into an unsigned imgproxy path.
|
|
212
|
+
|
|
213
|
+
Subclass and override :meth:`__call__` to customize path construction.
|
|
214
|
+
Set the subclass on :attr:`imgproxykit.Client.path_serializer_cls`.
|
|
215
|
+
"""
|
|
216
|
+
|
|
217
|
+
__slots__ = (
|
|
218
|
+
'_source_serializer',
|
|
219
|
+
'_transform_serializer',
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
transform_serializer_cls: ClassVar[type[TransformSerializer]] = (
|
|
223
|
+
TransformSerializer
|
|
224
|
+
)
|
|
225
|
+
presets_serializer_cls: ClassVar[type[PresetsSerializer]] = (
|
|
226
|
+
PresetsSerializer
|
|
227
|
+
)
|
|
228
|
+
source_serializer_cls: ClassVar[type[SourceSerializer]] = SourceSerializer
|
|
229
|
+
|
|
230
|
+
def __init__(
|
|
231
|
+
self,
|
|
232
|
+
*,
|
|
233
|
+
argument_separator: str,
|
|
234
|
+
use_short_options: bool,
|
|
235
|
+
source_encoding: SourceEncoding,
|
|
236
|
+
only_presets: bool,
|
|
237
|
+
) -> None:
|
|
238
|
+
"""
|
|
239
|
+
Configure processing options and select the source URL serializer.
|
|
240
|
+
|
|
241
|
+
Args:
|
|
242
|
+
argument_separator: Separator between processing option names
|
|
243
|
+
and their arguments. Must match the server's
|
|
244
|
+
`IMGPROXY_ARGUMENTS_SEPARATOR
|
|
245
|
+
<https://docs.imgproxy.net/latest/configuration/options#miscellaneous>`__.
|
|
246
|
+
source_encoding: Source URL representation: ``'base64'`` without
|
|
247
|
+
padding or ``'plain'`` with full UTF-8 percent-encoding.
|
|
248
|
+
See `imgproxy source URLs
|
|
249
|
+
<https://docs.imgproxy.net/usage/processing#source-url>`_.
|
|
250
|
+
use_short_options: Use short aliases when available,
|
|
251
|
+
otherwise full names. See `imgproxy processing options
|
|
252
|
+
<https://docs.imgproxy.net/usage/processing#processing-options>`_.
|
|
253
|
+
only_presets: Match server presets-only mode. Require presets,
|
|
254
|
+
optionally combined with output format options. See
|
|
255
|
+
`imgproxy presets-only mode
|
|
256
|
+
<https://docs.imgproxy.net/usage/presets#only-presets>`_.
|
|
257
|
+
"""
|
|
258
|
+
self._transform_serializer = (
|
|
259
|
+
self.presets_serializer_cls(argument_separator=argument_separator)
|
|
260
|
+
if only_presets
|
|
261
|
+
else self.transform_serializer_cls(
|
|
262
|
+
argument_separator=argument_separator,
|
|
263
|
+
use_short_options=use_short_options,
|
|
264
|
+
)
|
|
265
|
+
)
|
|
266
|
+
self._source_serializer = self.source_serializer_cls(
|
|
267
|
+
source_encoding=source_encoding,
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
def __call__(
|
|
271
|
+
self,
|
|
272
|
+
source: str,
|
|
273
|
+
*,
|
|
274
|
+
transform: Transform | None,
|
|
275
|
+
) -> str:
|
|
276
|
+
"""
|
|
277
|
+
Build path using processing options and preset references.
|
|
278
|
+
|
|
279
|
+
Serialize options before the encoded source. In presets-only mode,
|
|
280
|
+
the last format becomes a source suffix; otherwise it stays an option.
|
|
281
|
+
Omitted processing parameters are left to the server.
|
|
282
|
+
|
|
283
|
+
Args:
|
|
284
|
+
source: Complete source URI understood by the imgproxy server.
|
|
285
|
+
Encoded according to ``source_encoding``.
|
|
286
|
+
transform: Reusable processing options and optional output format.
|
|
287
|
+
"""
|
|
288
|
+
if transform is None:
|
|
289
|
+
# Give transform serializers a concrete instance, even when omitted.
|
|
290
|
+
transform = Transform()
|
|
291
|
+
|
|
292
|
+
serialized_transform = self._transform_serializer(transform)
|
|
293
|
+
source_path = self._source_serializer(
|
|
294
|
+
source,
|
|
295
|
+
format_type=serialized_transform.format_type,
|
|
296
|
+
)
|
|
297
|
+
return '/'.join(('', *serialized_transform.segments, source_path))
|