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 | |
5995b304 | 6 | |
e5914347 | 7 | from bt2 import object as bt2_object |
5995b304 | 8 | from bt2 import native_bt |
d47b87ac SM |
9 | |
10 | ||
e7ceb9df PP |
11 | class FieldPathScope: |
12 | PACKET_CONTEXT = native_bt.FIELD_PATH_SCOPE_PACKET_CONTEXT | |
13 | EVENT_COMMON_CONTEXT = native_bt.FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT | |
14 | EVENT_SPECIFIC_CONTEXT = native_bt.FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT | |
15 | EVENT_PAYLOAD = native_bt.FIELD_PATH_SCOPE_EVENT_PAYLOAD | |
d47b87ac SM |
16 | |
17 | ||
18 | class _FieldPathItem: | |
19 | pass | |
20 | ||
21 | ||
22 | class _IndexFieldPathItem(_FieldPathItem): | |
23 | def __init__(self, index): | |
24 | self._index = index | |
25 | ||
26 | @property | |
27 | def index(self): | |
28 | return self._index | |
29 | ||
30 | ||
31 | class _CurrentArrayElementFieldPathItem(_FieldPathItem): | |
32 | pass | |
33 | ||
34 | ||
cec0261d PP |
35 | class _CurrentOptionContentFieldPathItem(_FieldPathItem): |
36 | pass | |
37 | ||
38 | ||
e5914347 | 39 | class _FieldPathConst(bt2_object._SharedObject, collections.abc.Iterable): |
9dee90bd SM |
40 | @staticmethod |
41 | def _get_ref(ptr): | |
42 | native_bt.field_path_get_ref(ptr) | |
43 | ||
44 | @staticmethod | |
45 | def _put_ref(ptr): | |
46 | native_bt.field_path_put_ref(ptr) | |
d47b87ac SM |
47 | |
48 | @property | |
49 | def root_scope(self): | |
50 | scope = native_bt.field_path_get_root_scope(self._ptr) | |
51 | return _SCOPE_TO_OBJ[scope] | |
52 | ||
53 | def __len__(self): | |
54 | return native_bt.field_path_get_item_count(self._ptr) | |
55 | ||
56 | def __iter__(self): | |
57 | for idx in range(len(self)): | |
58 | item_ptr = native_bt.field_path_borrow_item_by_index_const(self._ptr, idx) | |
59 | assert item_ptr is not None | |
60 | item_type = native_bt.field_path_item_get_type(item_ptr) | |
61 | if item_type == native_bt.FIELD_PATH_ITEM_TYPE_INDEX: | |
62 | idx = native_bt.field_path_item_index_get_index(item_ptr) | |
63 | yield _IndexFieldPathItem(idx) | |
64 | elif item_type == native_bt.FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT: | |
cfbd7cf3 | 65 | yield _CurrentArrayElementFieldPathItem() |
cec0261d PP |
66 | elif item_type == native_bt.FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT: |
67 | yield _CurrentOptionContentFieldPathItem() | |
d47b87ac SM |
68 | else: |
69 | assert False | |
70 | ||
71 | ||
72 | _SCOPE_TO_OBJ = { | |
e7ceb9df PP |
73 | native_bt.FIELD_PATH_SCOPE_PACKET_CONTEXT: FieldPathScope.PACKET_CONTEXT, |
74 | native_bt.FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT: FieldPathScope.EVENT_COMMON_CONTEXT, | |
75 | native_bt.FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT: FieldPathScope.EVENT_SPECIFIC_CONTEXT, | |
76 | native_bt.FIELD_PATH_SCOPE_EVENT_PAYLOAD: FieldPathScope.EVENT_PAYLOAD, | |
d47b87ac | 77 | } |