imgproxykit 0.4.0__tar.gz → 0.6.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.
Files changed (87) hide show
  1. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/PKG-INFO +54 -18
  2. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/README.md +47 -17
  3. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/client.py +77 -34
  4. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/integrations/django/settings.py +17 -7
  5. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/integrations/django/shortcuts.py +28 -13
  6. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/integrations/django/sources.py +0 -2
  7. imgproxykit-0.6.0/imgproxykit/integrations/drf/__init__.py +16 -0
  8. imgproxykit-0.6.0/imgproxykit/integrations/drf/serializers.py +123 -0
  9. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/integrations/fastapi/dependencies.py +1 -1
  10. imgproxykit-0.6.0/imgproxykit/integrations/litestar/__init__.py +13 -0
  11. imgproxykit-0.6.0/imgproxykit/integrations/litestar/plugin.py +93 -0
  12. imgproxykit-0.6.0/imgproxykit/internal/encoding.py +31 -0
  13. imgproxykit-0.6.0/imgproxykit/internal/types.py +9 -0
  14. imgproxykit-0.6.0/imgproxykit/keyword_options.py +92 -0
  15. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/auto_rotate.py +2 -3
  16. imgproxykit-0.6.0/imgproxykit/options/background.py +67 -0
  17. imgproxykit-0.6.0/imgproxykit/options/base.py +68 -0
  18. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/blur.py +2 -3
  19. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/cache_buster.py +3 -3
  20. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/crop.py +18 -15
  21. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/dpr.py +1 -3
  22. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/enforce_thumbnail.py +2 -3
  23. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/enlarge.py +2 -3
  24. imgproxykit-0.6.0/imgproxykit/options/expires.py +57 -0
  25. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/extend.py +18 -14
  26. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/extend_aspect_ratio.py +18 -14
  27. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/filename.py +3 -3
  28. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/flip.py +3 -3
  29. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/format.py +3 -6
  30. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/format_quality.py +3 -3
  31. imgproxykit-0.6.0/imgproxykit/options/gravity.py +152 -0
  32. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/height.py +2 -3
  33. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/keep_copyright.py +2 -3
  34. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/max_animation_frame_resolution.py +2 -3
  35. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/max_animation_frames.py +2 -3
  36. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/max_bytes.py +2 -3
  37. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/max_result_dimension.py +2 -3
  38. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/max_src_file_size.py +2 -3
  39. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/max_src_resolution.py +2 -3
  40. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/min_height.py +2 -3
  41. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/min_width.py +2 -3
  42. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/padding.py +21 -5
  43. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/pixelate.py +2 -3
  44. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/preserve_hdr.py +2 -3
  45. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/preset.py +5 -6
  46. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/quality.py +2 -6
  47. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/raw.py +1 -3
  48. imgproxykit-0.6.0/imgproxykit/options/resize.py +149 -0
  49. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/resizing_type.py +3 -3
  50. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/return_attachment.py +2 -3
  51. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/rotate.py +2 -3
  52. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/sharpen.py +2 -3
  53. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/size.py +3 -3
  54. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/skip_processing.py +3 -3
  55. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/strip_color_profile.py +2 -3
  56. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/strip_metadata.py +2 -3
  57. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/trim.py +32 -6
  58. imgproxykit-0.6.0/imgproxykit/options/watermark.py +236 -0
  59. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/width.py +2 -3
  60. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/zoom.py +3 -3
  61. imgproxykit-0.6.0/imgproxykit/serialization.py +149 -0
  62. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/signing.py +5 -7
  63. imgproxykit-0.6.0/imgproxykit/transform.py +81 -0
  64. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/types.py +24 -69
  65. imgproxykit-0.6.0/imgproxykit/variants.py +91 -0
  66. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/pyproject.toml +27 -9
  67. imgproxykit-0.4.0/imgproxykit/internal/encoding.py +0 -12
  68. imgproxykit-0.4.0/imgproxykit/options/background.py +0 -45
  69. imgproxykit-0.4.0/imgproxykit/options/base.py +0 -129
  70. imgproxykit-0.4.0/imgproxykit/options/expires.py +0 -27
  71. imgproxykit-0.4.0/imgproxykit/options/gravity.py +0 -52
  72. imgproxykit-0.4.0/imgproxykit/options/resize.py +0 -54
  73. imgproxykit-0.4.0/imgproxykit/options/watermark.py +0 -59
  74. imgproxykit-0.4.0/imgproxykit/serialization.py +0 -118
  75. imgproxykit-0.4.0/imgproxykit/transform.py +0 -100
  76. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/LICENSE +0 -0
  77. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/__init__.py +0 -0
  78. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/integrations/__init__.py +0 -0
  79. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/integrations/django/__init__.py +0 -0
  80. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/integrations/django/apps.py +0 -0
  81. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/integrations/django/client.py +0 -0
  82. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/integrations/django/templatetags/__init__.py +0 -0
  83. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/integrations/django/templatetags/imgproxykit.py +0 -0
  84. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/integrations/fastapi/__init__.py +0 -0
  85. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/internal/__init__.py +0 -0
  86. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/options/__init__.py +0 -0
  87. {imgproxykit-0.4.0 → imgproxykit-0.6.0}/imgproxykit/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: imgproxykit
