def __load_tfs(self):
        """
        Loads and connects all transfer functions
        """
        self._notify("Loading transfer functions")

        for tf in self.sim_config.transfer_functions:
            self._notify("Loading transfer function: {}".format(tf.name))
            #tf.code = correct_indentation(tf.code, 0)
            tf.code = tf.code.strip() + "\n"
            logger.debug("TF: " + tf.name + "\n" + tf.code + '\n')

            try:
                new_code = compile_restricted(tf.code, '<string>', 'exec')
            # pylint: disable=broad-except
            except Exception as e:
                logger.error(
                    "Error while compiling the transfer function {name} in restricted "
                    "mode with error {err}".format(name=tf.name, err=str(e)))
                tfm.set_flawed_transfer_function(tf.code, tf.name, e)
                continue

            try:
                tfm.set_transfer_function(tf.code, new_code, tf.name,
                                          tf.active)
            except tfm.TFLoadingException as loading_e:
                logger.error(loading_e)
                tfm.set_flawed_transfer_function(tf.code, tf.name, loading_e)
예제 #2
0
    def __load_tfs(self):
        """
        Loads and connects all transfer functions
        """
        self._notify("Loading transfer functions")

        # Create transfer functions
        import_referenced_python_tfs(self.bibi, self.exc.dir)

        for i, tf in enumerate(self.bibi.transferFunction):
            self._notify("Generating transfer function: %i" % (i + 1))
            tf_code = generate_tf(tf, self.bibi)
            self._notify("Loading transfer function: %s" % tf.name)
            tf_code = correct_indentation(tf_code, 0)
            tf_code = tf_code.strip() + "\n"
            logger.debug("TF: " + tf.name + "\n" + tf_code + '\n')

            try:
                new_code = compile_restricted(tf_code, '<string>', 'exec')
            # pylint: disable=broad-except
            except Exception as e:
                message = "Error while compiling the updated transfer function named "\
                          + tf.name +\
                          " in restricted mode.\n"\
                          + str(e)
                logger.error(message)
                nrp.set_flawed_transfer_function(tf_code, tf.name, e)
                continue

            try:
                nrp.set_transfer_function(tf_code, new_code, tf.name)
            except nrp.TFLoadingException as loading_e:
                logger.error(loading_e)
                nrp.set_flawed_transfer_function(tf_code, tf.name, loading_e)
예제 #3
0
    def test_shutdown(self):

        self.tfm.robot_adapter = self.rcm
        self.tfm.brain_adapter = self.bcm
        # add tf
        @nrp.MapRobotSubscriber("camera", Husky.Eye.camera)
        @nrp.MapSpikeSink("device", nrp.brain.actors[1],
                          nrp.leaky_integrator_alpha)
        @nrp.Robot2Neuron()
        def camera_trans(t, camera, device):
            pass

        # add flawed tf
        tf_name = "tf_name"
        tf_src = "def tf_name(self): pass"
        tf_error = Exception()
        nrp.set_flawed_transfer_function(tf_src, tf_name, tf_error)

        self.tfm.initialize("tfnode")

        self.tfm.shutdown()

        self.assertEqual(len(self.tfm.n2r), 0)
        self.assertEqual(len(self.tfm.r2n), 0)
        self.assertEqual(len(self.tfm.flawed), 0)
        self.assertEqual(len(self.tfm.global_data), 0)
        self.assertFalse(self.tfm.initialized)
예제 #4
0
파일: test_tf_ok.py 프로젝트: hmsgit/nrpcle
    def test_delete_flawed_tf(self):
        nrp.start_new_tf_manager()

        tf_name = "tf_name"
        tf_src = "def tf_name(self): pass"
        tf_error = Exception()

        nrp.set_flawed_transfer_function(tf_src, tf_name, tf_error)

        result = nrp.delete_flawed_transfer_function(tf_name)
        self.assertTrue(result)

        # can't find it anymore
        self.assertIsNone(nrp.get_flawed_transfer_function(tf_name))
