瀏覽代碼

[Refactor] Implement a mother class for Signal and Signals Exceptions

+ Update tests
ThiBuff 7 年之前
父節點
當前提交
01141fb9fd

+ 42 - 0
kalliope/core/SignalModule.py

@@ -0,0 +1,42 @@
+import logging
+from kalliope.core import Utils
+
+from kalliope.core.ConfigurationManager import BrainLoader
+
+logging.basicConfig()
+logger = logging.getLogger("kalliope")
+
+
+class MissingParameter(Exception):
+    """
+    A geolocation must contain latitude, longitude, radius.
+
+    .. seealso:: Geolocation
+    """
+    pass
+
+
+class SignalModule(object):
+    def __init__(self, **kwargs):
+        super(SignalModule, self).__init__(**kwargs)
+        # get the child who called the class
+        self.signal_name = self.__class__.__name__
+
+        Utils.print_info('Init Signal :' + self.signal_name)
+        self.brain = BrainLoader().get_brain()
+
+    def get_list_synapse(self):
+        for synapse in self.brain.synapses:
+            for signal in synapse.signals:
+                # if the signal is a child we add it to the synapses list
+                if signal.name == self.signal_name.lower(): # Lowercase !
+                    if not self.check_parameters(parameters=signal.parameters):
+                        logger.debug(
+                            "[SignalModule] The signal " + self.signal_name + " is missing mandatory parameters, check documentation")
+                        raise MissingParameter()
+                    else:
+                        yield synapse
+
+    @staticmethod
+    def check_parameters(parameters):
+        raise NotImplementedError("[SignalModule] Must override check_parameters method !")

+ 1 - 0
kalliope/core/__init__.py

@@ -9,4 +9,5 @@ from kalliope.core.SynapseLauncher import SynapseLauncher
 from kalliope.core.LIFOBuffer import LIFOBuffer
 from kalliope.core.LIFOBuffer import LIFOBuffer
 from kalliope.core.NeuronParameterLoader import NeuronParameterLoader
 from kalliope.core.NeuronParameterLoader import NeuronParameterLoader
 from kalliope.core.NeuronModule import NeuronModule
 from kalliope.core.NeuronModule import NeuronModule
+from kalliope.core.SignalModule import SignalModule
 from kalliope.core.PlayerModule import PlayerModule
 from kalliope.core.PlayerModule import PlayerModule

+ 1 - 1
kalliope/signals/geolocation/README.md

@@ -73,4 +73,4 @@ If the syntax is NOT ok, Kalliope will raise an error and log a message:
 
 
 ### Note
 ### Note
 
 
