bt2: rename _FieldPath to _FieldPathConst
[babeltrace.git] / src / 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
e7ceb9df
PP
27class 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
d47b87ac
SM
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
cec0261d
PP
51class _CurrentOptionContentFieldPathItem(_FieldPathItem):
52 pass
53
54
5679964c 55class _FieldPathConst(object._SharedObject, collections.abc.Iterable):
d47b87ac
SM
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:
cfbd7cf3 76 yield _CurrentArrayElementFieldPathItem()
cec0261d
PP
77 elif item_type == native_bt.FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT:
78 yield _CurrentOptionContentFieldPathItem()
d47b87ac
SM
79 else:
80 assert False
81
82
83_SCOPE_TO_OBJ = {
e7ceb9df
PP
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,
d47b87ac 88}
This page took 0.035143 seconds and 4 git commands to generate.