doc/api/libbabeltrace2/DoxygenLayout.xml: use `topics` tab
[babeltrace.git] / src / bindings / python / bt2 / bt2 / graph.py
index 9d5ba3aad42810979f7053f356d6ff3506ed6c65..6bd4e1e7ee69ba8035344941b26420836ca153d8 100644 (file)
@@ -1,33 +1,20 @@
-# The MIT License (MIT)
+# SPDX-License-Identifier: MIT
 #
 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-
-from bt2 import native_bt, object, utils
-from bt2 import interrupter as bt2_interrupter
-from bt2 import connection as bt2_connection
-from bt2 import component as bt2_component
+
 import functools
+
+from bt2 import mip as bt2_mip
 from bt2 import port as bt2_port
+from bt2 import error as bt2_error
+from bt2 import utils as bt2_utils
+from bt2 import value as bt2_value
+from bt2 import object as bt2_object
 from bt2 import logging as bt2_logging
-import bt2
+from bt2 import component as bt2_component
+from bt2 import native_bt
+from bt2 import connection as bt2_connection
+from bt2 import interrupter as bt2_interrupter
 
 
 def _graph_port_added_listener_from_native(
@@ -40,20 +27,25 @@ def _graph_port_added_listener_from_native(
     user_listener(component, port)
 
 
-class Graph(object._SharedObject):
-    _get_ref = staticmethod(native_bt.graph_get_ref)
-    _put_ref = staticmethod(native_bt.graph_put_ref)
+class Graph(bt2_object._SharedObject):
+    @staticmethod
+    def _get_ref(ptr):
+        native_bt.graph_get_ref(ptr)
+
+    @staticmethod
+    def _put_ref(ptr):
+        native_bt.graph_put_ref(ptr)
 
     def __init__(self, mip_version=0):
-        utils._check_uint64(mip_version)
+        bt2_utils._check_uint64(mip_version)
 
-        if mip_version > bt2.get_maximal_mip_version():
-            raise ValueError('unknown MIP version {}'.format(mip_version))
+        if mip_version > bt2_mip.get_maximal_mip_version():
+            raise ValueError("unknown MIP version {}".format(mip_version))
 
         ptr = native_bt.graph_create(mip_version)
 
         if ptr is None:
-            raise bt2._MemoryError('cannot create graph object')
+            raise bt2_error._MemoryError("cannot create graph object")
 
         super().__init__(ptr)
 
@@ -100,36 +92,40 @@ class Graph(object._SharedObject):
                 )
             )
 
-        utils._check_str(name)
-        utils._check_log_level(logging_level)
+        bt2_utils._check_str(name)
+        bt2_utils._check_log_level(logging_level)
         base_cc_ptr = component_class._bt_component_class_ptr()
 
         if obj is not None and not native_bt.bt2_is_python_component_class(base_cc_ptr):
-            raise ValueError('cannot pass a Python object to a non-Python component')
+            raise ValueError("cannot pass a Python object to a non-Python component")
 
-        if params is not None and not isinstance(params, (dict, bt2.MapValue)):
+        if params is not None and not isinstance(params, (dict, bt2_value.MapValue)):
             raise TypeError("'params' parameter is not a 'dict' or a 'bt2.MapValue'.")
 
-        params = bt2.create_value(params)
+        params = bt2_value.create_value(params)
 
         params_ptr = params._ptr if params is not None else None
 
         status, comp_ptr = add_fn(
             self._ptr, cc_ptr, name, params_ptr, obj, logging_level
         )
-        utils._handle_func_status(status, 'cannot add component to graph')
+        bt2_utils._handle_func_status(status, "cannot add component to graph")
         assert comp_ptr
-        return bt2_component._create_component_from_const_ptr(comp_ptr, cc_type)
+        return bt2_component._create_component_from_const_ptr_and_get_ref(
+            comp_ptr, cc_type
+        )
 
     def connect_ports(self, upstream_port, downstream_port):
-        utils._check_type(upstream_port, bt2_port._OutputPortConst)
-        utils._check_type(downstream_port, bt2_port._InputPortConst)
+        bt2_utils._check_type(upstream_port, bt2_port._OutputPortConst)
+        bt2_utils._check_type(downstream_port, bt2_port._InputPortConst)
         status, conn_ptr = native_bt.graph_connect_ports(
             self._ptr, upstream_port._ptr, downstream_port._ptr
         )
-        utils._handle_func_status(status, 'cannot connect component ports within graph')
+        bt2_utils._handle_func_status(
+            status, "cannot connect component ports within graph"
+        )
         assert conn_ptr
-        return bt2_connection._ConnectionConst._create_from_ptr(conn_ptr)
+        return bt2_connection._ConnectionConst._create_from_ptr_and_get_ref(conn_ptr)
 
     def add_port_added_listener(self, listener):
         if not callable(listener):
@@ -142,21 +138,21 @@ class Graph(object._SharedObject):
 
         listener_ids = fn(self._ptr, listener_from_native)
         if listener_ids is None:
-            raise bt2._Error('cannot add listener to graph object')
+            raise bt2_error._Error("cannot add listener to graph object")
 
         # keep the partial's reference
         self._listener_partials.append(listener_from_native)
 
     def run_once(self):
         status = native_bt.graph_run_once(self._ptr)
-        utils._handle_func_status(status, 'graph object could not run once')
+        bt2_utils._handle_func_status(status, "graph object could not run once")
 
     def run(self):
         status = native_bt.graph_run(self._ptr)
-        utils._handle_func_status(status, 'graph object stopped running')
+        bt2_utils._handle_func_status(status, "graph object stopped running")
 
     def add_interrupter(self, interrupter):
-        utils._check_type(interrupter, bt2_interrupter.Interrupter)
+        bt2_utils._check_type(interrupter, bt2_interrupter.Interrupter)
         native_bt.graph_add_interrupter(self._ptr, interrupter._ptr)
 
     @property
This page took 0.025846 seconds and 4 git commands to generate.