-/!\ this feature is supported by the Kalliope official smartphone application.
+/!\ this feature is supported by the [Kalliope official smartphone application.](https://github.com/kalliope-project/kalliope-app)

+ 8 - 39
kalliope/signals/geolocation/geolocation.py

@@ -1,59 +1,28 @@
 import logging
 import logging
 from threading import Thread
 from threading import Thread
 
 
-from kalliope.core import Utils
-from kalliope.core.ConfigurationManager import BrainLoader
+from kalliope.core import SignalModule
 
 
 logging.basicConfig()
 logging.basicConfig()
 logger = logging.getLogger("kalliope")
 logger = logging.getLogger("kalliope")
 
 
 
 
-class MissingParameter(Exception):
-    """
-    A geolocation must contain latitude, longitude, radius.
-
-    .. seealso:: Geolocation
-    """
-    pass
-
-
-class Geolocation(Thread):
-
-    def __init__(self):
-        super(Geolocation, self).__init__()
-        Utils.print_info('Init Geolocation')
-        self.brain = BrainLoader().get_brain()
+class Geolocation(SignalModule, Thread):
+    def __init__(self, **kwargs):
+        super(Geolocation, self).__init__(**kwargs)
 
 
     def run(self):
     def run(self):
         logger.debug("[Geolocalisation] Loading ...")
         logger.debug("[Geolocalisation] Loading ...")
-        self.list_synapses_with_geolocalion = self._get_list_synapse_with_geolocation(brain=self.brain)
-
-    @classmethod
-    def _get_list_synapse_with_geolocation(cls, brain):
-        """
-        return the list of synapse that use geolocation as signal in the provided brain
-        :param brain: Brain object that contain all synapses loaded
-        :type brain: Brain
-        :return: generator of synapse that use geolocation as signal
-        """
-        for synapse in brain.synapses:
-            for signal in synapse.signals:
-                # if the signal is an event we add it to the task list
-                if signal.name == "geolocation":
-                    if not cls._check_geolocation(parameters=signal.parameters):
-                        logger.debug("[Geolocation] The signal is missing mandatory parameters, check documentation")
-                        raise MissingParameter()
-                    else:
-                        yield synapse
-
+        self.list_synapses_with_geolocalion = list(super(Geolocation, self).get_list_synapse())
 
 
     @staticmethod
     @staticmethod
-    def _check_geolocation(parameters):
+    def check_parameters(parameters):
         """
         """
+        Overwritten method
         receive a dict of parameter from a geolocation signal and them
         receive a dict of parameter from a geolocation signal and them
         :param parameters: dict of parameters
         :param parameters: dict of parameters
         :return: True if parameters are valid
         :return: True if parameters are valid
         """
         """
         # check mandatory parameters
         # check mandatory parameters
         mandatory_parameters = ["latitude", "longitude", "radius"]
         mandatory_parameters = ["latitude", "longitude", "radius"]
-        return all(key in parameters for key in mandatory_parameters)
+        return all(key in parameters for key in mandatory_parameters)

+ 20 - 11
kalliope/signals/geolocation/tests/test_geolocalisation.py

@@ -1,35 +1,35 @@
 import unittest
 import unittest
 
 
+from kalliope.core.SignalModule import MissingParameter
 
 
 from kalliope.core.Models import Brain
 from kalliope.core.Models import Brain
 from kalliope.core.Models import Neuron
 from kalliope.core.Models import Neuron
 from kalliope.core.Models import Synapse
 from kalliope.core.Models import Synapse
 from kalliope.core.Models.Signal import Signal
 from kalliope.core.Models.Signal import Signal
 
 
-from kalliope.signals.geolocation.geolocation import Geolocation, MissingParameter
+from kalliope.signals.geolocation.geolocation import Geolocation
 
 
 
 
 class Test_Geolocation(unittest.TestCase):
 class Test_Geolocation(unittest.TestCase):
-
     def test_check_geolocation_valid(self):
     def test_check_geolocation_valid(self):
         expected_parameters = ["latitude", "longitude", "radius"]
         expected_parameters = ["latitude", "longitude", "radius"]
-        self.assertTrue(Geolocation._check_geolocation(expected_parameters))
+        self.assertTrue(Geolocation.check_parameters(expected_parameters))
 
 
     def test_check_geolocation_valid_with_other(self):
     def test_check_geolocation_valid_with_other(self):
         expected_parameters = ["latitude", "longitude", "radius", "kalliope", "random"]
         expected_parameters = ["latitude", "longitude", "radius", "kalliope", "random"]
-        self.assertTrue(Geolocation._check_geolocation(expected_parameters))
+        self.assertTrue(Geolocation.check_parameters(expected_parameters))
 
 
     def test_check_geolocation_no_radius(self):
     def test_check_geolocation_no_radius(self):
         expected_parameters = ["latitude", "longitude", "kalliope", "random"]
         expected_parameters = ["latitude", "longitude", "kalliope", "random"]
-        self.assertFalse(Geolocation._check_geolocation(expected_parameters))
+        self.assertFalse(Geolocation.check_parameters(expected_parameters))
 
 
     def test_check_geolocation_no_latitude(self):
     def test_check_geolocation_no_latitude(self):
         expected_parameters = ["longitude", "radius", "kalliope", "random"]
         expected_parameters = ["longitude", "radius", "kalliope", "random"]
-        self.assertFalse(Geolocation._check_geolocation(expected_parameters))
+        self.assertFalse(Geolocation.check_parameters(expected_parameters))
 
 
     def test_check_geolocation_no_longitude(self):
     def test_check_geolocation_no_longitude(self):
         expected_parameters = ["latitude", "radius", "kalliope", "random"]
         expected_parameters = ["latitude", "radius", "kalliope", "random"]
-        self.assertFalse(Geolocation._check_geolocation(expected_parameters))
+        self.assertFalse(Geolocation.check_parameters(expected_parameters))
 
 
     def test_get_list_synapse_with_geolocation(self):
     def test_get_list_synapse_with_geolocation(self):
         # Init
         # Init
@@ -53,7 +53,13 @@ class Test_Geolocation(unittest.TestCase):
         br = Brain(synapses=synapses_list)
         br = Brain(synapses=synapses_list)
 
 
         expected_list = [synapse1]
         expected_list = [synapse1]
-        self.assertEqual(expected_list, list(Geolocation._get_list_synapse_with_geolocation(brain=br)))
+
+        # Stubbing the Geolocation Signal with the brain
+        geo = Geolocation()
+        geo.brain = br
+        geo.run()
+
+        self.assertEqual(expected_list, geo.list_synapses_with_geolocalion)
 
 
     def test_get_list_synapse_with_raise_missing_parameters(self):
     def test_get_list_synapse_with_raise_missing_parameters(self):
         # Init
         # Init
@@ -75,10 +81,13 @@ class Test_Geolocation(unittest.TestCase):
         synapses_list = [synapse1, synapse2]
         synapses_list = [synapse1, synapse2]
         br = Brain(synapses=synapses_list)
         br = Brain(synapses=synapses_list)
 
 
+        # Stubbing the Geolocation Signal with the brain
+        geo = Geolocation()
+        geo.brain = br
+
         with self.assertRaises(MissingParameter):
         with self.assertRaises(MissingParameter):
-            # /!\ Note: impossible to call a generator method directly ! need to use a list !
-            list(Geolocation._get_list_synapse_with_geolocation(brain=br))
+            geo.run()
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    unittest.main()
+    unittest.main()