Browse Source

[Fix] #263 encoding on neuron launcher and serialize + add test

....encoding sucks ...
monf 8 years ago
parent
commit
1e43c594de

+ 18 - 0
Tests/test_neuron_launcher.py

@@ -1,3 +1,5 @@
+# coding: utf-8
+
 import unittest
 import mock
 
@@ -94,6 +96,22 @@ class TestNeuronLauncher(unittest.TestCase):
             "param1": "this is a value replaced successfully"
         }
 
+        self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
+                                                                                               loaded_parameters))
+
+        # the target value with unicode to replace is present in the loaded parameter dict
+        neuron_parameters = {
+            "param1": "this is a value {{ replaced }}"
+        }
+
+        loaded_parameters = {
+            "replaced": u"rêmpläcée successfülly"
+        }
+
+        expected_result = {
+            "param1": "this is a value rêmpläcée successfülly"
+        }
+
         self.assertEqual(expected_result, NeuronLauncher._replace_brackets_by_loaded_parameter(neuron_parameters,
                                                                                                loaded_parameters))
 

+ 2 - 0
kalliope/core/Models/APIResponse.py

@@ -16,6 +16,8 @@ class APIResponse(object):
         :return: A dict of name and parameters
         :rtype: Dict
         """
+        if isinstance(self.user_order, unicode):
+            self.user_order = self.user_order.encode("utf-8")
         return {
             'user_order': self.user_order,
             'matched_synapses': [e.serialize() for e in self.list_processed_matched_synapse],

+ 2 - 0
kalliope/core/NeuronLauncher.py

@@ -72,6 +72,8 @@ class NeuronLauncher:
                 # 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):
                     neuron_parameters = jinja2.Template(neuron_parameters).render(loaded_parameters)
+                    if isinstance(neuron_parameters, unicode):
+                        neuron_parameters = neuron_parameters.encode("utf-8")
                     return str(neuron_parameters)
                 else:
                     raise NeuronParameterNotAvailable

+ 2 - 1
kalliope/core/NeuronModule.py

@@ -113,6 +113,8 @@ class NeuronModule(object):
         :return: A dict of name and parameters
         :rtype: Dict
         """
+        if isinstance(self.tts_message, unicode):
+            self.tts_message = self.tts_message.encode("utf-8")
         return {
             'neuron_name': self.neuron_name,
             'generated_message': self.tts_message
@@ -137,7 +139,6 @@ class NeuronModule(object):
             logger.debug("message is string")
             tts_message = message
 
-
         if isinstance(message, list):
             logger.debug("message is list")
             tts_message = random.choice(message)

+ 2 - 1
kalliope/core/Utils/Utils.py

@@ -12,7 +12,8 @@ logger = logging.getLogger("kalliope")
 
 def pipe_print(line):
     if sys.version_info[0] < 3:
-        line = line.encode('utf-8')
+        if isinstance(line, unicode):
+            line = line.encode('utf-8')
     print(line)