1 # The MIT License (MIT)
3 # Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
23 from bt2
import native_bt
, object, utils
24 import bt2
.field_types
25 import collections
.abc
32 class _EventClassAttributesIterator(collections
.abc
.Iterator
):
33 def __init__(self
, attributes
):
34 self
._attributes
= attributes
38 if self
._at
== len(self
._attributes
):
41 name
= native_bt
.ctf_event_class_get_attribute_name(self
._attributes
._event
_class
_ptr
,
43 utils
._handle
_ptr
("cannot get event class object's attribute name")
48 class _EventClassAttributes(collections
.abc
.MutableMapping
):
49 def __init__(self
, event_class_ptr
):
50 self
._event
_class
_ptr
= event_class_ptr
52 def __getitem__(self
, key
):
54 value_ptr
= native_bt
.ctf_event_class_get_attribute_value_by_name(self
._event
_class
_ptr
,
60 return bt2
.values
._create
_from
_ptr
(value_ptr
)
62 def __setitem__(self
, key
, value
):
64 value
= bt2
.create_value(value
)
65 ret
= native_bt
.ctf_event_class_set_attribute(self
._event
_class
_ptr
, key
,
67 utils
._handle
_ret
(ret
, "cannot set event class object's attribute")
69 def __delitem__(self
, key
):
70 raise NotImplementedError
73 count
= native_bt
.ctf_event_class_get_attribute_count(self
._event
_class
_ptr
)
74 utils
._handle
_ret
(count
, "cannot get event class object's attribute count")
78 return _EventClassAttributesIterator(self
)
81 class EventClass(object._Object
):
82 def __init__(self
, name
, id=None, context_field_type
=None,
83 payload_field_type
=None, attributes
=None):
84 utils
._check
_str
(name
)
85 ptr
= native_bt
.ctf_event_class_create(name
)
88 raise bt2
.CreationError('cannot create event class object')
95 if context_field_type
is not None:
96 self
.context_field_type
= context_field_type
98 if payload_field_type
is not None:
99 self
.payload_field_type
= payload_field_type
101 if attributes
is not None:
102 for name
, value
in attributes
.items():
103 self
.attributes
[name
] = value
106 def stream_class(self
):
107 sc_ptr
= native_bt
.ctf_event_class_get_stream_class(self
._ptr
)
109 if sc_ptr
is not None:
110 return bt2
.StreamClass
._create
_from
_ptr
(sc_ptr
)
113 def attributes(self
):
114 return _EventClassAttributes(self
._ptr
)
118 return native_bt
.ctf_event_class_get_name(self
._ptr
)
122 id = native_bt
.ctf_event_class_get_id(self
._ptr
)
124 if utils
._is
_m
1ull(id):
125 raise bt2
.Error("cannot get event class object's ID")
131 utils
._check
_int
64(id)
132 ret
= native_bt
.ctf_event_class_set_id(self
._ptr
, id)
133 utils
._handle
_ret
(ret
, "cannot set event class object's ID")
136 def context_field_type(self
):
137 ft_ptr
= native_bt
.ctf_event_class_get_context_type(self
._ptr
)
142 return bt2
.field_types
._create
_from
_ptr
(ft_ptr
)
144 @context_field_type.setter
145 def context_field_type(self
, context_field_type
):
146 context_field_type_ptr
= None
148 if context_field_type
is not None:
149 utils
._check
_type
(context_field_type
, bt2
.field_types
._FieldType
)
150 context_field_type_ptr
= context_field_type
._ptr
152 ret
= native_bt
.ctf_event_class_set_context_type(self
._ptr
, context_field_type_ptr
)
153 utils
._handle
_ret
(ret
, "cannot set event class object's context field type")
156 def payload_field_type(self
):
157 ft_ptr
= native_bt
.ctf_event_class_get_payload_type(self
._ptr
)
162 return bt2
.field_types
._create
_from
_ptr
(ft_ptr
)
164 @payload_field_type.setter
165 def payload_field_type(self
, payload_field_type
):
166 payload_field_type_ptr
= None
168 if payload_field_type
is not None:
169 utils
._check
_type
(payload_field_type
, bt2
.field_types
._FieldType
)
170 payload_field_type_ptr
= payload_field_type
._ptr
172 ret
= native_bt
.ctf_event_class_set_payload_type(self
._ptr
, payload_field_type_ptr
)
173 utils
._handle
_ret
(ret
, "cannot set event class object's payload field type")
176 event_ptr
= native_bt
.ctf_event_create(self
._ptr
)
178 if event_ptr
is None:
179 raise bt2
.CreationError('cannot create event field object')
181 return bt2
.event
._create
_from
_ptr
(event_ptr
)
183 def __eq__(self
, other
):
184 if type(other
) is not type(self
):
187 if self
.addr
== other
.addr
:
190 self_attributes
= {name
: val
for name
, val
in self
.attributes
.items()}
191 other_attributes
= {name
: val
for name
, val
in other
.attributes
.items()}
196 self
.context_field_type
,
197 self
.payload_field_type
203 other
.context_field_type
,
204 other
.payload_field_type
206 return self_props
== other_props
208 def _copy(self
, ft_copy_func
):
209 cpy
= EventClass(self
.name
)
212 for name
, value
in self
.attributes
.items():
213 cpy
.attributes
[name
] = value
215 cpy
.context_field_type
= ft_copy_func(self
.context_field_type
)
216 cpy
.payload_field_type
= ft_copy_func(self
.payload_field_type
)
220 return self
._copy
(lambda ft
: ft
)
222 def __deepcopy__(self
, memo
):
223 cpy
= self
._copy
(copy
.deepcopy
)
This page took 0.034868 seconds and 4 git commands to generate.