python_decoder

This commit is contained in:
Martin Kavík 2024-06-25 02:50:45 +02:00
parent 714fc0cd00
commit 2fbb9377f6
12 changed files with 124 additions and 0 deletions

View file

@ -0,0 +1 @@
https://component-model.bytecodealliance.org/

View file

@ -0,0 +1 @@
__pycache__

View file

@ -0,0 +1,11 @@
How to create and build the Python component:
1. `pip install componentize-py`
2. Create the `python_decoder` folder
3. `cd python_decoder`
4. Create `.gitignore` with content `__pycache__`
5. Create the `src` folder with the file `app.py`
6. Create the `wit` folder with the file `world.wit`
7. Update code as needed
8. `componentize-py --wit-path wit/world.wit bindings src/bindings`
9. `componentize-py --wit-path wit/world.wit componentize src.app --output python_decoder.wasm`

View file

@ -0,0 +1,14 @@
from .bindings.component import exports
from .bindings.component.imports import host
name = "Python Test Decoder"
class Decoder(exports.Decoder):
def init(self) -> None:
host.log(f"{name} initialized")
def name(self) -> str:
return name
def format_signal_value(self, value: str) -> str:
return str + "!"

View file

@ -0,0 +1,12 @@
from typing import TypeVar, Generic, Union, Optional, Protocol, Tuple, List, Any, Self
from enum import Flag, Enum, auto
from dataclasses import dataclass
from abc import abstractmethod
import weakref
from .types import Result, Ok, Err, Some
class Component(Protocol):
pass

View file

@ -0,0 +1,24 @@
from typing import TypeVar, Generic, Union, Optional, Protocol, Tuple, List, Any, Self
from enum import Flag, Enum, auto
from dataclasses import dataclass
from abc import abstractmethod
import weakref
from ..types import Result, Ok, Err, Some
class Decoder(Protocol):
@abstractmethod
def init(self) -> None:
raise NotImplementedError
@abstractmethod
def name(self) -> str:
raise NotImplementedError
@abstractmethod
def format_signal_value(self, value: str) -> str:
raise NotImplementedError

View file

@ -0,0 +1,9 @@
from typing import TypeVar, Generic, Union, Optional, Protocol, Tuple, List, Any, Self
from enum import Flag, Enum, auto
from dataclasses import dataclass
from abc import abstractmethod
import weakref
from ..types import Result, Ok, Err, Some

View file

@ -0,0 +1,13 @@
from typing import TypeVar, Generic, Union, Optional, Protocol, Tuple, List, Any, Self
from enum import Flag, Enum, auto
from dataclasses import dataclass
from abc import abstractmethod
import weakref
from ..types import Result, Ok, Err, Some
def log(message: str) -> None:
raise NotImplementedError

View file

@ -0,0 +1,23 @@
from typing import TypeVar, Generic, Union, Optional, Protocol, Tuple, List, Any, Self
from enum import Flag, Enum, auto
from dataclasses import dataclass
from abc import abstractmethod
import weakref
S = TypeVar('S')
@dataclass
class Some(Generic[S]):
value: S
T = TypeVar('T')
@dataclass
class Ok(Generic[T]):
value: T
E = TypeVar('E')
@dataclass(frozen=True)
class Err(Generic[E], Exception):
value: E
Result = Union[Ok[T], Err[E]]

View file

@ -0,0 +1,16 @@
package component:python-decoder;
interface host {
log: func(message: string);
}
interface decoder {
init: func();
name: func() -> string;
format-signal-value: func(value: string) -> string;
}
world component {
import host;
export decoder;
}