Python bindings: cleanup whitespace
[babeltrace.git] / bindings / python / babeltrace.i.in
index b9bba79e6c29e165d9e408767168f3d0d31e4b04..724ed45d7e5d76351cdf905ca75e2690b57c9253 100644 (file)
@@ -33,6 +33,7 @@ trace to it."
 
 %module(docstring=DOCSTRING) babeltrace
 
+%include "stdint.i"
 %include "typemaps.i"
 %{
 #define SWIG_FILE_WITH_INIT
@@ -57,8 +58,6 @@ trace to it."
 #include <babeltrace/ctf-writer/writer.h>
 %}
 
-typedef unsigned long long uint64_t;
-typedef long long int64_t;
 typedef int bt_intern_str;
 typedef int64_t ssize_t;
 
@@ -111,6 +110,10 @@ const char *_bt_python_ctf_field_type_variant_get_field_name(
                struct bt_ctf_field_type *variant, size_t index);
 struct bt_ctf_field_type *_bt_python_ctf_field_type_variant_get_field_type(
                struct bt_ctf_field_type *variant, size_t index);
+const char *_bt_python_ctf_event_class_get_field_name(
+               struct bt_ctf_event_class *event_class, size_t index);
+struct bt_ctf_field_type *_bt_python_ctf_event_class_get_field_type(
+               struct bt_ctf_event_class *event_class, size_t index);
 
 /* =================================================================
                CONTEXT.H, CONTEXT-INTERNAL.H
@@ -223,6 +226,17 @@ class TraceCollection:
                """
                Generator function to iterate over the events of open in the current
                TraceCollection.
+
+               Due to limitations of the native Babeltrace API, only one event
+               may be "alive" at a time (i.e. a user should never store a copy
+               of the events returned by this function for ulterior use). Users
+               shall make sure to copy the information they need from an event
+               before accessing the next one.
+
+               Furthermore, event objects become invalid when the generator goes
+               out of scope as the underlying iterator will be reclaimed. Using an
+               event after the the generator has gone out of scope may result in a
+               crash or data corruption.
                """
                begin_pos_ptr = _bt_iter_pos()
                end_pos_ptr = _bt_iter_pos()
@@ -639,6 +653,7 @@ _scopes = [CTFScope.EVENT_FIELDS, CTFScope.EVENT_CONTEXT, CTFScope.STREAM_EVENT_
        CTFScope.STREAM_EVENT_HEADER, CTFScope.STREAM_PACKET_CONTEXT, CTFScope.TRACE_PACKET_HEADER]
 
 import collections
+from datetime import datetime
 class Event(collections.Mapping):
        """
        This class represents an event from the trace.
@@ -669,6 +684,15 @@ class Event(collections.Mapping):
                """
                return _bt_ctf_get_timestamp(self._e)
 
+       @property
+       def datetime(self):
+               """
+               Return a datetime object based on the event's
+               timestamp. Note that the datetime class' precision
+               is limited to microseconds.
+               """
+               return datetime.fromtimestamp(self.timestamp / 1E9)
+
        def field_with_scope(self, field_name, scope):
                """
                Get field_name's value in scope.
@@ -796,7 +820,7 @@ class Event(collections.Mapping):
        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
@@ -1455,22 +1479,42 @@ void bt_ctf_field_put(struct bt_ctf_field *field);
                ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 */
 %rename("_bt_ctf_event_class_create") bt_ctf_event_class_create(const char *name);
+%rename("_bt_ctf_event_class_get_name") bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class);
+%rename("_bt_ctf_event_class_get_id") bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class);
+%rename("_bt_ctf_event_class_set_id") bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class, uint32_t id);
+%rename("_bt_ctf_event_class_get_stream_class") bt_ctf_event_class_get_stream_class(struct bt_ctf_event_class *event_class);
 %rename("_bt_ctf_event_class_add_field") bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class, struct bt_ctf_field_type *type, const char *name);
+%rename("_bt_ctf_event_class_get_field_count") bt_ctf_event_class_get_field_count(struct bt_ctf_event_class *event_class);
+%rename("_bt_ctf_event_class_get_field") bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class, const char **field_name, struct bt_ctf_field_type **field_type, size_t index);
+%rename("_bt_ctf_event_class_get_field_by_name") bt_ctf_event_class_get_field_by_name(struct bt_ctf_event_class *event_class, const char *name);
 %rename("_bt_ctf_event_class_get") bt_ctf_event_class_get(struct bt_ctf_event_class *event_class);
 %rename("_bt_ctf_event_class_put") bt_ctf_event_class_put(struct bt_ctf_event_class *event_class);
 %rename("_bt_ctf_event_create") bt_ctf_event_create(struct bt_ctf_event_class *event_class);
