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): |
9dee90bd SM |
38 | @staticmethod |
39 | def _get_ref(ptr): | |
40 | native_bt.field_path_get_ref(ptr) | |
41 | ||
42 | @staticmethod | |
43 | def _put_ref(ptr): | |
44 | native_bt.field_path_put_ref(ptr) | |
d47b87ac SM |
45 | |
46 | @property | |
47 | def root_scope(self): | |
48 | scope = native_bt.field_path_get_root_scope(self._ptr) | |
49 | return _SCOPE_TO_OBJ[scope] | |
50 | ||
51 | def __len__(self): | |
52 | return native_bt.field_path_get_item_count(self._ptr) | |
53 | ||
54 | def __iter__(self): | |
55 | for idx in range(len(self)): | |
56 | item_ptr = native_bt.field_path_borrow_item_by_index_const(self._ptr, idx) | |
57 | assert item_ptr is not None | |
58 | item_type = native_bt.field_path_item_get_type(item_ptr) | |
59 | if item_type == native_bt.FIELD_PATH_ITEM_TYPE_INDEX: | |
60 | idx = native_bt.field_path_item_index_get_index(item_ptr) | |
61 | yield _IndexFieldPathItem(idx) | |
62 | elif item_type == native_bt.FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT: | |
cfbd7cf3 | 63 | yield _CurrentArrayElementFieldPathItem() |
cec0261d PP |
64 | elif item_type == native_bt.FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT: |
65 | yield _CurrentOptionContentFieldPathItem() | |
d47b87ac SM |
66 | else: |
67 | assert False | |
68 | ||
69 | ||
70 | _SCOPE_TO_OBJ = { | |
e7ceb9df PP |
71 | native_bt.FIELD_PATH_SCOPE_PACKET_CONTEXT: FieldPathScope.PACKET_CONTEXT, |
72 | native_bt.FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT: FieldPathScope.EVENT_COMMON_CONTEXT, | |
73 | native_bt.FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT: FieldPathScope.EVENT_SPECIFIC_CONTEXT, | |
74 | native_bt.FIELD_PATH_SCOPE_EVENT_PAYLOAD: FieldPathScope.EVENT_PAYLOAD, | |
d47b87ac | 75 | } |