def _replace_brackets_by_loaded_parameter(cls, neuron_parameters, loaded_parameters): """ Receive a value (which can be a str or dict or list) and instantiate value in double brace bracket by the value specified in the loaded_parameters dict. This method will call itself until all values has been instantiated :param neuron_parameters: value to instantiate. Str or dict or list :param loaded_parameters: dict of parameters """ logger.debug("[NeuronLauncher] replacing brackets from %s, using %s" % (neuron_parameters, loaded_parameters)) # add variables from the short term memory to the list of loaded parameters that can be used in a template # the final dict is added into a key "kalliope_memory" to not override existing keys loaded form the order memory_dict = dict() memory_dict["kalliope_memory"] = Cortex.get_memory() if loaded_parameters is None: loaded_parameters = dict( ) # instantiate an empty dict in order to be able to add memory in it loaded_parameters.update(memory_dict) if isinstance(neuron_parameters, str) or isinstance( neuron_parameters, six.text_type): # replace bracket parameter only if the str contains brackets if Utils.is_containing_bracket(neuron_parameters): # check that the parameter to replace is available in the loaded_parameters dict if cls._neuron_parameters_are_available_in_loaded_parameters( neuron_parameters, loaded_parameters): # add parameters from global variable into the final loaded parameter dict settings = cls.load_settings() loaded_parameters.update(settings.variables) neuron_parameters = jinja2.Template( neuron_parameters).render(loaded_parameters) neuron_parameters = Utils.encode_text_utf8( neuron_parameters) return str(neuron_parameters) else: raise NeuronParameterNotAvailable return neuron_parameters if isinstance(neuron_parameters, dict): returned_dict = dict() for key, value in neuron_parameters.items(): # following keys are reserved by kalliope core if key in "say_template" or key in "file_template" or key in "kalliope_memory" \ or key in "from_answer_link": returned_dict[key] = value else: returned_dict[ key] = cls._replace_brackets_by_loaded_parameter( value, loaded_parameters) return returned_dict if isinstance(neuron_parameters, list): returned_list = list() for el in neuron_parameters: templated_value = cls._replace_brackets_by_loaded_parameter( el, loaded_parameters) returned_list.append(templated_value) return returned_list # in all other case (boolean or int for example) we return the value as it return neuron_parameters
def test_get_memory(self): test_memory = { "key1": "value1", "key2": "value2" } Cortex.memory = test_memory self.assertDictEqual(test_memory, Cortex.get_memory())
def _replace_brackets_by_loaded_parameter(cls, neuron_parameters, loaded_parameters=dict()): """ Receive a value (which can be a str or dict or list) and instantiate value in double brace bracket by the value specified in the loaded_parameters dict. This method will call itself until all values has been instantiated :param neuron_parameters: value to instantiate. Str or dict or list :param loaded_parameters: dict of parameters """ logger.debug("[NeuronLauncher] replacing brackets from %s, using %s" % (neuron_parameters, loaded_parameters)) if isinstance(neuron_parameters, str) or isinstance(neuron_parameters, six.text_type): # replace bracket parameter only if the str contains brackets if Utils.is_containing_bracket(neuron_parameters): settings = cls.load_settings() # Priority to memory over the variables loaded_parameters.update(settings.variables) memory_dict = dict() # add variables from the short term memory to the list of loaded parameters that can be used in a template # the final dict is added into a key "kalliope_memory" to not override existing keys loaded form the order memory_dict["kalliope_memory"] = Cortex.get_memory() loaded_parameters.update(memory_dict) # check that the parameter to replace is available in the loaded_parameters dict if cls._neuron_parameters_are_available_in_loaded_parameters(neuron_parameters, loaded_parameters): # add parameters from global variable into the final loaded parameter dict neuron_parameters = jinja2.Template(neuron_parameters).render(loaded_parameters) neuron_parameters = Utils.encode_text_utf8(neuron_parameters) return str(neuron_parameters) else: raise NeuronParameterNotAvailable return neuron_parameters if isinstance(neuron_parameters, dict): returned_dict = dict() for key, value in neuron_parameters.items(): # following keys are reserved by kalliope core if key in "say_template" or key in "file_template" or key in "kalliope_memory" \ or key in "from_answer_link": returned_dict[key] = value else: returned_dict[key] = cls._replace_brackets_by_loaded_parameter(value, loaded_parameters) return returned_dict if isinstance(neuron_parameters, list): returned_list = list() for el in neuron_parameters: templated_value = cls._replace_brackets_by_loaded_parameter(el, loaded_parameters) returned_list.append(templated_value) return returned_list # in all other case (boolean or int for example) we return the value as it return neuron_parameters
def _get_file_template(cls, file_template, message_dict): real_file_template_path = Utils.get_real_file_path(file_template) if real_file_template_path is None: raise TemplateFileNotFoundException( "Template file %s not found in templates folder" % real_file_template_path) # load the content of the file as template t = Template(cls._get_content_of_file(real_file_template_path)) # add kalliope memory final_message_dict = dict() final_message_dict["kalliope_memory"] = Cortex.get_memory() if message_dict: final_message_dict.update(**message_dict) returned_message = t.render(final_message_dict) return returned_message
def test_get_memory(self): test_memory = {"key1": "value1", "key2": "value2"} Cortex.memory = test_memory self.assertDictEqual(test_memory, Cortex.get_memory())