Преглед изворни кода

Merge pull request #225 from kalliope-project/global_parameters

#203 Global parameters update to load while brain is loaded
Nicolas Marcq пре 8 година
родитељ
комит
0ffe7ac667

+ 153 - 1
Tests/test_brain_loader.py

@@ -9,6 +9,7 @@ from kalliope.core.Models import Neuron
 from kalliope.core.Models import Synapse
 from kalliope.core.Models import Order
 from kalliope.core.Models.Brain import Brain
+from kalliope.core.Models.Settings import Settings
 
 
 class TestBrainLoader(unittest.TestCase):
@@ -63,12 +64,37 @@ class TestBrainLoader(unittest.TestCase):
         self.assertEqual(brain, brain_loader.brain)
 
     def test_get_neurons(self):
+        """
+        Test to get neurons from the brainLoader
+        scenarii:
+            - 1/ get a simple neuron from the brainloader
+            - 2/ get a neuron with global variables as parameters
+        """
+        # 1/ get a simple neuron from the brainloader
+        st = Settings()
         neuron_list = [{'say': {'message': ['test message']}}]
 
         neuron = Neuron(name='say', parameters={'message': ['test message']})
 
         bl = BrainLoader(file_path=self.brain_to_test)
-        neurons_from_brain_loader = bl._get_neurons(neuron_list)
+        neurons_from_brain_loader = bl._get_neurons(neuron_list,
+                                                    settings=st)
+
+        self.assertEqual([neuron], neurons_from_brain_loader)
+
+        # 2/ get a neuron with global variables as parameters
+        neuron_list = [{'say': {'message': ['bonjour {{name}}']}}]
+        variables = {
+            "author": "Lamonf",
+            "test_number": 60,
+            "name": "kalliope"
+        }
+        st = Settings(variables=variables)
+        bl = BrainLoader(file_path=self.brain_to_test)
+        neurons_from_brain_loader = bl._get_neurons(neuron_list,
+                                                    settings=st)
+
+        neuron = Neuron(name='say', parameters={'message': ['bonjour kalliope']})
 
         self.assertEqual([neuron], neurons_from_brain_loader)
 
@@ -103,5 +129,131 @@ class TestBrainLoader(unittest.TestCase):
 
         self.assertTrue(bl1.brain is bl2.brain)
 
