-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bindgen): Add async support to Python dispatch package
- Loading branch information
Showing
12 changed files
with
383 additions
and
10 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
...n/itkwasm-compress-stringify-emscripten/itkwasm_compress_stringify_emscripten/__init__.py
This file contains 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,6 @@ | ||
"""itkwasm-compress-stringify-emscripten: Zstandard compression and decompression and base64 encoding and decoding in WebAssembly. Emscripten implementation.""" | ||
|
||
from .compress_stringify_async import compress_stringify_async | ||
from .parse_string_decompress_async import parse_string_decompress_async | ||
|
||
from ._version import __version__ |
1 change: 1 addition & 0 deletions
1
...n/itkwasm-compress-stringify-emscripten/itkwasm_compress_stringify_emscripten/_version.py
This file contains 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 @@ | ||
__version__ = "0.4.2" |
69 changes: 69 additions & 0 deletions
69
...ss-stringify-emscripten/itkwasm_compress_stringify_emscripten/compress_stringify_async.py
This file contains 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,69 @@ | ||
# Generated file. Do not edit. | ||
|
||
from pathlib import Path | ||
import os | ||
from typing import Dict, Tuple | ||
|
||
from .pyodide import js_package | ||
|
||
from itkwasm.pyodide import ( | ||
to_js, | ||
to_py, | ||
js_resources | ||
) | ||
|
||
async def compress_stringify_async( | ||
input: bytes, | ||
stringify: bool = False, | ||
compression_level: int = 3, | ||
data_url_prefix: str = "data:base64,", | ||
) -> bytes: | ||
"""Given a binary, compress and optionally base64 encode. | ||
Parameters | ||
---------- | ||
input: bytes | ||
Input binary | ||
stringify: bool, optional | ||
Stringify the output | ||
compression_level: int, optional | ||
Compression level, typically 1-9 | ||
data_url_prefix: str, optional | ||
dataURL prefix | ||
Returns | ||
------- | ||
bytes | ||
Output compressed binary | ||
""" | ||
js_module = await js_package.js_module | ||
web_worker = js_resources.web_worker | ||
|
||
outputs = await js_module.compressStringify(web_worker, to_js(input), stringify=to_js(stringify), compressionLevel=to_js(compression_level), dataUrlPrefix=to_js(data_url_prefix), ) | ||
|
||
output_web_worker = None | ||
output_list = [] | ||
print(dir(outputs)) | ||
outputs_object_map = outputs.as_object_map() | ||
for output_name in outputs.object_keys(): | ||
if output_name == 'webWorker': | ||
output_web_worker = outputs_object_map[output_name] | ||
else: | ||
print(output_name) | ||
print(type(outputs_object_map[output_name])) | ||
print(outputs_object_map[output_name].constructor.name) | ||
print(outputs_object_map[output_name]) | ||
output_list.append(to_py(outputs_object_map[output_name])) | ||
|
||
js_resources.web_worker = output_web_worker | ||
|
||
if len(output_list) == 1: | ||
return output_list[0] | ||
return tuple(output_list) |
61 changes: 61 additions & 0 deletions
61
...ringify-emscripten/itkwasm_compress_stringify_emscripten/parse_string_decompress_async.py
This file contains 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,61 @@ | ||
# Generated file. Do not edit. | ||
|
||
from pathlib import Path | ||
import os | ||
from typing import Dict, Tuple | ||
|
||
from .pyodide import js_package | ||
|
||
from itkwasm.pyodide import ( | ||
to_js, | ||
to_py, | ||
js_resources | ||
) | ||
|
||
async def parse_string_decompress_async( | ||
input: bytes, | ||
parse_string: bool = False, | ||
) -> bytes: | ||
"""Given a binary or string produced with compress-stringify, decompress and optionally base64 decode. | ||
Parameters | ||
---------- | ||
input: bytes | ||
Compressed input | ||
parse_string: bool, optional | ||
Parse the input string before decompression | ||
Returns | ||
------- | ||
bytes | ||
Output decompressed binary | ||
""" | ||
js_module = await js_package.js_module | ||
web_worker = js_resources.web_worker | ||
|
||
outputs = await js_module.parseStringDecompress(web_worker, to_js(input), parseString=to_js(parse_string), ) | ||
|
||
output_web_worker = None | ||
output_list = [] | ||
print(dir(outputs)) | ||
outputs_object_map = outputs.as_object_map() | ||
for output_name in outputs.object_keys(): | ||
if output_name == 'webWorker': | ||
output_web_worker = outputs_object_map[output_name] | ||
else: | ||
print(output_name) | ||
print(type(outputs_object_map[output_name])) | ||
print(outputs_object_map[output_name].constructor.name) | ||
print(outputs_object_map[output_name]) | ||
output_list.append(to_py(outputs_object_map[output_name])) | ||
|
||
js_resources.web_worker = output_web_worker | ||
|
||
if len(output_list) == 1: | ||
return output_list[0] | ||
return tuple(output_list) |
4 changes: 4 additions & 0 deletions
4
...on/itkwasm-compress-stringify-emscripten/itkwasm_compress_stringify_emscripten/pyodide.py
This file contains 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,4 @@ | ||
from itkwasm.pyodide import JsPackageConfig, JsPackage | ||
|
||
default_config = JsPackageConfig("https://cdn.jsdelivr.net/npm/@itk-wasm/[email protected]/dist/bundles/compress-stringify.js") | ||
js_package = JsPackage(default_config) |
64 changes: 64 additions & 0 deletions
64
...ss-stringify/python/itkwasm-compress-stringify-emscripten/test/test_compress_stringify.py
This file contains 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,64 @@ | ||
import pytest | ||
import sys | ||
|
||
if sys.version_info < (3,10): | ||
pytest.skip("Skipping pyodide tests on older Python", allow_module_level=True) | ||
|
||
from pytest_pyodide import run_in_pyodide | ||
|
||
from itkwasm_compress_stringify_emscripten import __version__ as test_package_version | ||
|
||
@pytest.fixture | ||
def package_wheel(): | ||
return f"itkwasm_compress_stringify_emscripten-{test_package_version}-py3-none-any.whl" | ||
|
||
@run_in_pyodide(packages=['micropip']) | ||
async def test_decompress_returns_what_was_compressed(selenium, package_wheel): | ||
import micropip | ||
await micropip.install([package_wheel]) | ||
|
||
from itkwasm_compress_stringify_emscripten import compress_stringify_async, parse_string_decompress_async | ||
|
||
data = bytes([222, 173, 190, 239]) | ||
compressed_data = await compress_stringify_async(data, compression_level=8) | ||
decompressed_data = await parse_string_decompress_async(compressed_data) | ||
|
||
assert decompressed_data[0] == 222 | ||
assert decompressed_data[1] == 173 | ||
assert decompressed_data[2] == 190 | ||
assert decompressed_data[3] == 239 | ||
|
||
@run_in_pyodide(packages=['micropip']) | ||
async def test_we_can_stringify_during_compression(selenium, package_wheel): | ||
import micropip | ||
await micropip.install([package_wheel]) | ||
|
||
from itkwasm_compress_stringify_emscripten import compress_stringify_async, parse_string_decompress_async | ||
|
||
data = bytes([222, 173, 190, 239]) | ||
compressed_data = await compress_stringify_async(data, compression_level=8, stringify=True) | ||
decoded = compressed_data.decode() | ||
assert decoded.lower() == 'data:base64,kluv/saeiqaa3q2+7w==' | ||
decompressed_data = await parse_string_decompress_async(compressed_data, parse_string=True) | ||
|
||
assert decompressed_data[0] == 222 | ||
assert decompressed_data[1] == 173 | ||
assert decompressed_data[2] == 190 | ||
assert decompressed_data[3] == 239 | ||
|
||
@run_in_pyodide(packages=['micropip']) | ||
async def test_we_can_use_a_custom_data_url_prefix(selenium, package_wheel): | ||
import micropip | ||
await micropip.install([package_wheel]) | ||
|
||
from itkwasm_compress_stringify_emscripten import compress_stringify_async, parse_string_decompress_async | ||
|
||
data = bytes([222, 173, 190, 239]) | ||
compressed_data = await compress_stringify_async(data, compression_level=8, stringify=True, data_url_prefix='custom,') | ||
assert compressed_data.decode().lower() == 'custom,kluv/saeiqaa3q2+7w==' | ||
decompressed_data = await parse_string_decompress_async(compressed_data, parse_string=True) | ||
|
||
assert decompressed_data[0] == 222 | ||
assert decompressed_data[1] == 173 | ||
assert decompressed_data[2] == 190 | ||
assert decompressed_data[3] == 239 |
2 changes: 2 additions & 0 deletions
2
...mpress-stringify/python/itkwasm-compress-stringify/itkwasm_compress_stringify/__init__.py
This file contains 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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
"""itkwasm-compress-stringify: Zstandard compression and decompression and base64 encoding and decoding in WebAssembly.""" | ||
|
||
from .compress_stringify_async import compress_stringify_async | ||
from .compress_stringify import compress_stringify | ||
from .parse_string_decompress_async import parse_string_decompress_async | ||
from .parse_string_decompress import parse_string_decompress | ||
|
||
from ._version import __version__ |
41 changes: 41 additions & 0 deletions
41
.../python/itkwasm-compress-stringify/itkwasm_compress_stringify/compress_stringify_async.py
This file contains 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,41 @@ | ||
# Generated file. Do not edit. | ||
|
||
from itkwasm import ( | ||
environment_dispatch, | ||
BinaryStream, | ||
) | ||
|
||
async def compress_stringify_async( | ||
input: bytes, | ||
stringify: bool = False, | ||
compression_level: int = 3, | ||
data_url_prefix: str = "data:base64,", | ||
) -> bytes: | ||
"""Given a binary, compress and optionally base64 encode. | ||
Parameters | ||
---------- | ||
input: bytes | ||
Input binary | ||
stringify: bool, optional | ||
Stringify the output | ||
compression_level: int, optional | ||
Compression level, typically 1-9 | ||
data_url_prefix: str, optional | ||
dataURL prefix | ||
Returns | ||
------- | ||
bytes | ||
Output compressed binary | ||
""" | ||
func = environment_dispatch("itkwasm_compress_stringify", "compress_stringify_async") | ||
output = await func(input, stringify=stringify, compression_level=compression_level, data_url_prefix=data_url_prefix) | ||
return output |
33 changes: 33 additions & 0 deletions
33
...on/itkwasm-compress-stringify/itkwasm_compress_stringify/parse_string_decompress_async.py
This file contains 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,33 @@ | ||
# Generated file. Do not edit. | ||
|
||
from itkwasm import ( | ||
environment_dispatch, | ||
BinaryStream, | ||
) | ||
|
||
async def parse_string_decompress_async( | ||
input: bytes, | ||
parse_string: bool = False, | ||
) -> bytes: | ||
"""Given a binary or string produced with compress-stringify, decompress and optionally base64 decode. | ||
Parameters | ||
---------- | ||
input: bytes | ||
Compressed input | ||
parse_string: bool, optional | ||
Parse the input string before decompression | ||
Returns | ||
------- | ||
bytes | ||
Output decompressed binary | ||
""" | ||
func = environment_dispatch("itkwasm_compress_stringify", "parse_string_decompress_async") | ||
output = await func(input, parse_string=parse_string) | ||
return output |
This file contains 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
64 changes: 64 additions & 0 deletions
64
...ss-stringify/python/itkwasm-compress-stringify/test/test_compress_stringify_emscripten.py
This file contains 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,64 @@ | ||
import pytest | ||
import sys | ||
|
||
if sys.version_info < (3,10): | ||
pytest.skip("Skipping pyodide tests on older Python", allow_module_level=True) | ||
|
||
from pytest_pyodide import run_in_pyodide | ||
|
||
from itkwasm_compress_stringify import __version__ as test_package_version | ||
|
||
@pytest.fixture | ||
def package_wheel(): | ||
return f"itkwasm_compress_stringify-{test_package_version}-py3-none-any.whl" | ||
|
||
@run_in_pyodide(packages=['micropip']) | ||
async def test_decompress_returns_what_was_compressed(selenium, package_wheel): | ||
import micropip | ||
await micropip.install([package_wheel]) | ||
|
||
from itkwasm_compress_stringify import compress_stringify_async, parse_string_decompress_async | ||
|
||
data = bytes([222, 173, 190, 239]) | ||
compressed_data = await compress_stringify_async(data, compression_level=8) | ||
decompressed_data = await parse_string_decompress_async(compressed_data) | ||
|
||
assert decompressed_data[0] == 222 | ||
assert decompressed_data[1] == 173 | ||
assert decompressed_data[2] == 190 | ||
assert decompressed_data[3] == 239 | ||
|
||
@run_in_pyodide(packages=['micropip']) | ||
async def test_we_can_stringify_during_compression(selenium, package_wheel): | ||
import micropip | ||
await micropip.install([package_wheel]) | ||
|
||
from itkwasm_compress_stringify import compress_stringify_async, parse_string_decompress_async | ||
|
||
data = bytes([222, 173, 190, 239]) | ||
compressed_data = await compress_stringify_async(data, compression_level=8, stringify=True) | ||
decoded = compressed_data.decode() | ||
assert decoded.lower() == 'data:base64,kluv/saeiqaa3q2+7w==' | ||
decompressed_data = await parse_string_decompress_async(compressed_data, parse_string=True) | ||
|
||
assert decompressed_data[0] == 222 | ||
assert decompressed_data[1] == 173 | ||
assert decompressed_data[2] == 190 | ||
assert decompressed_data[3] == 239 | ||
|
||
@run_in_pyodide(packages=['micropip']) | ||
async def test_we_can_use_a_custom_data_url_prefix(selenium, package_wheel): | ||
import micropip | ||
await micropip.install([package_wheel]) | ||
|
||
from itkwasm_compress_stringify import compress_stringify_async, parse_string_decompress_async | ||
|
||
data = bytes([222, 173, 190, 239]) | ||
compressed_data = await compress_stringify_async(data, compression_level=8, stringify=True, data_url_prefix='custom,') | ||
assert compressed_data.decode().lower() == 'custom,kluv/saeiqaa3q2+7w==' | ||
decompressed_data = await parse_string_decompress_async(compressed_data, parse_string=True) | ||
|
||
assert decompressed_data[0] == 222 | ||
assert decompressed_data[1] == 173 | ||
assert decompressed_data[2] == 190 | ||
assert decompressed_data[3] == 239 |
Oops, something went wrong.