Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add python example #12

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions python-example/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.10"
3 changes: 3 additions & 0 deletions python-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# python example

This example was adapted from [python-wasm-component](https://github.com/michelleN/python-wasm-component) repo. See repo for more information on how to build python component.
Binary file added python-example/__pycache__/app.cpython-311.pyc
Binary file not shown.
25 changes: 25 additions & 0 deletions python-example/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from http_app import exports

from http_app.imports.types import (
IncomingRequest,
OutgoingResponse,
ResponseOutparam,
OutgoingBody,
Fields,
Ok,
)


class IncomingHandler(exports.IncomingHandler):
def handle(self, request: IncomingRequest, response_out: ResponseOutparam):
response = OutgoingResponse(200, Fields([("HELLO", b"WORLD")]))

response_body = response.write()

ResponseOutparam.set(response_out, Ok(response))

response_stream = response_body.write()
response_stream.blocking_write_and_flush(b"Hello from python!")
response_stream.drop()

OutgoingBody.finish(response_body, None)
5 changes: 5 additions & 0 deletions python-example/componentize_py_runtime/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

from typing import List, Any

def call_import(index: int, args: List[Any], result_count: int) -> List[Any]:
raise NotImplementedError
13 changes: 13 additions & 0 deletions python-example/http_app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import TypeVar, Generic, Union, Optional, Union, Protocol, Tuple, List, Any
from enum import Flag, Enum, auto
from dataclasses import dataclass
from abc import abstractmethod
import weakref

from .types import Result, Ok, Err, Some
import componentize_py_runtime



class HttpApp(Protocol):
pass
16 changes: 16 additions & 0 deletions python-example/http_app/exports/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import TypeVar, Generic, Union, Optional, Union, Protocol, Tuple, List, Any
from enum import Flag, Enum, auto
from dataclasses import dataclass
from abc import abstractmethod
import weakref

from ..types import Result, Ok, Err, Some
from ..imports import types

class IncomingHandler(Protocol):

@abstractmethod
def handle(self, request: types.IncomingRequest, response_out: types.ResponseOutparam) -> None:
raise NotImplementedError


9 changes: 9 additions & 0 deletions python-example/http_app/exports/incoming_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import TypeVar, Generic, Union, Optional, Union, Protocol, Tuple, List, Any
from enum import Flag, Enum, auto
from dataclasses import dataclass
from abc import abstractmethod
import weakref

from ..types import Result, Ok, Err, Some


Empty file.
60 changes: 60 additions & 0 deletions python-example/http_app/imports/poll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
A poll API intended to let users wait for I/O events on multiple handles
at once.
"""
from typing import TypeVar, Generic, Union, Optional, Union, Protocol, Tuple, List, Any
from enum import Flag, Enum, auto
from dataclasses import dataclass
from abc import abstractmethod
import weakref

from ..types import Result, Ok, Err, Some
import componentize_py_runtime


class Pollable:
"""
A "pollable" handle.
"""

def drop(self):
(_, func, args, _) = self.finalizer.detach()
self.handle = None
func(args[0], args[1])



def poll_list(in_: List[Pollable]) -> List[int]:
"""
Poll for completion on a set of pollables.

This function takes a list of pollables, which identify I/O sources of
interest, and waits until one or more of the events is ready for I/O.

The result `list<u32>` contains one or more indices of handles in the
argument list that is ready for I/O.

If the list contains more elements than can be indexed with a `u32`
value, this function traps.

A timeout can be implemented by adding a pollable from the
wasi-clocks API to the list.

This function does not return a `result`; polling in itself does not
do any I/O so it doesn't fail. If any of the I/O sources identified by
the pollables has an error, it is indicated by marking the source as
being reaedy for I/O.
"""
result = componentize_py_runtime.call_import(1, [in_], 1)
return result[0]

def poll_one(in_: Pollable) -> None:
"""
Poll for completion on a single pollable.

This function is similar to `poll-list`, but operates on only a single
pollable. When it returns, the handle is ready for I/O.
"""
result = componentize_py_runtime.call_import(2, [in_], 0)
return

Loading