Python: document writer.Stream
[babeltrace.git] / bindings / python / writer.py
index a1f83e9d7f55c7bad4bf01efe2c38ec1a74cd1d5..5be99c218bb326c82e55f503eaf6ab74122d89b0 100644 (file)
@@ -1235,10 +1235,17 @@ class IntegerField(Field):
 
 
 class EnumerationField(Field):
+    """
+    Enumeration field, based on an
+    :class:`EnumerationFieldDeclaration` object.
+    """
+
     @property
     def container(self):
         """
-        Return the enumeration's underlying container field (an integer field).
+        Underlying container (:class:`IntegerField`).
+
+        :exc:`TypeError` is raised on error.
         """
 
         container = IntegerField.__new__(IntegerField)
@@ -1252,23 +1259,23 @@ class EnumerationField(Field):
     @property
     def value(self):
         """
-        Get the enumeration field's mapping name.
+        Current label of this enumeration field (:class:`str`).
+
+        Set this attribute to an integer (:class:`int`) to change the
+        enumeration field's value.
+
+        :exc:`ValueError` is raised on error.
         """
 
         value = nbt._bt_ctf_field_enumeration_get_mapping_name(self._f)
 
         if value is None:
-            raise ValueError("Could not get enumeration's mapping name.")
+            raise ValueError("Could not get enumeration mapping name.")
 
         return value
 
     @value.setter
     def value(self, value):
-        """
-        Set the enumeration field's value. Must be an integer as mapping names
-        may be ambiguous.
-        """
-
         if not isinstance(value, int):
             raise TypeError("EnumerationField value must be an int")
 
@@ -1276,10 +1283,20 @@ class EnumerationField(Field):
 
 
 class FloatingPointField(Field):
+    """
+    Floating point number field, based on a
+    :class:`FloatingPointFieldDeclaration` object.
+    """
+
     @property
     def value(self):
         """
-        Get a floating point field's value.
+        Floating point number value (:class:`float`).
+
+        Set this attribute to change the floating point number field's
+        value.
+
+        :exc:`ValueError` or :exc:`TypeError` are raised on error.
         """
 
         ret, value = nbt._bt_ctf_field_floating_point_get_value(self._f)
@@ -1291,10 +1308,6 @@ class FloatingPointField(Field):
 
     @value.setter
     def value(self, value):
-        """
-        Set a floating point field's value.
-        """
-
         if not isinstance(value, int) and not isinstance(value, float):
             raise TypeError("Value must be either a float or an int")
 
@@ -1311,9 +1324,16 @@ class FloatFieldingPoint(FloatingPointField):
 
 
 class StructureField(Field):