3
- Version: 0.4.0
3
+ Version: 0.6.0
4
4
  Summary: A typed Python toolkit for building imgproxy URLs
5
5
  License-Expression: MIT
6
6
  License-File: LICENSE
@@ -21,11 +21,17 @@ Classifier: Programming Language :: Python :: 3 :: Only
21
21
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
22
  Classifier: Typing :: Typed
23
23
  Provides-Extra: django
24
+ Provides-Extra: drf
24
25
  Provides-Extra: fastapi
26
+ Provides-Extra: litestar
25
27
  Requires-Dist: django (>=5.2,<7) ; extra == "django"
28
+ Requires-Dist: django (>=5.2,<7) ; extra == "drf"
29
+ Requires-Dist: djangorestframework (>=3.16,<4) ; extra == "drf"
26
30
  Requires-Dist: fastapi (>=0.135.2,<1) ; extra == "fastapi"
31
+ Requires-Dist: litestar (>=2.24,<3) ; extra == "litestar"
27
32
  Requires-Dist: typing-extensions (>=4.16,<5)
28
33
  Project-URL: Changelog, https://github.com/kondratevdev/imgproxykit/releases
34
+ Project-URL: Documentation, https://imgproxykit.readthedocs.io/en/latest/
29
35
  Project-URL: GitHub, https://github.com/kondratevdev/imgproxykit
30
36
  Project-URL: Issues, https://github.com/kondratevdev/imgproxykit/issues
31
37
  Description-Content-Type: text/markdown
@@ -46,30 +52,46 @@ Description-Content-Type: text/markdown
46
52
 
47
53
  </div>
48
54
 
