X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fbindings%2Fpython%2Fbt2%2Fbt2%2Fgraph.py;h=3812788f0b4bc05a9e2f08adbf8f82f76c8f2498;hb=6c373cc9;hp=1f280ba119ff4de691398980b9c4cd6e54f9eb2c;hpb=cfbd7cf3bde05e8a6606478889dcd663604ef7b5;p=babeltrace.git diff --git a/src/bindings/python/bt2/bt2/graph.py b/src/bindings/python/bt2/bt2/graph.py index 1f280ba1..3812788f 100644 --- a/src/bindings/python/bt2/bt2/graph.py +++ b/src/bindings/python/bt2/bt2/graph.py @@ -21,21 +21,22 @@ # THE SOFTWARE. from bt2 import native_bt, object, utils -import bt2.connection -import bt2.component +from bt2 import interrupter as bt2_interrupter +from bt2 import connection as bt2_connection +from bt2 import component as bt2_component import functools -import bt2.port -import bt2.logging +from bt2 import port as bt2_port +from bt2 import logging as bt2_logging import bt2 def _graph_port_added_listener_from_native( user_listener, component_ptr, component_type, port_ptr, port_type ): - component = bt2.component._create_component_from_ptr_and_get_ref( + component = bt2_component._create_component_from_ptr_and_get_ref( component_ptr, component_type ) - port = bt2.port._create_from_ptr_and_get_ref(port_ptr, port_type) + port = bt2_port._create_from_ptr_and_get_ref(port_ptr, port_type) user_listener(component, port) @@ -48,16 +49,16 @@ def _graph_ports_connected_listener_from_native( downstream_component_type, downstream_port_ptr, ): - upstream_component = bt2.component._create_component_from_ptr_and_get_ref( + upstream_component = bt2_component._create_component_from_ptr_and_get_ref( upstream_component_ptr, upstream_component_type ) - upstream_port = bt2.port._create_from_ptr_and_get_ref( + upstream_port = bt2_port._create_from_ptr_and_get_ref( upstream_port_ptr, native_bt.PORT_TYPE_OUTPUT ) - downstream_component = bt2.component._create_component_from_ptr_and_get_ref( + downstream_component = bt2_component._create_component_from_ptr_and_get_ref( downstream_component_ptr, downstream_component_type ) - downstream_port = bt2.port._create_from_ptr_and_get_ref( + downstream_port = bt2_port._create_from_ptr_and_get_ref( downstream_port_ptr, native_bt.PORT_TYPE_INPUT ) user_listener( @@ -73,7 +74,7 @@ class Graph(object._SharedObject): ptr = native_bt.graph_create() if ptr is None: - raise bt2.CreationError('cannot create graph object') + raise bt2._MemoryError('cannot create graph object') super().__init__(ptr) @@ -82,31 +83,32 @@ class Graph(object._SharedObject): component_class, name, params=None, - logging_level=bt2.logging.LoggingLevel.NONE, + obj=None, + logging_level=bt2_logging.LoggingLevel.NONE, ): - if isinstance(component_class, bt2.component._GenericSourceComponentClass): + if isinstance(component_class, bt2_component._SourceComponentClass): cc_ptr = component_class._ptr - add_fn = native_bt.graph_add_source_component + add_fn = native_bt.bt2_graph_add_source_component cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE - elif isinstance(component_class, bt2.component._GenericFilterComponentClass): + elif isinstance(component_class, bt2_component._FilterComponentClass): cc_ptr = component_class._ptr - add_fn = native_bt.graph_add_filter_component + add_fn = native_bt.bt2_graph_add_filter_component cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER - elif isinstance(component_class, bt2.component._GenericSinkComponentClass): + elif isinstance(component_class, bt2_component._SinkComponentClass): cc_ptr = component_class._ptr - add_fn = native_bt.graph_add_sink_component + add_fn = native_bt.bt2_graph_add_sink_component cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK - elif issubclass(component_class, bt2.component._UserSourceComponent): + elif issubclass(component_class, bt2_component._UserSourceComponent): cc_ptr = component_class._bt_cc_ptr - add_fn = native_bt.graph_add_source_component + add_fn = native_bt.bt2_graph_add_source_component cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE - elif issubclass(component_class, bt2.component._UserSinkComponent): + elif issubclass(component_class, bt2_component._UserSinkComponent): cc_ptr = component_class._bt_cc_ptr - add_fn = native_bt.graph_add_sink_component + add_fn = native_bt.bt2_graph_add_sink_component cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK - elif issubclass(component_class, bt2.component._UserFilterComponent): + elif issubclass(component_class, bt2_component._UserFilterComponent): cc_ptr = component_class._bt_cc_ptr - add_fn = native_bt.graph_add_filter_component + add_fn = native_bt.bt2_graph_add_filter_component cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER else: raise TypeError( @@ -117,24 +119,30 @@ class Graph(object._SharedObject): utils._check_str(name) utils._check_log_level(logging_level) - params = bt2.create_value(params) + 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') + params = bt2.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, logging_level) + 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') assert comp_ptr - return bt2.component._create_component_from_ptr(comp_ptr, cc_type) + return bt2_component._create_component_from_ptr(comp_ptr, cc_type) def connect_ports(self, upstream_port, downstream_port): - utils._check_type(upstream_port, bt2.port._OutputPort) - utils._check_type(downstream_port, bt2.port._InputPort) + utils._check_type(upstream_port, bt2_port._OutputPort) + utils._check_type(downstream_port, bt2_port._InputPort) 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') assert conn_ptr - return bt2.connection._Connection._create_from_ptr(conn_ptr) + return bt2_connection._Connection._create_from_ptr(conn_ptr) def add_port_added_listener(self, listener): if not callable(listener): @@ -147,8 +155,9 @@ class Graph(object._SharedObject): listener_ids = fn(self._ptr, listener_from_native) if listener_ids is None: - utils._raise_bt2_error('cannot add listener to graph object') - return bt2._ListenerHandle(listener_ids, self) + raise bt2._Error('cannot add listener to graph object') + + return utils._ListenerHandle(listener_ids, self) def add_ports_connected_listener(self, listener): if not callable(listener): @@ -161,39 +170,28 @@ class Graph(object._SharedObject): listener_ids = fn(self._ptr, listener_from_native) if listener_ids is None: - utils._raise_bt2_error('cannot add listener to graph object') - return bt2._ListenerHandle(listener_ids, self) + raise bt2._Error('cannot add listener to graph object') + + return utils._ListenerHandle(listener_ids, self) + + def run_once(self): + status = native_bt.graph_run_once(self._ptr) + utils._handle_func_status(status, 'graph object could not run once') def run(self): status = native_bt.graph_run(self._ptr) try: - utils._handle_func_status( - status, 'graph object stopped running because of an unexpected error' - ) + utils._handle_func_status(status, 'graph object stopped running') except bt2.Stop: # done return except Exception: raise - def cancel(self): - status = native_bt.graph_cancel(self._ptr) - utils._handle_func_status(status, 'cannot cancel graph object') - - @property - def is_canceled(self): - is_canceled = native_bt.graph_is_canceled(self._ptr) - assert is_canceled >= 0 - return is_canceled > 0 - - def create_output_port_message_iterator(self, output_port): - utils._check_type(output_port, bt2.port._OutputPort) - msg_iter_ptr = native_bt.port_output_message_iterator_create( - self._ptr, output_port._ptr - ) - - if msg_iter_ptr is None: - raise bt2.CreationError('cannot create output port message iterator') + def add_interrupter(self, interrupter): + utils._check_type(interrupter, bt2_interrupter.Interrupter) + native_bt.graph_add_interrupter(self._ptr, interrupter._ptr) - return bt2.message_iterator._OutputPortMessageIterator(msg_iter_ptr) + def interrupt(self): + native_bt.graph_interrupt(self._ptr)