bt2: Adapt test_stream_class.py and make it pass
[babeltrace.git] / bindings / python / bt2 / bt2 / stream.py
CommitLineData
81447b5b
PP
1# The MIT License (MIT)
2#
3# Copyright (c) 2016-2017 Philippe Proulx <pproulx@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
23from bt2 import native_bt, object, utils
24import bt2.packet
25import bt2.event
26import abc
27import bt2
28
29
30def _create_from_ptr(stream_ptr):
50842bdc 31 import bt2.ctf_writer
81447b5b 32
50842bdc 33 if native_bt.stream_is_writer(stream_ptr):
e29b342c 34 cls = bt2.ctf_writer._CtfWriterStream
81447b5b
PP
35 else:
36 cls = _Stream
37
38 return cls._create_from_ptr(stream_ptr)
39
40
78288f58 41class _StreamBase(object._SharedObject):
81447b5b
PP
42 @property
43 def stream_class(self):
50842bdc 44 stream_class_ptr = native_bt.stream_get_class(self._ptr)
811644b8 45 assert(stream_class_ptr)
81447b5b
PP
46 return bt2.StreamClass._create_from_ptr(stream_class_ptr)
47
48 @property
49 def name(self):
50842bdc 50 return native_bt.stream_get_name(self._ptr)
81447b5b 51
811644b8
PP
52 @property
53 def id(self):
50842bdc 54 id = native_bt.stream_get_id(self._ptr)
811644b8
PP
55 return id if id >= 0 else None
56
81447b5b
PP
57 def __eq__(self, other):
58 if self.addr == other.addr:
59 return True
60
811644b8 61 return (self.name, self.id) == (other.name, other.id)
81447b5b
PP
62
63
64class _Stream(_StreamBase):
8c2367b8
SM
65 _get_ref = staticmethod(native_bt.stream_get_ref)
66 _put_ref = staticmethod(native_bt.stream_put_ref)
67
81447b5b 68 def create_packet(self):
50842bdc 69 packet_ptr = native_bt.packet_create(self._ptr)
81447b5b
PP
70
71 if packet_ptr is None:
72 raise bt2.CreationError('cannot create packet object')
73
74 return bt2.packet._Packet._create_from_ptr(packet_ptr)
75
76 def __eq__(self, other):
77 if type(other) is not type(self):
78 return False
79
80 return _StreamBase.__eq__(self, other)
81
82 def _copy(self):
811644b8 83 return self.stream_class(self.name, self.id)
81447b5b
PP
84
85 def __copy__(self):
86 return self._copy()
87
88 def __deepcopy__(self, memo):
89 cpy = self._copy()
90 memo[id(self)] = cpy
91 return cpy
This page took 0.040015 seconds and 4 git commands to generate.