+    """
+    Structure field, based on a
+    :class:`StructureFieldDeclaration` object.
+    """
+
     def field(self, field_name):
         """
-        Get the structure's field corresponding to the provided field name.
+        Returns the structure :class:`Field` named *field_name*.
+
+        :exc:`ValueError` is raised on error.
         """
 
         native_instance = nbt._bt_ctf_field_structure_get_field(self._f,
@@ -1326,9 +1346,17 @@ class StructureField(Field):
 
 
 class VariantField(Field):
+    """
+    Variant field, based on a
+    :class:`VariantFieldDeclaration` object.
+    """
+
     def field(self, tag):
         """
-        Return the variant's selected field. The "tag" field is the selector enum field.
+        Returns the :class:`Field` selected by the current label of
+        *tag* (:class:`EnumerationField`).
+
+        :exc:`ValueError` is raised on error.
         """
 
         native_instance = nbt._bt_ctf_field_variant_get_field(self._f, tag._f)
@@ -1340,9 +1368,17 @@ class VariantField(Field):
 
 
 class ArrayField(Field):
+    """
+    Static array field, based on an
+    :class:`ArrayFieldDeclaration` object.
+    """
+
     def field(self, index):
         """
-        Return the array's field at position "index".
+        Returns the :class:`Field` at index *index* in this static
+        array.
+
+        :exc:`IndexError` is raised on error.
         """
 
         native_instance = nbt._bt_ctf_field_array_get_field(self._f, index)
@@ -1354,10 +1390,20 @@ class ArrayField(Field):
 
 
 class SequenceField(Field):
+    """
+    Sequence (dynamic array) field, based on a
+    :class:`SequenceFieldDeclaration` object.
+    """
+
     @property
     def length(self):
         """
-        Get the sequence's length field (IntegerField).
+        Sequence length (:class:`IntegerField`).
+
+        Set this attribute to change the sequence length's integer
+        field (integer must be unsigned).
+
+        :exc:`ValueError` or :exc:`TypeError` are raised on error.
         """
 
         native_instance = nbt._bt_ctf_field_sequence_get_length(self._f)
@@ -1369,10 +1415,6 @@ class SequenceField(Field):
 
     @length.setter
     def length(self, length_field):
-        """
-        Set the sequence's length field (IntegerField).
-        """
-
         if not isinstance(length_field, IntegerField):
             raise TypeError("Invalid length field.")
 
@@ -1386,7 +1428,9 @@ class SequenceField(Field):
 
     def field(self, index):
         """
-        Return the sequence's field at position "index".
+        Returns the :class:`Field` at index *index* in this sequence.
+
+        :exc:`ValueError` is raised on error.
         """
 
         native_instance = nbt._bt_ctf_field_sequence_get_field(self._f, index)
@@ -1398,20 +1442,24 @@ class SequenceField(Field):
 
 
 class StringField(Field):
+    """
+    String (NULL-terminated array of bytes) field.
+    """
+
     @property
     def value(self):
         """
-        Get a string field's value.
+        String value (:class:`str`).
+
+        Set this attribute to change the string's value.
+
+        :exc:`ValueError` or :exc:`TypeError` are raised on error.
         """
 
         return nbt._bt_ctf_field_string_get_value(self._f)
 
     @value.setter
     def value(self, value):
-        """
-        Set a string field's value.
-        """
-
         ret = nbt._bt_ctf_field_string_set_value(self._f, str(value))
 
         if ret < 0:
@@ -1419,9 +1467,22 @@ class StringField(Field):
 
 
 class EventClass:
+    """
+    An event class contains the properties of specific
+    events (:class:`Event`). Any concrete event must be linked with an
+    :class:`EventClass`.
+
+    Some attributes are automatically set when creating an event class.
+    For example, if no numeric ID is explicitly set using the
+    :attr:`id` attribute, a default, unique ID within the stream class
+    containing this event class will be created when needed.
+    """
+
     def __init__(self, name):
         """
-        Create a new event class of the given name.
+        Creates an event class named *name*.
+
+        :exc:`ValueError` is raised on error.
         """
 
         self._ec = nbt._bt_ctf_event_class_create(name)
@@ -1434,7 +1495,21 @@ class EventClass:
 
     def add_field(self, field_type, field_name):
         """
-        Add a field of type "field_type" to the event class.
+        Adds a field declaration *field_type* named *field_name* to
+        this event class.
+
+        *field_type* must be one of:
+
+        * :class:`IntegerFieldDeclaration`
+        * :class:`FloatingPointFieldDeclaration`
+        * :class:`EnumerationFieldDeclaration`
+        * :class:`StringFieldDeclaration`
+        * :class:`ArrayFieldDeclaration`
+        * :class:`SequenceFieldDeclaration`
+        * :class:`StructureFieldDeclaration`
+        * :class:`VariantFieldDeclaration`
+
+        :exc:`ValueError` is raised on error.
         """
 
         ret = nbt._bt_ctf_event_class_add_field(self._ec, field_type._ft,
@@ -1446,7 +1521,7 @@ class EventClass:
     @property
     def name(self):
         """
-        Get the event class' name.
+        Event class' name.
         """
 
         name = nbt._bt_ctf_event_class_get_name(self._ec)
@@ -1459,7 +1534,13 @@ class EventClass:
     @property
     def id(self):
         """
-        Get the event class' id. Returns a negative value if unset.
+        Event class' numeric ID.
+
+        Set this attribute to assign a numeric ID to this event class.
+        This ID must be unique amongst all the event class IDs of a
+        given stream class.
+
+        :exc:`TypeError` is raised on error.
         """
 
         id = nbt._bt_ctf_event_class_get_id(self._ec)
@@ -1471,21 +1552,18 @@ class EventClass:
 
     @id.setter
     def id(self, id):
-        """
-        Set the event class' id. Throws a TypeError if the event class
-        is already registered to a stream class.
-        """
-
         ret = nbt._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")
+            raise TypeError("Can't change an Event Class id after it has been assigned to a stream class")
 
     @property
     def stream_class(self):
         """
-        Get the event class' stream class. Returns None if unset.
+        :class:`StreamClass` object containing this event class,
+        or ``None`` if not set.
         """
+
         stream_class_native = nbt._bt_ctf_event_class_get_stream_class(self._ec)
 
         if stream_class_native is None:
@@ -1499,7 +1577,10 @@ class EventClass:
     @property
     def fields(self):
         """
-        Generator returning the event class' fields as tuples of (field name, field declaration).
+        Generates the (field name, :class:`FieldDeclaration`) pairs of
+        this event class.
+
+        :exc:`TypeError` is raised on error.
         """
 
         count = nbt._bt_ctf_event_class_get_field_count(self._ec)
@@ -1525,7 +1606,10 @@ class EventClass:
 
     def get_field_by_name(self, name):
         """
-        Get a field declaration by name (FieldDeclaration).
+        Returns the :class:`FieldDeclaration` object named *name* in
+        this event class.
+
+        :exc:`TypeError` is raised on error.
         """
 
         field_type_native = nbt._bt_ctf_event_class_get_field_by_name(self._ec, name)
@@ -1538,9 +1622,18 @@ class EventClass:
 
 
 class Event:
+    """
+    Events are specific instances of event classes
+    (:class:`EventClass`), which means they may contain actual,
+    concrete field values.
+    """
+
     def __init__(self, event_class):
         """
-        Create a new event of the given event class.
+        Creates an event linked with the :class:`EventClass`
+        *event_class*.
+
+        :exc:`ValueError` is raised on error.
         """
 
         if not isinstance(event_class, EventClass):
@@ -1557,7 +1650,7 @@ class Event:
     @property
     def event_class(self):
         """
-        Get the event's class.
+        :class:`EventClass` object to which this event is linked.
         """
 
         event_class_native = nbt._bt_ctf_event_get_class(self._e)
@@ -1572,8 +1665,8 @@ class Event:
 
     def clock(self):
         """
-        Get a clock from event. Returns None if the event's class
-        is not registered to a stream class.
+        :class:`Clock` object used by this object, or ``None`` if
+        the event class is not registered to a stream class.
         """
 
         clock_instance = nbt._bt_ctf_event_get_clock(self._e)
@@ -1588,7 +1681,24 @@ class Event:
 
     def payload(self, field_name):
         """
-        Get a field from event.
+        Returns the :class:`Field` object named *field_name* in this
+        event.
+
+        The returned field object is created using the event class'
+        field declaration named *field_name*.
+
+        The return type is one of:
+
+        * :class:`IntegerField`
+        * :class:`FloatingPointField`
+        * :class:`EnumerationField`
+        * :class:`StringField`
+        * :class:`ArrayField`
+        * :class:`SequenceField`
+        * :class:`StructureField`
+        * :class:`VariantField`
+
+        :exc:`TypeError` is raised on error.
         """
 
         native_instance = nbt._bt_ctf_event_get_payload(self._e,
@@ -1601,7 +1711,21 @@ class Event:
 
     def set_payload(self, field_name, value_field):
         """
-        Set a manually created field as an event's payload.
+        Set the event's field named *field_name* to the manually
+        created :class:`Field` object *value_field*.
+
+        *value_field*'s type must be one of:
+
+        * :class:`IntegerField`
+        * :class:`FloatingPointField`
+        * :class:`EnumerationField`
+        * :class:`StringField`
+        * :class:`ArrayField`
+        * :class:`SequenceField`
+        * :class:`StructureField`
+        * :class:`VariantField`
+
+        :exc:`ValueError` is raised on error.
         """
 
         if not isinstance(value, Field):
@@ -1615,9 +1739,23 @@ class Event:
 
 
 class StreamClass:
+    """
+    A stream class contains the properties of specific
+    streams (:class:`Stream`). Any concrete stream must be linked with
+    a :class:`StreamClass`, usually by calling
+    :meth:`Writer.create_stream`.
+
+    Some attributes are automatically set when creating a stream class.
+    For example, if no clock is explicitly set using the
+    :attr:`clock` attribute, a default clock will be created
+    when needed.
+    """
+
     def __init__(self, name):
         """
-        Create a new stream class of the given name.
+        Creates a stream class named *name*.
+
+        :exc:`ValueError` is raised on error.
         """
 
         self._sc = nbt._bt_ctf_stream_class_create(name)
@@ -1631,7 +1769,9 @@ class StreamClass:
     @property
     def name(self):
         """
-        Get a stream class' name.
+        Stream class' name.
+
+        :exc:`TypeError` is raised on error.
         """
 
         name = nbt._bt_ctf_stream_class_get_name(self._sc)
@@ -1644,7 +1784,11 @@ class StreamClass:
     @property
     def clock(self):
         """
-        Get a stream class' clock.
+        Stream class' clock (:class:`Clock` object).
+
+        Set this attribute to change the clock of this stream class.
+
+        :exc:`ValueError` is raised on error.
         """
 
         clock_instance = nbt._bt_ctf_stream_class_get_clock(self._sc)
@@ -1659,10 +1803,6 @@ class StreamClass:
 
     @clock.setter
     def clock(self, clock):
-        """
-        Assign a clock to a stream class.
-        """
-
         if not isinstance(clock, Clock):
             raise TypeError("Invalid clock type.")
 
@@ -1674,7 +1814,11 @@ class StreamClass:
     @property
     def id(self):
         """
-        Get a stream class' id.
+        Stream class' numeric ID.
+
+        Set this attribute to change the ID of this stream class.
+
+        :exc:`ValueError` is raised on error.
         """
 
         ret = nbt._bt_ctf_stream_class_get_id(self._sc)
@@ -1686,10 +1830,6 @@ class StreamClass:
 
     @id.setter
     def id(self, id):
-        """
-        Assign an id to a stream class.
-        """
-
         ret = nbt._bt_ctf_stream_class_set_id(self._sc, id)
 
         if ret < 0:
@@ -1698,7 +1838,10 @@ class StreamClass:
     @property
     def event_classes(self):
         """
-        Generator returning the stream class' event classes.
+        Generates the event classes (:class:`EventClass` objects) of
+        this stream class.
+
+        :exc:`TypeError` is raised on error.
         """
 
         count = nbt._bt_ctf_stream_class_get_event_class_count(self._sc)
@@ -1719,10 +1862,13 @@ class StreamClass:
 
     def add_event_class(self, 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
-        will not accept events of a class that has not been added to the stream
-        class beforehand.
+        Registers the :class:`EventClass` *event_class* to this stream
+        class.
+
+        Once the event class is registered, it will be generated as one
+        of the event classes generated by :attr:`event_classes`.
+
+        :exc:`ValueError` is raised on error.
         """
 
         if not isinstance(event_class, EventClass):
@@ -1737,7 +1883,14 @@ class StreamClass:
     @property
     def packet_context_type(self):
         """
-        Get the StreamClass' packet context type (StructureFieldDeclaration)
+        Stream packet context declaration.
+
+        Set this attribute to change the stream packet context
+        declaration (must be an instance of
+        :class:`StructureFieldDeclaration`).
+
+        :exc:`ValueError` is raised on error.
+
         """
 
         field_type_native = nbt._bt_ctf_stream_class_get_packet_context_type(self._sc)
@@ -1751,11 +1904,6 @@ class StreamClass:
 
     @packet_context_type.setter
     def packet_context_type(self, field_type):
-        """
-        Set a StreamClass' packet context type. Must be of type
-        StructureFieldDeclaration.
-        """
-
         if not isinstance(field_type, StructureFieldDeclaration):
             raise TypeError("field_type argument must be of type StructureFieldDeclaration.")
 
@@ -1767,6 +1915,24 @@ class StreamClass:
 
 
 class Stream:
+    """
+    Streams are specific instances of stream classes, which means they
+    may contain actual, concrete events.
+
+    :class:`Stream` objects are returned by
+    :meth:`Writer.create_stream`; they are not meant to be
+    instantiated by the user.
+
+    Concrete :class:`Event` objects are appended to
+    :class:`Stream` objects using :meth:`append_event`.
+
+    When :meth:`flush` is called, a CTF packet is created, containing
+    all the appended events since the last flush. Although the stream
+    is flushed on object destruction, it is **strongly recommended**
+    that the user call :meth:`flush` manually before exiting the
+    script, as :meth:`__del__` is not always reliable.
+    """
+
     def __init__(self):
         raise NotImplementedError("Stream cannot be instantiated; use Writer.create_stream()")
 
@@ -1776,28 +1942,34 @@ class Stream:
     @property
     def discarded_events(self):
         """
-        Get a stream's discarded event count.
+        Number of discarded (lost) events in this stream so far.
+
+        :exc:`ValueError` is raised on error.
         """
 
         ret, count = nbt._bt_ctf_stream_get_discarded_events_count(self._s)
 
         if ret < 0:
-            raise ValueError("Could not get the stream's discarded events count")
+            raise ValueError("Could not get the stream discarded events count")
 
         return count
 
     def append_discarded_events(self, event_count):
         """
-        Increase the current packet's discarded event count.
+        Appends *event_count* discarded events to this stream.
         """
 
         nbt._bt_ctf_stream_append_discarded_events(self._s, event_count)
 
     def append_event(self, event):
         """
-        Append "event" to the stream's current packet. The stream's associated clock
-        will be sampled during this call. The event shall not be modified after
-        being appended to a stream.
+        Appends event *event* (:class:`Event` object) to this stream.
+
+        The stream's associated clock will be sampled during this call.
+        *event* **shall not** be modified after being appended to this
+        stream.
+
+        :exc:`ValueError` is raised on error.
         """
 
         ret = nbt._bt_ctf_stream_append_event(self._s, event._e)
@@ -1808,7 +1980,13 @@ class Stream:
     @property
     def packet_context(self):
         """
-        Get a Stream's packet context field (a StructureField).
+        Stream packet context field (instance of
+        :class:`StructureField`).
+
+        Set this attribute to assign a stream packet context field
+        to this stream.
+
+        :exc:`ValueError` is raised on error.
         """
 
         native_field = nbt._bt_ctf_stream_get_packet_context(self._s)
@@ -1820,10 +1998,6 @@ class Stream:
 
     @packet_context.setter
     def packet_context(self, field):
-        """
-        Set a Stream's packet context field (must be a StructureField).
-        """
-
         if not isinstance(field, StructureField):
             raise TypeError("Argument field must be of type StructureField")
 
@@ -1834,8 +2008,11 @@ class Stream:
 
     def flush(self):
         """
-        The stream's current packet's events will be flushed to disk. Events
-        subsequently appended to the stream will be added to a new packet.
+        Flushes the current packet of this stream to disk. Events
+        subsequently appended to the stream will be added to a new
+        packet.
+
+        :exc:`ValueError` is raised on error.
         """
 
         ret = nbt._bt_ctf_stream_flush(self._s)
This page took 0.030426 seconds and 4 git commands to generate.