Browse Source

string/bytes fixes

Julien Schueller 8 năm trước cách đây
mục cha
commit
a6d2b177a8

+ 3 - 3
Tests/test_file_manager.py

@@ -81,7 +81,7 @@ class TestFileManager(unittest.TestCase):
 
         # Test FileManager.file_is_empty
         with open(file_path, "wb") as file_open:
-            file_open.write("")
+            file_open.write(b"")
             file_open.close()
         self.assertTrue(FileManager.file_is_empty(file_path=file_path),
                         "Fail matching to verify that file is empty ")
@@ -107,7 +107,7 @@ class TestFileManager(unittest.TestCase):
         # Test to remove the file
         # FileManager.remove_file
         with open(file_path, "wb") as file_open:
-            file_open.write("")
+            file_open.write(b"")
             file_open.close()
         FileManager.remove_file(file_path=file_path)
         self.assertFalse(os.path.exists(file_path),
@@ -157,7 +157,7 @@ class TestFileManager(unittest.TestCase):
 
         # Test the file exist and creatable : return True
         with open(file_path, "wb") as file_open:
-            file_open.write("[Kalliope] Test Running the test_is_path_exists_or_creatable method")
+            file_open.write(b"[Kalliope] Test Running the test_is_path_exists_or_creatable method")
             file_open.close()
         self.assertTrue(FileManager.is_path_exists_or_creatable(file_path),
                         "Fail to assert the file exist ")

+ 1 - 1
kalliope/core/ConfigurationManager/ConfigurationChecker.py

@@ -288,6 +288,6 @@ class ConfigurationChecker:
             synapse_name = synapse.name.encode('utf-8')
             if synapse_name in seen:
                 raise MultipleSameSynapseName("Multiple synapse found with the same name: %s" % synapse_name)
-            seen.add(synapse.name)
+            seen.add(synapse_name)
 
         return True

+ 9 - 3
kalliope/core/NeuronModule.py

@@ -133,9 +133,15 @@ class NeuronModule(object):
 
         tts_message = None
 
-        if isinstance(message, str) or isinstance(message, unicode):
-            logger.debug("message is string")
-            tts_message = message
+        if sys.version_info[0] >= 3:
+            if isinstance(message, bytes) or isinstance(message, str):
+                logger.debug("message is string")
+                tts_message = message
+        else:
+            if isinstance(message, str) or isinstance(message, unicode):
+                logger.debug("message is string")
+                tts_message = message
+
 
         if isinstance(message, list):
             logger.debug("message is list")

+ 1 - 0
kalliope/core/OrderAnalyser.py

@@ -1,6 +1,7 @@
 # coding: utf8
 import collections
 from collections import Counter
+import sys
 
 from kalliope.core.Models.MatchedSynapse import MatchedSynapse
 from kalliope.core.Utils.Utils import Utils

+ 7 - 2
kalliope/core/TTS/TTSModule.py

@@ -2,6 +2,7 @@
 import hashlib
 import logging
 import os
+import sys
 
 from kalliope.core.ConfigurationManager import SettingLoader
 from kalliope.core.Players import Mplayer
@@ -132,8 +133,12 @@ class TTSModule(object):
         :param words: Text to convert into md5 hash
         :return: String md5 hash from the received words
         """
-        if isinstance(words, unicode):
-            words = words.encode('utf-8')
+        if sys.version_info[0] >= 3:
+            if isinstance(words, str):
+                words = words.encode('utf-8')
+        else:
+            if isinstance(words, unicode):
+                words = words.encode('utf-8')
         return hashlib.md5(words).hexdigest()
 
     @staticmethod

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

@@ -36,7 +36,7 @@ class FileManager:
         """
         try:
             with open(file_path, "wb") as file_open:
-                file_open.write(content)
+                file_open.write(content.encode())
                 file_open.close()
             return not FileManager.file_is_empty(file_path)
         except IOError as e:

+ 1 - 1
kalliope/neurons/script/script.py

@@ -39,7 +39,7 @@ class Script(NeuronModule):
             if not self.async:
                 p = subprocess.Popen(self.path, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
                 (output, err) = p.communicate()
-                self.output = output
+                self.output = output.decode()
                 self.returncode = p.returncode
                 message = {
                     "output": self.output,

+ 1 - 1
kalliope/neurons/shell/shell.py

@@ -50,7 +50,7 @@ class Shell(NeuronModule):
             if not self.async:
                 p = subprocess.Popen(self.cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
                 (output, err) = p.communicate()
-                self.output = output
+                self.output = output.decode()
                 self.returncode = p.returncode
                 message = {
                     "output": self.output,

+ 3 - 3
kalliope/neurons/uri/tests/test_uri_neuron.py

@@ -25,7 +25,7 @@ class TestUri(unittest.TestCase):
         self.assertEqual(uri_neuron.text, expected_content)
 
     def testGetRaw(self):
-        expected_content = 'raw line'
+        expected_content = b'raw line'
         httpretty.enable()
         httpretty.register_uri(httpretty.GET, self.test_url, body=expected_content)
         parameters = {
@@ -151,7 +151,7 @@ class TestUri(unittest.TestCase):
         :return:
         """
         def request_callback(request, url, headers):
-            data = json.loads(request.body)
+            data = json.loads(request.body.decode())
             if "title" in data and "body" in data and "userId" in data:
                 return 200, headers, "all key received from URL %s" % url
 
@@ -178,7 +178,7 @@ class TestUri(unittest.TestCase):
         :return:
         """
         def request_callback(request, url, headers):
-            data = json.loads(request.body)
+            data = json.loads(request.body.decode())
             if "title" in data and "body" in data and "userId" in data:
                 return 200, headers, "all key received from URL %s" % url
 

+ 1 - 1
kalliope/neurons/uri/uri.py

@@ -127,7 +127,7 @@ class Uri(NeuronModule):
         self.content = r.content
         # we try to load into a json object the content. So Kalliope can use it to talk
         try:
-            self.content = json.loads(self.content)
+            self.content = json.loads(self.content.decode())
         except ValueError:
             logger.debug(self.neuron_name + "cannot get a valid json from returned content")
             pass