+    def test_replace_global_variables(self):
+        """
+        Testing the _replace_global_variables function from the NeuronLauncher.
+        Scenarii:
+            - 1/ only one global variable
+            - 2/ global variable with string after
+            - 3/ global variable with int after
+            - 4/ multiple global variables
+            - 5/ parameter value is a list
+
+        """
+
+        # 1/ only one global variable
+        parameters={
+            'var1': '{{hello}}'
+        }
+
+        variables = {
+            "hello": "test",
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_parameters={
+            'var1': 'test'
+        }
+
+        self.assertEquals(BrainLoader._replace_global_variables(parameters=parameters,
+                                                                settings=st),
+                          expected_parameters,
+                          "Fail to assign a single global variable to parameters")
+
+        # 2/ global variable with string after
+        parameters={
+            'var1': '{{hello}} Sispheor'
+        }
+        variables = {
+            "hello": "test",
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_parameters = {
+            'var1': 'test Sispheor'
+        }
+
+        self.assertEquals(BrainLoader._replace_global_variables(parameters=parameters,
+                                                                settings=st),
+                          expected_parameters,
+                          "Fail to assign a global variable with string after to parameters")
+
+        # 3/ global variable with int after
+        parameters={
+            'var1': '{{hello}}0'
+        }
+        variables = {
+            "hello": 60,
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_parameters={
+            'var1': '600'
+        }
+
+        self.assertEquals(BrainLoader._replace_global_variables(parameters=parameters,
+                                                                settings=st),
+                          expected_parameters,
+                          "Fail to assign global variable with int after to parameters")
+
+        # 4/ multiple global variables
+        parameters={
+            'var1': '{{hello}} {{me}}'
+        }
+        variables = {
+            "hello": "hello",
+            "me": "LaMonf"
+        }
+        st = Settings(variables=variables)
+
+        expected_parameters={
+            'var1': 'hello LaMonf'
+        }
+
+        self.assertEquals(BrainLoader._replace_global_variables(parameters=parameters,
+                                                                settings=st),
+                          expected_parameters,
+                          "Fail to assign multiple global variables to parameters")
+
+        # 5/ parameter value is a list
+        parameters={
+            'var1': '[hello {{name}}, bonjour {{name}}]'
+        }
+        variables = {
+            "name": "LaMonf",
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_parameters={
+            'var1': '[hello LaMonf, bonjour LaMonf]'
+        }
+
+        self.assertEquals(BrainLoader._replace_global_variables(parameters=parameters,
+                                                                settings=st),
+                          expected_parameters,
+                          "Fail to assign a single global when parameter value is a list to neuron")
+
+    def test_get_global_variable(self):
+        """
+        Test the get_global_variable of the OrderAnalyser Class
+        """
+        sentence = "i am {{name2}}"
+        variables = {
+            "name": "LaMonf",
+            "name2": "kalliope",
+        }
+        st = Settings(variables=variables)
+
+        expected_result = "i am kalliope"
+
+        self.assertEquals(BrainLoader._get_global_variable(sentence=sentence,
+                                                           settings=st),
+                          expected_result,
+                          "Fail to get the global variable from the sentence")
+
 if __name__ == '__main__':
     unittest.main()

+ 0 - 113
Tests/test_launchers.py

@@ -137,117 +137,4 @@ class TestLaunchers(unittest.TestCase):
                                                                  resources_dir=sl.settings.resources.neuron_folder)
             mock_get_class_instantiation.reset_mock()
 
-    def test_replace_global_variables(self):
-        """
-        Testing the _replace_global_variables function from the NeuronLauncher.
-        Scenarii:
-            - 1/ only one global variable
-            - 2/ global variable with string after
-            - 3/ global variable with int after
-            - 4/ multiple global variables
-            - 5/ parameter value is a list
-
-        """
 
-        # 1/ only one global variable
-        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}}'})
-        variables = {
-            "hello": "test",
-            "hello2": "test2",
-        }
-        st = Settings(variables=variables)
-
-        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': 'test'})
-
-        # assign global variable to neuron1
-        NeuronLauncher._replace_global_variables(neuron=neuron1,
-                                                settings=st)
-        self.assertEquals(neuron1,
-                          expected_neuron_result,
-                          "Fail to assign a single global variable to neuron")
-
-        # 2/ global variable with string after
-        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}} Sispheor'})
-        variables = {
-            "hello": "test",
-            "hello2": "test2",
-        }
-        st = Settings(variables=variables)
-
-        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': 'test Sispheor'})
-
-        # assign global variable to neuron1
-        NeuronLauncher._replace_global_variables(neuron=neuron1,
-                                                settings=st)
-        self.assertEquals(neuron1,
-                          expected_neuron_result,
-                          "Fail to assign a global variable with string after to neuron")
-
-        # 3/ global variable with int after
-        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}}0'})
-        variables = {
-            "hello": 60,
-            "hello2": "test2",
-        }
-        st = Settings(variables=variables)
-
-        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': '600'})
-
-        # assign global variable to neuron1
-        NeuronLauncher._replace_global_variables(neuron=neuron1,
-                                                settings=st)
-        self.assertEquals(neuron1,
-                          expected_neuron_result,
-                          "Fail to assign global variable with int after to neuron")
-
-        # 4/ multiple global variables
-        neuron1 = Neuron(name='neuron1', parameters={'var1': '{{hello}} {{me}}'})
-        variables = {
-            "hello": "hello",
-            "me": "LaMonf"
-        }
-        st = Settings(variables=variables)
-
-        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': 'hello LaMonf'})
-
-        # assign global variable to neuron1
-        NeuronLauncher._replace_global_variables(neuron=neuron1,
-                                                settings=st)
-        self.assertEquals(neuron1,
-                          expected_neuron_result,
-                          "Fail to assign multiple global variables to neuron")
-
-        # 5/ parameter value is a list
-        neuron1 = Neuron(name='neuron1', parameters={'var1': '[hello {{name}}, bonjour {{name}}]'})
-        variables = {
-            "name": "LaMonf",
-            "hello2": "test2",
-        }
-        st = Settings(variables=variables)
-
-        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': '[hello LaMonf, bonjour LaMonf]'})
-
-        # assign global variable to neuron1
-        NeuronLauncher._replace_global_variables(neuron=neuron1,
-                                                settings=st)
-        self.assertEquals(neuron1,
-                          expected_neuron_result,
-                          "Fail to assign a single global when parameter value is a list to neuron")
-
-    def test_get_global_variable(self):
-        """
-        Test the get_global_variable of the OrderAnalyser Class
-        """
-        sentence = "i am {{name2}}"
-        variables = {
-            "name": "LaMonf",
-            "name2": "kalliope",
-        }
-        st = Settings(variables=variables)
-
-        expected_result = "i am kalliope"
-
-        self.assertEquals(NeuronLauncher._get_global_variable(sentence=sentence,
-                                                             settings=st),
-                          expected_result,
-                          "Fail to get the global variable from the sentence")

