{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbinding.py
More file actions
518 lines (418 loc) · 15.5 KB
/
Copy pathbinding.py
File metadata and controls
518 lines (418 loc) · 15.5 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
"""
Author: Kevin Vu te Laar <vu.te@rwth-aachen.de>
SPDX-FileCopyrightText: 2014-2025 Institute for Automation of Complex Power Systems, RWTH Aachen University
SPDX-License-Identifier: Apache-2.0
""" # noqa: E501
from typing import Any, Optional, Union
import functools
import json
import logging
import weakref
import villas.node.python_binding as vn
Capsule = Any
logger = logging.getLogger("villas.node")
# helper functions
# function decorator for optional node_compat function calls
# that would return -1 if a function is not implemented
def _warn_if_not_implemented(func):
"""
Decorator to warn if specific `node_*()` functions are not implemented.
Returns:
Wrapping function that logs a warning if the return value is -1.
"""
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
ret = func(self, *args, **kwargs)
if ret == -1 and hasattr(self, "_hndle") and self._hndle is not None:
msg = (
f"[\033[93mWarning\033[0m]: Function '{func.__name__}()' "
+ "is not implemented for node type "
+ f"'{vn.node_name(self._hndle)}'."
)
logger.warning(msg)
return ret
return wrapper
# node API bindings
class Node:
class _SampleSlice:
"""
Helper class to achieve overloaded functions.
Nodes accessed via the `[]` operator, it always returns a _SampleSlice.
"""
def __init__(self, node, idx):
self.node = weakref.proxy(node)
self.idx = idx
def details(self):
return self.node.sample_details(self.idx)
def read_from(self, sample_length, count=None):
return self.node.read_from(sample_length, count, self.idx)
def write_to(self, node, count=None):
return self.node.write_to(node, count, self.idx)
def pack_from(
self,
values: Union[float, list[float], Capsule],
ts_origin: Optional[int] = None,
ts_received: Optional[int] = None,
seq: int = 0,
):
if isinstance(values, self.__class__):
return self.node.pack_from(
self.idx,
values.node._smps[values.idx],
ts_origin,
ts_received,
seq,
)
else:
return self.node.pack_from(self.idx, values, ts_origin, ts_received, seq)
def unpack_to(
self,
target: Capsule,
):
if isinstance(target, self.__class__):
return self.node.unpack_to(
self.idx,
target.node,
target.idx,
)
else:
raise ValueError(
"The destination must be an existing Node with an index!"
)
# helper functions
@staticmethod
def _ensure_capacity(smps, cap: int):
"""
Resize SamplesArray if its capacity is less than the desired capacity.
Args:
smps: SamplesArray stored in the Node
cap (int): Desired capacity of the SamplesArray
"""
smp_cap = len(smps)
if smp_cap < cap:
smps.grow(cap - smp_cap)
@staticmethod
def _resolve_range(
start: Optional[int], stop: Optional[int], count: Optional[int]
) -> tuple[int, int, int]:
"""
Resolve a range dependent on start, stop and count.
At least two must be provided.
Args:
start (int): Desired start index
stop (int): Desired stop index
count (int): Desired span of the range
Returns:
tuple(start, stop, count)
"""
provided = sum(i is not None for i in (start, stop, count))
if provided == 1:
raise ValueError("Two of start, stop, count must be provided")
elif provided == 2:
if start is None:
start = stop - count
if start < 0:
raise ValueError("Negative start index")
elif stop is None:
stop = start + count
else:
count = stop - start
return start, stop, count
def __init__(self, config, uuid: Optional[str] = None, size=0):
"""
Initiallize a new node from config.
Notes:
- Capsule is available via self._hndle.
- Capsule is available via self.config.
"""
self.config = config
self._smps = vn.smps_array(size)
if uuid is None:
self._hndle = vn.node_new(config, "")
else:
self._hndle = vn.node_new(config, uuid)
def __del__(self):
"""
Stop and delete a node if the class object is deleted.
"""
vn.node_stop(self._hndle)
vn.node_destroy(self._hndle)
def __getitem__(self, idx: Union[int, slice]):
"""Return tuple containing self and index/slice for node operations."""
if isinstance(idx, (int, slice)):
return Node._SampleSlice(self, idx)
else:
logger.warning("Improper array index")
raise ValueError("Improper Index")
def __setitem__(self, obj):
if isinstance(obj, Node):
self.__del__()
self.config = obj.config
self._smps = obj._smps
else:
raise RuntimeError(f"{obj} is not of type `Node`")
def __len__(self):
return len(self._smps)
def __copy__(self):
"""Disallow shallow copying."""
raise RuntimeError("Copying Node is not allowed")
def __deepcopy__(self):
"""Disallow deep copying"""
raise RuntimeError("Copying a Node is not allowed")
# bindings
@staticmethod
def memory_init(hugepages: int):
"""
Initialize internal VILLASnode memory system.
Args:
hugepages (int): Amount of hugepages to be used.
Notes:
- Should be called once before any memory allocation is done.
- Falls back to mmap if hugepages or root privilege unavailable.
"""
return vn.memory_init(hugepages)
def check(self):
"""Check node."""
return vn.node_check(self._hndle)
def details(self):
"""Get node details."""
return vn.node_details(self._hndle)
def input_signals_max_cnt(self):
"""Get max input signal count."""
return vn.node_input_signals_max_cnt(self._hndle)
def is_enabled(self):
"""Check whether or not node is enabled."""
return vn.node_is_enabled(self._hndle)
@staticmethod
def is_valid_name(name: str):
"""Check if a name can be used for a node."""
return vn.node_is_valid_name(name)
def name(self):
"""Get node name."""
return vn.node_name(self._hndle)
def name_full(self):
"""Get node name with full details."""
return vn.node_name_full(self._hndle)
def name_short(self):
"""Get node name with less details."""
return vn.node_name_short(self._hndle)
def output_signals_max_cnt(self):
"""Get max output signal count."""
return vn.node_output_signals_max_cnt(self._hndle)
def pause(self):
"""Pause a node"""
return vn.node_pause(self._hndle)
def prepare(self):
"""Prepare a node"""
return vn.node_prepare(self._hndle)
@_warn_if_not_implemented
def read_from(
self,
sample_length: int,
cnt: Optional[int] = None,
idx=None,
):
"""
Read samples from a node into SamplesArray or block slice of samples.
Args:
sample_length (int): Length of each sample (number of signals).
cnt (int): Number of samples to read.
Returns:
int: Number of samples read on success or -1.
Notes:
- Return value may vary depending on node type.
- This function may be blocking.
"""
if idx is None:
if cnt is None:
raise ValueError("Count is None")
# resize _smps if too small
Node._ensure_capacity(self._smps, cnt)
# allocate new samples
self._smps.bulk_alloc(0, len(self._smps), sample_length)
return vn.node_read(self._hndle, self._smps.get_block(0), cnt)
if isinstance(idx, int):
if cnt is None:
raise ValueError("Count is None")
start = idx
stop = start + cnt
# if too small, resize _smps to match stop index
Node._ensure_capacity(self._smps, stop)
# allocate new samples
self._smps.bulk_alloc(start, stop, sample_length)
# read onward from index start
return vn.node_read(self._hndle, self._smps.get_block(start), cnt)
elif isinstance(idx, slice):
start, stop, cnt = Node._resolve_range(idx.start, idx.stop, cnt)
# check for length mismatch
if (stop - start) != cnt:
raise ValueError("Slice length and sample count do not match!")
# if too small, resize _smps to match stop index
Node._ensure_capacity(self._smps, stop)
# allocate new samples
self._smps.bulk_alloc(start, stop, sample_length)
# read onward from index start
return vn.node_read(self._hndle, self._smps.get_block(start), cnt)
else:
logger.warning("Invalid samples Parameter")
return -1
def restart(self):
"""Restart a node."""
return vn.node_restart(self._hndle)
def resume(self):
"""Resume a node."""
return vn.node_resume(self._hndle)
@_warn_if_not_implemented
def reverse(self):
"""
Reverse node input and output.
Notes:
- Hooks are not reversed.
- Some nodes should be stopped or restarted before reversing.
- Nodes with in-/output buffers should be stopped before reversing.
"""
return vn.node_reverse(self._hndle)
def start(self):
"""
Start a node.
Notes:
- Nodes are not meant to be started again without stopping first.
"""
return vn.node_start(self._hndle)
def stop(self):
"""
Stop a node.
Notes:
- Use before starting a node again.
- May delete in-/output buffers of a node.
"""
return vn.node_stop(self._hndle)
def to_json(self):
"""
Return the node configuration as json object.
Notes:
- Node configuration may not match self made configurations.
- Node configuration does not contain node name.
"""
json_str = vn.node_to_json_str(self._hndle)
json_obj = json.loads(json_str)
return json_obj
def to_json_str(self):
"""
Returns the node configuration as string.
Notes:
- Node configuration may not match self made configurations.
- Node configuration does not contain node name.
"""
return vn.node_to_json_str(self._hndle)
@_warn_if_not_implemented
def write_to(self, node, cnt: Optional[int] = None, idx=None):
"""
Write samples from a SamplesArray fully or as block slice into a node.
Args:
node: Node handle.
cnt (int): Number of samples to write.
Returns:
int: Number of samples written on success or -1.
Notes:
- Return value may vary depending on node type.
"""
if idx is None:
if cnt is None:
raise ValueError("Count is None")
return vn.node_write(self._hndle, node._smps.get_block(0), cnt)
if isinstance(idx, int):
if cnt is None:
raise ValueError("Count is None")
start = idx
stop = start + cnt
return vn.node_write(self._hndle, node._smps.get_block(start), cnt)
if isinstance(idx, slice):
start, stop, _ = idx.indices(len(self._smps))
if cnt is None:
cnt = stop - start
print(start, stop, cnt, len(self._smps))
# check for length mismatch
if (stop - start) != cnt:
raise ValueError("Slice length and sample count do not match.")
# check for out of bounds
if stop > len(self._smps):
raise IndexError("Out of bounds")
return vn.node_write(self._hndle, node._smps.get_block(start), cnt)
logger.warning("Invalid samples Parameter")
return -1
def sample_length(self, idx: int):
"""Get the length of a sample."""
if 0 <= idx and idx < len(self._smps):
return vn.sample_length(self._smps[idx])
else:
raise IndexError(f"No Sample at index: {idx}")
def pack_from(
self,
idx: int,
values: Union[float, list[float], Capsule],
ts_orig: Optional[int] = None,
ts_recv: Optional[int] = None,
seq: int = 0,
):
"""
Packs a given sample from a source sample or value list.
Args:
idx (int): Node index to store packed sample in.
ts_orig (Optional[int]): Supposed creation time in ns.
ts_recv (Optional[int]): Supposed arrival time in ns.
values (Union[float, list[float], sample]):
- Packed sample will only hold referenced values.
seq (int): supposed sequence number of the sample.
"""
if seq < 0:
raise ValueError("seq has to be positive")
Node._ensure_capacity(self._smps, idx + 1)
if len(self._smps) <= idx:
self._smps.grow(idx + 1 - len(self._smps))
if isinstance(values, (float, int)):
self._smps[idx] = vn.sample_pack(
[values],
ts_orig,
ts_recv,
seq,
)
elif isinstance(values, list):
self._smps[idx] = vn.sample_alloc(len(values))
self._smps[idx] = vn.sample_pack(values, ts_orig, ts_recv, seq)
else: # assume a PyCapsule
self._smps[idx] = vn.sample_pack(values, ts_orig, ts_recv)
def unpack_to(
self,
r_idx: int,
target_node,
w_idx: int,
):
"""
Unpacks a given sample to a destined target.
Args:
r_idx (int): Originating Node index to read from.
target_node (Node): Target node.
w_idx (int): Target Node index to unpack to.
"""
Node._ensure_capacity(self._smps, r_idx + 1)
Node._ensure_capacity(target_node._smps, w_idx + 1)
vn.sample_unpack(
self._smps[r_idx],
target_node._smps.get_block(w_idx),
)
def sample_details(self, idx):
"""
Retrieve a dict with information about a sample.
Keys:
`sequence` (int): Sequence number of the sample.
`length` (int): Sample length.
`capacity` (int): Allocated sample length.
`flags` (int): Number representing flags set of the sample.
`refcnt` (int): Reference count of the given sample.
`ts_origin` (float): Supposed timestamp of creation.
`ts_received` (float): Supposed timestamp of arrival.
Returns:
Dict with listed keys and values.
"""
return vn.sample_details(self._smps[idx])
You can’t perform that action at this time.
