Add Python support.
This commit is contained in:
43
slingshot_microservice/__init__.py
Normal file
43
slingshot_microservice/__init__.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import pathlib
|
||||
import sys
|
||||
from types import ModuleType
|
||||
|
||||
|
||||
def _load_native() -> ModuleType:
|
||||
package_dir = pathlib.Path(__file__).resolve().parent
|
||||
project_root = package_dir.parent
|
||||
|
||||
# Common library locations for local cargo builds and wheel installs.
|
||||
candidates = [
|
||||
project_root / "target" / "debug" / "libslingshot_microservice.so",
|
||||
project_root / "target" / "release" / "libslingshot_microservice.so",
|
||||
]
|
||||
candidates.extend(package_dir.glob("_native*.so"))
|
||||
candidates.extend(package_dir.glob("libslingshot_microservice*.so"))
|
||||
|
||||
module_name = "slingshot_microservice._native"
|
||||
for candidate in candidates:
|
||||
if not candidate.exists():
|
||||
continue
|
||||
|
||||
spec = importlib.util.spec_from_file_location(module_name, candidate)
|
||||
if spec is None or spec.loader is None:
|
||||
continue
|
||||
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules[module_name] = module
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
raise ModuleNotFoundError(
|
||||
"Native extension is not built. Build it with: cargo build --features python"
|
||||
)
|
||||
|
||||
|
||||
_native = _load_native()
|
||||
Microservice = _native.Microservice
|
||||
|
||||
__all__ = ["Microservice"]
|
||||
BIN
slingshot_microservice/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
slingshot_microservice/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
slingshot_microservice/__pycache__/__init__.cpython-314.pyc
Normal file
BIN
slingshot_microservice/__pycache__/__init__.cpython-314.pyc
Normal file
Binary file not shown.
BIN
slingshot_microservice/__pycache__/typing.cpython-312.pyc
Normal file
BIN
slingshot_microservice/__pycache__/typing.cpython-312.pyc
Normal file
Binary file not shown.
BIN
slingshot_microservice/__pycache__/typing.cpython-314.pyc
Normal file
BIN
slingshot_microservice/__pycache__/typing.cpython-314.pyc
Normal file
Binary file not shown.
30
slingshot_microservice/typing.py
Normal file
30
slingshot_microservice/typing.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import BinaryIO, Generator, Protocol, TypeAlias
|
||||
|
||||
CaseVariable: TypeAlias = bool | int | str
|
||||
|
||||
|
||||
class ReadFileFn(Protocol):
|
||||
def __call__(self, key: str, id: int) -> BinaryIO: ...
|
||||
|
||||
|
||||
class WriteFileFn(Protocol):
|
||||
def __call__(self, key: str, id: int) -> BinaryIO: ...
|
||||
|
||||
|
||||
class ProcessFn(Protocol):
|
||||
def __call__(
|
||||
self,
|
||||
request: int,
|
||||
read_file: ReadFileFn,
|
||||
write_file: WriteFileFn,
|
||||
) -> Generator[tuple[int, CaseVariable], None, None]: ...
|
||||
|
||||
|
||||
__all__ = [
|
||||
"CaseVariable",
|
||||
"ReadFileFn",
|
||||
"WriteFileFn",
|
||||
"ProcessFn",
|
||||
]
|
||||
Reference in New Issue
Block a user