Exemplo n.º 1
0
    def run_ansible_playbook_module(self, install_file_path):
        """
        Run the install.yml file through an Ansible playbook using the dedicated neuron !

        :param sudo_password: local machine sudo password required to install libraries
        :param install_file_path: the path of the Ansible playbook to run.
        :return:
        """
        logger.debug("[ResourcesManager] Run ansible playbook")
        Utils.print_info("Starting neuron installation")
        # ask the sudo password
        if self.sudo_password is not None:
            pswd = self.sudo_password
        else:
            pswd = getpass.getpass('Sudo password:')
        if not pswd or pswd == "":
            Utils.print_warning("You must enter a sudo password")
            return False
        else:
            ansible_neuron_parameters = {
                "task_file": install_file_path,
                "sudo": True,
                "sudo_user": "******",
                "sudo_password": pswd
            }
            neuron = Neuron(name="ansible_playbook",
                            parameters=ansible_neuron_parameters)
            NeuronLauncher.start_neuron(neuron)
            return True
Exemplo n.º 2
0
    def test_parameters_are_available_in_loaded_parameters(self):
        # the parameter in bracket is available in the dict
        string_parameters = "this is a {{ parameter1 }}"
        loaded_parameters = {"parameter1": "value"}

        self.assertTrue(NeuronLauncher._neuron_parameters_are_available_in_loaded_parameters(string_parameters,
                                                                                             loaded_parameters))

        # the parameter in bracket is NOT available in the dict
        string_parameters = "this is a {{ parameter1 }}"
        loaded_parameters = {"parameter2": "value"}

        self.assertFalse(NeuronLauncher._neuron_parameters_are_available_in_loaded_parameters(string_parameters,
                                                                                              loaded_parameters))

        # the string_parameters doesn't contains bracket in bracket is available in the dict
        string_parameters = "this is a {{ parameter1 }}"
        loaded_parameters = {"parameter1": "value"}

        self.assertTrue(NeuronLauncher._neuron_parameters_are_available_in_loaded_parameters(string_parameters,
                                                                                             loaded_parameters))

        # the string_parameters contains 2 parameters available in the dict
        string_parameters = "this is a {{ parameter1 }} and this is {{ parameter2 }}"
        loaded_parameters = {"parameter1": "value", "parameter2": "other value"}

        self.assertTrue(NeuronLauncher._neuron_parameters_are_available_in_loaded_parameters(string_parameters,
                                                                                             loaded_parameters))

        # the string_parameters contains 2 parameters and one of them is not available in the dict
        string_parameters = "this is a {{ parameter1 }} and this is {{ parameter2 }}"
        loaded_parameters = {"parameter1": "value", "parameter3": "other value"}

        self.assertFalse(NeuronLauncher._neuron_parameters_are_available_in_loaded_parameters(string_parameters,
                                                                                              loaded_parameters))
Exemplo n.º 3
0
 def _run_synapse(cls, synapse):
     """
     Start all neurons in the synapse
     :param synapse: Synapse for which we run neurons
     :return:
     """
     for neuron in synapse.neurons:
         NeuronLauncher.start_neuron(neuron)
     return True
Exemplo n.º 4
0
    def test_launch_neuron(self):
        """
        Test the Neuron Launcher trying to start a Neuron
        """
        neuron = Neuron(name='neurone1', parameters={'var1': 'val1'})
        sl = SettingLoader()
        resources = Resources(neuron_folder='/var/tmp/test/resources')
        sl.settings.resources = resources
        with mock.patch("kalliope.core.Utils.get_dynamic_class_instantiation") as mock_get_class_instantiation:
            NeuronLauncher.launch_neuron(neuron=neuron)

            mock_get_class_instantiation.assert_called_once_with(package_name="neurons",
                                                                 module_name=neuron.name,
                                                                 parameters=neuron.parameters,
                                                                 resources_dir=sl.settings.resources.neuron_folder)
            mock_get_class_instantiation.reset_mock()
