-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp_framework.py
More file actions
159 lines (127 loc) · 4.92 KB
/
Copy pathapp_framework.py
File metadata and controls
159 lines (127 loc) · 4.92 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import json
import inspect
import functools
from typing import Any, Dict, List, TypeVar, Callable, Optional
from dataclasses import dataclass
T = TypeVar("T")
# Context definition
@dataclass
class KernelContext:
"""Context object passed to action handlers"""
invocation_id: str
# Action definition
@dataclass
class KernelAction:
"""Action that can be invoked on a Kernel app"""
name: str
handler: Callable[..., Any]
# JSON interfaces
@dataclass
class KernelActionJson:
"""JSON representation of a Kernel action"""
name: str
@dataclass
class KernelAppJson:
"""JSON representation of a Kernel app"""
name: str
actions: List[KernelActionJson]
@dataclass
class KernelJson:
"""JSON representation of Kernel manifest"""
apps: List[KernelAppJson]
# App class
class KernelApp:
def __init__(self, name: str):
self.name = name
self.actions: Dict[str, KernelAction] = {}
# Register this app in the global registry
_app_registry.register_app(self)
def action(self, name: str) -> Callable[..., Any]:
"""
Decorator to register an action with the app
Usage:
@app.action("action-name")
def my_handler(ctx: KernelContext):
# ...
@app.action("action-with-payload")
def my_handler(ctx: KernelContext, payload: dict):
# ...
"""
def decorator(handler: Callable[..., Any]) -> Callable[..., Any]:
return self._register_action(name, handler)
return decorator
def _register_action(self, name: str, handler: Callable[..., Any]) -> Callable[..., Any]:
"""Internal method to register an action"""
# Validate handler signature
sig = inspect.signature(handler)
param_count = len(sig.parameters)
if param_count == 0:
raise TypeError("Action handler must accept at least the context parameter")
elif param_count > 2:
raise TypeError("Action handler can only accept context and payload parameters")
param_names = list(sig.parameters.keys())
@functools.wraps(handler)
def wrapper(*args: Any, **kwargs: Any) -> Any:
# Ensure the first argument is the context
if not args or not isinstance(args[0], KernelContext):
raise TypeError("First argument to action handler must be a KernelContext")
ctx = args[0]
if param_count == 1:
# Handler takes only context
return handler(ctx)
else: # param_count == 2
# Handler takes context and payload
if len(args) >= 2:
return handler(ctx, args[1])
else:
# Try to find payload in kwargs
payload_name = param_names[1]
if payload_name in kwargs:
return handler(ctx, kwargs[payload_name])
else:
raise TypeError(f"Missing required payload parameter '{payload_name}'")
action = KernelAction(name=name, handler=wrapper)
self.actions[name] = action
return wrapper
def get_actions(self) -> List[KernelAction]:
"""Get all actions for this app"""
return list(self.actions.values())
def get_action(self, name: str) -> Optional[KernelAction]:
"""Get an action by name"""
return self.actions.get(name)
def to_dict(self) -> KernelAppJson:
"""Export app information without handlers"""
return KernelAppJson(
name=self.name,
actions=[KernelActionJson(name=action.name) for action in self.get_actions()]
)
# Registry for storing Kernel apps
class KernelAppRegistry:
def __init__(self) -> None:
self.apps: Dict[str, KernelApp] = {}
def register_app(self, app: KernelApp) -> None:
self.apps[app.name] = app
def get_apps(self) -> List[KernelApp]:
return list(self.apps.values())
def get_app_by_name(self, name: str) -> Optional[KernelApp]:
return self.apps.get(name)
def export(self) -> KernelJson:
"""Export the registry as a KernelJson object"""
apps = [app.to_dict() for app in self.get_apps()]
return KernelJson(apps=apps)
def export_json(self) -> str:
"""Export the registry as JSON"""
kernel_json = self.export()
return json.dumps(kernel_json.__dict__, indent=2)
# Create singleton registry for apps
_app_registry = KernelAppRegistry()
# Create a simple function for creating apps
def App(name: str) -> KernelApp:
"""Create a new Kernel app"""
return KernelApp(name)
# Export the app registry for boot loader
app_registry = _app_registry
# Function to export registry as JSON
def export_registry() -> str:
"""Export the registry as JSON"""
return _app_registry.export_json()