浏览代码

[Refacto] First Refactoring review of the neuronModule method _get_message_from_dict()

monf 8 年之前
父节点
当前提交
02fc10d111
共有 1 个文件被更改,包括 34 次插入27 次删除
  1. 34 27
      kalliope/core/NeuronModule.py

+ 34 - 27
kalliope/core/NeuronModule.py

@@ -144,37 +144,44 @@ class NeuronModule(object):
         """
         returned_message = None
 
-        if (self.say_template is not None and self.file_template is None) or \
-                (self.say_template is None and self.file_template is not None):
-
-            # the user choose a say_template option
-            if self.say_template is not None:
-                if isinstance(self.say_template, list):
-                    # then we pick randomly one template
-                    self.say_template = random.choice(self.say_template)
-                t = Template(self.say_template)
-                returned_message = t.render(**message_dict)
-
-            # trick to remobe unicode problem when loading jinja template with non ascii char
-            reload(sys)
-            sys.setdefaultencoding('utf-8')
-            # the user choose a file_template option
-            if self.file_template is not None:  # the user choose a file_template option
-                real_file_template_path = Utils.get_real_file_path(self.file_template)
-
-                if os.path.isfile(real_file_template_path):
-                    # load the content of the file as template
-                    t = Template(self._get_content_of_file(real_file_template_path))
-                    returned_message = t.render(**message_dict)
-                else:
-                    raise TemplateFileNotFoundException("Template file %s not found in templates folder"
-                                                        % real_file_template_path)
-            return returned_message
+        # the user choose a say_template option
+        if self.say_template is not None:
+            returned_message = self._get_say_template(self.say_template, message_dict)
+
+        # trick to remove unicode problem when loading jinja template with non ascii char
+        reload(sys)
+        sys.setdefaultencoding('utf-8')
+
+        # the user choose a file_template option
+        if self.file_template is not None:  # the user choose a file_template option
+            returned_message = self._get_file_template(self.file_template, message_dict)
+
+        return returned_message
 
         # we don't force the usage of a template. The user can choose to do nothing with returned value
-        # else:
+        # if not returned_message:
         #     raise NoTemplateException("You must specify a say_template or a file_template")
 
+    @staticmethod
+    def _get_say_template(list_say_template, message_dict):
+        if isinstance(list_say_template, list):
+            # then we pick randomly one template
+            list_say_template = random.choice(list_say_template)
+        t = Template(list_say_template)
+        return t.render(**message_dict)
+
+    @classmethod
+    def _get_file_template(cls, file_template, message_dict):
+        real_file_template_path = Utils.get_real_file_path(file_template)
+        if os.path.isfile(real_file_template_path):
+            # load the content of the file as template
+            t = Template(cls._get_content_of_file(real_file_template_path))
+            returned_message = t.render(**message_dict)
+        else:
+            raise TemplateFileNotFoundException("Template file %s not found in templates folder"
+                                                % real_file_template_path)
+        return returned_message
+
     def run_synapse_by_name(self, name):
         SynapseLauncher.start_synapse(name=name, brain=self.brain)