def _get_observer_in_state(self, observer_name, observer_type, for_state, **kwargs): """Return Observable object assigned to observer_name of given device""" # TODO: return observer object wrapped in decorator mocking it's start() available_observer_names = [] if not for_state: for_state = self.current_state if observer_type == TextualDevice.cmds: available_observer_names = self._cmdnames_available_in_state[for_state] elif observer_type == TextualDevice.events: available_observer_names = self._eventnames_available_in_state[for_state] if observer_name in available_observer_names: observer_params = dict(kwargs, connection=self.io_connection.moler_connection) observer = create_instance_from_class_fullname( class_fullname=available_observer_names[observer_name], constructor_parameters=observer_params ) return observer exc = DeviceFailure( device=self.__class__.__name__, message="Failed to create {}-object for '{}' {}. '{}' {} is unknown for state '{}' of device '{}'. Available names: {}".format( observer_type, observer_name, observer_type, observer_name, observer_type, for_state, self.__class__.__name__, available_observer_names)) self._log(logging.ERROR, exc) raise exc
def _create_instance_and_remember_it(cls, device_class, constructor_parameters, establish_connection, name): """ Creates instance of device class. :param device_class: Full class name of device (with package) :param constructor_parameters: Constructor parameters of device :param establish_connection: True then connect to device immediately and change state. False to do not connect. :param name: Name of device. :return: Instance of device. """ org_name = name if name: name = cls._calculate_unique_name(name=name) constructor_parameters['name'] = name device = create_instance_from_class_fullname( class_fullname=device_class, constructor_parameters=constructor_parameters) if establish_connection: device.goto_state(state=device.initial_state) if not name: name = device.name org_name = name cls._devices[name] = device cls._devices_params[name] = dict() cls._devices_params[name]['class_fullname'] = device_class cls._devices_params[name][ 'constructor_parameters'] = constructor_parameters cls._devices_params[name]['cloned_from'] = None handler = functools.partial(cls.forget_device_handler, name) device.register_device_removal_callback(callback=handler) device.public_name = org_name return device
def _create_device(cls, device_class, connection_desc, connection_hops): constructor_parameters = { "io_type": connection_desc["io_type"], "variant": connection_desc["variant"], "sm_params": connection_hops } device = create_instance_from_class_fullname( class_fullname=device_class, constructor_parameters=constructor_parameters) return device
def _create_device(cls, name, device_class, connection_desc, connection_hops, initial_state): constructor_parameters = { "name": name, "io_type": connection_desc["io_type"], "variant": connection_desc["variant"], "sm_params": connection_hops, "initial_state": initial_state } device = create_instance_from_class_fullname( class_fullname=device_class, constructor_parameters=constructor_parameters) return device