def test_remove_spaces_in_brackets(self):
        """
        Test the Utils remove_spaces_in_brackets
        """

        sentence = "This is the {{ bracket   }}"
        expected_result = "This is the {{bracket}}"
        self.assertEqual(Utils.remove_spaces_in_brackets(sentence=sentence),
                         expected_result,
                         "Fail to remove spaces in one bracket")

        sentence = "This is the {{ bracket   }} {{  second     }}"
        expected_result = "This is the {{bracket}} {{second}}"
        self.assertEqual(Utils.remove_spaces_in_brackets(sentence=sentence),
                         expected_result,
                         "Fail to remove spaces in two brackets")

        # test with json
        sentence = "{\"params\": {\"apikey\": \"ISNOTMYPASSWORD\", " \
                   "\"query\": \"met le chauffage a {{ valeur }} degres\"}}"
        expected_result = "{\"params\": {\"apikey\": \"ISNOTMYPASSWORD\", " \
                          "\"query\": \"met le chauffage a {{valeur}} degres\"}}"
        self.assertEqual(Utils.remove_spaces_in_brackets(sentence=sentence),
                         expected_result,
                         "Fail to remove spaces in two brackets")
Beispiel #2
0
    def test_remove_spaces_in_brackets(self):
        """
        Test the Utils remove_spaces_in_brackets
        """

        sentence = "This is the {{ bracket   }}"
        expected_result = "This is the {{bracket}}"
        self.assertEqual(Utils.remove_spaces_in_brackets(sentence=sentence),
                         expected_result,
                         "Fail to remove spaces in one bracket")

        sentence = "This is the {{ bracket   }} {{  second     }}"
        expected_result = "This is the {{bracket}} {{second}}"
        self.assertEqual(Utils.remove_spaces_in_brackets(sentence=sentence),
                         expected_result,
                         "Fail to remove spaces in two brackets")

        # test with json
        sentence = "{\"params\": {\"apikey\": \"ISNOTMYPASSWORD\", " \
                   "\"query\": \"met le chauffage a {{ valeur }} degres\"}}"
        expected_result = "{\"params\": {\"apikey\": \"ISNOTMYPASSWORD\", " \
                          "\"query\": \"met le chauffage a {{valeur}} degres\"}}"
        self.assertEqual(Utils.remove_spaces_in_brackets(sentence=sentence),
                         expected_result,
                         "Fail to remove spaces in two brackets")
Beispiel #3
0
    def test_remove_spaces_in_brackets(self):
        """
        Test the Utils remove_spaces_in_brackets
        """

        sentence = "This is the {{ bracket   }}"
        expected_result = "This is the {{bracket}}"
        self.assertEqual(Utils.remove_spaces_in_brackets(sentence=sentence),
                         expected_result,
                         "Fail to remove spaces in one bracket")

        sentence = "This is the {{ bracket   }} {{  second     }}"
        expected_result = "This is the {{bracket}} {{second}}"
        self.assertEqual(Utils.remove_spaces_in_brackets(sentence=sentence),
                         expected_result,
                         "Fail to remove spaces in two brackets")
Beispiel #4
0
 def _neuron_parameters_are_available_in_loaded_parameters(
         string_parameters, loaded_parameters):
     """
     Check that all parameters in brackets are available in the loaded_parameters dict
     
     E.g:
     string_parameters = "this is a {{ parameter1 }}"
     
     Will return true if the loaded_parameters looks like the following
     loaded_parameters { "parameter1": "a value"}        
     
     :param string_parameters: The string that contains one or more parameters in brace brackets
     :param loaded_parameters: Dict of parameter
     :return: True if all parameters in brackets have an existing key in loaded_parameters dict
     """
     list_parameters_with_brackets = Utils.find_all_matching_brackets(
         string_parameters)
     # remove brackets to keep only the parameter name
     for parameter_with_brackets in list_parameters_with_brackets:
         parameter = Utils.remove_spaces_in_brackets(
             parameter_with_brackets)
         parameter = parameter.replace("{{", "").replace("}}", "")
         if loaded_parameters is None or parameter not in loaded_parameters:
             Utils.print_danger(
                 "The parameter %s is not available in the order" %
                 str(parameter))
             return False
     return True
Beispiel #5
0
    def _associate_order_params_to_values(order, order_to_check):
        """
        Associate the variables from the order to the incoming user order
        :param order_to_check: the order to check incoming from the brain
        :type order_to_check: str
        :param order: the order from user
        :type order: str
        :return: the dict corresponding to the key / value of the params
        """
        logger.debug(
            "[OrderAnalyser._associate_order_params_to_values] user order: %s, "
            "order to check: %s" % (order, order_to_check))

        list_word_in_order = Utils.remove_spaces_in_brackets(
            order_to_check).split()

        # get the order, defined by the first words before {{
        # /!\ Could be empty if order starts with double brace
        the_order = order_to_check[:order_to_check.find('{{')]

        # remove sentence before order which are sentences not matching anyway
        truncate_user_sentence = order[order.find(the_order):]
        truncate_list_word_said = truncate_user_sentence.split()

        # make dict var:value
        dict_var = dict()
        for idx, ow in enumerate(list_word_in_order):
            if Utils.is_containing_bracket(ow):
                # remove bracket and grab the next value / stop value
                var_name = ow.replace("{{", "").replace("}}", "")
                stop_value = Utils.get_next_value_list(
                    list_word_in_order[idx:])
                if stop_value is None:
                    dict_var[var_name] = " ".join(truncate_list_word_said)
                    break
                for word_said in truncate_list_word_said:
                    if word_said == stop_value:
                        break
                    if var_name in dict_var:
                        dict_var[var_name] += " " + word_said
                        truncate_list_word_said = truncate_list_word_said[1:]
                    else:
                        dict_var[var_name] = word_said
            truncate_list_word_said = truncate_list_word_said[1:]
        return dict_var
Beispiel #6
0
 def _neuron_parameters_are_available_in_loaded_parameters(string_parameters, loaded_parameters):
     """
     Check that all parameters in brackets are available in the loaded_parameters dict
     
     E.g:
     string_parameters = "this is a {{ parameter1 }}"
     
     Will return true if the loaded_parameters looks like the following
     loaded_parameters { "parameter1": "a value"}        
     
     :param string_parameters: The string that contains one or more parameters in brace brackets
     :param loaded_parameters: Dict of parameter
     :return: True if all parameters in brackets have an existing key in loaded_parameters dict
     """
     list_parameters_with_brackets = Utils.find_all_matching_brackets(string_parameters)
     # remove brackets to keep only the parameter name
     for parameter_with_brackets in list_parameters_with_brackets:
         parameter = Utils.remove_spaces_in_brackets(parameter_with_brackets)
         parameter = parameter.replace("{{", "").replace("}}", "")
         if loaded_parameters is None or parameter not in loaded_parameters:
             Utils.print_danger("The parameter %s is not available in the order" % str(parameter))
             return False
     return True