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