nico 7 жил өмнө
parent
commit
6ec142a6e6

+ 13 - 1
Tests/test_cortex.py

@@ -131,7 +131,7 @@ class TestCortex(unittest.TestCase):
         self.assertDictEqual(dict(), Cortex.memory)
 
     def test_save_parameter_from_order_in_memory(self):
-        # define temp saved order parameter
+        # Test with a value that exist in the temp memory
         order_parameters = {
             "key1": "value1",
             "key2": "value2"
@@ -147,5 +147,17 @@ class TestCortex(unittest.TestCase):
 
         self.assertDictEqual(expected_dict, Cortex.memory)
 
+        # test with a value that does not exsit
+        order_parameters = {
+            "key1": "value1",
+            "key2": "value2"
+        }
+
+        Cortex.temp = order_parameters
+        dict_val_to_save = {"my_key_in_memory": "{{key3}}"}
+
+        self.assertFalse(Cortex.save_parameter_from_order_in_memory(dict_val_to_save))
+
+
 if __name__ == '__main__':
     unittest.main()

+ 5 - 2
kalliope/core/Cortex.py

@@ -100,6 +100,7 @@ class Cortex(with_metaclass(Singleton, object)):
         Save key from the temp dict (where parameters loaded from the voice order where placed temporary)
         into the memory dict
         :param order_parameters: dict of key to save.  {'key_name_in_memory': 'key_name_in_temp_dict'}
+        :return True if a value has been saved in the kalliope memory
         """
 
         if order_parameters is not None:
@@ -111,6 +112,8 @@ class Cortex(with_metaclass(Singleton, object)):
                 # ask the cortex to save in memory the target "key" if it was in the order
                 if Utils.is_containing_bracket(value):
                     # if the key exist in the temp dict we can load it with jinja
-                    if value in Cortex.temp:
-                        value = jinja2.Template(value).render(Cortex.temp)
+                    value = jinja2.Template(value).render(Cortex.temp)
+                    if value:
                         Cortex.save(key, value)
+                        return True
+        return False

+ 25 - 42
kalliope/core/RestAPI/FlaskAPI.py

@@ -161,7 +161,7 @@ class FlaskAPI(threading.Thread):
         synapse_target = BrainLoader().get_brain().get_synapse_by_name(synapse_name=synapse_name)
 
         # get no_voice_flag if present
-        no_voice = self.get_boolean_flag_from_request(request, json_name="no_voice")
+        no_voice = self.get_boolean_flag_from_request(request, boolean_flag_to_find="no_voice")
 
         # get parameters
         parameters = self.get_parameters_from_request(request)
@@ -208,7 +208,7 @@ class FlaskAPI(threading.Thread):
 
         order = request.get_json('order')
         # get no_voice_flag if present
-        no_voice = self.get_boolean_flag_from_request(request, json_name="no_voice")
+        no_voice = self.get_boolean_flag_from_request(request, boolean_flag_to_find="no_voice")
         if order is not None:
             # get the order
             order_to_run = order["order"]
@@ -321,12 +321,12 @@ class FlaskAPI(threading.Thread):
         """
 
         # find the order signal and call the mute method
-        for signal in SignalLauncher.get_launched_signals_list():
-            if isinstance(signal, Order):
-                data = {
-                    "mute": signal.get_mute_status()
-                }
-                return jsonify(data), 200
+        signal_order = SignalLauncher.get_order_instance()
+        if signal_order is not None:
+            data = {
+                "mute": signal_order.get_mute_status()
+            }
+            return jsonify(data), 200
 
         # if no Order instance
         data = {
@@ -348,38 +348,22 @@ class FlaskAPI(threading.Thread):
             abort(400)
 
         # get mute if present
-        mute = self.get_boolean_flag_from_request(request, json_name="mute")
+        mute = self.get_boolean_flag_from_request(request, boolean_flag_to_find="mute")
 
         # find the order signal and call the mute method
-        for signal in SignalLauncher.get_launched_signals_list():
-            if isinstance(signal, Order):
-                signal.set_mute_status(mute)
-                data = {
-                    "mute": signal.get_mute_status()
-                }
-                return jsonify(data), 200
+        signal_order = SignalLauncher.get_order_instance()
+        if signal_order is not None:
+            signal_order.set_mute_status(mute)
+            data = {
+                "mute": signal_order.get_mute_status()
+            }
+            return jsonify(data), 200
 
         data = {
             "error": "Cannot switch mute status"
         }
         return jsonify(error=data), 400
 
-    @requires_auth
-    def unmute(self):
-        # find the order signal and call the mute method
-        for signal in SignalLauncher.get_launched_signals_list():
-            if isinstance(signal, Order):
-                signal.set_mute_status(False)
-                data = {
-                    "mute": "False"
-                }
-                return jsonify(data), 200
-
-        data = {
-            "error": "Cannot unmute"
-        }
-        return jsonify(error=data), 400
-
     def audio_analyser_callback(self, order):
         """
         Callback of the OrderListener. Called after the processing of the audio file
@@ -402,24 +386,23 @@ class FlaskAPI(threading.Thread):
         # this boolean will notify the main process that the order have been processed
         self.order_analyser_return = True
 
-    def get_boolean_flag_from_request(self, http_request, json_name):
+    def get_boolean_flag_from_request(self, http_request, boolean_flag_to_find):
         """
-        Get the no_voice flag from the request if exist
+        Get the boolean flag from the request if exist
         :param http_request:
-        :param json_name
-        :return:
+        :param boolean_flag_to_find: json flag to find in the http_request
+        :return: True or False if the boolean flag has been found in the request
         """
-
-        no_voice = False
+        boolean_flag = False
         try:
             received_json = http_request.get_json(force=True, silent=True, cache=True)
-            if json_name in received_json:
-                no_voice = self.str_to_bool(received_json[json_name])
+            if boolean_flag_to_find in received_json:
+                boolean_flag = self.str_to_bool(received_json[boolean_flag_to_find])
         except TypeError:
             # no json received
             pass
-        logger.debug("[FlaskAPI] Boolean %s : %s" % (json_name, no_voice))
-        return no_voice
+        logger.debug("[FlaskAPI] Boolean %s : %s" % (boolean_flag_to_find, boolean_flag))
+        return boolean_flag
 
     @staticmethod
     def str_to_bool(s):

+ 12 - 0
kalliope/core/SignalLauncher.py

@@ -1,6 +1,7 @@
 import logging
 
 from kalliope import Utils
+from kalliope.signals.order import Order
 
 logging.basicConfig()
 logger = logging.getLogger("kalliope")
@@ -40,3 +41,14 @@ class SignalLauncher:
     @classmethod
     def get_launched_signals_list(cls):
         return cls.list_launched_signals
+
+    @classmethod
+    def get_order_instance(cls):
+        """
+        Return the Order instance from the list of launched signals if exist
+        :return:
+        """
+        for signal in cls.list_launched_signals:
+            if isinstance(signal, Order):
+                return signal
+        return None

+ 4 - 5
kalliope/neurons/mute/mute.py

@@ -1,8 +1,7 @@
 import logging
 
 from kalliope import SignalLauncher
-from kalliope.core.NeuronModule import NeuronModule, MissingParameterException
-from kalliope.signals.order import Order
+from kalliope.core.NeuronModule import NeuronModule
 
 logging.basicConfig()
 logger = logging.getLogger("kalliope")
@@ -17,9 +16,9 @@ class Mute(NeuronModule):
 
         # check if parameters have been provided
         if self._is_parameters_ok():
-            for signal in SignalLauncher.get_launched_signals_list():
-                if isinstance(signal, Order):
-                    signal.set_mute_status(self.status)
+            signal_order = SignalLauncher.get_order_instance()
+            if signal_order is not None:
+                signal_order.set_mute_status(self.status)
 
     def _is_parameters_ok(self):
         """