-%rename("_bt_ctf_event_set_payload") bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, struct bt_ctf_field *value);
+%rename("_bt_ctf_event_get_class") bt_ctf_event_get_class(struct bt_ctf_event *event);
+%rename("_bt_ctf_event_get_clock") bt_ctf_event_get_clock(struct bt_ctf_event *event);
 %rename("_bt_ctf_event_get_payload") bt_ctf_event_get_payload(struct bt_ctf_event *event, const char *name);
+%rename("_bt_ctf_event_set_payload") bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, struct bt_ctf_field *value);
+%rename("_bt_ctf_event_get_payload_by_index") bt_ctf_event_get_payload_by_index(struct bt_ctf_event *event, size_t index);
 %rename("_bt_ctf_event_get") bt_ctf_event_get(struct bt_ctf_event *event);
 %rename("_bt_ctf_event_put") bt_ctf_event_put(struct bt_ctf_event *event);
 
 struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name);
+const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class);
+int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class);
+int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class, uint32_t id);
+struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class(struct bt_ctf_event_class *event_class);
 int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class, struct bt_ctf_field_type *type, const char *name);
+int64_t bt_ctf_event_class_get_field_count(struct bt_ctf_event_class *event_class);
+int bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class, const char **field_name, struct bt_ctf_field_type **field_type, size_t index);
+struct bt_ctf_field_type *bt_ctf_event_class_get_field_by_name(struct bt_ctf_event_class *event_class, const char *name);
 void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class);
 void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class);
 struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class);
-int bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, struct bt_ctf_field *value);
+struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event);
+struct bt_ctf_clock *bt_ctf_event_get_clock(struct bt_ctf_event *event);
 struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, const char *name);
+int bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, struct bt_ctf_field *value);
+struct bt_ctf_field *bt_ctf_event_get_payload_by_index(struct bt_ctf_event *event, size_t index);
 void bt_ctf_event_get(struct bt_ctf_event *event);
 void bt_ctf_event_put(struct bt_ctf_event *event);
 
@@ -1479,10 +1523,18 @@ void bt_ctf_event_put(struct bt_ctf_event *event);
                ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 */
 %rename("_bt_ctf_stream_class_create") bt_ctf_stream_class_create(const char *name);
+%rename("_bt_ctf_stream_class_get_name") bt_ctf_stream_class_get_name(struct bt_ctf_stream_class *stream_class);
+%rename("_bt_ctf_stream_class_get_clock") bt_ctf_stream_class_get_clock(struct bt_ctf_stream_class *stream_class);
 %rename("_bt_ctf_stream_class_set_clock") bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, struct bt_ctf_clock *clock);
+%rename("_bt_ctf_stream_class_get_id") bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class);
+%rename("_bt_ctf_stream_class_set_id") bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, uint32_t id);
 %rename("_bt_ctf_stream_class_add_event_class") bt_ctf_stream_class_add_event_class(struct bt_ctf_stream_class *stream_class, struct bt_ctf_event_class *event_class);
+%rename("_bt_ctf_stream_class_get_event_class_count") bt_ctf_stream_class_get_event_class_count(struct bt_ctf_stream_class *stream_class);
+%rename("_bt_ctf_stream_class_get_event_class") bt_ctf_stream_class_get_event_class(struct bt_ctf_stream_class *stream_class, size_t index);
+%rename("_bt_ctf_stream_class_get_event_class_by_name") bt_ctf_stream_class_get_event_class_by_name(struct bt_ctf_stream_class *stream_class, const char *name);
 %rename("_bt_ctf_stream_class_get") bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class);
 %rename("_bt_ctf_stream_class_put") bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class);
+%rename("_bt_ctf_stream_get_discarded_events_count") bt_ctf_stream_get_discarded_events_count(struct bt_ctf_stream *stream, uint64_t *count);
 %rename("_bt_ctf_stream_append_discarded_events") bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, uint64_t event_count);
 %rename("_bt_ctf_stream_append_event") bt_ctf_stream_append_event(struct bt_ctf_stream *stream, struct bt_ctf_event *event);
 %rename("_bt_ctf_stream_flush") bt_ctf_stream_flush(struct bt_ctf_stream *stream);
