X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=bindings%2Fpython%2Fbabeltrace.i.in;h=04ef3512f9ed7bc4272e40b8ad941ff752cd50c3;hp=0e3d3d4c327c4a84050594d4449ac0bdbfd38057;hb=cb1fcc68fd8c397ad94691ced4a70b3307f7a86b;hpb=ec8c88d7d67bfcd57d2a09a135edae4737f3f4d9 diff --git a/bindings/python/babeltrace.i.in b/bindings/python/babeltrace.i.in index 0e3d3d4c..04ef3512 100644 --- a/bindings/python/babeltrace.i.in +++ b/bindings/python/babeltrace.i.in @@ -6,6 +6,7 @@ * Copyright 2012 EfficiOS Inc. * * Author: Danny Serres + * Author: Jérémie Galarneau * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,7 +27,7 @@ Babeltrace is a trace viewer and converter reading and writing the Common Trace Format (CTF). Its main use is to pretty-print CTF traces into a human-readable text output. -To use this module, the first step is to create a Context and add a +To use this module, the first step is to create a TraceCollection and add a trace to it." %enddef @@ -118,26 +119,22 @@ void bt_context_get(struct bt_context *ctx); void bt_context_put(struct bt_context *ctx); struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *event); -// class Context to prevent direct access to struct bt_context +// class TraceCollection to prevent direct access to struct bt_context %pythoncode%{ -class Context: +class TraceCollection: """ - The context represents the object in which a trace_collection is - open. As long as this structure is allocated, the trace_collection - is open and the traces it contains can be read and seeked by the - iterators and callbacks. + The TraceCollection is the object that contains all currently opened traces. """ def __init__(self): - self._c = _bt_context_create() + self._tc = _bt_context_create() def __del__(self): - _bt_context_put(self._c) + _bt_context_put(self._tc) - def add_trace(self, path, format_str, - packet_seek=None, stream_list=None, metadata=None): + def add_trace(self, path, format_str): """ - Add a trace by path to the context. + Add a trace by path to the TraceCollection. Open a trace. @@ -148,28 +145,15 @@ class Context: format is a string containing the format name in which the trace was produced. - packet_seek is not implemented for Python. Should be left None to - use the default packet_seek handler provided by the trace format. - - stream_list is a linked list of streams, it is used to open a trace - where the trace data is located in memory mapped areas instead of - trace files, this argument should be None when path is not None. - - The metadata parameter acts as a metadata override when not None, - otherwise the format handles the metadata opening. - Return: the corresponding TraceHandle on success or None on error. """ - if metadata is not None: - metadata = metadata._file - - ret = _bt_context_add_trace(self._c, path, format_str, packet_seek, - stream_list, metadata) + ret = _bt_context_add_trace(self._tc, path, format_str, None, None, None) if ret < 0: return None th = TraceHandle.__new__(TraceHandle) th._id = ret + th._trace_collection = self return th def add_traces_recursive(self, path, format_str): @@ -177,7 +161,7 @@ class Context: Open a trace recursively. Find each trace present in the subdirectory starting from the given - path, and add them to the context. + path, and add them to the TraceCollection. Return a dict of TraceHandle instances (the full path is the key). Return None on error. @@ -206,14 +190,88 @@ class Context: def remove_trace(self, trace_handle): """ - Remove a trace from the context. + Remove a trace from the TraceCollection. Effectively closing the trace. """ try: - _bt_context_remove_trace(self._c, trace_handle._id) + _bt_context_remove_trace(self._tc, trace_handle._id) except AttributeError: raise TypeError("in remove_trace, " "argument 2 must be a TraceHandle instance") + + @property + def events(self): + """ + Generator function to iterate over the events of open in the current + TraceCollection. + """ + begin_pos_ptr = _bt_iter_pos() + end_pos_ptr = _bt_iter_pos() + begin_pos_ptr.type = SEEK_BEGIN + end_pos_ptr.type = SEEK_LAST + + for event in self._events(begin_pos_ptr, end_pos_ptr): + yield event + + def events_timestamps(self, timestamp_begin, timestamp_end): + """ + Generator function to iterate over the events of open in the current + TraceCollection from timestamp_begin to timestamp_end. + """ + begin_pos_ptr = _bt_iter_pos() + end_pos_ptr = _bt_iter_pos() + begin_pos_ptr.type = end_pos_ptr.type = SEEK_TIME + begin_pos_ptr.u.seek_time = timestamp_begin + end_pos_ptr.u.seek_time = timestamp_end + + for event in self._events(begin_pos_ptr, end_pos_ptr): + yield event + + @property + def timestamp_begin(self): + pos_ptr = _bt_iter_pos() + pos_ptr.type = SEEK_BEGIN + return self._timestamp_at_pos(pos_ptr) + + @property + def timestamp_end(self): + pos_ptr = _bt_iter_pos() + pos_ptr.type = SEEK_LAST + return self._timestamp_at_pos(pos_ptr) + + def _timestamp_at_pos(self, pos_ptr): + ctf_it_ptr = _bt_ctf_iter_create(self._tc, pos_ptr, pos_ptr) + if ctf_it_ptr is None: + raise NotImplementedError("Creation of multiple iterators is unsupported.") + ev_ptr = _bt_ctf_iter_read_event(ctf_it_ptr) + _bt_ctf_iter_destroy(ctf_it_ptr) + if ev_ptr is None: + return None; + + def _events(self, begin_pos_ptr, end_pos_ptr): + ctf_it_ptr = _bt_ctf_iter_create(self._tc, begin_pos_ptr, end_pos_ptr) + if ctf_it_ptr is None: + raise NotImplementedError( + "Creation of multiple iterators is unsupported.") + + while True: + ev_ptr = _bt_ctf_iter_read_event(ctf_it_ptr) + if ev_ptr is None: + break + + ev = Event.__new__(Event) + ev._e = ev_ptr + try: + yield ev + except GeneratorExit: + break + + ret = _bt_iter_next(_bt_ctf_get_iter(ctf_it_ptr)) + if ret != 0: + break + + _bt_ctf_iter_destroy(ctf_it_ptr) + %} @@ -299,182 +357,6 @@ struct bt_iter_pos { } u; }; - -%pythoncode%{ - -class IterPos: - """This class represents the position where to set an iterator.""" - - __can_access = False - - def __init__(self, seek_type, seek_time = None): - """ - seek_type represents the type of seek to use. - seek_time is the timestamp to seek to when using SEEK_TIME, it - is expressed in nanoseconds - Only use SEEK_RESTORE on IterPos obtained from the get_pos function - in Iter class. - """ - - self._pos = _bt_iter_pos() - self._pos.type = seek_type - if seek_time and seek_type == SEEK_TIME: - self._pos.u.seek_time = seek_time - self.__can_access = True - - def __del__(self): - if not self.__can_access: - _bt_iter_free_pos(self._pos) - - def _get_type(self): - if not __can_access: - raise AttributeError("seek_type is not available") - return self._pos.type - - def _set_type(self, seek_type): - if not __can_access: - raise AttributeError("seek_type is not available") - self._pos.type = seek_type - - def _get_time(self): - if not __can_access: - raise AttributeError("seek_time is not available") - - elif self._pos.type is not SEEK_TIME: - raise TypeError("seek_type is not SEEK_TIME") - - return self._pos.u.seek_time - - def _set_time(self, time): - if not __can_access: - raise AttributeError("seek_time is not available") - - elif self._pos.type is not SEEK_TIME: - raise TypeError("seek_type is not SEEK_TIME") - - self._pos.u.seek_time = time - - def _get_pos(self): - return self._pos - - - seek_type = property(_get_type, _set_type) - seek_time = property(_get_time, _set_time) - - -class Iterator: - - __with_init = False - - def __init__(self, context, begin_pos = None, end_pos = None, _no_init = None): - """ - Allocate a trace collection iterator. - - begin_pos and end_pos are optional parameters to specify the - position at which the trace collection should be seeked upon - iterator creation, and the position at which iteration will - start returning "EOF". - - By default, if begin_pos is None, a BT_SEEK_CUR is performed at - creation. By default, if end_pos is None, a BT_SEEK_END (end of - trace) is the EOF criterion. - """ - if _no_init is None: - if begin_pos is None: - bp = None - else: - try: - bp = begin_pos._pos - except AttributeError: - raise TypeError("in __init__, " - "argument 3 must be a IterPos instance") - - if end_pos is None: - ep = None - else: - try: - ep = end_pos._pos - except AttributeError: - raise TypeError("in __init__, " - "argument 4 must be a IterPos instance") - - try: - self._bi = _bt_iter_create(context._c, bp, ep) - except AttributeError: - raise TypeError("in __init__, " - "argument 2 must be a Context instance") - - self.__with_init = True - - else: - self._bi = _no_init - - def __del__(self): - if self.__with_init: - _bt_iter_destroy(self._bi) - - def next(self): - """ - Move trace collection position to the next event. - Returns 0 on success, a negative value on error. - """ - return _bt_iter_next(self._bi) - - def get_pos(self): - """Return a IterPos class of the current iterator position.""" - ret = IterPos(0) - ret.__can_access = False - ret._pos = _bt_iter_get_pos(self._bi) - return ret - - def set_pos(self, pos): - """ - Move the iterator to a given position. - - On error, the stream_heap is reinitialized and returned empty. - Return 0 for success. - Return EOF if the position requested is after the last event of the - trace collection. - Return -EINVAL when called with invalid parameter. - Return -ENOMEM if the stream_heap could not be properly initialized. - """ - try: - return _bt_iter_set_pos(self._bi, pos._pos) - except AttributeError: - raise TypeError("in set_pos, " - "argument 2 must be a IterPos instance") - - def create_time_pos(self, timestamp): - """ - Create a position based on time - This function allocates and returns a new IterPos to be able to - restore an iterator position based on a timestamp. - """ - - if timestamp < 0: - raise TypeError("timestamp must be an unsigned int") - - ret = IterPos(0) - ret.__can_access = False - ret._pos = _bt_iter_create_time_pos(self._bi, timestamp) - return ret -%} - - -/* ================================================================= - CLOCK-TYPE.H - ¯¯¯¯¯¯¯¯¯¯¯¯ - *** Enum copied from clock-type.h­ - All changes must also be made here -*/ -%rename("CLOCK_CYCLES") BT_CLOCK_CYCLES; -%rename("CLOCK_REAL") BT_CLOCK_REAL; - -enum bt_clock_type { - BT_CLOCK_CYCLES = 0, - BT_CLOCK_REAL -}; - /* ================================================================= TRACE-HANDLE.H, TRACE-HANDLE-INTERNAL.H ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ @@ -504,6 +386,11 @@ int bt_ctf_event_get_handle_id(const struct bt_ctf_event *event); %pythoncode%{ +# Based on enum bt_clock_type in clock-type.h­ +class ClockType: + CLOCK_CYCLES = 0 + CLOCK_REAL = 1 + class TraceHandle(object): """ The TraceHandle allows the user to manipulate a trace file directly. @@ -517,35 +404,27 @@ class TraceHandle(object): def __repr__(self): return "Babeltrace TraceHandle: trace_id('{0}')".format(self._id) - def get_id(self): + @property + def id(self): """Return the TraceHandle id.""" return self._id - def get_path(self, context): + @property + def path(self): """Return the path of a TraceHandle.""" - try: - return _bt_trace_handle_get_path(context._c, self._id) - except AttributeError: - raise TypeError("in get_path, " - "argument 2 must be a Context instance") + return _bt_trace_handle_get_path(self._trace_collection._tc, self._id) - def get_timestamp_begin(self, context, clock_type): + @property + def timestamp_begin(self): """Return the creation time of the buffers of a trace.""" - try: - return _bt_trace_handle_get_timestamp_begin( - context._c, self._id,clock_type) - except AttributeError: - raise TypeError("in get_timestamp_begin, " - "argument 2 must be a Context instance") + return _bt_trace_handle_get_timestamp_begin( + self._trace_collection._tc, self._id, ClockType.CLOCK_REAL) - def get_timestamp_end(self, context, clock_type): + @property + def timestamp_end(self): """Return the destruction timestamp of the buffers of a trace.""" - try: - return _bt_trace_handle_get_timestamp_end( - context._c, self._id, clock_type) - except AttributeError: - raise TypeError("in get_timestamp_end, " - "argument 2 must be a Context instance") + return _bt_trace_handle_get_timestamp_end( + self._trace_collection._tc, self._id, ClockType.CLOCK_REAL) %} @@ -577,7 +456,6 @@ struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter); //Events - %rename("_bt_ctf_get_top_level_scope") bt_ctf_get_top_level_scope(const struct bt_ctf_event *event, enum bt_ctf_scope scope); %rename("_bt_ctf_event_name") bt_ctf_event_name(const struct bt_ctf_event *ctf_event); @@ -691,640 +569,498 @@ class CTFTypeId: break return name -class CTFReader: - class scope: - TRACE_PACKET_HEADER = 0 - STREAM_PACKET_CONTEXT = 1 - STREAM_EVENT_HEADER = 2 - STREAM_EVENT_CONTEXT = 3 - EVENT_CONTEXT = 4 - EVENT_FIELDS = 5 +class scope: + TRACE_PACKET_HEADER = 0 + STREAM_PACKET_CONTEXT = 1 + STREAM_EVENT_HEADER = 2 + STREAM_EVENT_CONTEXT = 3 + EVENT_CONTEXT = 4 + EVENT_FIELDS = 5 - class Iterator(Iterator, object): - """ - Allocate a CTF trace collection iterator. +# Priority of the scopes when searching for event fields +_scopes = [scope.EVENT_FIELDS, scope.EVENT_CONTEXT, scope.STREAM_EVENT_CONTEXT, + scope.STREAM_EVENT_HEADER, scope.STREAM_PACKET_CONTEXT, scope.TRACE_PACKET_HEADER] - begin_pos and end_pos are optional parameters to specify the - position at which the trace collection should be seeked upon - iterator creation, and the position at which iteration will - start returning "EOF". +import collections +class Event(collections.Mapping): + """ + This class represents an event from the trace. + It is obtained using the TraceCollection generator functions. + Do not instantiate. + """ + def __init__(self): + raise NotImplementedError("Event cannot be instantiated") - By default, if begin_pos is None, a SEEK_CUR is performed at - creation. By default, if end_pos is None, a SEEK_END (end of - trace) is the EOF criterion. + @property + def name(self): + """Return the name of the event or None on error.""" + return _bt_ctf_event_name(self._e) - Only one iterator can be created against a context. If more than one - iterator is being created for the same context, the second creation - will return None. The previous iterator must be destroyed before - creation of the new iterator for this function to succeed. + @property + def cycles(self): """ + Return the timestamp of the event as written in + the packet (in cycles) or -1ULL on error. + """ + return _bt_ctf_get_cycles(self._e) - def __new__(cls, context, begin_pos = None, end_pos = None): - # __new__ is used to control the return value - # as the CTFReader.Iterator class should return None - # if bt_ctf_iter_create returns NULL - - if begin_pos is None: - bp = None - else: - bp = begin_pos._pos - if end_pos is None: - ep = None - else: - ep = end_pos._pos - try: - it = _bt_ctf_iter_create(context._c, bp, ep) - except AttributeError: - raise TypeError("in __init__, " - "argument 2 must be a Context instance") - if it is None: - return None - - ret_class = super(CTFReader.Iterator, cls).__new__(cls) - ret_class._i = it - return ret_class + @property + def timestamp(self): + """ + Return the timestamp of the event offset with the + system clock source or -1ULL on error. + """ + return _bt_ctf_get_timestamp(self._e) - def __init__(self, context, begin_pos = None, end_pos = None): - Iterator.__init__(self, None, None, None, - _bt_ctf_get_iter(self._i)) + def field_with_scope(self, field_name, scope): + """ + Get field_name's value in scope. + None is returned if no field matches field_name. + """ + if not scope in _scopes: + raise ValueError("Invalid scope provided") + field = self._field_with_scope(field_name, scope) + if field is not None: + return field.value + return None - def __del__(self): - _bt_ctf_iter_destroy(self._i) + def field_list_with_scope(self, scope): + """Return a list of field names in scope.""" + if not scope in _scopes: + raise ValueError("Invalid scope provided") + field_names = [] + for field in self._field_list_with_scope(scope): + field_names.append(field.name) + return field_names - def read_event(self): - """ - Read the iterator's current event data. - Return current event on success, None on end of trace. - """ - ret = _bt_ctf_iter_read_event(self._i) - if ret is None: - return ret - ev = CTFReader.Event.__new__(CTFReader.Event) - ev._e = ret - return ev + @property + def handle(self): + """ + Get the TraceHandle associated with this event + Return None on error + """ + ret = _bt_ctf_event_get_handle_id(self._e) + if ret < 0: + return None + th = TraceHandle.__new__(TraceHandle) + th._id = ret + th._trace_collection = self.get_trace_collection() + return th - class Event(object): + @property + def trace_collection(self): """ - This class represents an event from the trace. - It is obtained with read_event() from CTFReader.Iterator. - Do not instantiate. + Get the TraceCollection associated with this event. + Return None on error. """ + trace_collection = TraceCollection() + trace_collection._tc = _bt_ctf_event_get_context(self._e); + if trace_collection._tc is None: + return None + else: + return trace_collection + + def __getitem__(self, field_name): + """ + Get field_name's value. If the field_name exists in multiple + scopes, the first field found is returned. The scopes are searched + in the following order: + 1) EVENT_FIELDS + 2) EVENT_CONTEXT + 3) STREAM_EVENT_CONTEXT + 4) STREAM_EVENT_HEADER + 5) STREAM_PACKET_CONTEXT + 6) TRACE_PACKET_HEADER + None is returned if no field matches field_name. + + Use field_with_scope() to explicitly access fields in a given + scope. + """ + field = self._field(field_name) + if field is not None: + return field.value + raise KeyError(field_name) + + def __iter__(self): + for key in self.keys(): + yield key + + def __len__(self): + count = 0 + for scope in _scopes: + scope_ptr = _bt_ctf_get_top_level_scope(self._e, scope) + ret = _bt_python_field_listcaller(self._e, scope_ptr) + if isinstance(ret, list): + count += ret[1] + return count + + def __contains__(self, field_name): + return self._field(field_name) is not None + + def keys(self): + """Return a list of field names.""" + field_names = set() + for scope in _scopes: + for name in self.field_list_with_scope(scope): + field_names.add(name) + return list(field_names) + + def get(self, field_name, default = None): + field = self._field(field_name) + if field is None: + return default + return field.value + + def items(self): + for field in self.keys(): + yield (field, self[field]) + + def _field_with_scope(self, field_name, scope): + scope_ptr = _bt_ctf_get_top_level_scope(self._e, scope) + if scope_ptr is None: + return None - def __init__(self): - raise NotImplementedError("CTFReader.Event cannot be instantiated") - - def get_top_level_scope(self, scope): - """ - Return a definition of the top-level scope - Top-level scopes are defined in CTFReader.scope. - In order to get a field or a field list, the user needs to pass a - scope as argument, this scope can be a top-level scope or a scope - relative to an arbitrary field. This function provides the mapping - between the scope and the actual definition of top-level scopes. - On error return None. - """ - evDef = CTFReader.Definition.__new__(CTFReader.Definition) - evDef._d = _bt_ctf_get_top_level_scope(self._e, scope) - if evDef._d is None: - return None - return evDef - - def get_name(self): - """Return the name of the event or None on error.""" - return _bt_ctf_event_name(self._e) - - def get_cycles(self): - """ - Return the timestamp of the event as written in - the packet (in cycles) or -1ULL on error. - """ - return _bt_ctf_get_cycles(self._e) - - def get_timestamp(self): - """ - Return the timestamp of the event offsetted with the - system clock source or -1ULL on error. - """ - return _bt_ctf_get_timestamp(self._e) - - def get_field_with_scope(self, scope, field): - """ - Return the definition of a specific field. - Return None on error. - """ - evDef = CTFReader.Definition.__new__(CTFReader.Definition) - try: - evDef._d = _bt_ctf_get_field(self._e, scope._d, field) - except AttributeError: - raise TypeError("in get_field, argument 2 must be a " - "Definition (scope) instance") - if evDef._d is None: - return None - evDef._s = scope - return evDef - - def get_field(self, field): - """ - Return the definition of fields by a name - Return None on error - """ - eventScope = self.get_top_level_scope(CTFReader.scope.EVENT_FIELDS) - streamScope = self.get_top_level_scope(CTFReader.scope.STREAM_EVENT_CONTEXT) - fields_by_name = [] - - if eventScope is not None: - evDef = self.get_field_with_scope(eventScope, field) - if evDef is not None: - fields_by_name.append(evDef) - - if streamScope is not None: - evDef = self.get_field_with_scope(streamScope, field) - if evDef is not None: - fields_by_name.append(evDef); - - if not fields_by_name: - return None - return fields_by_name - - def get_field_list_with_scope(self, scope): - """ - Return a list of Definitions associated with the scope - Return None on error. - """ - try: - field_lc, count = _bt_python_field_listcaller(self._e, scope._d) - except AttributeError: - raise TypeError("in get_field_list, argument 2 must be a " - "Definition (scope) instance") - - if field_lc is None: - return None - - def_list = [] - for i in range(count): - tmp = CTFReader.Definition.__new__(CTFReader.Definition) - tmp._d = _bt_python_field_one_from_list(field_lc, i) - tmp._s = scope - def_list.append(tmp) - - return def_list - - def get_field_list(self): - """Return a list of Definitions or None on error.""" - eventScope = self.get_top_level_scope(CTFReader.scope.EVENT_FIELDS) - streamScope = self.get_top_level_scope(CTFReader.scope.STREAM_EVENT_CONTEXT) - - def_list = [] - if eventScope is not None: - event_field_list = self.get_field_list_with_scope(eventScope) - if event_field_list is not None: - def_list = event_field_list - - if streamScope is not None: - event_field_list = self.get_field_list_with_scope(streamScope) - if event_field_list is not None: - def_list.extend(event_field_list) - - if not def_list: - return None - return def_list - - def get_index(self, field, index): - """ - If the field is an array or a sequence, return the element - at position index, otherwise return None - """ - evDef = CTFReader.Definition.__new__(CTFReader.Definition) - try: - evDef._d = _bt_ctf_get_index(self._e, field._d, index) - except AttributeError: - raise TypeError("in get_index, argument 2 must be a " - "Definition (field) instance") - - if evDef._d is None: - return None - return evDef - - def get_handle(self): - """ - Get the TraceHandle associated with an event - Return None on error - """ - ret = _bt_ctf_event_get_handle_id(self._e) - if ret < 0: - return None - - th = TraceHandle.__new__(TraceHandle) - th._id = ret - return th - - def get_context(self): - """ - Get the context associated with an event. - Return None on error. - """ - ctx = Context() - ctx._c = _bt_ctf_event_get_context(self._e); - if ctx._c is None: - return None - else: - return ctx + definition_ptr = _bt_ctf_get_field(self._e, scope_ptr, field_name) + if definition_ptr is None: + return None - class FieldError(Exception): - def __init__(self, value): - self.value = value + field = _Definition(definition_ptr, scope) + return field - def __str__(self): - return repr(self.value) + def _field(self, field_name): + field = None + for scope in _scopes: + field = self._field_with_scope(field_name, scope) + if field is not None: + break + return field + + def _field_list_with_scope(self, scope): + fields = [] + scope_ptr = _bt_ctf_get_top_level_scope(self._e, scope) + + # Returns a list [list_ptr, count]. If list_ptr is NULL, SWIG will only + # provide the "count" return value + count = 0 + list_ptr = None + ret = _bt_python_field_listcaller(self._e, scope_ptr) + if isinstance(ret, list): + list_ptr, count = ret - class Definition(object): - """Definition class. Do not instantiate.""" + for i in range(count): + definition_ptr = _bt_python_field_one_from_list(list_ptr, i) + if definition_ptr is not None: + definition = _Definition(definition_ptr, scope) + fields.append(definition) + return fields - def __init__(self): - raise NotImplementedError("CTFReader.Definition cannot be instantiated") - - def __repr__(self): - return "Babeltrace Definition: name('{0}'), type({1})".format( - self.field_name(), self.field_type()) - - def field_name(self): - """Return the name of a field or None on error.""" - return _bt_ctf_field_name(self._d) - - def field_type(self): - """Return the type of a field or -1 if unknown.""" - return _bt_ctf_field_type(_bt_ctf_get_decl_from_def(self._d)) - - def get_int_signedness(self): - """ - Return the signedness of an integer: - 0 if unsigned; 1 if signed; -1 on error. - """ - return _bt_ctf_get_int_signedness(_bt_ctf_get_decl_from_def(self._d)) - - def get_int_base(self): - """Return the base of an int or a negative value on error.""" - return _bt_ctf_get_int_base(_bt_ctf_get_decl_from_def(self._d)) - - def get_int_byte_order(self): - """ - Return the byte order of an int or a negative - value on error. - """ - return _bt_ctf_get_int_byte_order(_bt_ctf_get_decl_from_def(self._d)) - - def get_int_len(self): - """ - Return the size, in bits, of an int or a negative - value on error. - """ - return _bt_ctf_get_int_len(_bt_ctf_get_decl_from_def(self._d)) - - def get_enum_str(self): - """ - Return the string matching the current enumeration. - Return None on error. - """ - return _bt_ctf_get_enum_str(self._d) - - def get_encoding(self): - """ - Return the encoding of an int or a string. - Return a negative value on error. - """ - return _bt_ctf_get_encoding(_bt_ctf_get_decl_from_def(self._d)) - - def get_array_len(self): - """ - Return the len of an array or a negative - value on error. - """ - return _bt_ctf_get_array_len(_bt_ctf_get_decl_from_def(self._d)) - - def get_array_element_at(self, index): - """ - Return the array's element at position index. - Return None on error - """ - array = _bt_python_get_array_from_def(self._d) - if array is None: - return None - - element = CTFReader.Definition.__new__(CTFReader.Definition) - element._d = _bt_array_index(array, index) - if element._d is None: - return None - return element - - def get_sequence_len(self): - """ - Return the len of a sequence or a negative - value on error. - """ - seq = _bt_python_get_sequence_from_def(self._d) - return _bt_sequence_len(seq) - - def get_sequence_element_at(self, index): - """ - Return the sequence's element at position index, - otherwise return None - """ - seq = _bt_python_get_sequence_from_def(self._d) - if seq is not None: - element = CTFReader.Definition.__new__(CTFReader.Definition) - element._d = _bt_sequence_index(seq, index) - if element._d is not None: - return element - return None +class FieldError(Exception): + def __init__(self, value): + self.value = value - def get_uint64(self): - """ - Return the value associated with the field. - If the field does not exist or is not of the type requested, - the value returned is undefined. To check if an error occured, - use the CTFReader.field_error() function after accessing a field. - """ - return _bt_ctf_get_uint64(self._d) - - def get_int64(self): - """ - Return the value associated with the field. - If the field does not exist or is not of the type requested, - the value returned is undefined. To check if an error occured, - use the CTFReader.field_error() function after accessing a field. - """ - return _bt_ctf_get_int64(self._d) - - def get_char_array(self): - """ - Return the value associated with the field. - If the field does not exist or is not of the type requested, - the value returned is undefined. To check if an error occured, - use the CTFReader.field_error() function after accessing a field. - """ - return _bt_ctf_get_char_array(self._d) - - def get_str(self): - """ - Return the value associated with the field. - If the field does not exist or is not of the type requested, - the value returned is undefined. To check if an error occured, - use the CTFReader.field_error() function after accessing a field. - """ - return _bt_ctf_get_string(self._d) - - def get_float(self): - """ - Return the value associated with the field. - If the field does not exist or is not of the type requested, - the value returned is undefined. To check if an error occured, - use the CTFReader.field_error() function after accessing a field. - """ - return _bt_ctf_get_float(self._d) - - def get_variant(self): - """ - Return the variant's selected field. - If the field does not exist or is not of the type requested, - the value returned is undefined. To check if an error occured, - use the CTFReader.field_error() function after accessing a field. - """ - return _bt_ctf_get_variant(self._d) - - def get_struct_field_count(self): - """ - Return the number of fields contained in the structure. - If the field does not exist or is not of the type requested, - the value returned is undefined. - """ - return _bt_ctf_get_struct_field_count(self._d) - - def get_struct_field_at(self, i): - """ - Return the structure's field at position i. - If the field does not exist or is not of the type requested, - the value returned is undefined. To check if an error occured, - use the CTFReader.field_error() function after accessing a field. - """ - return _bt_ctf_get_struct_field_index(self._d, i) - - def get_value(self): - """ - Return the value associated with the field according to its type. - Return None on error. - """ - id = self.field_type() - value = None - if id == CTFTypeId.STRING: - value = self.get_str() - elif id == CTFTypeId.ARRAY: - value = [] - for i in range(self.get_array_len()): - element = self.get_array_element_at(i) - value.append(element.get_value()) - elif id == CTFTypeId.INTEGER: - if self.get_int_signedness() == 0: - value = self.get_uint64() - else: - value = self.get_int64() - elif id == CTFTypeId.ENUM: - value = self.get_enum_str() - elif id == CTFTypeId.SEQUENCE: - seq_len = self.get_sequence_len() - value = [] - for i in range(seq_len): - evDef = self.get_sequence_element_at(i) - value.append(evDef.get_value()) - elif id == CTFTypeId.FLOAT: - value = self.get_float() - elif id == CTFTypeId.VARIANT: - variant = CTFReader.Definition.__new__(CTFReader.Definition) - variant._d = self.get_variant(); - value = variant.get_value() - elif id == CTFTypeId.STRUCT: - value = {} - for i in range(self.get_struct_field_count()): - member = CTFReader.Definition.__new__(CTFReader.Definition) - member._d = self.get_struct_field_at(i); - value[member.field_name()] = member.get_value() - - if CTFReader.field_error(): - raise CTFReader.FieldError("Error occured while accessing field {} of type {}".format(self.field_name(), CTFTypeId.get_type_name(self.field_type()))) - return value - - def get_scope(self): - """Return the scope of a field or None on error.""" - return self._s - - class EventDecl(object): - """Event declaration class. Do not instantiate.""" + def __str__(self): + return repr(self.value) - def __init__(self): - raise NotImplementedError("CTFReader.EventDecl cannot be instantiated") +class EventDeclaration(object): + """Event declaration class. Do not instantiate.""" + + def __init__(self): + raise NotImplementedError("EventDeclaration cannot be instantiated") - def __repr__(self): - return "Babeltrace EventDecl: name {0}".format(self.get_name()) + @property + def name(self): + """Return the name of the event or None on error""" + return _bt_ctf_get_decl_event_name(self._ed) - def get_name(self): - """Return the name of the event or None on error""" - return _bt_ctf_get_decl_event_name(self._d) + def fields(self, scope): + """ + Return a list of FieldDeclaration + Return None on error. + """ + ret = _by_python_field_decl_listcaller(self._ed, scope) - def get_decl_fields(self, scope): - """ - Return a list of CTFReader.FieldDecl - Return None on error. - """ - ptr_list = _by_python_field_decl_listcaller(self._d, scope) + if not isinstance(ret, list): + return None - if ptr_list is None: - return None + list_ptr, count = ret + declarations = [] + for i in range(count): + declaration_ptr = _bt_python_field_decl_one_from_list(list_ptr, i) + if declaration_ptr is not None: + declaration = FieldDeclaration.__new__(FieldDeclaration) + declaration._fd = declaration_ptr + declarations.append(declaration) + return declarations + +class FieldDeclaration(object): + """Field declaration class. Do not instantiate.""" + def __init__(self): + raise NotImplementedError("FieldDeclaration cannot be instantiated") - decl_list = [] - i = 0 - while True: - tmp = CTFReader.FieldDecl.__new__(CTFReader.FieldDecl) - tmp._d = _bt_python_field_decl_one_from_list( - ptr_list, i) + @property + def name(self): + """Return the name of a FieldDeclaration or None on error""" + return _bt_ctf_get_decl_field_name(self._fd) - if tmp._d is None: - #Last item of list is None - break +def field_error(): + """ + Return the last error code encountered while + accessing a field and reset the error flag. + Return 0 if no error, a negative value otherwise. + """ + return _bt_ctf_field_get_error() - decl_list.append(tmp) - i += 1 - return decl_list +def event_declaration_list(trace_handle, trace_collection): + """ + Return a list of EventDeclaration + Return None on error. + """ + try: + handle_id = trace_handle._id + except AttributeError: + raise TypeError("in get_event_decl_list, " + "argument 1 must be a TraceHandle instance") + try: + ptr_list, count = _bt_python_event_decl_listcaller(handle_id, trace_collection._tc) + except AttributeError: + raise TypeError("in get_event_decl_list, " + "argument 2 must be a TraceCollection instance") + if ptr_list is None: + return None - class FieldDecl(object): - """Field declaration class. Do not instantiate.""" + decl_list = [] + for i in range(count): + tmp = EventDeclaration.__new__(EventDeclaration) + tmp._ed = _bt_python_decl_one_from_list(ptr_list, i) + decl_list.append(tmp) - def __init__(self): - raise NotImplementedError("CTFReader.FieldDecl cannot be instantiated") + return decl_list - def __repr__(self): - return "Babeltrace FieldDecl: name {0}".format(self.get_name()) +class _Definition(object): + def __init__(self, definition_ptr, scope): + self._d = definition_ptr + self._s = scope + if not scope in _scopes: + ValueError("Invalid scope provided") - def get_name(self): - """Return the name of a FieldDecl or None on error""" - return _bt_ctf_get_decl_field_name(self._d) + def __repr__(self): + return "Babeltrace Definition: name('{0}'), type({1})".format(self.name, self.type) + @property + def name(self): + """Return the name of a field or None on error.""" + return _bt_ctf_field_name(self._d) - @staticmethod - def field_error(): - """ - Return the last error code encountered while - accessing a field and reset the error flag. - Return 0 if no error, a negative value otherwise. - """ - return _bt_ctf_field_get_error() + @property + def type(self): + """Return the type of a field or -1 if unknown.""" + return _bt_ctf_field_type(_bt_ctf_get_decl_from_def(self._d)) - @staticmethod - def get_event_decl_list(trace_handle, context): + def get_int_signedness(self): """ - Return a list of CTFReader.EventDecl - Return None on error. + Return the signedness of an integer: + 0 if unsigned; 1 if signed; -1 on error. """ - try: - handle_id = trace_handle._id - except AttributeError: - raise TypeError("in get_event_decl_list, " - "argument 1 must be a TraceHandle instance") - try: - ptr_list, count = _bt_python_event_decl_listcaller(handle_id, context._c) - except AttributeError: - raise TypeError("in get_event_decl_list, " - "argument 2 must be a Context instance") + return _bt_ctf_get_int_signedness(_bt_ctf_get_decl_from_def(self._d)) - if ptr_list is None: - return None + def get_int_base(self): + """Return the base of an int or a negative value on error.""" + return _bt_ctf_get_int_base(_bt_ctf_get_decl_from_def(self._d)) - decl_list = [] - for i in range(count): - tmp = CTFReader.EventDecl.__new__(CTFReader.EventDecl) - tmp._d = _bt_python_decl_one_from_list(ptr_list, i) - decl_list.append(tmp) + def get_int_byte_order(self): + """ + Return the byte order of an int or a negative + value on error. + """ + return _bt_ctf_get_int_byte_order(_bt_ctf_get_decl_from_def(self._d)) - return decl_list + def get_int_len(self): + """ + Return the size, in bits, of an int or a negative + value on error. + """ + return _bt_ctf_get_int_len(_bt_ctf_get_decl_from_def(self._d)) -%} + def get_enum_str(self): + """ + Return the string matching the current enumeration. + Return None on error. + """ + return _bt_ctf_get_enum_str(self._d) + def get_encoding(self): + """ + Return the encoding of an int or a string. + Return a negative value on error. + """ + return _bt_ctf_get_encoding(_bt_ctf_get_decl_from_def(self._d)) + def get_array_len(self): + """ + Return the len of an array or a negative + value on error. + """ + return _bt_ctf_get_array_len(_bt_ctf_get_decl_from_def(self._d)) -// ================================================================= -// NEW FUNCTIONS -// File and list-related -// python-complements.h -// ================================================================= + def get_array_element_at(self, index): + """ + Return the array's element at position index. + Return None on error + """ + array_ptr = _bt_python_get_array_from_def(self._d) + if array_ptr is None: + return None -%pythoncode %{ + definition_ptr = _bt_array_index(array_ptr, index) + if definition_ptr is None: + return None + return _Definition(definition_ptr, self.scope) -class File(object): - """ - Open a file for babeltrace. + def get_sequence_len(self): + """ + Return the len of a sequence or a negative + value on error. + """ + seq = _bt_python_get_sequence_from_def(self._d) + return _bt_sequence_len(seq) - file_path is a string containing the path or None to use the - standard output in writing mode. + def get_sequence_element_at(self, index): + """ + Return the sequence's element at position index, + otherwise return None + """ + seq = _bt_python_get_sequence_from_def(self._d) + if seq is not None: + definition_ptr = _bt_sequence_index(seq, index) + if definition_ptr is not None: + return _Definition(definition_ptr, self.scope) + return None - The mode can be 'r', 'w' or 'a' for reading (default), writing or - appending. The file will be created if it doesn't exist when - opened for writing or appending; it will be truncated when opened - for writing. Add a 'b' to the mode for binary files. Add a '+' - to the mode to allow simultaneous reading and writing. - """ + def get_uint64(self): + """ + Return the value associated with the field. + If the field does not exist or is not of the type requested, + the value returned is undefined. To check if an error occured, + use the field_error() function after accessing a field. + """ + return _bt_ctf_get_uint64(self._d) - def __new__(cls, file_path, mode='r'): - # __new__ is used to control the return value - # as the File class should return None - # if _bt_file_open returns NULL + def get_int64(self): + """ + Return the value associated with the field. + If the field does not exist or is not of the type requested, + the value returned is undefined. To check if an error occured, + use the field_error() function after accessing a field. + """ + return _bt_ctf_get_int64(self._d) - # Type check - if file_path is not None and type(file_path) is not str: - raise TypeError("in method __init__, argument 2 of type 'str'") - if type(mode) is not str: - raise TypeError("in method __init__, argument 3 of type 'str'") + def get_char_array(self): + """ + Return the value associated with the field. + If the field does not exist or is not of the type requested, + the value returned is undefined. To check if an error occurred, + use the field_error() function after accessing a field. + """ + return _bt_ctf_get_char_array(self._d) - # Opening file - file_ptr = _bt_file_open(file_path, mode) - if file_ptr is None: - return None + def get_str(self): + """ + Return the value associated with the field. + If the field does not exist or is not of the type requested, + the value returned is undefined. To check if an error occurred, + use the field_error() function after accessing a field. + """ + return _bt_ctf_get_string(self._d) - # Class instantiation - file_inst = super(File, cls).__new__(cls) - file_inst._file = file_ptr - return file_inst + def get_float(self): + """ + Return the value associated with the field. + If the field does not exist or is not of the type requested, + the value returned is undefined. To check if an error occurred, + use the field_error() function after accessing a field. + """ + return _bt_ctf_get_float(self._d) - def __init__(self, file_path, mode='r'): - self._opened = True - self._use_stdout = False + def get_variant(self): + """ + Return the variant's selected field. + If the field does not exist or is not of the type requested, + the value returned is undefined. To check if an error occurred, + use the field_error() function after accessing a field. + """ + return _bt_ctf_get_variant(self._d) - if file_path is None: - # use stdout - file_path = "stdout" - mode = 'w' - self._use_stdout = True + def get_struct_field_count(self): + """ + Return the number of fields contained in the structure. + If the field does not exist or is not of the type requested, + the value returned is undefined. + """ + return _bt_ctf_get_struct_field_count(self._d) - self._file_path = file_path - self._mode = mode + def get_struct_field_at(self, i): + """ + Return the structure's field at position i. + If the field does not exist or is not of the type requested, + the value returned is undefined. To check if an error occurred, + use the field_error() function after accessing a field. + """ + return _bt_ctf_get_struct_field_index(self._d, i) - def __del__(self): - self.close() + @property + def value(self): + """ + Return the value associated with the field according to its type. + Return None on error. + """ + id = self.type + value = None + if id == CTFTypeId.STRING: + value = self.get_str() + elif id == CTFTypeId.ARRAY: + value = [] + for i in range(self.get_array_len()): + element = self.get_array_element_at(i) + value.append(element.value) + elif id == CTFTypeId.INTEGER: + if self.get_int_signedness() == 0: + value = self.get_uint64() + else: + value = self.get_int64() + elif id == CTFTypeId.ENUM: + value = self.get_enum_str() + elif id == CTFTypeId.SEQUENCE: + seq_len = self.get_sequence_len() + value = [] + for i in range(seq_len): + evDef = self.get_sequence_element_at(i) + value.append(evDef.value) + elif id == CTFTypeId.FLOAT: + value = self.get_float() + elif id == CTFTypeId.VARIANT: + variant = Definition.__new__(Definition) + variant._d = self.get_variant(); + value = variant.value + elif id == CTFTypeId.STRUCT: + value = {} + for i in range(self.get_struct_field_count()): + member = _Definition(self.get_struct_field_at(i), self.scope) + value[member.name] = member.value + + if field_error(): + raise FieldError("Error occurred while accessing field {} of type {}".format(self.field_name(), CTFTypeId.get_type_name(self.field_type()))) + return value + + @property + def scope(self): + """Return the scope of a field or None on error.""" + return self._s - def __repr__(self): - if self._opened: - stat = 'opened' - else: - stat = 'closed' - return "{0} babeltrace File; file_path('{1}'), mode('{2}')".format( - stat, self._file_path, self._mode) - - def close(self): - """Close the file. Is also called using del.""" - if self._opened and not self._use_stdout: - _bt_file_close(self._file) - self._opened = False %} + // ================================================================= // CTF Writer // =================================================================