Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
d47b87ac SM |
2 | # |
3 | # Copyright (c) 2018 Francis Deslauriers <francis.deslauriers@efficios.com> | |
d47b87ac SM |
4 | |
5 | import collections | |
6 | from bt2 import native_bt, object | |
7 | ||
8 | ||
e7ceb9df PP |
9 | class FieldPathScope: |
10 | PACKET_CONTEXT = native_bt.FIELD_PATH_SCOPE_PACKET_CONTEXT | |
11 | EVENT_COMMON_CONTEXT = native_bt.FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT | |
12 | EVENT_SPECIFIC_CONTEXT = native_bt.FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT | |
13 | EVENT_PAYLOAD = native_bt.FIELD_PATH_SCOPE_EVENT_PAYLOAD | |
d47b87ac SM |
14 | |
15 | ||
16 | class _FieldPathItem: | |
17 | pass | |
18 | ||
19 | ||
20 | class _IndexFieldPathItem(_FieldPathItem): | |
21 | def __init__(self, index): | |
22 | self._index = index | |
23 | ||
24 | @property | |
25 | def index(self): | |
26 | return self._index | |
27 | ||
28 | ||
29 | class _CurrentArrayElementFieldPathItem(_FieldPathItem): | |
30 | pass | |
31 | ||
32 | ||
cec0261d PP |
33 | class _CurrentOptionContentFieldPathItem(_FieldPathItem): |
34 | pass | |
35 | ||
36 | ||
5679964c | 37 | class _FieldPathConst(object._SharedObject, collections.abc.Iterable): |
d47b87ac SM |
38 | _get_ref = staticmethod(native_bt.field_path_get_ref) |
39 | _put_ref = staticmethod(native_bt.field_path_put_ref) | |
40 | ||
41 | @property | |
42 | def root_scope(self): | |
43 | scope = native_bt.field_path_get_root_scope(self._ptr) | |
44 | return _SCOPE_TO_OBJ[scope] | |
45 | ||
46 | def __len__(self): | |
47 | return native_bt.field_path_get_item_count(self._ptr) | |
48 | ||
49 | def __iter__(self): | |
50 | for idx in range(len(self)): | |
51 | item_ptr = native_bt.field_path_borrow_item_by_index_const(self._ptr, idx) | |
52 | assert item_ptr is not None | |
53 | item_type = native_bt.field_path_item_get_type(item_ptr) | |
54 | if item_type == native_bt.FIELD_PATH_ITEM_TYPE_INDEX: | |
55 | idx = native_bt.field_path_item_index_get_index(item_ptr) | |
56 | yield _IndexFieldPathItem(idx) | |
57 | elif item_type == native_bt.FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT: | |
cfbd7cf3 | 58 | yield _CurrentArrayElementFieldPathItem() |
cec0261d PP |
59 | elif item_type == native_bt.FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT: |
60 | yield _CurrentOptionContentFieldPathItem() | |
d47b87ac SM |
61 | else: |
62 | assert False | |
63 | ||
64 | ||
65 | _SCOPE_TO_OBJ = { | |
e7ceb9df PP |
66 | native_bt.FIELD_PATH_SCOPE_PACKET_CONTEXT: FieldPathScope.PACKET_CONTEXT, |
67 | native_bt.FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT: FieldPathScope.EVENT_COMMON_CONTEXT, | |
68 | native_bt.FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT: FieldPathScope.EVENT_SPECIFIC_CONTEXT, | |
69 | native_bt.FIELD_PATH_SCOPE_EVENT_PAYLOAD: FieldPathScope.EVENT_PAYLOAD, | |
d47b87ac | 70 | } |