Move to kernel style SPDX license identifiers
[babeltrace.git] / src / bindings / python / bt2 / bt2 / field_path.py
1 # SPDX-License-Identifier: MIT
2 #
3 # Copyright (c) 2018 Francis Deslauriers <francis.deslauriers@efficios.com>
4
5 import collections
6 from bt2 import native_bt, object
7
8
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
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
33 class _CurrentOptionContentFieldPathItem(_FieldPathItem):
34 pass
35
36
37 class _FieldPathConst(object._SharedObject, collections.abc.Iterable):
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:
58 yield _CurrentArrayElementFieldPathItem()
59 elif item_type == native_bt.FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT:
60 yield _CurrentOptionContentFieldPathItem()
61 else:
62 assert False
63
64
65 _SCOPE_TO_OBJ = {
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,
70 }
This page took 0.029851 seconds and 4 git commands to generate.