示例#1
0
    def loadHandler(self, r_type, h_type, h_name):
        """
        Load the handler config object from the file based on the given info
        """
        # create a handler config object first
        handler_config = HandlerConfig()

        if r_type in ['share']:
            # this is a shared handler, we will require a handler type
            # if the h_type is a handler type class object, translate it into a str
            if h_type is None:
                h_type = self.findHandlerTypeStringFromName(h_name)
            if not isinstance(h_type, basestring):
                h_type = ht.getHandlerTypeName(h_type)
            handler_module = '.'.join(
                ['lib', 'handlers', r_type, h_type, h_name])
        else:
            # this is a robot handler, no handler type required
            handler_module = '.'.join(['lib', 'handlers', r_type, h_name])

        try:
            if h_type in ("Sensor", "Actuator"):
                handler_config.loadHandlerMethod(handler_module)
            else:
                handler_config.loadHandlerMethod(handler_module,
                                                 onlyLoadInit=True)

            handler_config.robot_type = r_type
        except ImportError as import_error:
            # TODO: Log an error here if the handler is necessary
            handler_config = None

        return handler_config
示例#2
0
    def prepareHandler(self, handler_config):
        """
        Instantiate the handler object of the given handler config if it is not already instantiated
        Return the handler instance
        """

        # Check if we alreay instantiated the handler
        handler_name = handler_config.name
        h = self.getHandlerInstanceByName(handler_name)

        if h is None:
            # we need to instantiate the handler
            robot_type = handler_config.robot_type

            # construct the handler module path for importing
            if robot_type == "share":
                handler_type_string = ht.getHandlerTypeName(
                    handler_config.h_type)
                handler_module_path = ".".join([
                    "lib", "handlers", robot_type, handler_type_string,
                    handler_name
                ])
            else:
                handler_module_path = ".".join(
                    ["lib", "handlers", robot_type, handler_name])

            # find the handler class object
            h_name, h_type, handler_class = HandlerConfig.loadHandlerClass(
                handler_module_path)

            # get the HMC for the init_method
            init_method_config = handler_config.getMethodByName("__init__")

            # construct the arguments list; everyone gets a reference to executor
            arg_dict = {"executor": self.executor}

            # everyone except for InitHandler gets shared_data too
            if h_type is not ht.InitHandler:
                arg_dict.update(
                    {"shared_data": self.executor.proj.shared_data})

            # add any arguments specific to this method
            arg_dict.update(init_method_config.getArgDict())

            # instantiate the handler
            try:
                h = handler_class(**arg_dict)
            except Exception:
                logging.exception(
                    "Failed during handler {} instantiation".format(
                        handler_module_path))
            else:
                self.handler_instance.append(h)
        return h
示例#3
0
    def loadHandler(self, r_type, h_type, h_name):
        """
        Load the handler config object from the file based on the given info
        """
        # create a handler config object first
        handler_config = HandlerConfig()

        if r_type in ['share']:
            # this is a shared handler, we will require a handler type
            # if the h_type is a handler type class object, translate it into a str
            if h_type is None:
                h_type =  self.findHandlerTypeStringFromName(h_name)
            if not isinstance(h_type, basestring):
                h_type = ht.getHandlerTypeName(h_type)
            handler_module = '.'.join(['lib', 'handlers', r_type, h_type, h_name])
        else:
            # this is a robot handler, no handler type required
            handler_module = '.'.join(['lib', 'handlers', r_type, h_name])

        try:
            if h_type in ("Sensor", "Actuator"):
                handler_config.loadHandlerMethod(handler_module)
            else:
                handler_config.loadHandlerMethod(handler_module, onlyLoadInit=True)

            handler_config.robot_type = r_type
        except ImportError as import_error:
            # TODO: Log an error here if the handler is necessary
            handler_config = None

        return handler_config
示例#4
0
    def prepareHandler(self, handler_config):
        """
        Instantiate the handler object of the given handler config if it is not already instantiated
        Return the handler instance
        """

        # Check if we alreay instantiated the handler
        handler_name = handler_config.name
        h = self.getHandlerInstanceByName(handler_name)

        if h is None:
            # we need to instantiate the handler
            robot_type = handler_config.robot_type

            # construct the handler module path for importing
            if robot_type == "share":
                handler_type_string = ht.getHandlerTypeName(handler_config.h_type)
                handler_module_path = ".".join(["lib", "handlers", robot_type, handler_type_string,  handler_name])
            else:
                handler_module_path = ".".join(["lib", "handlers", robot_type, handler_name])

            # find the handler class object
            h_name, h_type, handler_class = HandlerConfig.loadHandlerClass(handler_module_path)

            # get the HMC for the init_method
            init_method_config = handler_config.getMethodByName("__init__")

            # construct the arguments list; everyone gets a reference to executor
            arg_dict = {"executor": self.executor}

            # everyone except for InitHandler gets shared_data too
            if h_type is not ht.InitHandler:
                arg_dict.update({"shared_data":self.executor.proj.shared_data})

            # add any arguments specific to this method
            arg_dict.update(init_method_config.getArgDict())

            # instantiate the handler
            try:
                h = handler_class(**arg_dict)
            except Exception:
                logging.exception("Failed during handler {} instantiation".format(handler_module_path))
            else:
                self.handler_instance.append(h)
        return h