dflowfm-io 0.1.0.dev1__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dflowfm_io/__init__.py +11 -0
- dflowfm_io/_lib/dflowfm_io_api.dll +0 -0
- dflowfm_io/_lib/library_path.py +4 -0
- dflowfm_io/base/__init__.py +27 -0
- dflowfm_io/base/bindings.py +88 -0
- dflowfm_io/base/errors.py +16 -0
- dflowfm_io/base/handle.py +11 -0
- dflowfm_io/base/library.py +53 -0
- dflowfm_io/issues.py +28 -0
- dflowfm_io/mdu/__init__.py +11 -0
- dflowfm_io/mdu/document.py +92 -0
- dflowfm_io/mdu/model.py +123 -0
- dflowfm_io/mdu/schema.py +7525 -0
- dflowfm_io-0.1.0.dev1.dist-info/METADATA +129 -0
- dflowfm_io-0.1.0.dev1.dist-info/RECORD +17 -0
- dflowfm_io-0.1.0.dev1.dist-info/WHEEL +5 -0
- dflowfm_io-0.1.0.dev1.dist-info/top_level.txt +1 -0
dflowfm_io/__init__.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Python bindings for the shared D-Flow FM IO library (``dflowfm_io_api``).
|
|
2
|
+
|
|
3
|
+
Importing this package loads the native library and exposes the public API:
|
|
4
|
+
:class:`MduDocument` (with its :class:`MduModel`, :class:`MduReport`, and typed
|
|
5
|
+
:class:`MduSchema`) and the :class:`Issue` / :class:`Severity` value types.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from dflowfm_io.issues import Issue, Severity
|
|
9
|
+
from dflowfm_io.mdu import MduDocument, MduModel, MduReport, MduSchema
|
|
10
|
+
|
|
11
|
+
__all__ = ["MduDocument", "MduModel", "MduReport", "MduSchema", "Issue", "Severity"]
|
|
Binary file
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Reusable infrastructure for the dflowfm_io bindings.
|
|
2
|
+
|
|
3
|
+
Native-library loading, C return-code handling, and native handle management — the pieces any module
|
|
4
|
+
wrapping the shared library needs, not just the MDU one.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from dflowfm_io.base.errors import (
|
|
8
|
+
DFLOWFM_IO_RESULT_ERROR,
|
|
9
|
+
DFLOWFM_IO_RESULT_SUCCESS,
|
|
10
|
+
check_result,
|
|
11
|
+
)
|
|
12
|
+
from dflowfm_io.base.handle import HandleRef
|
|
13
|
+
from dflowfm_io.base.library import Lib, LibLoader, lib
|
|
14
|
+
|
|
15
|
+
# Importing bindings applies the generated argtypes/restype to `lib` (generated from the C header).
|
|
16
|
+
from dflowfm_io.base import bindings # noqa: E402,F401
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"lib",
|
|
21
|
+
"Lib",
|
|
22
|
+
"LibLoader",
|
|
23
|
+
"check_result",
|
|
24
|
+
"DFLOWFM_IO_RESULT_SUCCESS",
|
|
25
|
+
"DFLOWFM_IO_RESULT_ERROR",
|
|
26
|
+
"HandleRef",
|
|
27
|
+
]
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Hand-maintained ctypes ABI mirror of the dflowfm_io_api C header.
|
|
2
|
+
|
|
3
|
+
Enum constants, the mdu_issue_t struct, and argtypes/restype for every exported function, applied
|
|
4
|
+
to the loaded library. This is a small, rarely-changing file kept in sync with the C header by
|
|
5
|
+
hand; the typed model (mdu/model.py) and schema (mdu/schema.py) are generated, this is not.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import ctypes
|
|
9
|
+
|
|
10
|
+
from dflowfm_io.base.library import lib
|
|
11
|
+
|
|
12
|
+
# --- enum mdu_severity_t ---
|
|
13
|
+
MDU_SEVERITY_DEBUG = 0
|
|
14
|
+
MDU_SEVERITY_INFO = 1
|
|
15
|
+
MDU_SEVERITY_WARNING = 2
|
|
16
|
+
MDU_SEVERITY_ERROR = 3
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# --- struct mdu_issue_t ---
|
|
20
|
+
class mdu_issue_t(ctypes.Structure):
|
|
21
|
+
_fields_ = [
|
|
22
|
+
("line_number", ctypes.c_int32),
|
|
23
|
+
("severity", ctypes.c_int32),
|
|
24
|
+
("message", ctypes.c_char_p),
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# --- function signatures ---
|
|
29
|
+
lib.dflowfm_io_get_last_error.argtypes = []
|
|
30
|
+
lib.dflowfm_io_get_last_error.restype = ctypes.c_char_p
|
|
31
|
+
lib.mdu_create.argtypes = [ctypes.POINTER(ctypes.c_void_p)]
|
|
32
|
+
lib.mdu_create.restype = ctypes.c_int32
|
|
33
|
+
lib.mdu_destroy.argtypes = [ctypes.POINTER(ctypes.c_void_p)]
|
|
34
|
+
lib.mdu_destroy.restype = ctypes.c_int32
|
|
35
|
+
lib.mdu_load_from_file.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
|
|
36
|
+
lib.mdu_load_from_file.restype = ctypes.c_int32
|
|
37
|
+
lib.mdu_load_from_string.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_uint64]
|
|
38
|
+
lib.mdu_load_from_string.restype = ctypes.c_int32
|
|
39
|
+
lib.mdu_save_to_file.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
|
|
40
|
+
lib.mdu_save_to_file.restype = ctypes.c_int32
|
|
41
|
+
lib.mdu_save_to_string.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_char_p)]
|
|
42
|
+
lib.mdu_save_to_string.restype = ctypes.c_int32
|
|
43
|
+
lib.mdu_get_int.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_int32)]
|
|
44
|
+
lib.mdu_get_int.restype = ctypes.c_int32
|
|
45
|
+
lib.mdu_get_bool.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_int32)]
|
|
46
|
+
lib.mdu_get_bool.restype = ctypes.c_int32
|
|
47
|
+
lib.mdu_get_double.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_double)]
|
|
48
|
+
lib.mdu_get_double.restype = ctypes.c_int32
|
|
49
|
+
lib.mdu_get_string.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p)]
|
|
50
|
+
lib.mdu_get_string.restype = ctypes.c_int32
|
|
51
|
+
lib.mdu_get_path.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p)]
|
|
52
|
+
lib.mdu_get_path.restype = ctypes.c_int32
|
|
53
|
+
lib.mdu_get_datetime.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_int64), ctypes.POINTER(ctypes.c_int32)]
|
|
54
|
+
lib.mdu_get_datetime.restype = ctypes.c_int32
|
|
55
|
+
lib.mdu_get_string_enum.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p)]
|
|
56
|
+
lib.mdu_get_string_enum.restype = ctypes.c_int32
|
|
57
|
+
lib.mdu_get_int_enum.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_int32)]
|
|
58
|
+
lib.mdu_get_int_enum.restype = ctypes.c_int32
|
|
59
|
+
lib.mdu_get_string_list.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.POINTER(ctypes.c_char_p)), ctypes.POINTER(ctypes.c_uint64)]
|
|
60
|
+
lib.mdu_get_string_list.restype = ctypes.c_int32
|
|
61
|
+
lib.mdu_get_path_list.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.POINTER(ctypes.c_char_p)), ctypes.POINTER(ctypes.c_uint64)]
|
|
62
|
+
lib.mdu_get_path_list.restype = ctypes.c_int32
|
|
63
|
+
lib.mdu_get_double_list.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.POINTER(ctypes.c_double)), ctypes.POINTER(ctypes.c_uint64)]
|
|
64
|
+
lib.mdu_get_double_list.restype = ctypes.c_int32
|
|
65
|
+
lib.mdu_set_int.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int32]
|
|
66
|
+
lib.mdu_set_int.restype = ctypes.c_int32
|
|
67
|
+
lib.mdu_set_bool.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int32]
|
|
68
|
+
lib.mdu_set_bool.restype = ctypes.c_int32
|
|
69
|
+
lib.mdu_set_double.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_double]
|
|
70
|
+
lib.mdu_set_double.restype = ctypes.c_int32
|
|
71
|
+
lib.mdu_set_string.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p]
|
|
72
|
+
lib.mdu_set_string.restype = ctypes.c_int32
|
|
73
|
+
lib.mdu_set_path.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p]
|
|
74
|
+
lib.mdu_set_path.restype = ctypes.c_int32
|
|
75
|
+
lib.mdu_set_datetime.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int64, ctypes.c_int32]
|
|
76
|
+
lib.mdu_set_datetime.restype = ctypes.c_int32
|
|
77
|
+
lib.mdu_set_string_enum.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p]
|
|
78
|
+
lib.mdu_set_string_enum.restype = ctypes.c_int32
|
|
79
|
+
lib.mdu_set_int_enum.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int32]
|
|
80
|
+
lib.mdu_set_int_enum.restype = ctypes.c_int32
|
|
81
|
+
lib.mdu_set_string_list.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p), ctypes.c_uint64]
|
|
82
|
+
lib.mdu_set_string_list.restype = ctypes.c_int32
|
|
83
|
+
lib.mdu_set_path_list.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p), ctypes.c_uint64]
|
|
84
|
+
lib.mdu_set_path_list.restype = ctypes.c_int32
|
|
85
|
+
lib.mdu_set_double_list.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_double), ctypes.c_uint64]
|
|
86
|
+
lib.mdu_set_double_list.restype = ctypes.c_int32
|
|
87
|
+
lib.mdu_get_issue_list.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.POINTER(mdu_issue_t)), ctypes.POINTER(ctypes.c_uint64)]
|
|
88
|
+
lib.mdu_get_issue_list.restype = ctypes.c_int32
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Translating dflowfm_io_api C return codes into Python exceptions."""
|
|
2
|
+
|
|
3
|
+
from dflowfm_io.base.library import lib
|
|
4
|
+
|
|
5
|
+
DFLOWFM_IO_RESULT_SUCCESS = 0
|
|
6
|
+
DFLOWFM_IO_RESULT_ERROR = 1
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def check_result(result):
|
|
10
|
+
"""Raise ``RuntimeError`` with the backend's last error message on a non-success result."""
|
|
11
|
+
if result != DFLOWFM_IO_RESULT_SUCCESS:
|
|
12
|
+
# restype for dflowfm_io_get_last_error is applied at import by base.bindings.
|
|
13
|
+
error_message = lib.dflowfm_io_get_last_error()
|
|
14
|
+
# The C side may return NULL when no message is set; don't mask the failure with an
|
|
15
|
+
# AttributeError from decoding None.
|
|
16
|
+
raise RuntimeError((error_message or b"unknown dflowfm_io error").decode("utf-8"))
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Opaque handle to a native MDU document."""
|
|
2
|
+
|
|
3
|
+
import ctypes
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class HandleRef:
|
|
7
|
+
"""Holds the native document handle and keeps a reference to its owner alive."""
|
|
8
|
+
|
|
9
|
+
def __init__(self, handle: ctypes.c_void_p, owner: object):
|
|
10
|
+
self.handle = handle
|
|
11
|
+
self._owner = owner
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Locating and loading the native dflowfm_io_api library.
|
|
2
|
+
|
|
3
|
+
Importing this module loads the shared library once and exposes it as :data:`lib` — a single
|
|
4
|
+
process-wide :class:`Lib` handle that every other module (``bindings``, ``errors``, ``mdu``)
|
|
5
|
+
shares. See :class:`LibLoader` for where it is loaded from.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import ctypes
|
|
9
|
+
import os
|
|
10
|
+
import platform
|
|
11
|
+
|
|
12
|
+
from dflowfm_io._lib.library_path import DFLOWFM_LIBRARY_PATH
|
|
13
|
+
|
|
14
|
+
class LibLoader:
|
|
15
|
+
def __init__(self):
|
|
16
|
+
self._loaded: ctypes.CDLL | None = None
|
|
17
|
+
|
|
18
|
+
def load(self) -> ctypes.CDLL:
|
|
19
|
+
"""Load the native library (but only once!) and return the shared :class:`ctypes.CDLL` handle."""
|
|
20
|
+
if self._loaded is None:
|
|
21
|
+
# Make sibling runtime DLLs next to the library discoverable (Windows).
|
|
22
|
+
if platform.system() == "Windows" and hasattr(os, "add_dll_directory"):
|
|
23
|
+
os.add_dll_directory(str(DFLOWFM_LIBRARY_PATH.parent))
|
|
24
|
+
self._loaded = ctypes.CDLL(str(DFLOWFM_LIBRARY_PATH))
|
|
25
|
+
return self._loaded
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class Lib:
|
|
29
|
+
"""The loaded native dflowfm_io_api library.
|
|
30
|
+
|
|
31
|
+
A thin wrapper over the underlying :class:`ctypes.CDLL`: attribute access forwards to it, so
|
|
32
|
+
``lib.mdu_get_int(...)`` calls the C function and ``lib.mdu_get_int.argtypes = [...]`` configures
|
|
33
|
+
it — exactly as on a raw ``CDLL``. Instances *are* the library, hence ``lib = Lib()``.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(self, loader: LibLoader | None = None):
|
|
37
|
+
self.loader = loader or LibLoader()
|
|
38
|
+
self._cdll = self.loader.load()
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def cdll(self) -> ctypes.CDLL:
|
|
42
|
+
"""The underlying ctypes handle."""
|
|
43
|
+
return self._cdll
|
|
44
|
+
|
|
45
|
+
def __getattr__(self, name: str):
|
|
46
|
+
# Reached only for names not found on the wrapper itself (the C symbols); forward to the CDLL.
|
|
47
|
+
# Guard `_cdll` to avoid infinite recursion before it is assigned in __init__.
|
|
48
|
+
if name == "_cdll":
|
|
49
|
+
raise AttributeError(name)
|
|
50
|
+
return getattr(self._cdll, name)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
lib = Lib()
|
dflowfm_io/issues.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Validation issue value types reported by the MDU document."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from enum import IntEnum
|
|
5
|
+
|
|
6
|
+
from dflowfm_io.base import bindings
|
|
7
|
+
|
|
8
|
+
# The ctypes wire form of an issue is the generated struct, so it is the exact type the generated
|
|
9
|
+
# argtypes expect (re-exported here under the wrapper's naming).
|
|
10
|
+
MduIssue = bindings.mdu_issue_t
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Severity(IntEnum):
|
|
14
|
+
"""Severity of a validation issue."""
|
|
15
|
+
|
|
16
|
+
DEBUG = bindings.MDU_SEVERITY_DEBUG
|
|
17
|
+
INFO = bindings.MDU_SEVERITY_INFO
|
|
18
|
+
WARNING = bindings.MDU_SEVERITY_WARNING
|
|
19
|
+
ERROR = bindings.MDU_SEVERITY_ERROR
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class Issue:
|
|
24
|
+
"""A single validation finding: a message with a severity and (optional) source line."""
|
|
25
|
+
|
|
26
|
+
line_number: int
|
|
27
|
+
severity: Severity
|
|
28
|
+
message: str
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""The MDU document API: the typed model, the validation report, and the document itself.
|
|
2
|
+
|
|
3
|
+
:class:`MduModel` is generated from the C header (see :mod:`dflowfm_io.mdu.model`); the document
|
|
4
|
+
lifecycle and report in :mod:`dflowfm_io.mdu.document` are hand-written.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from dflowfm_io.mdu.document import MduDocument, MduReport
|
|
8
|
+
from dflowfm_io.mdu.model import MduModel
|
|
9
|
+
from dflowfm_io.mdu.schema import MduSchema
|
|
10
|
+
|
|
11
|
+
__all__ = ["MduModel", "MduReport", "MduDocument", "MduSchema"]
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""The MDU document (:class:`MduDocument`) and its validation issues (:class:`MduReport`).
|
|
2
|
+
|
|
3
|
+
The typed property access (:class:`MduModel`) is generated from the C header (see
|
|
4
|
+
:mod:`dflowfm_io.mdu.model`) and re-exported here. The document lifecycle and the report stay
|
|
5
|
+
hand-written: they encode Python design (handle ownership, ``__del__``, convenience methods) with
|
|
6
|
+
no 1:1 C-function counterpart.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import ctypes
|
|
10
|
+
|
|
11
|
+
from dflowfm_io.base import HandleRef, check_result, lib
|
|
12
|
+
from dflowfm_io.issues import Issue, MduIssue, Severity
|
|
13
|
+
from dflowfm_io.mdu.model import MduModel
|
|
14
|
+
from dflowfm_io.mdu.schema import MduSchema
|
|
15
|
+
|
|
16
|
+
__all__ = ["MduModel", "MduReport", "MduDocument", "MduSchema"]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class MduReport:
|
|
20
|
+
"""The validation issues (info/warning/error) collected while loading an MDU document."""
|
|
21
|
+
|
|
22
|
+
def __init__(self, ref: HandleRef):
|
|
23
|
+
self._ref = ref
|
|
24
|
+
|
|
25
|
+
def get_issues(self) -> list[Issue]:
|
|
26
|
+
array_out = ctypes.POINTER(MduIssue)()
|
|
27
|
+
size_out = ctypes.c_uint64()
|
|
28
|
+
check_result(lib.mdu_get_issue_list(self._ref.handle, ctypes.byref(array_out), ctypes.byref(size_out)))
|
|
29
|
+
issues = []
|
|
30
|
+
for i in range(size_out.value):
|
|
31
|
+
raw = array_out[i]
|
|
32
|
+
message = raw.message.decode("utf-8") if raw.message else ""
|
|
33
|
+
issues.append(Issue(raw.line_number, Severity(raw.severity), message))
|
|
34
|
+
return issues
|
|
35
|
+
|
|
36
|
+
def has_errors(self) -> bool:
|
|
37
|
+
return any(issue.severity == Severity.ERROR for issue in self.get_issues())
|
|
38
|
+
|
|
39
|
+
def print_overview(self) -> None:
|
|
40
|
+
for issue in self.get_issues():
|
|
41
|
+
location = f"line {issue.line_number}" if issue.line_number >= 0 else "no line"
|
|
42
|
+
print(f"[{issue.severity.name}] ({location}) {issue.message}")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class MduDocument:
|
|
46
|
+
"""An MDU document — create/load/save, with typed access via :attr:`model` and :attr:`report`."""
|
|
47
|
+
|
|
48
|
+
def __init__(self):
|
|
49
|
+
handle = ctypes.c_void_p()
|
|
50
|
+
check_result(lib.mdu_create(ctypes.byref(handle)))
|
|
51
|
+
self._ref = HandleRef(handle, self)
|
|
52
|
+
self._model = MduModel(self._ref)
|
|
53
|
+
self._report = MduReport(self._ref)
|
|
54
|
+
self._schema = MduSchema(self._model)
|
|
55
|
+
|
|
56
|
+
def __del__(self):
|
|
57
|
+
if hasattr(self, "_ref") and self._ref.handle:
|
|
58
|
+
try:
|
|
59
|
+
lib.mdu_destroy(ctypes.byref(self._ref.handle))
|
|
60
|
+
except Exception:
|
|
61
|
+
# At interpreter shutdown the module globals (lib, ctypes) may already be torn
|
|
62
|
+
# down to None; swallow so the finalizer doesn't raise noisily.
|
|
63
|
+
pass
|
|
64
|
+
self._ref.handle = None
|
|
65
|
+
|
|
66
|
+
@property
|
|
67
|
+
def model(self) -> MduModel:
|
|
68
|
+
return self._model
|
|
69
|
+
|
|
70
|
+
@property
|
|
71
|
+
def report(self) -> MduReport:
|
|
72
|
+
return self._report
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def schema(self) -> MduSchema:
|
|
76
|
+
"""Typed, per-keyword access to the MDU sections (Layer 2), e.g. ``doc.schema.geometry.netFile``."""
|
|
77
|
+
return self._schema
|
|
78
|
+
|
|
79
|
+
def load_from_file(self, filename: str) -> None:
|
|
80
|
+
check_result(lib.mdu_load_from_file(self._ref.handle, filename.encode("utf-8")))
|
|
81
|
+
|
|
82
|
+
def load_from_lines(self, data: list[str]) -> None:
|
|
83
|
+
encoded = "\n".join(data).encode("utf-8")
|
|
84
|
+
check_result(lib.mdu_load_from_string(self._ref.handle, encoded, ctypes.c_uint64(len(encoded))))
|
|
85
|
+
|
|
86
|
+
def save_to_file(self, filename: str) -> None:
|
|
87
|
+
check_result(lib.mdu_save_to_file(self._ref.handle, filename.encode("utf-8")))
|
|
88
|
+
|
|
89
|
+
def save_to_lines(self) -> list[str]:
|
|
90
|
+
string_out = ctypes.c_char_p()
|
|
91
|
+
check_result(lib.mdu_save_to_string(self._ref.handle, ctypes.byref(string_out)))
|
|
92
|
+
return string_out.value.decode("utf-8").splitlines()
|
dflowfm_io/mdu/model.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""The typed MduModel: one get/set method per mdu_get_*/mdu_set_* C function, with the per-type
|
|
2
|
+
value marshalling.
|
|
3
|
+
|
|
4
|
+
Hand-maintained (like base/bindings.py): the C accessor set is small and rarely changes. When the C
|
|
5
|
+
ABI gains or changes an accessor, edit this file to match dflowfm_io_api.h.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import ctypes
|
|
9
|
+
from datetime import datetime, timedelta, timezone
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
from dflowfm_io.base import HandleRef, check_result, lib
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class MduModel:
|
|
16
|
+
"""Typed get/set access to MDU properties, keyed by dotted `section.property` names."""
|
|
17
|
+
|
|
18
|
+
def __init__(self, ref: HandleRef):
|
|
19
|
+
self._ref = ref
|
|
20
|
+
|
|
21
|
+
def get_int(self, key: str) -> int:
|
|
22
|
+
value = ctypes.c_int32()
|
|
23
|
+
check_result(lib.mdu_get_int(self._ref.handle, key.encode("utf-8"), ctypes.byref(value)))
|
|
24
|
+
return value.value
|
|
25
|
+
|
|
26
|
+
def get_bool(self, key: str) -> bool:
|
|
27
|
+
value = ctypes.c_int32()
|
|
28
|
+
check_result(lib.mdu_get_bool(self._ref.handle, key.encode("utf-8"), ctypes.byref(value)))
|
|
29
|
+
return value.value != 0
|
|
30
|
+
|
|
31
|
+
def get_double(self, key: str) -> float:
|
|
32
|
+
value = ctypes.c_double()
|
|
33
|
+
check_result(lib.mdu_get_double(self._ref.handle, key.encode("utf-8"), ctypes.byref(value)))
|
|
34
|
+
return value.value
|
|
35
|
+
|
|
36
|
+
def get_string(self, key: str) -> str:
|
|
37
|
+
value = ctypes.c_char_p()
|
|
38
|
+
check_result(lib.mdu_get_string(self._ref.handle, key.encode("utf-8"), ctypes.byref(value)))
|
|
39
|
+
return value.value.decode("utf-8")
|
|
40
|
+
|
|
41
|
+
def get_path(self, key: str) -> Path:
|
|
42
|
+
value = ctypes.c_char_p()
|
|
43
|
+
check_result(lib.mdu_get_path(self._ref.handle, key.encode("utf-8"), ctypes.byref(value)))
|
|
44
|
+
return Path(value.value.decode("utf-8"))
|
|
45
|
+
|
|
46
|
+
def get_datetime(self, key: str) -> datetime | None:
|
|
47
|
+
value = ctypes.c_int64()
|
|
48
|
+
has_value = ctypes.c_int32()
|
|
49
|
+
check_result(lib.mdu_get_datetime(self._ref.handle, key.encode("utf-8"), ctypes.byref(value), ctypes.byref(has_value)))
|
|
50
|
+
if has_value.value == 0:
|
|
51
|
+
return None
|
|
52
|
+
return datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(seconds=value.value)
|
|
53
|
+
|
|
54
|
+
def get_string_enum(self, key: str) -> str:
|
|
55
|
+
value = ctypes.c_char_p()
|
|
56
|
+
check_result(lib.mdu_get_string_enum(self._ref.handle, key.encode("utf-8"), ctypes.byref(value)))
|
|
57
|
+
return value.value.decode("utf-8")
|
|
58
|
+
|
|
59
|
+
def get_int_enum(self, key: str) -> int:
|
|
60
|
+
value = ctypes.c_int32()
|
|
61
|
+
check_result(lib.mdu_get_int_enum(self._ref.handle, key.encode("utf-8"), ctypes.byref(value)))
|
|
62
|
+
return value.value
|
|
63
|
+
|
|
64
|
+
def get_string_list(self, key: str) -> list[str]:
|
|
65
|
+
array_out = ctypes.POINTER(ctypes.c_char_p)()
|
|
66
|
+
size_out = ctypes.c_uint64()
|
|
67
|
+
check_result(lib.mdu_get_string_list(self._ref.handle, key.encode("utf-8"), ctypes.byref(array_out), ctypes.byref(size_out)))
|
|
68
|
+
return [array_out[i].decode("utf-8") for i in range(size_out.value)]
|
|
69
|
+
|
|
70
|
+
def get_path_list(self, key: str) -> list[Path]:
|
|
71
|
+
array_out = ctypes.POINTER(ctypes.c_char_p)()
|
|
72
|
+
size_out = ctypes.c_uint64()
|
|
73
|
+
check_result(lib.mdu_get_path_list(self._ref.handle, key.encode("utf-8"), ctypes.byref(array_out), ctypes.byref(size_out)))
|
|
74
|
+
return [Path(array_out[i].decode("utf-8")) for i in range(size_out.value)]
|
|
75
|
+
|
|
76
|
+
def get_double_list(self, key: str) -> list[float]:
|
|
77
|
+
array_out = ctypes.POINTER(ctypes.c_double)()
|
|
78
|
+
size_out = ctypes.c_uint64()
|
|
79
|
+
check_result(lib.mdu_get_double_list(self._ref.handle, key.encode("utf-8"), ctypes.byref(array_out), ctypes.byref(size_out)))
|
|
80
|
+
return [array_out[i] for i in range(size_out.value)]
|
|
81
|
+
|
|
82
|
+
def set_int(self, key: str, value: int) -> None:
|
|
83
|
+
check_result(lib.mdu_set_int(self._ref.handle, key.encode("utf-8"), ctypes.c_int32(value)))
|
|
84
|
+
|
|
85
|
+
def set_bool(self, key: str, value: bool) -> None:
|
|
86
|
+
check_result(lib.mdu_set_bool(self._ref.handle, key.encode("utf-8"), ctypes.c_int32(1 if value else 0)))
|
|
87
|
+
|
|
88
|
+
def set_double(self, key: str, value: float) -> None:
|
|
89
|
+
check_result(lib.mdu_set_double(self._ref.handle, key.encode("utf-8"), ctypes.c_double(value)))
|
|
90
|
+
|
|
91
|
+
def set_string(self, key: str, value: str) -> None:
|
|
92
|
+
check_result(lib.mdu_set_string(self._ref.handle, key.encode("utf-8"), value.encode("utf-8")))
|
|
93
|
+
|
|
94
|
+
def set_path(self, key: str, value: Path | str) -> None:
|
|
95
|
+
check_result(lib.mdu_set_path(self._ref.handle, key.encode("utf-8"), str(value).encode("utf-8")))
|
|
96
|
+
|
|
97
|
+
def set_datetime(self, key: str, value: datetime | None) -> None:
|
|
98
|
+
if value is None:
|
|
99
|
+
check_result(lib.mdu_set_datetime(self._ref.handle, key.encode("utf-8"), ctypes.c_int64(0), ctypes.c_int32(0)))
|
|
100
|
+
else:
|
|
101
|
+
epoch = int((value if value.tzinfo else value.replace(tzinfo=timezone.utc)).timestamp())
|
|
102
|
+
check_result(lib.mdu_set_datetime(self._ref.handle, key.encode("utf-8"), ctypes.c_int64(epoch), ctypes.c_int32(1)))
|
|
103
|
+
|
|
104
|
+
def set_string_enum(self, key: str, value: str) -> None:
|
|
105
|
+
check_result(lib.mdu_set_string_enum(self._ref.handle, key.encode("utf-8"), value.encode("utf-8")))
|
|
106
|
+
|
|
107
|
+
def set_int_enum(self, key: str, value: int) -> None:
|
|
108
|
+
check_result(lib.mdu_set_int_enum(self._ref.handle, key.encode("utf-8"), ctypes.c_int32(value)))
|
|
109
|
+
|
|
110
|
+
def set_string_list(self, key: str, values: list[str]) -> None:
|
|
111
|
+
encoded = [v.encode("utf-8") for v in values]
|
|
112
|
+
arr = (ctypes.c_char_p * len(encoded))(*encoded)
|
|
113
|
+
check_result(lib.mdu_set_string_list(self._ref.handle, key.encode("utf-8"), arr, ctypes.c_uint64(len(encoded))))
|
|
114
|
+
|
|
115
|
+
def set_path_list(self, key: str, values: list[Path | str]) -> None:
|
|
116
|
+
encoded = [str(v).encode("utf-8") for v in values]
|
|
117
|
+
arr = (ctypes.c_char_p * len(encoded))(*encoded)
|
|
118
|
+
check_result(lib.mdu_set_path_list(self._ref.handle, key.encode("utf-8"), arr, ctypes.c_uint64(len(encoded))))
|
|
119
|
+
|
|
120
|
+
def set_double_list(self, key: str, values: list[float]) -> None:
|
|
121
|
+
encoded = list(values)
|
|
122
|
+
arr = (ctypes.c_double * len(encoded))(*encoded)
|
|
123
|
+
check_result(lib.mdu_set_double_list(self._ref.handle, key.encode("utf-8"), arr, ctypes.c_uint64(len(encoded))))
|