@@ -1490,10 +1542,18 @@ void bt_ctf_event_put(struct bt_ctf_event *event);
 %rename("_bt_ctf_stream_put") bt_ctf_stream_put(struct bt_ctf_stream *stream);
 
 struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name);
+const char *bt_ctf_stream_class_get_name(struct bt_ctf_stream_class *stream_class);
+struct bt_ctf_clock *bt_ctf_stream_class_get_clock(struct bt_ctf_stream_class *stream_class);
 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, struct bt_ctf_clock *clock);
+int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class);
+int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, uint32_t id);
 int bt_ctf_stream_class_add_event_class(struct bt_ctf_stream_class *stream_class, struct bt_ctf_event_class *event_class);
+int64_t bt_ctf_stream_class_get_event_class_count(struct bt_ctf_stream_class *stream_class);
+struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(struct bt_ctf_stream_class *stream_class, size_t index);
+struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(struct bt_ctf_stream_class *stream_class, const char *name);
 void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class);
 void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class);
+int bt_ctf_stream_get_discarded_events_count(struct bt_ctf_stream *stream, uint64_t *OUTPUT);
 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, uint64_t event_count);
 int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, struct bt_ctf_event *event);
 int bt_ctf_stream_flush(struct bt_ctf_stream *stream);
@@ -2466,6 +2526,78 @@ class CTFWriter:
                        if ret < 0:
                                raise ValueError("Could not add field to event class.")
 
+               """
+               Get the event class' name.
+               """
+               @property
+               def name(self):
+                       name = _bt_ctf_event_class_get_name(self._ec)
+                       if name is None:
+                               raise TypeError("Could not get EventClass name")
+                       return name
+
+               """
+               Get the event class' id. Returns a negative value if unset.
+               """
+               @property
+               def id(self):
+                       id = _bt_ctf_event_class_get_id(self._ec)
+                       if id < 0:
+                               raise TypeError("Could not get EventClass id")
+                       return id
+
+               """
+               Set the event class' id. Throws a TypeError if the event class
+               is already registered to a stream class.
+               """
+               @id.setter
+               def id(self, id):
+                       ret = _bt_ctf_event_class_set_id(self._ec, id)
+                       if ret < 0:
+                               raise TypeError("Can't change an Event Class's id after it has been assigned to a stream class")
+
+               """
+               Get the event class' stream class. Returns None if unset.
+               """
+               @property
+               def stream_class(self):
+                       stream_class_native = _bt_ctf_event_class_get_stream_class(self._ec)
+                       if stream_class_native is None:
+                               return None
+                       stream_class = CTFWriter.StreamClass.__new__(CTFWriter.StreamClass)
+                       stream_class._sc = stream_class_native
+                       return stream_class
+
+               """
+               Generator returning the event class' fields as tuples of (field name, field declaration).
+               """
+               @property
+               def fields(self):
+                       count = _bt_ctf_event_class_get_field_count(self._ec)
+                       if count < 0:
+                               raise TypeError("Could not get EventClass' field count")
+
+                       for i in range(count):
+                               field_name = _bt_python_ctf_event_class_get_field_name(self._ec, i)
+                               if field_name is None:
+                                       raise TypeError("Could not get EventClass' field name at index {}".format(i))
+
+                               field_type_native = _bt_python_ctf_event_class_get_field_type(self._ec, i)
+                               if field_type_native is None:
+                                       raise TypeError("Could not get EventClass' field type at index {}".format(i))
+
+                               field_type = CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
+                               yield (field_name, field_type)
+
+               """
+               Get a field declaration by name (FieldDeclaration).
+               """
+               def get_field_by_name(self, name):
+                       field_type_native = _bt_ctf_event_class_get_field_by_name(self._ec, name)
+                       if field_type_native is None:
+                               raise TypeError("Could not find EventClass field with name {}".format(name))
+                       return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
+
        class Event:
                """
                Create a new event of the given event class.
@@ -2482,14 +2614,28 @@ class CTFWriter:
                        _bt_ctf_event_put(self._e)
 
                """
