Skip to content

Commit

Permalink
feat(bindgen): Add async support to Python dispatch package
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Apr 22, 2023
1 parent d52a836 commit 56bf081
Show file tree
Hide file tree
Showing 12 changed files with 383 additions and 10 deletions.
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__
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.4.2"
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)
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)
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)
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
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__
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ dependencies = [
"itkwasm >= 1.0.b97",
"itkwasm-compress-stringify-wasi; sys_platform != \"emscripten\"",
"itkwasm-compress-stringify-emscripten; sys_platform == \"emscripten\"",

]

[tool.hatch.version]
Expand All @@ -45,11 +44,26 @@ path = "itkwasm_compress_stringify/_version.py"
[tool.hatch.envs.default]
dependencies = [
"pytest",
"pytest-pyodide",
]


[tool.hatch.envs.default.scripts]
test = "pytest"
test = [
"hatch build -t wheel",
"pytest --dist-dir=./dist --rt=chrome",
]
download-pyodide = [
"curl -L https://github.com/pyodide/pyodide/releases/download/0.23.1/pyodide-0.23.1.tar.bz2 -o pyodide.tar.bz2",
"tar xjf pyodide.tar.bz2",
"rm -rf dist pyodide.tar.bz2",
"mv pyodide dist",
]
serve = [
"hatch build -t wheel",
'echo "\nVisit http://localhost:8877/console.html\n"',
"python -m http.server --directory=./dist 8877",
]


[tool.hatch.build]
Expand Down
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
Loading

0 comments on commit 56bf081

Please sign in to comment.