Exemplo n.º 5
0
    def test_start_neuron(self):
        """
        Test the Neuron Launcher trying to start a Neuron
        """
        neuron = Neuron(name='neurone1', parameters={'var1': 'val1'})
        sl = SettingLoader()
        resources = Resources(neuron_folder='/var/tmp/test/resources')
        sl.settings.resources = resources
        with mock.patch("kalliope.core.Utils.get_dynamic_class_instantiation"
                        ) as mock_get_class_instantiation:
            NeuronLauncher.start_neuron(neuron=neuron)

            mock_get_class_instantiation.assert_called_once_with(
                package_name="neurons",
                module_name=neuron.name,
                parameters=neuron.parameters,
                resources_dir=sl.settings.resources.neuron_folder)
            mock_get_class_instantiation.reset_mock()
Exemplo n.º 6
0
    def _process_neuron_list(self, matched_synapse):
        """
        Process the neuron list of the matched_synapse
        Execute the Neuron
        Executing a Neuron creates a NeuronModule object. This one can have 3 status:
        - waiting for an answer: The neuron wait for an answer from the caller. The api response object is returned.
                                 The neuron is not removed from the matched synapse to be executed again
        - want to execute a synapse: The neuron add a list of synapse to execute to the lifo. 
                                     The LIFO restart over to process it.The neuron is removed from the matched synapse
        - complete: The neuron has been executed and its not waiting for an answer and doesn't want to start a synapse
                    The neuron is removed from the matched synapse
        :param matched_synapse: MatchedSynapse object to process
        """

        logger.debug("[LIFOBuffer] number of neuron to process: %s" % len(matched_synapse.neuron_fifo_list))
        # while we have synapse to process in the list of synapse
        while matched_synapse.neuron_fifo_list:
            # get the first neuron in the FIFO neuron list
            neuron = matched_synapse.neuron_fifo_list[0]
            # from here, we are back into the last neuron we were processing.
            if self.answer is not None:  # we give the answer if exist to the first neuron
                neuron.parameters["answer"] = self.answer
                # the next neuron should not get this answer
                self.answer = None
            # todo fix this when we have a full client/server call. The client would be the voice or api call
            neuron.parameters["is_api_call"] = self.is_api_call
            logger.debug("[LIFOBuffer] process_neuron_list: is_api_call: %s" % (self.is_api_call))
            # execute the neuron
            instantiated_neuron = NeuronLauncher.start_neuron(neuron=neuron,
                                                              parameters_dict=matched_synapse.parameters)

            # the status of an execution is "complete" if no neuron are waiting for an answer
            self.api_response.status = "complete"
            if instantiated_neuron is not None:
                if instantiated_neuron.is_waiting_for_answer:  # the neuron is waiting for an answer
                    logger.debug("[LIFOBuffer] Wait for answer mode")
                    self.api_response.status = "waiting_for_answer"
                    self.is_running = False
                    raise Serialize
                else:
                    logger.debug("[LIFOBuffer] complete mode")
                    # we add the instantiated neuron to the neuron_module_list.
                    # This one contains info about generated text
                    matched_synapse.neuron_module_list.append(instantiated_neuron)
                    # the neuron is fully processed we can remove it from the list
                    matched_synapse.neuron_fifo_list.remove(neuron)

                if self.reset_lifo:  # the last executed neuron want to run a synapse
                    logger.debug("[LIFOBuffer] Last executed neuron want to run a synapse. Restart the LIFO")
                    # we have added a list of synapse to the LIFO ! this one must start over.
                    # break all while loop until the execution is back to the LIFO loop
                    self.reset_lifo = False
                    raise SynapseListAddedToLIFO
            else:
                # the neuron has not been processed but we still need to remove it from the list
                matched_synapse.neuron_fifo_list.remove(neuron)
Exemplo n.º 7
0
    def run_ansible_playbook_module(install_file_path):
        """
        Run the install.yml file through an Ansible playbook using the dedicated neuron !

        :param install_file_path: the path of the Ansible playbook to run.
        :return:
        """
        logger.debug("[ResourcesManager] Run ansible playbook")
        Utils.print_info("Starting neuron installation")
        # ask the sudo password
        pswd = getpass.getpass('Sudo password:')
        ansible_neuron_parameters = {
            "task_file": install_file_path,
            "sudo": True,
            "sudo_user": "******",
            "sudo_password": pswd
        }
        neuron = Neuron(name="ansible_playbook", parameters=ansible_neuron_parameters)
        NeuronLauncher.start_neuron(neuron)
