Skip to content

Commit

Permalink
feat(bindgen): Python interface package with environment dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Apr 12, 2023
1 parent 97d4f77 commit e2b2c84
Show file tree
Hide file tree
Showing 22 changed files with 313 additions and 56 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"build:emscripten": "node ./src/build-emscripten.js",
"build:emscripten:compress-stringify": "node ./src/itk-wasm-cli.js -s packages/compress-stringify -b emscripten-build build ",
"build:bindgen:typescript:compress-stringify": "./src/itk-wasm-cli.js -s packages/compress-stringify -b emscripten-build bindgen --package-name @itk-wasm/compress-stringify --package-description \"Zstandard compression and decompression and base64 encoding and decoding in WebAssembly.\" --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:bindgen:python:compress-stringify": "./src/itk-wasm-cli.js -s packages/compress-stringify -b wasi-build bindgen --language python --package-name compress-stringify --package-description \"Zstandard compression and decompression and base64 encoding and decoding in WebAssembly.\" --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:bindgen:python:compress-stringify": "./src/itk-wasm-cli.js -s packages/compress-stringify -b wasi-build bindgen --language python --package-name itkwasm-compress-stringify --package-description \"Zstandard compression and decompression and base64 encoding and decoding in WebAssembly.\" --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:emscripten:dicom": "node ./src/itk-wasm-cli.js -s packages/dicom -b emscripten-build build ",
"build:bindgen:typescript:dicom": "./src/itk-wasm-cli.js -s packages/dicom -b emscripten-build bindgen --package-name @itk-wasm/dicom --package-description \"Read files and images related to DICOM file format.\" --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:emscripten:packages": "npm run build:emscripten:compress-stringify && npm run build:bindgen:typescript:compress-stringify && npm run build:emscripten:dicom && npm run build:bindgen:typescript:dicom",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# itkwasm-compress-stringify-wasi

