lib/plugin/plugin.c: do not use G_MODULE_BIND_LOCAL for Python plugin provider
[babeltrace.git] / bindings / python / bt2 / event_class.py
CommitLineData
81447b5b
PP
1# The MIT License (MIT)
2#
3# Copyright (c) 2016 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.field_types
25import collections.abc
26import bt2.values
27import bt2.event
28import copy
29import bt2
30
31
32class _EventClassAttributesIterator(collections.abc.Iterator):
33 def __init__(self, attributes):
34 self._attributes = attributes
35 self._at = 0
36
37 def __next__(self):
38 if self._at == len(self._attributes):
39 raise StopIteration
40
41 name = native_bt.ctf_event_class_get_attribute_name(self._attributes._event_class_ptr,
42 self._at)
43 utils._handle_ptr("cannot get event class object's attribute name")
44 self._at += 1
45 return name
46
47
48class _EventClassAttributes(collections.abc.MutableMapping):
49 def __init__(self, event_class_ptr):
50 self._event_class_ptr = event_class_ptr
51
52 def __getitem__(self, key):
53 utils._check_str(key)
54 value_ptr = native_bt.ctf_event_class_get_attribute_value_by_name(self._event_class_ptr,
55 key)
56
57 if value_ptr is None:
58 raise KeyError(key)
59
60 return bt2.values._create_from_ptr(value_ptr)
61
62 def __setitem__(self, key, value):
63 utils._check_str(key)
64 value = bt2.create_value(value)
65 ret = native_bt.ctf_event_class_set_attribute(self._event_class_ptr, key,
66 value._ptr)
67 utils._handle_ret(ret, "cannot set event class object's attribute")
68
69 def __delitem__(self, key):
70 raise NotImplementedError
71
72 def __len__(self):
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")
75 return count
76
77 def __iter__(self):
78 return _EventClassAttributesIterator(self)
79
80
81class 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)
86
87 if ptr is None:
88 raise bt2.CreationError('cannot create event class object')
89
90 super().__init__(ptr)
91
92 if id is not None:
93 self.id = id
94
95 if context_field_type is not None:
96 self.context_field_type = context_field_type
97
98 if payload_field_type is not None:
99 self.payload_field_type = payload_field_type
100
101 if attributes is not None:
102 for name, value in attributes.items():
103 self.attributes[name] = value
104
105 @property
106 def stream_class(self):
107 sc_ptr = native_bt.ctf_event_class_get_stream_class(self._ptr)
108
109 if sc_ptr is not None:
110 return bt2.StreamClass._create_from_ptr(sc_ptr)
111
112 @property
113 def attributes(self):
114 return _EventClassAttributes(self._ptr)
115
116 @property
117 def name(self):
118 return native_bt.ctf_event_class_get_name(self._ptr)
119
120 @property
121 def id(self):
122 id = native_bt.ctf_event_class_get_id(self._ptr)
123
124 if utils._is_m1ull(id):
125 raise bt2.Error("cannot get event class object's ID")
126
127 return id
128
129 @id.setter
130 def id(self, id):
131 utils._check_int64(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")
134
135 @property
136 def context_field_type(self):
137 ft_ptr = native_bt.ctf_event_class_get_context_type(self._ptr)
138
139 if ft_ptr is None:
140 return
141
142 return bt2.field_types._create_from_ptr(ft_ptr)
143
144 @context_field_type.setter
145 def context_field_type(self, context_field_type):
146 context_field_type_ptr = None
147
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
151
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")
154
155 @property
156 def payload_field_type(self):
157 ft_ptr = native_bt.ctf_event_class_get_payload_type(self._ptr)
158
159 if ft_ptr is None:
160 return
161
162 return bt2.field_types._create_from_ptr(ft_ptr)
163
164 @payload_field_type.setter
165 def payload_field_type(self, payload_field_type):
166 payload_field_type_ptr = None
167
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
171
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")
174
175 def __call__(self):
176 event_ptr = native_bt.ctf_event_create(self._ptr)
177
178 if event_ptr is None:
179 raise bt2.CreationError('cannot create event field object')
180
181 return bt2.event._create_from_ptr(event_ptr)
182
183 def __eq__(self, other):
184 if type(other) is not type(self):
185 return False
186
187 if self.addr == other.addr:
188 return True
189
190 self_attributes = {name: val for name, val in self.attributes.items()}
191 other_attributes = {name: val for name, val in other.attributes.items()}
192 self_props = (
193 self_attributes,
194 self.name,
195 self.id,
196 self.context_field_type,
197 self.payload_field_type
198 )
199 other_props = (
200 other_attributes,
201 other.name,
202 other.id,
203 other.context_field_type,
204 other.payload_field_type
205 )
206 return self_props == other_props
207
208 def _copy(self, ft_copy_func):
209 cpy = EventClass(self.name)
210 cpy.id = self.id
211
212 for name, value in self.attributes.items():
213 cpy.attributes[name] = value
214
215 cpy.context_field_type = ft_copy_func(self.context_field_type)
216 cpy.payload_field_type = ft_copy_func(self.payload_field_type)
217 return cpy
218
219 def __copy__(self):
220 return self._copy(lambda ft: ft)
221
222 def __deepcopy__(self, memo):
223 cpy = self._copy(copy.deepcopy)
224 memo[id(self)] = cpy
225 return cpy
This page took 0.037287 seconds and 4 git commands to generate.