Exemplo n.º 8
0
    def _start_neuron(neuron, params):
        """
        Associate params and Starts a neuron.

        :param neuron: the neuron to start
        :param params: the params to check and associate to the neuron args.
        """

        problem_in_neuron_found = False
        if isinstance(neuron.parameters, dict):
            # print neuron.parameters
            if "args" in neuron.parameters:
                logger.debug("The neuron waits for parameter")
                # check that the user added parameters to his order
                if params is None:
                    # we don't raise an error and break the program but we don't run the neuron
                    problem_in_neuron_found = True
                    Utils.print_danger(
                        "Error: The neuron %s is waiting for argument. "
                        "Argument found in bracket in the given order" %
                        neuron.name)
                else:
                    # we add wanted arguments the existing neuron parameter dict
                    for arg in neuron.parameters["args"]:
                        if arg in params:
                            logger.debug(
                                "Parameter %s added to the current parameter "
                                "of the neuron: %s" % (arg, neuron.name))
                            neuron.parameters[arg] = params[arg]
                        else:
                            # we don't raise an error and break the program but
                            # we don't run the neuron
                            problem_in_neuron_found = True
                            Utils.print_danger(
                                "Error: Argument \"%s\" not found in the"
                                " order" % arg)

        # if no error detected, we run the neuron
        if not problem_in_neuron_found:
            NeuronLauncher.start_neuron(neuron)
        else:
            Utils.print_danger("A problem has been found in the Synapse.")
Exemplo n.º 9
0
    def test_start_neuron(self):
        """
        Testing params association and starting a Neuron
        """

        with mock.patch("kalliope.core.NeuronLauncher.launch_neuron"
                        ) as mock_launch_neuron_method:
            # Assert to the neuron is launched with not parameter from order
            neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})

            NeuronLauncher.start_neuron(neuron=neuron1)
            mock_launch_neuron_method.assert_called_with(neuron1)
            mock_launch_neuron_method.reset_mock()

            # Assert the params are well passed to the neuron
            neuron2 = Neuron(name='neurone2',
                             parameters={
                                 'var2': 'val2',
                                 'var3': "{{ var3 }}"
                             })
            params = {'var3': 'value3'}
            NeuronLauncher.start_neuron(neuron=neuron2, parameters_dict=params)
            neuron2_params = Neuron(name='neurone2',
                                    parameters={
                                        'var2': 'val2',
                                        'var3': 'value3'
                                    })

            mock_launch_neuron_method.assert_called_with(neuron2_params)
            mock_launch_neuron_method.reset_mock()

            # Assert the Neuron is not started when missing args
            neuron3 = Neuron(name='neurone3',
                             parameters={
                                 'var3': 'val3',
                                 'var4': '{{val4}}'
                             })
            params = {'not_exist': 'test'}
            NeuronLauncher.start_neuron(neuron=neuron3, parameters_dict=params)
            mock_launch_neuron_method.assert_not_called()
            mock_launch_neuron_method.reset_mock()

            # Assert no neuron is launched when waiting for args and none are given
            neuron4 = Neuron(name='neurone4',
                             parameters={
                                 'var5': 'val5',
                                 'var6': '{{val6}}'
                             })

            NeuronLauncher.start_neuron(neuron=neuron4)
            mock_launch_neuron_method.assert_not_called()
            mock_launch_neuron_method.reset_mock()
Exemplo n.º 10
0
    def test_start_neuron(self):
        """
        Testing params association and starting a Neuron
        """

        with mock.patch("kalliope.core.NeuronLauncher.launch_neuron") as mock_launch_neuron_method:
            # Assert to the neuron is launched with not parameter from order
            neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})

            NeuronLauncher.start_neuron(neuron=neuron1)
            mock_launch_neuron_method.assert_called_with(neuron1)
            mock_launch_neuron_method.reset_mock()

            # Assert the params are well passed to the neuron
            neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2', 'var3': "{{ var3 }}"})
            params = {
                'var3': 'value3'
            }
            NeuronLauncher.start_neuron(neuron=neuron2,
                                        parameters_dict=params)
            neuron2_params = Neuron(name='neurone2', parameters={'var2': 'val2', 'var3': 'value3'})

            mock_launch_neuron_method.assert_called_with(neuron2_params)
            mock_launch_neuron_method.reset_mock()

            # Assert the Neuron is not started when missing args
            neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3', 'var4': '{{val4}}'})
            params = {
                'not_exist': 'test'
            }
            NeuronLauncher.start_neuron(neuron=neuron3,
                                        parameters_dict=params)
            mock_launch_neuron_method.assert_not_called()
            mock_launch_neuron_method.reset_mock()

            # Assert no neuron is launched when waiting for args and none are given
            neuron4 = Neuron(name='neurone4', parameters={'var5': 'val5', 'var6': '{{val6}}'})

            NeuronLauncher.start_neuron(neuron=neuron4)
            mock_launch_neuron_method.assert_not_called()
            mock_launch_neuron_method.reset_mock()