예제 #5
0
파일: test_tf_ok.py 프로젝트: hmsgit/nrpcle
    def test_set_flawed_tf(self):
        nrp.start_new_tf_manager()

        tf_name = "tf_name"
        tf_src = "def tf_name(self): pass"
        tf_error = Exception()

        nrp.set_flawed_transfer_function(tf_src, tf_name, tf_error)
        #  check if correctly added
        flawed_tf = nrp.get_flawed_transfer_function(tf_name)

        self.assertEqual(flawed_tf.name, tf_name)
        self.assertEqual(flawed_tf.source, tf_src)
        self.assertTrue(flawed_tf.error is tf_error)
예제 #6
0
    def test_get_transfer_functions(self):
        tf_name = "tf_name"
        tf_src = "def tf_name(self): pass"
        tf_error = Exception()

        nrp.set_flawed_transfer_function(tf_src, tf_name, tf_error)

        # get also flawed tfs
        tfs = self.tfm.transfer_functions(flawed=True)

        self.assertEqual(tfs[0].name, tf_name)

        # get only proper tfs
        tfs = self.tfm.transfer_functions(flawed=False)

        self.assertEqual(len(tfs), 0)
예제 #7
0
    def __set_transfer_function(self, request, new):
        """
        Patch a transfer function. This method is called when adding or editing a transfer function.

        :param request: The mandatory rospy request parameter
        :param new: A boolean indicating whether a transfer function is being added or modified.
        :return: empty string for a successful compilation in restricted mode
                 (executed synchronously), an error message otherwise.
        """
        new_source = textwrap.dedent(request.transfer_function_source)
        logger.info(
            "About to compile transfer function with the following python code: \n"
            + repr(new_source))

        # Make sure the TF has exactly one function definition
        get_tf_name_outcome, get_tf_name_outcome_value = self.get_tf_name(
            new_source)
        if get_tf_name_outcome is True:
            new_name = get_tf_name_outcome_value
        else:
            error_message = get_tf_name_outcome_value

            self.publish_error(CLEError.SOURCE_TYPE_TRANSFER_FUNCTION,
                               "NoOrMultipleNames",
                               error_message,
                               severity=CLEError.SEVERITY_ERROR)

            return error_message

        if not new:
            original_name = request.transfer_function_name
        else:
            original_name = None

        # Make sure function name is not a duplicate
        if new or (new_name != original_name):
            try:
                self.__check_duplicate_tf_name(new_name)
            except ValueError as value_e:
                message = "duplicate Transfer Function name"
                # Select the correct function to highlight
                function_name = new_name if original_name is None else original_name

                self._publish_error_from_exception(value_e, new_name)

                if new:
                    tf_framework.set_flawed_transfer_function(
                        new_source, function_name, value_e)

                return message

        # Compile (synchronously) transfer function's new code in restricted mode
        new_code = None
        try:
            new_code = compile_restricted(new_source, '<string>', 'exec')
        # pylint: disable=broad-except
        except Exception as e:
            message = "Error while compiling the updated" \
                      + " transfer function named " + new_name \
                      + " in restricted mode.\n" \
                      + str(e)
            if new:
                tf_framework.set_flawed_transfer_function(
                    new_source, new_name, e)

            self._publish_error_from_exception(e, new_name)

            return message

        # Make sure CLE is stopped. If already stopped, these calls are harmless.
        # (Execution of updated code is asynchronous)
        running = self.__cle.running
        err = ""

        if running:
            self.__cle.stop()

        # Try to load the TF
        try:
            # When editing a TF (i.e. not new), delete it before loading the new one
            if not new and original_name is not None:
                tf_framework.delete_transfer_function(original_name)
            tf_framework.set_transfer_function(new_source, new_code, new_name)
        except TFLoadingException as e:
            self.publish_error(CLEError.SOURCE_TYPE_TRANSFER_FUNCTION,
                               "Loading",
                               e.message,
                               severity=CLEError.SEVERITY_ERROR,
                               function_name=e.tf_name)
            err = e.message

            tf_framework.set_flawed_transfer_function(new_source, new_name, e)

        if running:
            self.__cle.start()

        return err