+ 56 - 5
kalliope/core/ConfigurationManager/BrainLoader.py

@@ -4,6 +4,7 @@ import os
 
 from YAMLLoader import YAMLLoader
 from kalliope.core.Utils import Utils
+from kalliope.core.ConfigurationManager import SettingLoader
 from kalliope.core.ConfigurationManager.ConfigurationChecker import ConfigurationChecker
 from kalliope.core.Models import Singleton
 from kalliope.core.Models.Brain import Brain
@@ -29,6 +30,9 @@ class BrainLoader(object):
     __metaclass__ = Singleton
 
     def __init__(self, file_path=None):
+        sl = SettingLoader()
+        self.settings = sl.settings
+
         self.file_path = file_path
         if self.file_path is None:  # we don't provide a file path, so search for the default one
             self.file_path = Utils.get_real_file_path(FILE_NAME)
@@ -85,7 +89,7 @@ class BrainLoader(object):
                 if ConfigurationChecker().check_synape_dict(synapses_dict):
                     # print "synapses_dict ok"
                     name = synapses_dict["name"]
-                    neurons = self._get_neurons(synapses_dict["neurons"])
+                    neurons = self._get_neurons(synapses_dict["neurons"], self.settings)
                     signals = self._get_signals(synapses_dict["signals"])
                     new_synapse = Synapse(name=name, neurons=neurons, signals=signals)
                     synapses.append(new_synapse)
@@ -100,13 +104,14 @@ class BrainLoader(object):
 
         return brain
 
-    @staticmethod
-    def _get_neurons(neurons_dict):
+    @classmethod
+    def _get_neurons(cls, neurons_dict, settings):
         """
         Get a list of Neuron object from a neuron dict
 
         :param neurons_dict: Neuron name or dictionary of Neuron_name/Neuron_parameters
         :type neurons_dict: String or dict
+        :param settings:  The Settings with the global variables
         :return: A list of Neurons
         :rtype: List
 
@@ -127,7 +132,11 @@ class BrainLoader(object):
 
                         name = neuron_name
                         parameters = neuron_dict[name]
-                        # print parameters
+
+                        # Update brackets
+                        parameters = cls._replace_global_variables(parameters=parameters,
+                                                                   settings=settings)
+
                         new_neuron = Neuron(name=name, parameters=parameters)
                         neurons.append(new_neuron)
             else:
@@ -234,4 +243,46 @@ class BrainLoader(object):
         second = get_key("second")
 
         return Event(year=year, month=month, day=day, week=week,
-                     day_of_week=day_of_week, hour=hour, minute=minute, second=second)
+                     day_of_week=day_of_week, hour=hour, minute=minute, second=second)
+
+    @classmethod
+    def _replace_global_variables(cls, parameters, settings):
+        """
+        Replace all the parameters with variables with the variable value.
+        :param parameters: the parameters dict with brackets
+        :param settings: the settings
+        :return: the parameter dict
+        """
+        for param in parameters:
+            if isinstance(parameters[param], list):
+                list_param_value = list()
+                for sentence in parameters[param]:
+                    sentence_with_global_variables = cls._get_global_variable(sentence=sentence,
+                                                                              settings=settings)
+                    list_param_value.append(sentence_with_global_variables)
+                parameters[param] = list_param_value
+
+            else:
+                if Utils.is_containing_bracket(parameters[param]):
+                    sentence_with_global_variables = cls._get_global_variable(sentence=parameters[param],
+                                                                              settings=settings)
+                    parameters[param] = sentence_with_global_variables
+        return parameters
+
+    @staticmethod
+    def _get_global_variable(sentence, settings):
+        """
+        Get the global variable from the sentence with brackets
+        :param sentence: the sentence to check
+        :return: the global variable
+        """
+        sentence_no_spaces = Utils.remove_spaces_in_brackets(sentence=sentence)
+        list_of_bracket_params = Utils.find_all_matching_brackets(sentence=sentence_no_spaces)
+        for param_with_bracket in list_of_bracket_params:
+            param_no_brackets = param_with_bracket.replace("{{", "").replace("}}", "")
+            if param_no_brackets in settings.variables:
+                logger.debug("Replacing variable %s with  %s" % (param_with_bracket,
+                                                                 settings.variables[param_no_brackets]))
+                sentence_no_spaces = sentence_no_spaces.replace(param_with_bracket,
+                                                                str(settings.variables[param_no_brackets]))
+        return sentence_no_spaces

+ 9 - 0
kalliope/core/ConfigurationManager/YAMLLoader.py

@@ -14,6 +14,13 @@ class YAMLFileNotFound(Exception):
     pass
 
 
+class YAMLFileEmpty(Exception):
+    """
+    YAML file empty
+    """
+    pass
+
+
 class YAMLLoader:
     """
     Simple Class to Verify / Load a YAML file.
