-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add Format constants for convert media types, including JXL #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sreini
wants to merge
1
commit into
master
Choose a base branch
from
feat/jxl-format
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # -*- coding: utf-8 -*- | ||
| import json | ||
| import pytest | ||
|
|
||
| import tinify | ||
| from tinify import Format, Source | ||
|
|
||
|
|
||
| class TestTinifyFormat: | ||
| def test_should_expose_supported_media_types(self): | ||
| assert Format.WEBP == "image/webp" | ||
| assert Format.PNG == "image/png" | ||
| assert Format.JPEG == "image/jpeg" | ||
| assert Format.JPG == "image/jpg" | ||
| assert Format.AVIF == "image/avif" | ||
| assert Format.JXL == "image/jxl" | ||
| assert Format.ANY == "*/*" | ||
|
|
||
| def test_should_not_be_instantiable(self): | ||
| with pytest.raises(TypeError): | ||
| Format() | ||
|
|
||
|
|
||
| class TestTinifyFormatConvert: | ||
| @pytest.fixture(autouse=True) | ||
| def setup(self, mock_requests): | ||
| tinify.key = "valid" | ||
| mock_requests.post( | ||
| "https://api.tinify.com/shrink", | ||
| status_code=201, | ||
| headers={"Location": "https://api.tinify.com/some/location"}, | ||
| ) | ||
| self.mock_requests = mock_requests | ||
| yield | ||
|
|
||
| def test_convert_with_format_should_serialize_media_type(self): | ||
| self.mock_requests.post( | ||
| "https://api.tinify.com/some/location", | ||
| status_code=200, | ||
| content=b"converted file", | ||
| ) | ||
|
|
||
| Source.from_buffer("png file").convert(type=Format.JXL).to_buffer() | ||
|
|
||
| body = json.loads(self.mock_requests.last_request.text) | ||
| assert body["convert"] == {"type": "image/jxl"} | ||
|
|
||
| def test_convert_with_multiple_formats_should_serialize_media_types(self): | ||
| self.mock_requests.post( | ||
| "https://api.tinify.com/some/location", | ||
| status_code=200, | ||
| content=b"converted file", | ||
| ) | ||
|
|
||
| Source.from_buffer("png file").convert( | ||
| type=[Format.JXL, Format.WEBP] | ||
| ).to_buffer() | ||
|
|
||
| body = json.loads(self.mock_requests.last_request.text) | ||
| assert body["convert"] == {"type": ["image/jxl", "image/webp"]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
|
||
|
|
||
| class Format(object): | ||
| """Media types that images can be converted to. | ||
|
|
||
| Use these constants with :meth:`Source.convert`:: | ||
|
|
||
| tinify.from_file("input.png") \\ | ||
| .convert(type=tinify.Format.JXL) \\ | ||
| .to_file("output.jxl") | ||
|
|
||
| Multiple types may be supplied, in which case the API returns the | ||
| smallest result:: | ||
|
|
||
| .convert(type=[tinify.Format.JXL, tinify.Format.WEBP]) | ||
| """ | ||
|
|
||
| #: WebP. | ||
| WEBP = "image/webp" | ||
|
|
||
| #: PNG. | ||
| PNG = "image/png" | ||
|
|
||
| #: JPEG. | ||
| JPEG = "image/jpeg" | ||
|
|
||
| #: JPEG, an alias of :attr:`JPEG`. | ||
| JPG = "image/jpg" | ||
|
|
||
| #: AVIF. | ||
| AVIF = "image/avif" | ||
|
|
||
| #: JPEG XL. | ||
| JXL = "image/jxl" | ||
|
|
||
| #: Wildcard, returns the smallest of the supported types. | ||
| ANY = "*/*" | ||
|
|
||
| def __init__(self): | ||
| raise TypeError("Format is a namespace of constants and cannot be instantiated") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need a type hint in the API to inform the user that they may use the enum from _format.py