Moved derive unit API to DLUnit

Derived units now use a DLUnit class similar to a DLPak to be easily
stored and loaded, the public API has been updated to either take a
DLUnit object or a Path where it reads the DLUnit from a *.dlunit file

The .dlunit file is a ZIP compressed archive containing two files, a
manifest.json which stores the units uuid, its input signals and its
output signal, and unit.py which is the python code for the unit, the
same rules apply (one python function with a specifically structured
signature) as when a string was passed in the old API

Errors have been added to have a DLUnitError similar to the DLPakError
as the DLUnit class is heavily inspired from the DLPak architecutre

Logging may still be primitive, this will be covered in the next AI
logging sweep to homogenize and clean up the logging aspect of things
This commit is contained in:
h3cx committed 2026-09-15 17:13:27 +02:00
1 parent 9407bfeb79
commit 207a91811e
10 files changed
+378 -44

No files matched your search

+34 -1
View File
@@ -1,14 +1,16 @@
import logging
from pathlib import Path
import threading
from uuid import uuid4
import pytest
from dynalab_core import Core
from dynalab_core.config import CoreConfig
from dynalab_core.dlunit import DLUnit
from dynalab_core.errors import CoreError
from dynalab_core.protocols.endpoint import ConnectorEndpoint
from dynalab_core.protocols.packets.data import ValueDescriptor
from dynalab_core.protocols.packets.handshake import ConnectorHello
from dynalab_core.protocols.packets.handshake import ConnectorHello, SignalDescriptor
from test.common import find_available_port
@@ -99,3 +101,34 @@ def test_core_mode_is_shared_with_endpoints() -> None:
assert raised.value.code == "no_playback_data"
finally:
core.stop()
def test_core_binds_in_memory_dlunit() -> None:
core = Core(CoreConfig(port=find_available_port(8765)))
input_signal = SignalDescriptor(id=uuid4(), name="Input", type="number")
return_signal = SignalDescriptor(
id=uuid4(), name="Output", type="number", origin="derived"
)
dlunit = DLUnit()
dlunit.set_manifest(uuid4(), [input_signal], return_signal)
dlunit.set_data(
"""
from dynalab_core.protocols.packets.data import ValueDescriptor
from dynalab_core.protocols.packets.handshake import SignalDescriptor
def identity(value: ValueDescriptor, output: SignalDescriptor) -> ValueDescriptor:
return ValueDescriptor(signal_id=output.id, value=value.value, timestamp=value.timestamp)
"""
)
try:
unit_id = core.bind_derive_unit(dlunit)
assert core.get_signal_descriptor(return_signal.id) == return_signal
with pytest.raises(CoreError, match="either a DLUnit or a file") as raised:
core.bind_derive_unit(dlunit, file=Path("unit.dlunit"))
assert raised.value.code == "bind_derive_failed"
core.unbind_derive_unit(unit_id)
finally:
core.stop()