Exemplo n.º 11
0
    def _process_neuron_list(self, matched_synapse):
        """
        Process the neuron list of the matched_synapse
        Execute the Neuron
        Executing a Neuron creates a NeuronModule object. This one can have 3 status:
        - waiting for an answer: The neuron wait for an answer from the caller. The api response object is returned.
                                 The neuron is not removed from the matched synapse to be executed again
        - want to execute a synapse: The neuron add a list of synapse to execute to the lifo. 
                                     The LIFO restart over to process it.The neuron is removed from the matched synapse
        - complete: The neuron has been executed and its not waiting for an answer and doesn't want to start a synapse
                    The neuron is removed from the matched synapse
        :param matched_synapse: MatchedSynapse object to process
        """

        logger.debug("[LIFOBuffer] number of neuron to process: %s" %
                     len(matched_synapse.neuron_fifo_list))
        # while we have synapse to process in the list of synapse
        while matched_synapse.neuron_fifo_list:
            # get the first neuron in the FIFO neuron list
            neuron = matched_synapse.neuron_fifo_list[0]
            # from here, we are back into the last neuron we were processing.
            if self.answer is not None:  # we give the answer if exist to the first neuron
                neuron.parameters["answer"] = self.answer
                # the next neuron should not get this answer
                self.answer = None
            # todo fix this when we have a full client/server call. The client would be the voice or api call
            neuron.parameters["is_api_call"] = self.is_api_call
            logger.debug("[LIFOBuffer] process_neuron_list: is_api_call: %s" %
                         self.is_api_call)
            # execute the neuron
            instantiated_neuron = NeuronLauncher.start_neuron(
                neuron=neuron, parameters_dict=matched_synapse.parameters)

            # the status of an execution is "complete" if no neuron are waiting for an answer
            self.api_response.status = "complete"
            if instantiated_neuron is not None:
                if instantiated_neuron.is_waiting_for_answer:  # the neuron is waiting for an answer
                    logger.debug("[LIFOBuffer] Wait for answer mode")
                    self.api_response.status = "waiting_for_answer"
                    self.is_running = False
                    raise Serialize
                else:
                    logger.debug("[LIFOBuffer] complete mode")
                    # we add the instantiated neuron to the neuron_module_list.
                    # This one contains info about generated text
                    matched_synapse.neuron_module_list.append(
                        instantiated_neuron)
                    # the neuron is fully processed we can remove it from the list
                    matched_synapse.neuron_fifo_list.remove(neuron)

                if self.reset_lifo:  # the last executed neuron want to run a synapse
                    logger.debug(
                        "[LIFOBuffer] Last executed neuron want to run a synapse. Restart the LIFO"
                    )
                    # we have added a list of synapse to the LIFO ! this one must start over.
                    # break all while loop until the execution is back to the LIFO loop
                    self.reset_lifo = False
                    raise SynapseListAddedToLIFO
            else:
                # the neuron has not been processed but we still need to remove it from the list
                matched_synapse.neuron_fifo_list.remove(neuron)
