Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
81447b5b PP |
2 | # |
3 | # Copyright (c) 2016-2017 Philippe Proulx <pproulx@efficios.com> | |
81447b5b | 4 | |
af4bbfc7 | 5 | from bt2 import native_bt, utils |
3fb99a22 PP |
6 | from bt2 import object as bt2_object |
7 | from bt2 import packet as bt2_packet | |
3fb99a22 | 8 | from bt2 import stream_class as bt2_stream_class |
5783664e | 9 | from bt2 import value as bt2_value |
81447b5b PP |
10 | import bt2 |
11 | ||
12 | ||
79935628 SM |
13 | def _bt2_trace(): |
14 | from bt2 import trace as bt2_trace | |
15 | ||
16 | return bt2_trace | |
17 | ||
18 | ||
f0a42b33 | 19 | class _StreamConst(bt2_object._SharedObject): |
af4bbfc7 SM |
20 | _get_ref = staticmethod(native_bt.stream_get_ref) |
21 | _put_ref = staticmethod(native_bt.stream_put_ref) | |
f0a42b33 FD |
22 | _borrow_class_ptr = staticmethod(native_bt.stream_borrow_class_const) |
23 | _borrow_user_attributes_ptr = staticmethod( | |
24 | native_bt.stream_borrow_user_attributes_const | |
25 | ) | |
26 | _create_value_from_ptr_and_get_ref = staticmethod( | |
27 | bt2_value._create_from_const_ptr_and_get_ref | |
28 | ) | |
29 | _borrow_trace_ptr = staticmethod(native_bt.stream_borrow_trace_const) | |
30 | _stream_class_pycls = bt2_stream_class._StreamClassConst | |
79935628 | 31 | _trace_pycls = property(lambda _: _bt2_trace()._TraceConst) |
81447b5b | 32 | |
81447b5b | 33 | @property |
e8ac1aae | 34 | def cls(self): |
f0a42b33 | 35 | stream_class_ptr = self._borrow_class_ptr(self._ptr) |
af4bbfc7 | 36 | assert stream_class_ptr is not None |
f0a42b33 | 37 | return self._stream_class_pycls._create_from_ptr_and_get_ref(stream_class_ptr) |
81447b5b PP |
38 | |
39 | @property | |
40 | def name(self): | |
50842bdc | 41 | return native_bt.stream_get_name(self._ptr) |
81447b5b | 42 | |
5783664e PP |
43 | @property |
44 | def user_attributes(self): | |
f0a42b33 | 45 | ptr = self._borrow_user_attributes_ptr(self._ptr) |
5783664e | 46 | assert ptr is not None |
f0a42b33 | 47 | return self._create_value_from_ptr_and_get_ref(ptr) |
5783664e | 48 | |
811644b8 PP |
49 | @property |
50 | def id(self): | |
50842bdc | 51 | id = native_bt.stream_get_id(self._ptr) |
811644b8 PP |
52 | return id if id >= 0 else None |
53 | ||
f0a42b33 FD |
54 | @property |
55 | def trace(self): | |
56 | trace_ptr = self._borrow_trace_ptr(self._ptr) | |
57 | assert trace_ptr is not None | |
58 | return self._trace_pycls._create_from_ptr_and_get_ref(trace_ptr) | |
59 | ||
60 | ||
61 | class _Stream(_StreamConst): | |
62 | _borrow_class_ptr = staticmethod(native_bt.stream_borrow_class) | |
63 | _borrow_user_attributes_ptr = staticmethod(native_bt.stream_borrow_user_attributes) | |
64 | _create_value_from_ptr_and_get_ref = staticmethod( | |
65 | bt2_value._create_from_ptr_and_get_ref | |
66 | ) | |
67 | _borrow_trace_ptr = staticmethod(native_bt.stream_borrow_trace) | |
68 | _stream_class_pycls = bt2_stream_class._StreamClass | |
79935628 | 69 | _trace_pycls = property(lambda _: _bt2_trace()._Trace) |
f0a42b33 | 70 | |
81447b5b | 71 | def create_packet(self): |
26fc5aed | 72 | if not self.cls.supports_packets: |
ce4923b0 | 73 | raise ValueError( |
cfbd7cf3 FD |
74 | 'cannot create packet: stream class does not support packets' |
75 | ) | |
26fc5aed | 76 | |
50842bdc | 77 | packet_ptr = native_bt.packet_create(self._ptr) |
81447b5b PP |
78 | |
79 | if packet_ptr is None: | |
694c792b | 80 | raise bt2._MemoryError('cannot create packet object') |
81447b5b | 81 | |
3fb99a22 | 82 | return bt2_packet._Packet._create_from_ptr(packet_ptr) |
e071d36f | 83 | |
f0a42b33 FD |
84 | def _user_attributes(self, user_attributes): |
85 | value = bt2_value.create_value(user_attributes) | |
86 | utils._check_type(value, bt2_value.MapValue) | |
87 | native_bt.stream_set_user_attributes(self._ptr, value._ptr) | |
88 | ||
89 | _user_attributes = property( | |
90 | fget=_StreamConst.user_attributes.fget, fset=_user_attributes | |
91 | ) | |
92 | ||
93 | def _name(self, name): | |
94 | utils._check_str(name) | |
95 | native_bt.stream_set_name(self._ptr, name) | |
96 | ||
97 | _name = property(fget=_StreamConst.name.fget, fset=_name) |