-
Notifications
You must be signed in to change notification settings - Fork 60
/
_modules.py
35 lines (25 loc) · 1.06 KB
/
_modules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
__all__ = ("namespaced_id", "module_ui", "module_server")
import sys
from typing import Callable, TypeVar
if sys.version_info < (3, 10):
from typing_extensions import ParamSpec, Concatenate
else:
from typing import ParamSpec, Concatenate
from ._namespaces import namespaced_id, namespace_context, Id
from .session import Inputs, Outputs, Session, require_active_session, session_context
P = ParamSpec("P")
R = TypeVar("R")
def module_ui(fn: Callable[P, R]) -> Callable[Concatenate[str, P], R]:
def wrapper(id: Id, *args: P.args, **kwargs: P.kwargs) -> R:
with namespace_context(id):
return fn(*args, **kwargs)
return wrapper
def module_server(
fn: Callable[Concatenate[Inputs, Outputs, Session, P], R]
) -> Callable[Concatenate[str, P], R]:
def wrapper(id: Id, *args: P.args, **kwargs: P.kwargs) -> R:
sess = require_active_session(None)
child_sess = sess.make_scope(id)
with session_context(child_sess):
return fn(child_sess.input, child_sess.output, child_sess, *args, **kwargs)
return wrapper