Tests: bt2: move `_create_const_field()` to utils.py
[babeltrace.git] / src / bindings / python / bt2 / bt2 / field_path.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2018 Francis Deslauriers <francis.deslauriers@efficios.com>
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
22
23 import collections
24 from bt2 import native_bt, object
25
26
27 class FieldPathScope:
28 PACKET_CONTEXT = native_bt.FIELD_PATH_SCOPE_PACKET_CONTEXT
29 EVENT_COMMON_CONTEXT = native_bt.FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
30 EVENT_SPECIFIC_CONTEXT = native_bt.FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
31 EVENT_PAYLOAD = native_bt.FIELD_PATH_SCOPE_EVENT_PAYLOAD
32
33
34 class _FieldPathItem:
35 pass
36
37
38 class _IndexFieldPathItem(_FieldPathItem):
39 def __init__(self, index):
40 self._index = index
41
42 @property
43 def index(self):
44 return self._index
45
46
47 class _CurrentArrayElementFieldPathItem(_FieldPathItem):
48 pass
49
50
51 class _CurrentOptionContentFieldPathItem(_FieldPathItem):
52 pass
53
54
55 class _FieldPathConst(object._SharedObject, collections.abc.Iterable):
56 _get_ref = staticmethod(native_bt.field_path_get_ref)
57 _put_ref = staticmethod(native_bt.field_path_put_ref)
58
59 @property
60 def root_scope(self):
61 scope = native_bt.field_path_get_root_scope(self._ptr)
62 return _SCOPE_TO_OBJ[scope]
63
64 def __len__(self):
65 return native_bt.field_path_get_item_count(self._ptr)
66
67 def __iter__(self):
68 for idx in range(len(self)):
69 item_ptr = native_bt.field_path_borrow_item_by_index_const(self._ptr, idx)
70 assert item_ptr is not None
71 item_type = native_bt.field_path_item_get_type(item_ptr)
72 if item_type == native_bt.FIELD_PATH_ITEM_TYPE_INDEX:
73 idx = native_bt.field_path_item_index_get_index(item_ptr)
74 yield _IndexFieldPathItem(idx)
75 elif item_type == native_bt.FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT:
76 yield _CurrentArrayElementFieldPathItem()
77 elif item_type == native_bt.FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT:
78 yield _CurrentOptionContentFieldPathItem()
79 else:
80 assert False
81
82
83 _SCOPE_TO_OBJ = {
84 native_bt.FIELD_PATH_SCOPE_PACKET_CONTEXT: FieldPathScope.PACKET_CONTEXT,
85 native_bt.FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT: FieldPathScope.EVENT_COMMON_CONTEXT,
86 native_bt.FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT: FieldPathScope.EVENT_SPECIFIC_CONTEXT,
87 native_bt.FIELD_PATH_SCOPE_EVENT_PAYLOAD: FieldPathScope.EVENT_PAYLOAD,
88 }
This page took 0.048796 seconds and 4 git commands to generate.