浏览代码

[Refacto] #136 Ability to install neurons, stt, tts, trigger + update Doc

monf 8 年之前
父节点
当前提交
a210e0cd46

+ 1 - 1
Docs/neuron_template.md

@@ -6,7 +6,7 @@ Little description of what the neuron does.
 
 ## Installation
 ```
-kalliope neuron-install --git-url "https://github.com/my_user/my_neuron.git"
+kalliope install --git-url "https://github.com/my_user/my_neuron.git"
 ```
 
 ## Options

+ 2 - 2
Docs/neurons.md

@@ -9,12 +9,12 @@ Core neurons are already packaged with the installation of kalliope an can be us
 
 Use the CLI
 ```
-kalliope neuron-install --git-url "<git_url>"
+kalliope install --git-url "<git_url>"
 ```
 
 E.g:
 ```
-kalliope neuron-install --git-url "https://github.com/kalliope-project/kalliope_neuron_wikipedia.git"
+kalliope install --git-url "https://github.com/kalliope-project/kalliope_neuron_wikipedia.git"
 ```
 
 You may be prompted to type your `sudo` password during the process. You can see the list of [available neuron here](neuron_list.md)

+ 3 - 3
kalliope/__init__.py

@@ -29,7 +29,7 @@ def signal_handler(signal, frame):
     sys.exit(0)
 
 # actions available
-ACTION_LIST = ["start", "gui", "neuron-install"]
+ACTION_LIST = ["start", "gui", "install"]
 
 
 def main():
@@ -38,7 +38,7 @@ def main():
     """
     # create arguments
     parser = argparse.ArgumentParser(description='Kalliope')
-    parser.add_argument("action", help="[start|gui|neuron-install]")
+    parser.add_argument("action", help="[start|gui|install]")
     parser.add_argument("--run-synapse", help="Name of a synapse to load surrounded by quote")
     parser.add_argument("--brain-file", help="Full path of a brain file")
     parser.add_argument("--debug", action='store_true', help="Show debug output")
@@ -91,7 +91,7 @@ def main():
     if args.action == "gui":
         ShellGui(brain=brain)
 
-    if args.action == "neuron-install":
+    if args.action == "install":
         if not args.git_url:
             Utils.print_danger("You must specify the git url")
         else:

+ 82 - 14
kalliope/core/ResourcesManager.py

@@ -15,10 +15,22 @@ from kalliope.core.NeuronLauncher import NeuronLauncher
 logging.basicConfig()
 logger = logging.getLogger("kalliope")
 
+# Global values for processing:
+LOCAL_TMP_FOLDER = "/tmp/kalliope/resources/"
 TMP_GIT_FOLDER = "kalliope_new_module_temp_name"
 DNA_FILE_NAME = "dna.yml"
 INSTALL_FILE_NAME = "install.yml"
 
+# Global values for required parameters in DNA:
+DNA_NAME = "name"
+DNA_TYPE = "type"
+
+# Global_Names for 'types' to match:
+TYPE_NEURON = "neuron"
+TYPE_TTS = "tts"
+TYPE_STT = "stt"
+TYPE_TRIGGER = "trigger"
+
 
 class ResourcesManager(object):
     def __init__(self, action, **kwargs):
@@ -34,7 +46,7 @@ class ResourcesManager(object):
         self.git_url = kwargs.get('git_url', None)
 
         # temp path where we install the new module
-        self.tmp_path = self.settings.resources.neuron_folder + os.sep + TMP_GIT_FOLDER
+        self.tmp_path = LOCAL_TMP_FOLDER + TMP_GIT_FOLDER
         self.dna_file_path = self.tmp_path + os.sep + DNA_FILE_NAME
         self.install_file_path = self.tmp_path + os.sep + INSTALL_FILE_NAME
         self.dna_file = None
@@ -44,8 +56,7 @@ class ResourcesManager(object):
 
     def install(self):
         """
-        Module installation method
-        :return:
+        Module installation method.
         """
         if self.is_settings_ok(resources=self.settings.resources,
                                folder_path=self.settings.resources.neuron_folder):
@@ -62,17 +73,22 @@ class ResourcesManager(object):
                 if self._check_dna(dna_file=self.dna_file,
                                    tmp_path=self.tmp_path):
 
-                    # let's move the tmp folder in the right folder and get a new path for the module
-                    module_name = self.dna_file["name"].lower()
-                    target_path = self._rename_temp_folder(name=module_name,
-                                                           target_folder=self.settings.resources.neuron_folder,
-                                                           tmp_path=self.tmp_path)
-
-                    # if the target_path exists, then run the install file within the new repository
-                    if target_path is not None:
-                        self.install_file_path = target_path + os.sep + INSTALL_FILE_NAME
-                        self.run_ansible_playbook_module(install_file_path=self.install_file_path)
-                        Utils.print_success("Neuron %s installed" % module_name)
+                    # Let's find the target folder depending the type
+                    module_type = self.dna_file["type"].lower()
+                    target_folder = self._get_target_folder(resources=self.settings.resources,
+                                                            module_type=module_type)
+                    if target_folder is not None:
+                        # let's move the tmp folder in the right folder and get a new path for the module
+                        module_name = self.dna_file["name"].lower()
+                        target_path = self._rename_temp_folder(name=module_name,
+                                                               target_folder=target_folder,
+                                                               tmp_path=self.tmp_path)
+
+                        # if the target_path exists, then run the install file within the new repository
+                        if target_path is not None:
+                            self.install_file_path = target_path + os.sep + INSTALL_FILE_NAME
+                            self.run_ansible_playbook_module(install_file_path=self.install_file_path)
+                            Utils.print_success("Module: %s installed" % module_name)
 
     def _set_dna_file(self):
         """
