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:
10 files changed
+378
-44
No files matched your search
@@ -19,6 +19,7 @@ from dynalab_core.config import CoreConfig
|
||||
from dynalab_core.constants import CORE_VERSION
|
||||
from dynalab_core.derive import DeriveRegistry
|
||||
from dynalab_core.dlpak import DLPak
|
||||
from dynalab_core.dlunit import DLUnit
|
||||
from dynalab_core.errors import CoreError
|
||||
from dynalab_core.playback import PlaybackEngine
|
||||
from dynalab_core.protocols.endpoint import ConnectorRegistry
|
||||
@@ -300,12 +301,22 @@ class Core:
|
||||
return {signal_id: value.get() for signal_id, value in live_values}
|
||||
|
||||
def bind_derive_unit(
|
||||
self,
|
||||
function: str,
|
||||
input_signals: list[SignalDescriptor],
|
||||
output_signal: SignalDescriptor,
|
||||
self, dlunit: DLUnit | None = None, file: Path | None = None
|
||||
) -> UUID:
|
||||
unit = self._derive_registry.register(function, input_signals, output_signal)
|
||||
if dlunit is not None and file is not None:
|
||||
raise CoreError(
|
||||
"Bind a derive unit from either a DLUnit or a file, not both",
|
||||
code="bind_derive_failed",
|
||||
)
|
||||
if dlunit is None and file is not None:
|
||||
dlunit = DLUnit().read(file)
|
||||
elif dlunit is None and file is None:
|
||||
raise CoreError(
|
||||
"Cannot bind derive unit with no dlunit OR file specified",
|
||||
code="bind_derive_failed",
|
||||
)
|
||||
|
||||
unit = self._derive_registry.register(dlunit)
|
||||
return unit.uuid()
|
||||
|
||||
def unbind_derive_unit(self, unit_id: UUID) -> None:
|
||||
|
||||
+16
-15
@@ -12,6 +12,7 @@ import threading
|
||||
import types
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from dynalab_core.dlunit import DLUnit
|
||||
from dynalab_core.errors import DeriveError
|
||||
from dynalab_core.protocols.packets.data import ValueDescriptor
|
||||
from dynalab_core.protocols.packets.handshake import SignalDescriptor
|
||||
@@ -214,17 +215,11 @@ class DeriveRegistry:
|
||||
)
|
||||
self._routing_worker_thread.start()
|
||||
|
||||
def register(
|
||||
self,
|
||||
function: str,
|
||||
input_signals: list[SignalDescriptor],
|
||||
output_signal: SignalDescriptor,
|
||||
unit_uuid: UUID | None = None,
|
||||
) -> DeriveUnit:
|
||||
def register(self, dlunit: DLUnit) -> DeriveUnit:
|
||||
|
||||
if unit_uuid is not None:
|
||||
if dlunit._manifest.unit_uuid is not None:
|
||||
with self._lock:
|
||||
current = self.get(unit_uuid)
|
||||
current = self.get(dlunit._manifest.unit_uuid)
|
||||
if current is not None:
|
||||
log.warning(
|
||||
"Derive Unit %s is already registered",
|
||||
@@ -236,14 +231,14 @@ class DeriveRegistry:
|
||||
},
|
||||
)
|
||||
raise DeriveError(
|
||||
f"Derive unit {unit_uuid} is already registered",
|
||||
f"Derive unit {dlunit._manifest.unit_uuid} is already registered",
|
||||
code="already_registered",
|
||||
)
|
||||
unit_id = unit_uuid or uuid4()
|
||||
unit_id = dlunit._manifest.unit_uuid or uuid4()
|
||||
|
||||
namespace = {}
|
||||
try:
|
||||
exec(function, namespace)
|
||||
exec(dlunit._data, namespace)
|
||||
except Exception as error:
|
||||
raise DeriveError(
|
||||
f"Could not load derive function: {error}",
|
||||
@@ -261,7 +256,11 @@ class DeriveRegistry:
|
||||
)
|
||||
|
||||
unit = DeriveUnit(
|
||||
unit_id, functions[0], input_signals, output_signal, self._core_input_queue
|
||||
unit_id,
|
||||
functions[0],
|
||||
dlunit._manifest.input_signals,
|
||||
dlunit._manifest.return_signal,
|
||||
self._core_input_queue,
|
||||
)
|
||||
|
||||
with self._lock:
|
||||
@@ -273,8 +272,10 @@ class DeriveRegistry:
|
||||
extra={
|
||||
"event": "derive.registered",
|
||||
"derive_uuid": str(unit_id),
|
||||
"input_signal_ids": [str(signal.id) for signal in input_signals],
|
||||
"output_signal_id": str(output_signal.id),
|
||||
"input_signal_ids": [
|
||||
str(signal.id) for signal in dlunit._manifest.input_signals
|
||||
],
|
||||
"output_signal_id": str(dlunit._manifest.return_signal.id),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
# Copyright (C) 2026 Hector van der Aa <hector@h3cx.dev>
|
||||
# Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Literal, Self
|
||||
from uuid import UUID
|
||||
from zipfile import ZIP_DEFLATED, ZipFile
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from dynalab_core.errors import DLPakError, DLUnitError
|
||||
from dynalab_core.protocols.packets.handshake import SignalDescriptor
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DeriveManifest(BaseModel):
|
||||
unit_uuid: UUID
|
||||
input_signals: list[SignalDescriptor] = Field(default_factory=list)
|
||||
return_signal: SignalDescriptor
|
||||
comments: str = ""
|
||||
|
||||
|
||||
class DLUnit:
|
||||
def __init__(self) -> None:
|
||||
self._dir: Path | None = None
|
||||
self._filename: str | None = None
|
||||
self._savetype: Literal["zip", "folder"] = "zip"
|
||||
self._data: str | None = None
|
||||
self._manifest: DeriveManifest | None = None
|
||||
|
||||
def set_data(self, data: str) -> None:
|
||||
self._data = data
|
||||
|
||||
def set_manifest(
|
||||
self,
|
||||
unit_uuid: UUID,
|
||||
input_signals: list[SignalDescriptor],
|
||||
return_signal: SignalDescriptor,
|
||||
) -> None:
|
||||
|
||||
self._manifest = DeriveManifest(
|
||||
unit_uuid=unit_uuid,
|
||||
input_signals=input_signals,
|
||||
return_signal=return_signal,
|
||||
)
|
||||
|
||||
def read(self, file: str | Path) -> Self:
|
||||
input_path = Path(file)
|
||||
|
||||
try:
|
||||
with ZipFile(input_path, mode="r") as archive:
|
||||
manifest = DeriveManifest.model_validate_json(
|
||||
archive.read("manifest.json")
|
||||
)
|
||||
data: str = archive.read("unit.py").decode("utf-8")
|
||||
except Exception as error:
|
||||
log.exception(
|
||||
"Failed to read DLUnit archive from %s",
|
||||
input_path.name,
|
||||
extra={
|
||||
"event": "dlunit.read_failed",
|
||||
"input_filename": input_path.name,
|
||||
"exception_type": type(error).__name__,
|
||||
},
|
||||
)
|
||||
raise DLUnitError(
|
||||
f"Could not read DLUnit archive {input_path}: {error}",
|
||||
code="read_failed",
|
||||
) from error
|
||||
|
||||
self._dir = input_path.parent
|
||||
self._filename = input_path.name
|
||||
self._data = data
|
||||
self._manifest = manifest
|
||||
|
||||
log.info(
|
||||
"Read DLUnit archive from %s",
|
||||
input_path.name,
|
||||
extra={
|
||||
"event": "dlunit.read",
|
||||
"input_filename": input_path.name,
|
||||
},
|
||||
)
|
||||
return self
|
||||
|
||||
def write(self, dir: str | Path, filename: str) -> None:
|
||||
output_dir = Path(dir)
|
||||
|
||||
if self._data is None:
|
||||
raise DLUnitError(
|
||||
"Cannot write a DLUnit archive before setting its data",
|
||||
code="no_data",
|
||||
)
|
||||
if self._manifest is None:
|
||||
raise DLUnitError(
|
||||
"Cannot write a DLUnit archive before creating its manifest",
|
||||
code="no_manifest",
|
||||
)
|
||||
|
||||
if output_dir.is_file():
|
||||
output_dir = output_dir.parent
|
||||
|
||||
file_path = Path(filename)
|
||||
|
||||
if file_path.suffix.lower() != ".dlunit":
|
||||
file_path = file_path.with_suffix(".dlunit")
|
||||
|
||||
output_path = output_dir / file_path
|
||||
|
||||
try:
|
||||
with ZipFile(output_path, mode="w", compression=ZIP_DEFLATED) as archive:
|
||||
archive.writestr(
|
||||
"manifest.json", self._manifest.model_dump_json(indent=2)
|
||||
)
|
||||
archive.writestr("unit.py", self._data.export_csv())
|
||||
except Exception as error:
|
||||
log.exception(
|
||||
"Failed to write DLUnit archive to %s",
|
||||
output_path.name,
|
||||
extra={
|
||||
"event": "dlunit.write_failed",
|
||||
"output_filename": output_path.name,
|
||||
"exception_type": type(error).__name__,
|
||||
},
|
||||
)
|
||||
raise DLUnitError(
|
||||
f"Could not write DLUnit archive {output_path}: {error}",
|
||||
code="write_failed",
|
||||
) from error
|
||||
|
||||
log.info(
|
||||
"Wrote DLUnit archive to %s",
|
||||
output_path.name,
|
||||
extra={
|
||||
"event": "dlunit.written",
|
||||
"output_filename": output_path.name,
|
||||
},
|
||||
)
|
||||
@@ -27,6 +27,10 @@ class DLPakError(DynaLabError):
|
||||
"""A DLPak archive cannot be prepared, read, or written."""
|
||||
|
||||
|
||||
class DLUnitError(DynaLabError):
|
||||
"""A DLPak archive cannot be prepared, read, or written."""
|
||||
|
||||
|
||||
class DeriveError(DynaLabError):
|
||||
"""A derive unit cannot be created or used as requested."""
|
||||
|
||||
|
||||
Reference in new issue
Block a user