bt2: Adapt test_field_class.py and make it pass
[babeltrace.git] / bindings / python / bt2 / bt2 / field_path.py
CommitLineData
d47b87ac
SM
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
23import collections
24from bt2 import native_bt, object
25
26
27class Scope:
28 PACKET_CONTEXT = native_bt.SCOPE_PACKET_CONTEXT
29 EVENT_COMMON_CONTEXT = native_bt.SCOPE_EVENT_COMMON_CONTEXT
30 EVENT_SPECIFIC_CONTEXT = native_bt.SCOPE_EVENT_SPECIFIC_CONTEXT
31 EVENT_PAYLOAD = native_bt.SCOPE_EVENT_PAYLOAD
32
33
34class _FieldPathItem:
35 pass
36
37
38class _IndexFieldPathItem(_FieldPathItem):
39 def __init__(self, index):
40 self._index = index
41
42 @property
43 def index(self):
44 return self._index
45
46
47class _CurrentArrayElementFieldPathItem(_FieldPathItem):
48 pass
49
50
51class _FieldPath(object._SharedObject, collections.abc.Iterable):
52 _get_ref = staticmethod(native_bt.field_path_get_ref)
53 _put_ref = staticmethod(native_bt.field_path_put_ref)
54
55 @property
56 def root_scope(self):
57 scope = native_bt.field_path_get_root_scope(self._ptr)
58 return _SCOPE_TO_OBJ[scope]
59
60 def __len__(self):
61 return native_bt.field_path_get_item_count(self._ptr)
62
63 def __iter__(self):
64 for idx in range(len(self)):
65 item_ptr = native_bt.field_path_borrow_item_by_index_const(self._ptr, idx)
66 assert item_ptr is not None
67 item_type = native_bt.field_path_item_get_type(item_ptr)
68 if item_type == native_bt.FIELD_PATH_ITEM_TYPE_INDEX:
69 idx = native_bt.field_path_item_index_get_index(item_ptr)
70 yield _IndexFieldPathItem(idx)
71 elif item_type == native_bt.FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT:
72 yield _CurrentArrayElementFieldPathItem()
73 else:
74 assert False
75
76
77_SCOPE_TO_OBJ = {
78 native_bt.SCOPE_PACKET_CONTEXT: Scope.PACKET_CONTEXT,
79 native_bt.SCOPE_EVENT_COMMON_CONTEXT: Scope.EVENT_COMMON_CONTEXT,
80 native_bt.SCOPE_EVENT_SPECIFIC_CONTEXT: Scope.EVENT_SPECIFIC_CONTEXT,
81 native_bt.SCOPE_EVENT_PAYLOAD: Scope.EVENT_PAYLOAD
82}
This page took 0.025541 seconds and 4 git commands to generate.