-               Set a manually created field as an event's payload.
+               Get the event's class.
                """
-               def set_payload(self, field_name, value_field):
-                       if not isinstance(value, CTFWriter.Field):
-                               raise TypeError("Invalid value type.")
-                       ret = _bt_ctf_event_set_payload(self._e, str(field_name), value_field._f)
-                       if ret < 0:
-                               raise ValueError("Could not set event field payload.")
+               @property
+               def event_class(self):
+                       event_class_native = _bt_ctf_event_get_class(self._e)
+                       if event_class_native is None:
+                               return None
+                       event_class = CTFWriter.EventClass.__new__(CTFWriter.EventClass)
+                       event_class._ec = event_class_native
+                       return event_class
+
+               """
+               Get a clock from event. Returns None if the event's class
+               is not registered to a stream class.
+               """
+               def clock(self):
+                       clock_instance = _bt_ctf_event_get_clock(self._e)
+                       if clock_instance is None:
+                               return None
+                       clock = CTFWriter.Clock.__new__(CTFWriter.Clock)
+                       clock._c = clock_instance
+                       return clock
 
                """
                Get a field from event.
@@ -2500,6 +2646,16 @@ class CTFWriter:
                                raise ValueError("Could not get event payload.")
                        return CTFWriter.Field._create_field_from_native_instance(native_instance)
 
+               """
+               Set a manually created field as an event's payload.
+               """
+               def set_payload(self, field_name, value_field):
+                       if not isinstance(value, CTFWriter.Field):
+                               raise TypeError("Invalid value type.")
+                       ret = _bt_ctf_event_set_payload(self._e, str(field_name), value_field._f)
+                       if ret < 0:
+                               raise ValueError("Could not set event field payload.")
+
        class StreamClass:
                """
                Create a new stream class of the given name.
@@ -2512,12 +2668,27 @@ class CTFWriter:
                def __del__(self):
                        _bt_ctf_stream_class_put(self._sc)
 
+               """
+               Get a stream class' name.
+               """
+               @property
+               def name(self):
+                       name = _bt_ctf_stream_class_get_name(self._sc)
+                       if name is None:
+                               raise TypeError("Could not get StreamClass name")
+                       return name
+
                """
                Get a stream class' clock.
                """
                @property
                def clock(self):
-                       raise NotImplementedError("Getter not implemented.")
+                       clock_instance = _bt_ctf_stream_class_get_clock(self._sc)
+                       if clock_instance is None:
+                               return None
+                       clock = CTFWriter.Clock.__new__(CTFWriter.Clock)
+                       clock._c = clock_instance
+                       return clock
 
                """
                Assign a clock to a stream class.
@@ -2531,6 +2702,43 @@ class CTFWriter:
                        if ret < 0:
                                raise ValueError("Could not set stream class clock.")
 
+               """
+               Get a stream class' id.
+               """
+               @property
+               def id(self):
+                       ret = _bt_ctf_stream_class_get_id(self._sc)
+                       if ret < 0:
+                               raise TypeError("Could not get StreamClass id")
+                       return ret
+
+               """
+               Assign an id to a stream class.
+               """
+               @id.setter
+               def id(self, id):
+                       ret = _bt_ctf_stream_class_set_id(self._sc, id)
+                       if ret < 0:
+                               raise TypeError("Could not set stream class id.")
+
+               """
+               Generator returning the stream class' event classes.
+               """
+               @property
+               def event_classes(self):
+                       count = _bt_ctf_stream_class_get_event_class_count(self._sc)
+                       if count < 0:
+                               raise TypeError("Could not get StreamClass' event class count")
+
+                       for i in range(count):
+                               event_class_native = _bt_ctf_stream_class_get_event_class(self._sc, i)
+                               if event_class_native is None:
+                                       raise TypeError("Could not get StreamClass' event class at index {}".format(i))
+
+                               event_class = CTFWriter.EventClass.__new__(CTFWriter.EventClass)
+                               event_class._ec = event_class_native
+                               yield event_class
+
                """
                Add an event class to a stream class. New events can be added even after a
                stream has been instantiated and events have been appended. However, a stream
@@ -2559,6 +2767,16 @@ class CTFWriter:
                def __del__(self):
                        _bt_ctf_stream_put(self._s)
 
+               """
+               Get a stream's discarded event count.
+               """
+               @property
+               def discarded_events(self):
+                       ret, count = _bt_ctf_stream_get_discarded_events_count(self._s)
+                       if ret < 0:
+                               raise ValueError("Could not get the stream's discarded events count")
+                       return count
+
                """
                Increase the current packet's discarded event count.
                """
This page took 0.028743 seconds and 4 git commands to generate.