[![PyPI version](https://badge.fury.io/py/itkwasm-compress-stringify-wasi.svg)](https://badge.fury.io/py/itkwasm-compress-stringify-wasi)

Zstandard compression and decompression and base64 encoding and decoding in WebAssembly. WASI implementation.

This package provides the WASI WebAssembly implementation. It is usually not called directly. Please use the [`itkwasm-compress-stringify`](https://pypi.org/project/itkwasm-compress-stringify/) instead.


## Installation

```sh
pip install itkwasm-compress-stringify-wasi
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""itkwasm-compress-stringify-wasi: Zstandard compression and decompression and base64 encoding and decoding in WebAssembly. WASI implementation."""

from .compress_stringify import compress_stringify
from .parse_string_decompress import parse_string_decompress

from ._version import __version__
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def compress_stringify(
Output compressed binary
"""
pipeline = Pipeline(file_resources('compress_stringify_wasi').joinpath(Path('wasm_modules') / Path('compress-stringify.wasi.wasm')))
pipeline = Pipeline(file_resources('itkwasm_compress_stringify_wasi').joinpath(Path('wasm_modules') / Path('compress-stringify.wasi.wasm')))

pipeline_outputs: List[PipelineOutput] = [
PipelineOutput(InterfaceTypes.BinaryStream),
Expand Down Expand Up @@ -73,7 +73,6 @@ def compress_stringify(
args.append(str(data_url_prefix))



outputs = pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = outputs[0].data.data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def parse_string_decompress(
Output decompressed binary
"""
pipeline = Pipeline(file_resources('compress_stringify_wasi').joinpath(Path('wasm_modules') / Path('parse-string-decompress.wasi.wasm')))
pipeline = Pipeline(file_resources('itkwasm_compress_stringify_wasi').joinpath(Path('wasm_modules') / Path('parse-string-decompress.wasi.wasm')))

pipeline_outputs: List[PipelineOutput] = [
PipelineOutput(InterfaceTypes.BinaryStream),
Expand All @@ -57,7 +57,6 @@ def parse_string_decompress(
args.append('--parse-string')



outputs = pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = outputs[0].data.data
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"

[project]
name = "itkwasm-compress-stringify-wasi"
readme = "README.md"
license = "Apache-2.0"
dynamic = ["version", "description"]
classifiers = [
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python",
"Programming Language :: C++",
"Environment :: WebAssembly",
"Environment :: WebAssembly :: Emscripten",
"Environment :: WebAssembly :: WASI",
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
]
keywords = [
"itkwasm",
"webassembly",
"emscripten",
"wasi",
]

requires-python = ">=3.7"
dependencies = [
"itkwasm >= 1.0b93",
"importlib_resources",
]

[tool.hatch.version]
path = "itkwasm_compress_stringify_wasi/_version.py"

[tool.hatch.envs.default]
dependencies = [
"pytest",
]

[tool.hatch.envs.default.scripts]
test = "pytest"

[tool.hatch.build]
exclude = [
"/examples",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from itkwasm_compress_stringify_wasi import compress_stringify, parse_string_decompress

def test_decompress_returns_what_was_compressed():
data = bytes([222, 173, 190, 239])
compressed_data = compress_stringify(data, compression_level=8)
decompressed_data = parse_string_decompress(compressed_data)

assert decompressed_data[0] == 222
assert decompressed_data[1] == 173
assert decompressed_data[2] == 190
assert decompressed_data[3] == 239

def test_we_can_stringify_during_compression():
data = bytes([222, 173, 190, 239])
compressed_data = compress_stringify(data, compression_level=8, stringify=True)
assert compressed_data.decode() == 'data:base64,KLUv/SAEIQAA3q2+7w=='
decompressed_data = parse_string_decompress(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

def test_we_can_use_a_custom_data_url_prefix():
data = bytes([222, 173, 190, 239])
compressed_data = compress_stringify(data, compression_level=8, stringify=True, data_url_prefix='custom,')
assert compressed_data.decode() == 'custom,KLUv/SAEIQAA3q2+7w=='
decompressed_data = parse_string_decompress(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
@@ -0,0 +1,11 @@
# itkwasm-compress-stringify

[![PyPI version](https://badge.fury.io/py/itkwasm-compress-stringify.svg)](https://badge.fury.io/py/itkwasm-compress-stringify)

Zstandard compression and decompression and base64 encoding and decoding in WebAssembly.

## Installation

```sh
pip install itkwasm-compress-stringify
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""compress-stringify-wasi: Zstandard compression and decompression and base64 encoding and decoding in WebAssembly. WASI implementation."""
"""itkwasm-compress-stringify: Zstandard compression and decompression and base64 encoding and decoding in WebAssembly."""

from .compress_stringify import compress_stringify
from .parse_string_decompress import parse_string_decompress
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.1.0"
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,
)

def compress_stringify(
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")
output = 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,
)

def parse_string_decompress(
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")
output = func(input, parse_string=parse_string)
return output
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"

[project]
name = "compress-stringify-wasi"
name = "itkwasm-compress-stringify"
readme = "README.md"
license = "Apache-2.0"
dynamic = ["version", "description"]
Expand Down Expand Up @@ -33,12 +33,12 @@ keywords = [

requires-python = ">=3.7"
dependencies = [
"itkwasm >= 1.0b.82",
"itkwasm >= 1.0b93",
"importlib_resources",
]

[tool.hatch.version]
path = "compress_stringify_wasi/_version.py"
path = "itkwasm_compress_stringify/_version.py"

[tool.hatch.envs.default]
dependencies = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from compress_stringify_wasi import compress_stringify, parse_string_decompress
from itkwasm_compress_stringify import compress_stringify, parse_string_decompress

def test_decompress_returns_what_was_compressed():
data = bytes([222, 173, 190, 239])
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/python-resources/template.pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ keywords = [

requires-python = ">=3.7"
dependencies = [
"itkwasm >= 1.0b.82",
"itkwasm >= 1.0b93",
"importlib_resources",
]

Expand Down
Loading

0 comments on commit e2b2c84

Please sign in to comment.