Exemplo n.º 12
0
    def test_replace_brackets_by_loaded_parameter(self):
        # -------------------
        # test with string
        # -------------------
        # the target value to replace is present in the loaded parameter dict
        neuron_parameters = {"param1": "this is a value {{ replaced }}"}

        loaded_parameters = {"replaced": "replaced successfully"}

        expected_result = {"param1": "this is a value replaced successfully"}

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # the target value with unicode to replace is present in the loaded parameter dict
        neuron_parameters = {"param1": "this is a value {{ replaced }}"}

        loaded_parameters = {"replaced": u"rêmpläcée successfülly"}

        expected_result = {"param1": "this is a value rêmpläcée successfülly"}

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # the target value to replace is NOT present in the loaded parameter dict
        neuron_parameters = {"param1": "this is a value {{ replaced }}"}

        loaded_parameters = {"not_exist": "replaced successfully"}

        with self.assertRaises(NeuronParameterNotAvailable):
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters)

        # one parameter doesn't contains bracket, the other one do
        neuron_parameters = {
            "param1": "this is a value {{ replaced }}",
            "param2": "value"
        }

        loaded_parameters = {"replaced": "replaced successfully"}

        expected_result = {
            "param1": "this is a value replaced successfully",
            "param2": "value"
        }

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # parameters are integer or boolean
        neuron_parameters = {"param1": 1, "param2": True}

        loaded_parameters = {"replaced": "replaced successfully"}

        expected_result = {"param1": 1, "param2": True}

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # parameters are say_template or file template. Should not be altered by the loader
        neuron_parameters = {
            "say_template": "{{output}}",
            "file_template": "here is a file"
        }

        loaded_parameters = {"output": "should not be used"}

        expected_result = {
            "say_template": "{{output}}",
            "file_template": "here is a file"
        }

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # replacing with variable
        sl = SettingLoader()
        sl.settings.variables = {
            "replaced": {
                "name": u'replaced successfully'
            }
        }

        neuron_parameters = {
            "param1": "this is a value {{ replaced['name'] }}"
        }

        loaded_parameters = {"name": "replaced successfully"}

        expected_result = {"param1": "this is a value replaced successfully"}

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # the parameter is a reserved key. for example from_answer_link from the neurotransmitter
        list_reserved_keys = [
            "say_template", "file_template", "kalliope_memory",
            "from_answer_link"
        ]

        for reserved_key in list_reserved_keys:
            neuron_parameters = {
                reserved_key: "this is a value with {{ 'brackets '}}"
            }

            loaded_parameters = dict()

            expected_result = {
                reserved_key: "this is a value with {{ 'brackets '}}"
            }

            self.assertEqual(
                expected_result,
                NeuronLauncher._replace_brackets_by_loaded_parameter(
                    neuron_parameters, loaded_parameters))