@@ -97,6 +113,12 @@ class ResourcesManager(object):
             Utils.print_danger("The DNA of does not contains a \"name\" tag")
             shutil.rmtree(tmp_path)
             success_loading = False
+
+        if "type" not in dna_file:
+            Utils.print_danger("The DNA of does not contains a \"type\" tag")
+            shutil.rmtree(tmp_path)
+            success_loading = False
+
         return success_loading
 
     @staticmethod
@@ -141,6 +163,50 @@ class ResourcesManager(object):
 
         return repo_ok
 
+    @staticmethod
+    def _get_target_folder(resources, module_type):
+        """
+        Return the folder from the resources and given a module type
+        :param resources: Resource
+        :param module_type: type of the module
+        :return: path of the folder
+        """
+        folder_path = None
+        message = "Does this type really exists ? No %s folder set in settings, cannot install." % module_type
+
+        # Let's find the right path depending of the type
+        if module_type == TYPE_NEURON:
+            if resources.neuron_folder is not None:
+                folder_path = resources.neuron_folder
+            else:
+                message = "No %s folder set in settings, cannot install." % TYPE_NEURON
+
+        elif module_type == TYPE_STT:
+            if resources.stt_folder is not None:
+                folder_path = resources.stt_folder
+            else:
+                message = "No %s folder set in settings, cannot install." % TYPE_STT
+
+        elif module_type == TYPE_TTS:
+            if resources.tts_folder is not None:
+                folder_path = resources.stt_folder
+            else:
+                message = "No %s folder set in settings, cannot install." % TYPE_TTS
+
+        elif module_type == TYPE_TRIGGER:
+            if resources.trigger_folder is not None:
+                folder_path = resources.trigger_folder
+            else:
+                message = "No %s folder set in settings, cannot install." % TYPE_TRIGGER
+
+        # No folder_path has been found
+        if folder_path is None:
+            logger.debug(message)
+            Utils.print_danger(message)
+
+        return folder_path
+
+
     @staticmethod
     def _clone_repo(path, git_url):
         """
@@ -153,6 +219,8 @@ class ResourcesManager(object):
         # if the folder already exist we remove it
         if os.path.exists(path):
             shutil.rmtree(path)
+        else:
+            os.makedirs(path)
         Repo.clone_from(git_url, path)
 
     @staticmethod

+ 3 - 0
kalliope/neurons/ansible_playbook/README.md

@@ -8,6 +8,9 @@ Playbooks are Ansible’s configuration, deployment, and orchestration language.
 
 This neuron can be used to perform complex operation with all [modules available from Ansible](http://docs.ansible.com/ansible/modules.html).
 
+## Installation
+
+CORE NEURON : No installation needed.  
 
 ## Options
 

+ 4 - 0
kalliope/neurons/kill_switch/README.md

@@ -4,6 +4,10 @@
 
 This neuron exits the Kalliope process.
 
+## Installation
+
+CORE NEURON : No installation needed.  
+
 ## Options
 
 No parameters

+ 4 - 0
kalliope/neurons/neurotransmitter/README.md

@@ -4,6 +4,10 @@
 
 Link synapses together. Call a synapse directly or depending on the captured speech from the user.
 
+## Installation
+
+CORE NEURON : No installation needed.  
+
 ## Options
 
 | parameter        | required | default | choices | comment                                                                                           |

+ 4 - 0
kalliope/neurons/say/README.md

@@ -4,6 +4,10 @@
 
 This neuron is the mouth of Kalliope and uses the [TTS](../../Docs/tts.md) to say the given message.
 
+## Installation
+
+CORE NEURON : No installation needed.  
+
 ## Options
 
 | parameter | required | default | choices | comment                                |

+ 4 - 0
kalliope/neurons/script/README.md

@@ -4,6 +4,10 @@
 
 This neuron runs a script located on the Kalliope system.
 
+## Installation
+
+CORE NEURON : No installation needed.  
+
 ## Options
 
 | parameter | required | default | choices | comment                                                                    |

+ 3 - 0
kalliope/neurons/shell/README.md

@@ -4,6 +4,9 @@
 
 Run a shell command on the local system where Kalliope is installed.
 
+## Installation
+
+CORE NEURON : No installation needed.  
 
 ## Options
 

+ 4 - 0
kalliope/neurons/sleep/README.md

@@ -4,6 +4,10 @@
 
 This neuron sleeps the system for a given time in seconds.
 
+## Installation
+
+CORE NEURON : No installation needed.  
+
 ## Options
 
 | parameter | required | default | choices | comment                         |

+ 4 - 0
kalliope/neurons/systemdate/README.md

@@ -4,6 +4,10 @@
 
 Give the current time from the system where Kalliope is installed. Return a dict of parameters that can be used in a template.
 
+## Installation
+
+CORE NEURON : No installation needed.  
+
 ## Options
 
 | parameter     | required | default | choices     | comment                                                               |

+ 4 - 0
kalliope/neurons/uri/README.md

@@ -4,6 +4,10 @@
 
 Interacts with HTTP and HTTPS web services.
 
+## Installation
+
+CORE NEURON : No installation needed.  
+
 ## Options
 
 | parameter      | required | default | choices                                      | comment                                                                    |

+ 2 - 2
kalliope/settings.yml

@@ -117,8 +117,8 @@ default_synapse: "Default-synapse"
 # ---------------------------
 # resource directory path
 # ---------------------------
-#resource_directory:
-#  neuron: "/var/tmp/resources/neurons"
+resource_directory:
+  neuron: "/var/tmp/resources/neurons"
 #  stt: "resources/stt"
 #  tts: "resources/tts"
 #  trigger: "resources/trigger"