-
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 python WASI support
- Loading branch information
Showing
21 changed files
with
892 additions
and
46 deletions.
There are no files selected for viewing
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
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
11 changes: 11 additions & 0 deletions
11
packages/compress-stringify/python/compress-stringify-wasi/README.md
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,11 @@ | ||
# compress-stringify-wasi | ||
|
||
[![PyPI version](https://badge.fury.io/py/compress-stringify-wasi.svg)](https://badge.fury.io/py/compress-stringify-wasi) | ||
|
||
Zstandard compression and decompression and base64 encoding and decoding in WebAssembly. WASI implementation. | ||
|
||
## Installation | ||
|
||
```sh | ||
pip install compress-stringify-wasi | ||
``` |
6 changes: 6 additions & 0 deletions
6
...ges/compress-stringify/python/compress-stringify-wasi/compress_stringify_wasi/__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 @@ | ||
"""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__ |
1 change: 1 addition & 0 deletions
1
...ges/compress-stringify/python/compress-stringify-wasi/compress_stringify_wasi/_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.1.0" |
83 changes: 83 additions & 0 deletions
83
...ss-stringify/python/compress-stringify-wasi/compress_stringify_wasi/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,83 @@ | ||
# Generated file. Do not edit. | ||
|
||
from pathlib import Path | ||
import os | ||
from typing import Dict, Tuple | ||
|
||
from importlib_resources import files as file_resources | ||
|
||
from itkwasm import ( | ||
InterfaceTypes, | ||
PipelineOutput, | ||
PipelineInput, | ||
Pipeline, | ||
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 | ||
""" | ||
pipeline = Pipeline(file_resources('compress_stringify_wasi').joinpath(Path('wasm_modules') / Path('compress-stringify.wasi.wasm'))) | ||
|
||
pipeline_outputs: List[PipelineOutput] = [ | ||
PipelineOutput(InterfaceTypes.BinaryStream), | ||
] | ||
|
||
pipeline_inputs: List[PipelineInput] = [ | ||
PipelineInput(InterfaceTypes.BinaryStream, BinaryStream(input)), | ||
] | ||
|
||
args: List[str] = ['--memory-io',] | ||
# Inputs | ||
args.append('0') | ||
# Outputs | ||
args.append('0') | ||
# Options | ||
if stringify is not None: | ||
args.append('--stringify') | ||
|
||
if compression_level is not None: | ||
args.append('--compression-level') | ||
args.append(str(compression_level)) | ||
|
||
if data_url_prefix is not None: | ||
args.append('--data-url-prefix') | ||
args.append(str(data_url_prefix)) | ||
|
||
|
||
|
||
outputs = pipeline.run(args, pipeline_outputs, pipeline_inputs) | ||
|
||
result = outputs[0].data.data | ||
return result | ||
|
||
|
||
del pipeline |
67 changes: 67 additions & 0 deletions
67
...ringify/python/compress-stringify-wasi/compress_stringify_wasi/parse_string_decompress.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,67 @@ | ||
# Generated file. Do not edit. | ||
|
||
from pathlib import Path | ||
import os | ||
from typing import Dict, Tuple | ||
|
||
from importlib_resources import files as file_resources | ||
|
||
from itkwasm import ( | ||
InterfaceTypes, | ||
PipelineOutput, | ||
PipelineInput, | ||
Pipeline, | ||
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 | ||
""" | ||
pipeline = Pipeline(file_resources('compress_stringify_wasi').joinpath(Path('wasm_modules') / Path('parse-string-decompress.wasi.wasm'))) | ||
|
||
pipeline_outputs: List[PipelineOutput] = [ | ||
PipelineOutput(InterfaceTypes.BinaryStream), | ||
] | ||
|
||
pipeline_inputs: List[PipelineInput] = [ | ||
PipelineInput(InterfaceTypes.BinaryStream, BinaryStream(input)), | ||
] | ||
|
||
args: List[str] = ['--memory-io',] | ||
# Inputs | ||
args.append('0') | ||
# Outputs | ||
args.append('0') | ||
# Options | ||
if parse_string is not None: | ||
args.append('--parse-string') | ||
|
||
|
||
|
||
outputs = pipeline.run(args, pipeline_outputs, pipeline_inputs) | ||
|
||
result = outputs[0].data.data | ||
return result | ||
|
||
|
||
del pipeline |
Binary file added
BIN
+961 KB
...compress-stringify-wasi/compress_stringify_wasi/wasm_modules/compress-stringify.wasi.wasm
Binary file not shown.
Binary file added
BIN
+661 KB
...ess-stringify-wasi/compress_stringify_wasi/wasm_modules/parse-string-decompress.wasi.wasm
Binary file not shown.
54 changes: 54 additions & 0 deletions
54
packages/compress-stringify/python/compress-stringify-wasi/pyproject.toml
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,54 @@ | ||
[build-system] | ||
requires = ["hatchling", "hatch-vcs"] | ||
build-backend = "hatchling.build" | ||
|
||
[project] | ||
name = "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.0b.82", | ||
"importlib_resources", | ||
] | ||
|
||
[tool.hatch.version] | ||
path = "compress_stringify_wasi/_version.py" | ||
|
||
[tool.hatch.envs.default] | ||
dependencies = [ | ||
"pytest", | ||
] | ||
|
||
[tool.hatch.envs.default.scripts] | ||
test = "pytest" | ||
|
||
[tool.hatch.build] | ||
exclude = [ | ||
"/examples", | ||
] |
33 changes: 33 additions & 0 deletions
33
packages/compress-stringify/python/compress-stringify-wasi/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,33 @@ | ||
from 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 |
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
2 changes: 1 addition & 1 deletion
2
packages/dicom/typescript/src/apply-presentation-state-to-image-options.ts
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
File renamed without changes.
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,20 @@ | ||
import { readFile } from 'node:fs/promises'; | ||
import { WASI } from 'wasi'; | ||
import { argv, env } from 'node:process'; | ||
|
||
const wasi = new WASI({ | ||
args: ['--interface-json'], | ||
env, | ||
preopens: { | ||
}, | ||
}); | ||
|
||
const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; | ||
|
||
const wasm = await WebAssembly.compile( | ||
await readFile(new URL(process.argv[2], import.meta.url)), | ||
); | ||
const instance = await WebAssembly.instantiate(wasm, importObject); | ||
|
||
wasi.initialize(instance); | ||
instance.exports['']() |
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,19 @@ | ||
const interfaceJsonTypeToInterfaceType = new Map([ | ||
['INPUT_TEXT_FILE:FILE', 'TextFile'], | ||
['OUTPUT_TEXT_FILE:FILE', 'TextFile'], | ||
['INPUT_BINARY_FILE:FILE', 'BinaryFile'], | ||
['OUTPUT_BINARY_FILE:FILE', 'BinaryFile'], | ||
['INPUT_TEXT_STREAM', 'TextStream'], | ||
['OUTPUT_TEXT_STREAM', 'TextStream'], | ||
['INPUT_BINARY_STREAM', 'BinaryStream'], | ||
['OUTPUT_BINARY_STREAM', 'BinaryStream'], | ||
['INPUT_IMAGE', 'Image'], | ||
['OUTPUT_IMAGE', 'Image'], | ||
['INPUT_MESH', 'Mesh'], | ||
['OUTPUT_MESH', 'Mesh'], | ||
['INPUT_POLYDATA', 'PolyData'], | ||
['OUTPUT_POLYDATA', 'PolyData'], | ||
['OUTPUT_JSON', 'JsonObject'], | ||
]) | ||
|
||
export default interfaceJsonTypeToInterfaceType |
Oops, something went wrong.