55
+ > Build [imgproxy](https://imgproxy.net/) URLs with typed Python options and
56
+ > reusable transformations. Define image settings once, compose them for
57
+ > different use cases, and generate named variants from a single source.
58
+
49
59
  ## Features
50
60
 
51
61
  - [x] Fully typed and checked with `mypy`, `pyright`, `ty` and `pyrefly` in strict modes
62
+ - [x] Signed URLs using your `imgproxy` server's `key` and `salt`
52
63
  - [x] Immutable, reusable transforms combining typed options and scalar arguments
53
64
  - [x] Variants: generate multiple image URLs from a reusable set of transforms
54
- - [x] Support for named presets configured on your imgproxy server
55
- - [x] Native `Django` integration with settings, shortcuts, and template tags
56
- - [x] Native `FastAPI` integration with dependency injection
65
+ - [x] Support for named presets configured on your `imgproxy` server
66
+ - [x] `Django`, `Django Modern REST`, `Django REST Framework`, `FastAPI`, `Litestar` integration
57
67
 
58
68
  ---------
59
69
 
60
70
  ## Installation
61
71
 
62
- Requires **Python 3.12+** and a running [imgproxy](https://github.com/imgproxy/imgproxy) server.
72
+ Requires **Python 3.12+**. A running [imgproxy](https://github.com/imgproxy/imgproxy)
73
+ server is needed to serve images from the generated URLs.
63
74
 
64
75
  ```bash
65
76
  pip install imgproxykit
66
77
  ```
67
78
 
79
+ There are several included extras:
80
+
81
+ - `'imgproxykit[django]'` provides [Django integration](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/django.html)
82
+ with shortcuts, template tags, and client configuration through Django settings
83
+ - `'imgproxykit[drf]'` provides [Django REST Framework integration](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/drf.html)
84
+ with serializer fields for image URLs and named variants
85
+ - `'imgproxykit[fastapi]'` provides [FastAPI integration](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/fastapi.html)
86
+ with a dependency for injecting the client into route handlers
87
+ - `'imgproxykit[litestar]'` provides [Litestar integration](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/litestar.html)
88
+ with a plugin that injects the client into route handlers
89
+
68
90
  ## Examples
69
91
 
70
- ### Single URL
92
+ ### Generate a thumbnail URL
71
93
 
72
- Build a URL for a cropped WebP thumbnail:
94
+ Build a URL for a cropped `webp` thumbnail:
73
95
 
74
96
  ```python
75
97
  >>> from imgproxykit import Client, Transform
@@ -79,7 +101,7 @@ Build a URL for a cropped WebP thumbnail:
79
101
  >>> client = Client('https://imgproxy.example.com')
80
102
 
81
103
  >>> thumbnail = Transform(
82
- ... Resize(width=300, height=300, mode='fill'), # Pass typed option objects.
104
+ ... Resize.fill(width=300, height=300), # Pass typed option objects.
83
105
  ... quality=80, # Or scalar keywords, equivalent to Quality(80).
84
106
  ... format='webp',
85
107
  ... )
@@ -89,12 +111,17 @@ Build a URL for a cropped WebP thumbnail:
89
111
 
90
112
  ```
91
113
 
92
- `thumbnail_url` points to a 300×300 WebP thumbnail with smart cropping.
93
- Imgproxy fetches and processes the original image when this URL is requested.
114
+ The generated URL has this structure, with the source URL encoded as URL-safe `base64`:
115
+
116
+ ```text
117
+ https://imgproxy.example.com/unsafe/resize:fill:300:300/quality:80/format:webp/<encoded-source>
118
+ ```
119
+
120
+ `thumbnail_url` points to a cropped `webp` thumbnail.
94
121
 
95
- ### Variants
122
+ ### Generate image variants
96
123
 
97
- Build variants by combining shared settings with different sizes:
124
+ Build variants by extending a shared transform with a width for each size:
98
125
 
99
126
  ```python
100
127
  >>> from imgproxykit import Client, Transform
@@ -104,9 +131,9 @@ Build variants by combining shared settings with different sizes:
104
131
  >>> # Share quality and output format across all variants.
105
132
  >>> base = Transform(quality=85, format='webp')
106
133
  >>> products = {
107
- ... 'small': Transform(*base.options, width=320),
108
- ... 'medium': Transform(*base.options, width=640),
109
- ... 'large': Transform(*base.options, width=1280),
134
+ ... 'small': base.extend(width=320),
135
+ ... 'medium': base.extend(width=640),
136
+ ... 'large': base.extend(width=1280),
110
137
  ... }
111
138
 
112
139
  >>> source_url = 'https://originals.example.com/products/backpack.jpg'
@@ -116,14 +143,23 @@ Build variants by combining shared settings with different sizes:
116
143
 
117
144
  ```
118
145
 
119
- `product_urls` contains a URL for each size, all using the shared quality
120
- and WebP format. Reuse `products` with other source images to keep the same
121
- variants across your catalog.
146
+ `product_urls` maps each variant name to its generated URL
147
+ (shown here with the encoded source abbreviated):
148
+
149
+ ```text
150
+ {
151
+ 'small': 'https://imgproxy.example.com/unsafe/quality:85/format:webp/width:320/<encoded-source>',
152
+ 'medium': 'https://imgproxy.example.com/unsafe/quality:85/format:webp/width:640/<encoded-source>',
153
+ 'large': 'https://imgproxy.example.com/unsafe/quality:85/format:webp/width:1280/<encoded-source>',
154
+ }
155
+ ```
122
156
 
123
157
  ## Next steps
124
158
 
125
159
  - Read the [documentation](https://imgproxykit.readthedocs.io/en/latest/)
126
160
  for client configuration, transformations, variants, and processing options.
161
+ - Check the [changelog](https://imgproxykit.readthedocs.io/en/latest/pages/changelog.html)
162
+ for new features, fixes, and breaking changes.
127
163
  - Find published releases on [PyPI](https://pypi.org/project/imgproxykit/).
128
164
 
129
165
  ## Contributing
@@ -14,30 +14,46 @@
14
14
 
15
15
  </div>
16
16
 
17
+ > Build [imgproxy](https://imgproxy.net/) URLs with typed Python options and
18
+ > reusable transformations. Define image settings once, compose them for
19
+ > different use cases, and generate named variants from a single source.
20
+
17
21
  ## Features
18
22
 
19
23
  - [x] Fully typed and checked with `mypy`, `pyright`, `ty` and `pyrefly` in strict modes
24
+ - [x] Signed URLs using your `imgproxy` server's `key` and `salt`
20
25
  - [x] Immutable, reusable transforms combining typed options and scalar arguments
21
26
  - [x] Variants: generate multiple image URLs from a reusable set of transforms
22
- - [x] Support for named presets configured on your imgproxy server
23
- - [x] Native `Django` integration with settings, shortcuts, and template tags
24
- - [x] Native `FastAPI` integration with dependency injection
27
+ - [x] Support for named presets configured on your `imgproxy` server
28
+ - [x] `Django`, `Django Modern REST`, `Django REST Framework`, `FastAPI`, `Litestar` integration
25
29
 
26
30
  ---------
27
31
 
28
32
  ## Installation
29
33
 
30
- Requires **Python 3.12+** and a running [imgproxy](https://github.com/imgproxy/imgproxy) server.
34
+ Requires **Python 3.12+**. A running [imgproxy](https://github.com/imgproxy/imgproxy)
35
+ server is needed to serve images from the generated URLs.
31
36
 
32
37
  ```bash
33
38
  pip install imgproxykit
34
39
  ```
35
40
 
41
+ There are several included extras:
42
+
43
+ - `'imgproxykit[django]'` provides [Django integration](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/django.html)
44
+ with shortcuts, template tags, and client configuration through Django settings
45
+ - `'imgproxykit[drf]'` provides [Django REST Framework integration](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/drf.html)
46
+ with serializer fields for image URLs and named variants
47
+ - `'imgproxykit[fastapi]'` provides [FastAPI integration](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/fastapi.html)
48
+ with a dependency for injecting the client into route handlers
49
+ - `'imgproxykit[litestar]'` provides [Litestar integration](https://imgproxykit.readthedocs.io/en/latest/pages/integrations/litestar.html)
50
+ with a plugin that injects the client into route handlers
51
+
36
52
  ## Examples
37
53
 
38
- ### Single URL
54
+ ### Generate a thumbnail URL
39
55
 
40
- Build a URL for a cropped WebP thumbnail:
56
+ Build a URL for a cropped `webp` thumbnail:
41
57
 
42
58
  ```python
43
59
  >>> from imgproxykit import Client, Transform
@@ -47,7 +63,7 @@ Build a URL for a cropped WebP thumbnail:
47
63
  >>> client = Client('https://imgproxy.example.com')
48
64
 
49
65
  >>> thumbnail = Transform(
50
- ... Resize(width=300, height=300, mode='fill'), # Pass typed option objects.
66
+ ... Resize.fill(width=300, height=300), # Pass typed option objects.
51
67
  ... quality=80, # Or scalar keywords, equivalent to Quality(80).
52
68
  ... format='webp',
53
69
  ... )
@@ -57,12 +73,17 @@ Build a URL for a cropped WebP thumbnail:
57
73
 
58
74
  ```
59
75
 
60
- `thumbnail_url` points to a 300×300 WebP thumbnail with smart cropping.
61
- Imgproxy fetches and processes the original image when this URL is requested.
76
+ The generated URL has this structure, with the source URL encoded as URL-safe `base64`:
77
+
78
+ ```text
79
+ https://imgproxy.example.com/unsafe/resize:fill:300:300/quality:80/format:webp/<encoded-source>
80
+ ```
81
+
82
+ `thumbnail_url` points to a cropped `webp` thumbnail.
62
83
 
63
- ### Variants
84
+ ### Generate image variants
64
85
 
65
- Build variants by combining shared settings with different sizes:
86
+ Build variants by extending a shared transform with a width for each size:
66
87
 
67
88
  ```python
68
89
  >>> from imgproxykit import Client, Transform
@@ -72,9 +93,9 @@ Build variants by combining shared settings with different sizes:
72
93
  >>> # Share quality and output format across all variants.
73
94
  >>> base = Transform(quality=85, format='webp')
74
95
  >>> products = {
75
- ... 'small': Transform(*base.options, width=320),
76
- ... 'medium': Transform(*base.options, width=640),
77
- ... 'large': Transform(*base.options, width=1280),
96
+ ... 'small': base.extend(width=320),
97
+ ... 'medium': base.extend(width=640),
98
+ ... 'large': base.extend(width=1280),
78
99
  ... }
79
100
 
80
101
  >>> source_url = 'https://originals.example.com/products/backpack.jpg'
@@ -84,14 +105,23 @@ Build variants by combining shared settings with different sizes:
84
105
 
85
106
  ```
86
107
 
87
- `product_urls` contains a URL for each size, all using the shared quality
88
- and WebP format. Reuse `products` with other source images to keep the same
89
- variants across your catalog.
108
+ `product_urls` maps each variant name to its generated URL
109
+ (shown here with the encoded source abbreviated):
110
+
111
+ ```text
112
+ {
113
+ 'small': 'https://imgproxy.example.com/unsafe/quality:85/format:webp/width:320/<encoded-source>',
114
+ 'medium': 'https://imgproxy.example.com/unsafe/quality:85/format:webp/width:640/<encoded-source>',
115
+ 'large': 'https://imgproxy.example.com/unsafe/quality:85/format:webp/width:1280/<encoded-source>',
116
+ }
117
+ ```
90
118
 
91
119
  ## Next steps
92
120
 
93
121
  - Read the [documentation](https://imgproxykit.readthedocs.io/en/latest/)
94
122
  for client configuration, transformations, variants, and processing options.
123
+ - Check the [changelog](https://imgproxykit.readthedocs.io/en/latest/pages/changelog.html)
124
+ for new features, fixes, and breaking changes.
95
125
  - Find published releases on [PyPI](https://pypi.org/project/imgproxykit/).
96
126
 
97
127
  ## Contributing
@@ -1,9 +1,15 @@
1
1
  from collections.abc import Mapping
2
- from typing import ClassVar, overload
2
+ from typing import TYPE_CHECKING, ClassVar, overload
3
3
 
4
+ from imgproxykit.internal.types import VariantsResult
4
5
  from imgproxykit.serialization import PathSerializer
5
6
  from imgproxykit.signing import UrlSigner
6
7
  from imgproxykit.transform import Transform
8
+ from imgproxykit.types import SourceEncoding
9
+ from imgproxykit.variants import DataclassVariantsResolver
10
+
11
+ if TYPE_CHECKING:
12
+ from _typeshed import DataclassInstance
7
13
 
8
14
 
9
15
  class Client:
@@ -11,17 +17,12 @@ class Client:
11
17
  Build imgproxy URLs from Python transforms or server presets.
12
18
 
13
19
  Stores the imgproxy address and signing configuration for reuse across
14
- images. Each call to :meth:`url` returns a URL string; imgproxy loads
15
- the source and processes the image when that URL is requested.
20
+ images. Imgproxy loads the source and processes the image when
21
+ that URL is requested.
16
22
 
17
23
  Pass processing options through :class:`~imgproxykit.Transform` as
18
24
  option instances or scalar keyword arguments. Preset options reference
19
25
  names configured on the imgproxy server.
20
-
21
- Subclass this client and override :attr:`path_serializer_cls` or
22
- :attr:`url_signer_cls` to customize path construction or signing.
23
- Each client creates its own instances of these components.
24
-
25
26
  """
26
27
 
27
28
  __slots__ = (
@@ -34,6 +35,10 @@ class Client:
34
35
  path_serializer_cls: ClassVar[type[PathSerializer]] = PathSerializer
35
36
  #: Component that signs paths or returns the unsigned URL marker.
36
37
  url_signer_cls: ClassVar[type[UrlSigner]] = UrlSigner
38
+ #: Component that validates dataclass variant declarations.
39
+ variants_resolver_cls: ClassVar[type[DataclassVariantsResolver]] = (
40
+ DataclassVariantsResolver
41
+ )
37
42
 
38
43
  @overload
39
44
  def __init__(
@@ -43,8 +48,10 @@ class Client:
43
48
  *,
44
49
  key: None = None,
45
50
  salt: None = None,
46
- unsigned_signature: str = 'unsafe',
47
- arguments_separator: str = ':',
51
+ source_encoding: SourceEncoding = 'base64',
52
+ unsigned_marker: str = 'unsafe',
53
+ argument_separator: str = ':',
54
+ use_short_options: bool = False,
48
55
  ) -> None: ...
49
56
 
50
57
  @overload
@@ -55,8 +62,10 @@ class Client:
55
62
  *,
56
63
  key: str,
57
64
  salt: str,
65
+ source_encoding: SourceEncoding = 'base64',
58
66
  signature_size: int = 32,
59
- arguments_separator: str = ':',
67
+ argument_separator: str = ':',
68
+ use_short_options: bool = False,
60
69
  ) -> None: ...
61
70
 
62
71
  def __init__( # noqa: WPS211
@@ -66,9 +75,11 @@ class Client:
66
75
  *,
67
76
  key: str | None = None,
68
77
  salt: str | None = None,
78
+ source_encoding: SourceEncoding = 'base64',
69
79
  signature_size: int = 32,
70
- unsigned_signature: str = 'unsafe',
71
- arguments_separator: str = ':',
80
+ unsigned_marker: str = 'unsafe',
81
+ argument_separator: str = ':',
82
+ use_short_options: bool = False,
72
83
  ) -> None:
73
84
  """
74
85
  Configure the imgproxy address and create the client components.
@@ -83,29 +94,37 @@ class Client:
83
94
  stored as supplied, without validation.
84
95
  key: Nonempty hex-encoded signing key matching the server.
85
96
  Supply together with ``salt``; ``None`` selects unsigned mode.
97
+ See `IMGPROXY_KEY
98
+ <https://docs.imgproxy.net/configuration/options#url-signature>`__.
86
99
  salt: Nonempty hex-encoded signing salt matching the server.
87
100
  Supply together with ``key``; ``None`` selects unsigned mode.
101
+ See `IMGPROXY_SALT
102
+ <https://docs.imgproxy.net/configuration/options#url-signature>`__.
88
103
  signature_size: Positive digest byte count to encode. Must match
89
104
  the server's `IMGPROXY_SIGNATURE_SIZE
90
105
  <https://docs.imgproxy.net/latest/configuration/options#url-signature>`__.
91
- Defaults to ``32``.
92
- Values of 32 or more use the full digest.
93
- unsigned_signature: URL marker used when key and salt are omitted.
106
+ unsigned_marker: URL marker used when key and salt are omitted.
94
107
  Defaults to ``'unsafe'``. With server signing enabled,
95
108
  configure this marker in `IMGPROXY_TRUSTED_SIGNATURES
96
109
  <https://docs.imgproxy.net/latest/configuration/options#url-signature>`__.
97
- arguments_separator: Separator between processing option names
110
+ argument_separator: Separator between processing option names
98
111
  and their arguments. Must match the server's
99
112
  `IMGPROXY_ARGUMENTS_SEPARATOR
100
113
  <https://docs.imgproxy.net/latest/configuration/options#miscellaneous>`__.
101
- Defaults to ``':'``.
114
+ source_encoding: Source URL representation: ``'base64'`` or
115
+ ``'plain'`` with full percent-encoding. See
116
+ `imgproxy source URLs
117
+ <https://docs.imgproxy.net/usage/processing#source-url>`__.
118
+ use_short_options: Use short processing option aliases when
119
+ available, falling back to full names. Defaults to ``False``.
102
120
 
103
121
  Raises:
104
122
  ValueError: With the default signer, if only one signing
105
123
  credential is supplied, either is empty or invalid hex,
106
124
  or ``signature_size`` is not positive, even in unsigned mode.
107
125
 
108
- Examples:
126
+ Usage:
127
+
109
128
  Create an unsigned client:
110
129
 
111
130
  >>> from imgproxykit import Client
@@ -118,18 +137,19 @@ class Client:
118
137
  ... key='736563726574',
119
138
  ... salt='73616c74',
120
139
  ... )
121
-
122
140
  """
123
141
  # Prepare `base_url` only once pre instance
124
142
  self._base_url = base_url.rstrip('/')
125
143
  self._path_serializer = self.path_serializer_cls(
126
- arguments_separator=arguments_separator,
144
+ argument_separator=argument_separator,
145
+ use_short_options=use_short_options,
146
+ source_encoding=source_encoding,
127
147
  )
128
148
  self._url_signer = self.url_signer_cls(
129
149
  key=key,
130
150
  salt=salt,
131
151
  signature_size=signature_size,
132
- unsigned_signature=unsigned_signature,
152
+ unsigned_marker=unsigned_marker,
133
153
  )
134
154
 
135
155
  def url(
@@ -151,43 +171,66 @@ class Client:
151
171
 
152
172
  Args:
153
173
  source: Complete source URI understood by the imgproxy server.
154
- The default serializer encodes its UTF-8 representation as
155
- URL-safe Base64 without padding. The URI is neither validated
156
- nor resolved against ``base_url``.
174
+ The default serializer uses URL-safe Base64 without padding
175
+ or percent-encoding according to ``source_encoding``. The URI
176
+ is neither validated nor resolved against ``base_url``.
157
177
  transform: Reusable processing options and optional output format.
158
178
  Omit or pass ``None`` to add no processing options.
159
179
 
160
180
  Returns:
161
181
  The complete imgproxy URL, signed when credentials were supplied
162
- or containing the unsigned marker otherwise. No network request
163
- is made.
182
+ or containing the unsigned marker otherwise.
164
183
  """
165
184
  path = self._path_serializer(source, transform=transform)
166
185
  signature = self._url_signer(path)
167
186
  return f'{self._base_url}/{signature}{path}'
168
187
 
188
+ @overload
169
189
  def variants[Key: str](
170
190
  self,
171
191
  source: str,
172
192
  *,
173
193
  variants: Mapping[Key, Transform],
174
- ) -> dict[Key, str]:
194
+ ) -> dict[Key, str]: ...
195
+
196
+ @overload
197
+ def variants[VariantSet: DataclassInstance](
198
+ self,
199
+ source: str,
200
+ *,
201
+ variants: type[VariantSet],
202
+ ) -> VariantSet: ...
203
+
204
+ def variants[Key: str](
205
+ self,
206
+ source: str,
207
+ *,
208
+ variants: Mapping[Key, Transform] | type['DataclassInstance'],
209
+ ) -> VariantsResult[Key]:
175
210
  """
176
211
  Build named URLs for one source using complete transformations.
177
212
 
178
213
  Args:
179
214
  source: Complete source URI understood by the imgproxy server.
180
215
  variants: Names mapped to the transformation for each variant.
181
- Each transformation is passed unchanged to :meth:`url`.
216
+ Alternatively, a dataclass class with fields declared as
217
+ ``Annotated[str, Transform(...)]``. Each transformation is
218
+ passed unchanged to :meth:`url`.
182
219
 
183
220
  Returns:
184
221
  A new dictionary preserving the input keys and iteration order.
185
222
  The key type is preserved, including literal string unions.
186
223
  An empty mapping produces an empty dictionary.
187
- No network request is made.
188
-
224
+ A dataclass class produces a new instance of that exact class,
225
+ with a generated URL in every field.
189
226
  """
190
- return {
191
- name: self.url(source, transform=transform)
192
- for name, transform in variants.items()
227
+ transforms = (
228
+ variants
229
+ if isinstance(variants, Mapping)
230
+ else self.variants_resolver_cls(model=variants)()
231
+ )
232
+ urls = {
233
+ name: self.url(source, transform=transform) # ty: ignore[invalid-argument-type]
234
+ for name, transform in transforms.items()
193
235
  }
236
+ return urls if isinstance(variants, Mapping) else variants(**urls) # ty: ignore[invalid-return-type, invalid-argument-type]
@@ -1,12 +1,17 @@
1
1
  import enum
2
2
  from collections.abc import Callable, Mapping
3
- from typing import Any, Final, Required, final
3
+ from typing import TYPE_CHECKING, Any, Final, Required, final
4
4
 
5
5
  from django.core.exceptions import ImproperlyConfigured
6
6
  from django.db.models.fields.files import FieldFile
7
7
  from typing_extensions import TypedDict
8
8
 
9
9
  from imgproxykit.transform import Transform
10
+ from imgproxykit.types import SourceEncoding
11
+
12
+ if TYPE_CHECKING:
13
+ from _typeshed import DataclassInstance
14
+
10
15
 
11
16
  #: Django setting containing the imgproxy client configuration.
12
17
  IMGPROXYKIT_SETTINGS: Final = 'IMGPROXYKIT'
@@ -21,8 +26,10 @@ class ClientSettings(enum.StrEnum):
21
26
  key = 'key'
22
27
  salt = 'salt'
23
28
  signature_size = 'signature_size'
24
- unsigned_signature = 'unsigned_signature'
25
- arguments_separator = 'arguments_separator'
29
+ unsigned_marker = 'unsigned_marker'
30
+ argument_separator = 'argument_separator'
31
+ use_short_options = 'use_short_options'
32
+ source_encoding = 'source_encoding'
26
33
 
27
34
 
28
35
  @final
@@ -48,9 +55,13 @@ class ClientSettingsDict(TypedDict, total=False, closed=True):
48
55
  #: Positive signature digest byte count; must match the server.
49
56
  signature_size: int
50
57
  #: Signature marker used when signing credentials are omitted.
51
- unsigned_signature: str
58
+ unsigned_marker: str
52
59
  #: Processing argument separator; must match the server.
53
- arguments_separator: str
60
+ argument_separator: str
61
+ #: Use short processing option aliases when available.
62
+ use_short_options: bool
63
+ #: Source URL representation using Base64 or percent-encoded plain mode.
64
+ source_encoding: SourceEncoding
54
65
 
55
66
 
56
67
  class SettingsDict(TypedDict, total=False, closed=True):
@@ -61,7 +72,7 @@ class SettingsDict(TypedDict, total=False, closed=True):
61
72
  #: Named Python transformations, distinct from imgproxy server presets.
62
73
  transforms: Mapping[str, Transform]
63
74
  #: Named sets of transformations; objects can be shared with transforms.
64
- variants: Mapping[str, Mapping[str, Transform]]
75
+ variants: Mapping[str, Mapping[str, Transform] | type['DataclassInstance']]
65
76
  #: Convert the original source to a URI, including empty file fields.
66
77
  source_resolver: Callable[[str | FieldFile], str] | str
67
78
 
@@ -97,7 +108,6 @@ def get_config() -> SettingsDict:
97
108
 
98
109
  Raises:
99
110
  ImproperlyConfigured: If ``IMGPROXYKIT`` is missing or ``None``.
100
-
101
111
  """
102
112
  # Keep LazySettings out of module globals: doctest inspection evaluates it.
103
113
  from django.conf import settings # noqa: PLC0415
@@ -1,13 +1,18 @@
1
1
  from collections.abc import Mapping
2
- from typing import overload
2
+ from dataclasses import asdict, is_dataclass
3
+ from typing import TYPE_CHECKING, overload
3
4
 
4
5
  from django.db.models.fields.files import FieldFile
5
6
 
6
7
  from imgproxykit.integrations.django.client import get_client
7
8
  from imgproxykit.integrations.django.settings import Settings, get_config
8
9
  from imgproxykit.integrations.django.sources import resolve_source
10
+ from imgproxykit.internal.types import VariantsResult
9
11
  from imgproxykit.transform import Transform
10
12
 
13
+ if TYPE_CHECKING:
14
+ from _typeshed import DataclassInstance
15
+
11
16
 
12
17
  def imgproxy_url(
13
18
  source: str | FieldFile,
@@ -27,13 +32,19 @@ def imgproxy_url(
27
32
  Raises:
28
33
  KeyError: If a named transformation is not configured.
29
34
  ValueError: If the default source resolver receives an empty file field.
30
-
31
35
  """
32
36
  if isinstance(transform, str):
33
37
  transform = get_config()[Settings.transforms.value][transform] # pyright: ignore[reportTypedDictNotRequiredAccess]
34
38
  return get_client().url(resolve_source(source), transform=transform)
35
39
 
36
40
 
41
+ @overload
42
+ def imgproxy_variants(
43
+ source: str | FieldFile,
44
+ variants: str,
45
+ ) -> dict[str, str]: ...
46
+
47
+
37
48
  @overload
38
49
  def imgproxy_variants[Key: str](
39
50
  source: str | FieldFile,
@@ -42,38 +53,42 @@ def imgproxy_variants[Key: str](
42
53
 
43
54
 
44
55
  @overload
45
- def imgproxy_variants(
56
+ def imgproxy_variants[VariantSet: DataclassInstance](
46
57
  source: str | FieldFile,
47
- variants: str,
48
- ) -> dict[str, str]: ...
58
+ variants: type[VariantSet],
59
+ ) -> VariantSet: ...
49
60
 
50
61
 
51
62
  def imgproxy_variants[Key: str](
52
63
  source: str | FieldFile,
53
- variants: Mapping[Key, Transform] | str,
54
- ) -> dict[Key, str] | dict[str, str]:
64
+ variants: Mapping[Key, Transform] | type['DataclassInstance'] | str,
65
+ ) -> VariantsResult[Key]:
55
66
  """
56
67
  Build named image URLs using Django's configured client.
57
68
 
58
69
  Args:
59
70
  source: Source URI or model file field, resolved once for all variants.
60
- variants: Transformation mapping or set name from
61
- ``IMGPROXYKIT['variants']``.
71
+ variants: Transformation mapping, dataclass class,
72
+ or set name from ``IMGPROXYKIT['variants']``.
62
73
 
63
74
  Returns:
64
75
  URLs in variant order. Direct mappings preserve their key type.
76
+ Dataclass classes passed directly produce instances of that class.
77
+ Named sets always produce dictionaries of URLs.
65
78
 
66
79
  Raises:
67
80
  KeyError: If a named variant set is not configured.
68
81
  ValueError: If the default source resolver receives an empty file field.
69
-
70
82
  """
71
83
  client = get_client()
72
84
 
73
- # Resolve the source inside each branch so named sets are looked up first.
74
- # Only one branch runs, so the resolver is called once per invocation.
85
+ # 1. For a mapping or dataclass class, return the client result as is.
86
+ # 2. For a set name, read its declaration from settings first.
87
+ # 3. Resolve the source once, then build the URLs.
88
+ # 4. Always return a dict for a set name, even if it uses a dataclass.
75
89
  if not isinstance(variants, str):
76
90
  return client.variants(resolve_source(source), variants=variants)
77
91
 
78
92
  configured = get_config()[Settings.variants.value][variants] # pyright: ignore[reportTypedDictNotRequiredAccess]
79
- return client.variants(resolve_source(source), variants=configured)
93
+ images = client.variants(resolve_source(source), variants=configured)
94
+ return asdict(images) if is_dataclass(images) else images