Exemplo n.º 13
0
    def test_replace_brackets_by_loaded_parameter(self):
        # -------------------
        # test with string
        # -------------------
        # the target value to replace is present in the loaded parameter dict
        neuron_parameters = {
            "param1": "this is a value {{ replaced }}"
        }

        loaded_parameters = {
            "replaced": "replaced successfully"
        }

        expected_result = {
            "param1": "this is a value replaced successfully"
        }

        self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
                                                                                               loaded_parameters))

        # the target value with unicode to replace is present in the loaded parameter dict
        neuron_parameters = {
            "param1": "this is a value {{ replaced }}"
        }

        loaded_parameters = {
            "replaced": u"rêmpläcée successfülly"
        }

        expected_result = {
            "param1": "this is a value rêmpläcée successfülly"
        }

        self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
                                                                                               loaded_parameters))

        # the target value to replace is NOT present in the loaded parameter dict
        neuron_parameters = {
            "param1": "this is a value {{ replaced }}"
        }

        loaded_parameters = {
            "not_exist": "replaced successfully"
        }

        with self.assertRaises(NeuronParameterNotAvailable):
            NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters, loaded_parameters)

        # one parameter doesn't contains bracket, the other one do
        neuron_parameters = {
            "param1": "this is a value {{ replaced }}",
            "param2": "value"
        }

        loaded_parameters = {
            "replaced": "replaced successfully"
        }

        expected_result = {
            "param1": "this is a value replaced successfully",
            "param2": "value"
        }

        self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
                                                                                               loaded_parameters))

        # parameters are integer or boolean
        neuron_parameters = {
            "param1": 1,
            "param2": True
        }

        loaded_parameters = {
            "replaced": "replaced successfully"
        }

        expected_result = {
            "param1": 1,
            "param2": True
        }

        self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
                                                                                               loaded_parameters))

        # parameters are say_template or file template. Should not be altered by the loader
        neuron_parameters = {
            "say_template": "{{output}}",
            "file_template": "here is a file"
        }

        loaded_parameters = {
            "output": "should not be used"
        }

        expected_result = {
            "say_template": "{{output}}",
            "file_template": "here is a file"
        }

        self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
                                                                                               loaded_parameters))

        # replacing with variable
        sl = SettingLoader()
        sl.settings.variables = {
            "replaced": {
                "name": u'replaced successfully'
            }
        }

        neuron_parameters = {
            "param1": "this is a value {{ replaced['name'] }}"
        }

        loaded_parameters = {
            "name": "replaced successfully"
        }

        expected_result = {
            "param1": "this is a value replaced successfully"
        }

        self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
                                                                                               loaded_parameters))

        # the parameter is a reserved key. for example from_answer_link from the neurotransmitter
        list_reserved_keys = ["say_template", "file_template", "kalliope_memory", "from_answer_link"]

        for reserved_key in list_reserved_keys:
            neuron_parameters = {
                reserved_key: "this is a value with {{ 'brackets '}}"
            }

            loaded_parameters = dict()

            expected_result = {
                reserved_key: "this is a value with {{ 'brackets '}}"
            }

            self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
                                                                                                   loaded_parameters))

        ####
        # tests with global variables
        ####

        # 1/ only one global variable
        sl = SettingLoader()
        sl.settings.variables = {
            "hello": "test",
            "hello2": "test2",
        }

        parameters = {
            'var1': '{{hello}}'
        }

        expected_parameters = {
            'var1': 'test'
        }

        self.assertEqual(expected_parameters, NeuronLauncher._replace_brackets_by_loaded_parameter(parameters,
                                                                                                   loaded_parameters))

        # 2/ global variable with string after
        sl.settings.variables = {
            "hello": "test",
            "hello2": "test2",
        }

        parameters = {
            'var1': '{{hello}} Sispheor'
        }

        expected_parameters = {
            'var1': 'test Sispheor'
        }

        self.assertEqual(expected_parameters, NeuronLauncher._replace_brackets_by_loaded_parameter(parameters,
                                                                                                   loaded_parameters))

        # 3/ global variable with int after
        parameters = {
            'var1': '{{hello}}0'
        }
        sl.settings.variables = {
            "hello": 60,
            "hello2": "test2",
        }

        expected_parameters = {
            'var1': '600'
        }

        self.assertEqual(expected_parameters, NeuronLauncher._replace_brackets_by_loaded_parameter(parameters,
                                                                                                   loaded_parameters))

        # 4/ multiple global variables
        parameters = {
            'var1': '{{hello}} {{me}}'
        }
        sl.settings.variables = {
            "hello": "hello",
            "me": "LaMonf"
        }

        expected_parameters = {
            'var1': 'hello LaMonf'
        }

        self.assertEqual(expected_parameters, NeuronLauncher._replace_brackets_by_loaded_parameter(parameters,
                                                                                                   loaded_parameters))

        # 5/ parameter value is a list
        parameters = {
            'var1': '[hello {{name}}, bonjour {{name}}]'
        }
        sl.settings.variables = {
            "name": "LaMonf",
            "hello2": "test2",
        }

        expected_parameters = {
            'var1': '[hello LaMonf, bonjour LaMonf]'
        }

        self.assertEqual(expected_parameters, NeuronLauncher._replace_brackets_by_loaded_parameter(parameters,
                                                                                                   loaded_parameters))

        # 6/ parameter is a dict
        parameters = {'random_dict': [{'synapse': 'synapse2', 'answers': ['absolument', '{{ name }}']},
                                      {'synapse': 'synapse3', 'answers': ['{{ name }}']}], 'default': 'synapse4'}

        sl.settings.variables = {
            "name": "nico"
        }

        expected_parameters = {
            'random_dict': [
                {'synapse': 'synapse2', 'answers': ['absolument', 'nico']},
                {'synapse': 'synapse3', 'answers': ['nico']}], 'default': 'synapse4'
        }

        self.assertEqual(expected_parameters, NeuronLauncher._replace_brackets_by_loaded_parameter(parameters,
                                                                                                   loaded_parameters))

        # 7/ parameter is a dict with a restricted word
        parameters = {'from_answer_link': [{'synapse': 'synapse2', 'answers': ['absolument', '{{ name }}']},
                                      {'synapse': 'synapse3', 'answers': ['{{ name }}']}], 'default': 'synapse4'}

        sl.settings.variables = {
            "name": "nico"
        }

        expected_parameters = {
            'from_answer_link': [
                {'synapse': 'synapse2', 'answers': ['absolument', '{{ name }}']},
                {'synapse': 'synapse3', 'answers': ['{{ name }}']}], 'default': 'synapse4'
        }

        self.assertEqual(expected_parameters, NeuronLauncher._replace_brackets_by_loaded_parameter(parameters,
                                                                                                   loaded_parameters))
    def test_replace_brackets_by_loaded_parameter(self):
        # -------------------
        # test with string
        # -------------------
        # the target value to replace is present in the loaded parameter dict
        neuron_parameters = {"param1": "this is a value {{ replaced }}"}

        loaded_parameters = {"replaced": "replaced successfully"}

        expected_result = {"param1": "this is a value replaced successfully"}

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # the target value with unicode to replace is present in the loaded parameter dict
        neuron_parameters = {"param1": "this is a value {{ replaced }}"}

        loaded_parameters = {"replaced": u"rêmpläcée successfülly"}

        expected_result = {"param1": "this is a value rêmpläcée successfülly"}

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # the target value to replace is NOT present in the loaded parameter dict
        neuron_parameters = {"param1": "this is a value {{ replaced }}"}

        loaded_parameters = {"not_exist": "replaced successfully"}

        with self.assertRaises(NeuronParameterNotAvailable):
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters)

        # one parameter doesn't contains bracket, the other one do
        neuron_parameters = {
            "param1": "this is a value {{ replaced }}",
            "param2": "value"
        }

        loaded_parameters = {"replaced": "replaced successfully"}

        expected_result = {
            "param1": "this is a value replaced successfully",
            "param2": "value"
        }

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # parameters are integer or boolean
        neuron_parameters = {"param1": 1, "param2": True}

        loaded_parameters = {"replaced": "replaced successfully"}

        expected_result = {"param1": 1, "param2": True}

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # parameters are say_template or file template. Should not be altered by the loader
        neuron_parameters = {
            "say_template": "{{output}}",
            "file_template": "here is a file"
        }

        loaded_parameters = {"output": "should not be used"}

        expected_result = {
            "say_template": "{{output}}",
            "file_template": "here is a file"
        }

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # replacing with variable
        sl = SettingLoader()
        sl.settings.variables = {
            "replaced": {
                "name": u'replaced successfully'
            }
        }

        neuron_parameters = {
            "param1": "this is a value {{ replaced['name'] }}"
        }

        loaded_parameters = {"name": "replaced successfully"}

        expected_result = {"param1": "this is a value replaced successfully"}

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # the parameter is a reserved key. for example from_answer_link from the neurotransmitter
        list_reserved_keys = [
            "say_template", "file_template", "kalliope_memory",
            "from_answer_link"
        ]

        for reserved_key in list_reserved_keys:
            neuron_parameters = {
                reserved_key: "this is a value with {{ 'brackets '}}"
            }

            loaded_parameters = dict()

            expected_result = {
                reserved_key: "this is a value with {{ 'brackets '}}"
            }

            self.assertEqual(
                expected_result,
                NeuronLauncher._replace_brackets_by_loaded_parameter(
                    neuron_parameters, loaded_parameters))

        ####
        # tests with global variables
        ####

        # 1/ only one global variable
        sl = SettingLoader()
        sl.settings.variables = {
            "hello": "test",
            "hello2": "test2",
        }

        parameters = {'var1': '{{hello}}'}

        expected_parameters = {'var1': 'test'}

        self.assertEqual(
            expected_parameters,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                parameters, loaded_parameters))

        # 2/ global variable with string after
        sl.settings.variables = {
            "hello": "test",
            "hello2": "test2",
        }

        parameters = {'var1': '{{hello}} Sispheor'}

        expected_parameters = {'var1': 'test Sispheor'}

        self.assertEqual(
            expected_parameters,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                parameters, loaded_parameters))

        # 3/ global variable with int after
        parameters = {'var1': '{{hello}}0'}
        sl.settings.variables = {
            "hello": 60,
            "hello2": "test2",
        }

        expected_parameters = {'var1': '600'}

        self.assertEqual(
            expected_parameters,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                parameters, loaded_parameters))

        # 4/ multiple global variables
        parameters = {'var1': '{{hello}} {{me}}'}
        sl.settings.variables = {"hello": "hello", "me": "LaMonf"}

        expected_parameters = {'var1': 'hello LaMonf'}

        self.assertEqual(
            expected_parameters,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                parameters, loaded_parameters))

        # 5/ parameter value is a list
        parameters = {'var1': '[hello {{name}}, bonjour {{name}}]'}
        sl.settings.variables = {
            "name": "LaMonf",
            "hello2": "test2",
        }

        expected_parameters = {'var1': '[hello LaMonf, bonjour LaMonf]'}

        self.assertEqual(
            expected_parameters,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                parameters, loaded_parameters))

        # 6/ parameter is a dict
        parameters = {
            'random_dict': [{
                'synapse': 'synapse2',
                'answers': ['absolument', '{{ name }}']
            }, {
                'synapse': 'synapse3',
                'answers': ['{{ name }}']
            }],
            'default':
            'synapse4'
        }

        sl.settings.variables = {"name": "nico"}

        expected_parameters = {
            'random_dict': [{
                'synapse': 'synapse2',
                'answers': ['absolument', 'nico']
            }, {
                'synapse': 'synapse3',
                'answers': ['nico']
            }],
            'default':
            'synapse4'
        }

        self.assertEqual(
            expected_parameters,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                parameters, loaded_parameters))

        # 7/ parameter is a dict with a restricted word
        parameters = {
            'from_answer_link': [{
                'synapse': 'synapse2',
                'answers': ['absolument', '{{ name }}']
            }, {
                'synapse': 'synapse3',
                'answers': ['{{ name }}']
            }],
            'default':
            'synapse4'
        }

        sl.settings.variables = {"name": "nico"}

        expected_parameters = {
            'from_answer_link': [{
                'synapse': 'synapse2',
                'answers': ['absolument', '{{ name }}']
            }, {
                'synapse': 'synapse3',
                'answers': ['{{ name }}']
            }],
            'default':
            'synapse4'
        }

        self.assertEqual(
            expected_parameters,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                parameters, loaded_parameters))
    def test_replace_brackets_by_loaded_parameter(self):
        # -------------------
        # test with string
        # -------------------
        # the target value to replace is present in the loaded parameter dict
        neuron_parameters = {"param1": "this is a value {{ replaced }}"}

        loaded_parameters = {"replaced": "replaced successfully"}

        expected_result = {"param1": "this is a value replaced successfully"}

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # the target value with unicode to replace is present in the loaded parameter dict
        neuron_parameters = {"param1": "this is a value {{ replaced }}"}

        loaded_parameters = {"replaced": u"rêmpläcée successfülly"}

        expected_result = {"param1": "this is a value rêmpläcée successfülly"}

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # the target value to replace is NOT present in the loaded parameter dict
        neuron_parameters = {"param1": "this is a value {{ replaced }}"}

        loaded_parameters = {"not_exist": "replaced successfully"}

        with self.assertRaises(NeuronParameterNotAvailable):
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters)

        # one parameter doesn't contains bracket, the other one do
        neuron_parameters = {
            "param1": "this is a value {{ replaced }}",
            "param2": "value"
        }

        loaded_parameters = {"replaced": "replaced successfully"}

        expected_result = {
            "param1": "this is a value replaced successfully",
            "param2": "value"
        }

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # parameters are integer or boolean
        neuron_parameters = {"param1": 1, "param2": True}

        loaded_parameters = {"replaced": "replaced successfully"}

        expected_result = {"param1": 1, "param2": True}

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))

        # parameters are say_template or file template. Should not be altered by the loader
        neuron_parameters = {
            "say_template": "{{output}}",
            "file_template": "here is a file"
        }

        loaded_parameters = {"output": "should not be used"}

        expected_result = {
            "say_template": "{{output}}",
            "file_template": "here is a file"
        }

        self.assertEqual(
            expected_result,
            NeuronLauncher._replace_brackets_by_loaded_parameter(
                neuron_parameters, loaded_parameters))