@@ -67,6 +74,8 @@ class IncludeImport(object):
         # load the yaml file
         self.data = yaml.load(open(file_path, 'r'))
 
+        if self.data is None:
+            raise YAMLFileEmpty("File %s is empty" % file_path)
         # add included brain
         if isinstance(self.data, list):
             for el in self.data:

+ 0 - 41
kalliope/core/NeuronLauncher.py

@@ -27,49 +27,8 @@ class NeuronLauncher:
         if settings.resources:
             neuron_folder = settings.resources.neuron_folder
 
-        cls._replace_global_variables(neuron, settings)
-
         return Utils.get_dynamic_class_instantiation(package_name="neurons",
                                                      module_name=neuron.name,
                                                      parameters=neuron.parameters,
                                                      resources_dir=neuron_folder)
 
-    @classmethod
-    def _replace_global_variables(cls, neuron, settings):
-        """
-        Replace all the parameters with variables with the variable value.
-        :param neuron: the neuron
-        :param settings: the settings
-        """
-        for param in neuron.parameters:
-            if isinstance(neuron.parameters[param], list):
-                list_param_value = list()
-                for sentence in neuron.parameters[param]:
-                    sentence_with_global_variables = cls._get_global_variable(sentence=sentence,
-                                                                              settings=settings)
-                    list_param_value.append(sentence_with_global_variables)
-                neuron.parameters[param] = list_param_value
-
-            else:
-                if Utils.is_containing_bracket(neuron.parameters[param]):
-                    sentence_with_global_variables = cls._get_global_variable(sentence=neuron.parameters[param],
-                                                                              settings=settings)
-                    neuron.parameters[param] = sentence_with_global_variables
-
-    @staticmethod
-    def _get_global_variable(sentence, settings):
-        """
-        Get the global variable from the sentence with brackets
-        :param sentence: the sentence to check
-        :return: the global variable
-        """
-        sentence_no_spaces = Utils.remove_spaces_in_brackets(sentence=sentence)
-        list_of_bracket_params = Utils.find_all_matching_brackets(sentence=sentence_no_spaces)
-        for param_with_bracket in list_of_bracket_params:
-            param_no_brackets = param_with_bracket.replace("{{", "").replace("}}", "")
-            if param_no_brackets in settings.variables:
-                logger.debug("Replacing variable %s with  %s" % (param_with_bracket,
-                                                                 settings.variables[param_no_brackets]))
-                sentence_no_spaces = sentence_no_spaces.replace(param_with_bracket,
-                                                                str(settings.variables[param_no_brackets]))
-        return sentence_no_spaces