Skip to content

Commit 41ba045

Browse files
author
Philip Z
committed
feat(runtime): compose and install passive modules
1 parent 62c12de commit 41ba045

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/teaql/runtime/context.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ def with_module(self, module: Any) -> 'UserContext':
159159
module.apply_to(self)
160160
return self
161161

162+
def install(self, module: Any) -> 'UserContext':
163+
"""Install a passive runtime manifest without changing database schemas."""
164+
return self.with_module(module)
165+
162166
def set_initial_graphs(self, graphs: List[Any]):
163167
self._initial_graphs = graphs
164168

@@ -630,6 +634,10 @@ def get_service(self, name: str) -> Optional[Any]:
630634
def require_service(self, name: str) -> Any:
631635
return self._ctx.require_resource(name)
632636

637+
def install(self, module: Any) -> 'TeaqlRuntime':
638+
self._ctx.install(module)
639+
return self
640+
633641
from enum import Enum, auto
634642
from dataclasses import dataclass, field
635643
from datetime import datetime, timedelta

src/teaql/runtime/module.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ def audit_event_sink(self, sink: Any) -> 'RuntimeModule':
3636
self._audit_sinks.append(sink)
3737
return self
3838

39+
def and_module(self, other: 'RuntimeModule') -> 'RuntimeModule':
40+
combined = RuntimeModule.new()
41+
combined._entities = [*self._entities, *other._entities]
42+
combined._behaviors = {**self._behaviors, **other._behaviors}
43+
combined._dependencies = {**self._dependencies, **other._dependencies}
44+
combined._audit_sinks = [*self._audit_sinks, *other._audit_sinks]
45+
combined._initial_graphs = [
46+
*getattr(self, '_initial_graphs', []),
47+
*getattr(other, '_initial_graphs', []),
48+
]
49+
return combined
50+
3951
def apply_to(self, ctx: UserContext):
4052
for name, dep in self._dependencies.items():
4153
ctx.insert_resource(name, dep)

tests/runtime/test_runtime.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ def test_runtime_module_initialization():
3030
assert "Dummy" in behaviors
3131
assert isinstance(behaviors["Dummy"], DummyBehavior)
3232

33+
def test_runtime_module_composes_and_installs_without_schema_changes():
34+
first = RuntimeModule.new().entity(DummyEntity)
35+
second = RuntimeModule.new().provide_custom_dependency("dummy", DummyDependency())
36+
ctx = UserContext.new().install(first.and_module(second))
37+
assert DummyEntity in ctx.require_resource("entities")
38+
assert isinstance(ctx.require_resource("dummy"), DummyDependency)
39+
3340
@pytest.mark.asyncio
3441
async def test_runtime_module_configure():
3542
module = RuntimeModule.new()

0 commit comments

Comments
 (0)