Przeglądaj źródła

add ResourcesManager #136 #135

nico 8 lat temu
rodzic
commit
c1ca421f6a

+ 1 - 0
install/files/python_requirements.txt

@@ -21,3 +21,4 @@ mock==2.0.0
 feedparser==5.2.1
 Flask-Testing==0.6.1
 apscheduler==3.3.0
+GitPython==2.1.1

+ 10 - 2
kalliope/__init__.py

@@ -11,6 +11,7 @@ from kalliope.core.MainController import MainController
 import signal
 import sys
 
+from kalliope.core.ResourcesManager import ResourcesManager
 from kalliope.core.SynapseLauncher import SynapseLauncher
 
 logging.basicConfig()
@@ -28,7 +29,7 @@ def signal_handler(signal, frame):
     sys.exit(0)
 
 # actions available
-ACTION_LIST = ["start", "gui"]
+ACTION_LIST = ["start", "gui", "neuron-install"]
 
 
 def main():
@@ -37,10 +38,11 @@ def main():
     """
     # create arguments
     parser = argparse.ArgumentParser(description='Kalliope')
-    parser.add_argument("action", help="[start|gui]")
+    parser.add_argument("action", help="[start|gui|neuron-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")
+    parser.add_argument("--git-url", action='store_true', help="Git URL of the neuron to install")
 
     # parse arguments from script parameters
     args = parser.parse_args()
@@ -89,6 +91,12 @@ def main():
     if args.action == "gui":
         ShellGui(brain=brain)
 
+    if args.action == "neuron-install":
+        if not args.git_url:
+            Utils.print_danger("You must specify the git url")
+        else:
+            ResourcesManager(args.git_url)
+
 
 def configure_logging(debug=None):
     """

+ 74 - 0
kalliope/core/ResourcesManager.py

@@ -0,0 +1,74 @@
+import os
+
+import logging
+from git import Repo
+from kalliope.core.Models import Neuron
+
+from kalliope import Utils
+from kalliope.core.ConfigurationManager import YAMLLoader
+from kalliope.core.ConfigurationManager import SettingLoader
+from kalliope.core.NeuronLauncher import NeuronLauncher
+
+logging.basicConfig()
+logger = logging.getLogger("kalliope")
+
+TMP_GIT_FOLDER = "kalliope_new_neuron_temp_name"
+DNA_FILE_NAME = "dna.yml"
+INSTALL_FILE_NAME = "install.yml"
+
+
+class ResourcesManager(object):
+    def __init__(self, action, **kwargs):
+        super(ResourcesManager, self).__init__()
+        # get settings
+        sl = SettingLoader()
+        self.settings = sl.settings
+
+        # action to perform (delete, install, update)
+        self.action = action
+
+        # in case of update or install, url where
+        self.git_url = kwargs.get('git_url', None)
+
+        if self.action == "install":
+            self.install()
+
+    def install(self):
+        """
+        Neuron installation method
+        :return:
+        """
+        # clone the repo
+        tmp_path = self.settings.resources.neuron_folder + os.sep + TMP_GIT_FOLDER
+        Utils.print_info("Cloning repository...")
+        Repo.clone_from(self.git_url, tmp_path)
+
+        # get the dna file
+        Utils.print_info("Checking DNA")
+        dna_file_path = tmp_path + os.sep + DNA_FILE_NAME
+        if os.path.exists(dna_file_path):
+            dna_file = YAMLLoader().get_config(dna_file_path)
+            logger.debug("[ResourcesManager] DNA file content: " + str(dna_file))
+
+            if "neuron_name" not in dna_file:
+                Utils.print_danger("The DNA of the neuron does not contains a \"neuron_name\"")
+                os.remove(tmp_path)
+            else:
+                # rename the folder
+                new_absolute_neuron_path = self.settings.resources.neuron_folder + os.sep + dna_file["neuron_name"].lower()
+                os.rename(tmp_path, new_absolute_neuron_path)
+
+                # check install file exists
+                install_file_path = new_absolute_neuron_path + os.sep + INSTALL_FILE_NAME
+                if os.path.exists(install_file_path):
+                    ansible_neuron_parameters = {
+                        "task_file": install_file_path
+                    }
+                    neuron = Neuron(name="ansible_playbook", parameters=ansible_neuron_parameters)
+                    NeuronLauncher.start_neuron(neuron)
+                else:
+                    Utils.print_danger("Missing %s file in %s" % (INSTALL_FILE_NAME, install_file_path))
+
+        else:
+            Utils.print_danger("Missing %s file" % DNA_FILE_NAME)
+

+ 0 - 69
kalliope/neurons/wikipedia_searcher/README.md

@@ -1,69 +0,0 @@
-# wikipedia_searcher
-
-## Synopsis
-
-Get the summary of a Wikipedia page.
-
-## Options
-
-| parameter | required | default | choices                     | comment                                                                                                                           |
-|-----------|----------|---------|-----------------------------|-----------------------------------------------------------------------------------------------------------------------------------|
-| language  | yes      |         | E.g: "fr", "en", "it", "es" | See the list of available language in the "Note" section                                                                          |
-| query     | yes      |         |                             | The wikipedia page you are looking for.  This parameter can be passed as an argument in the neuron from the order with {{ query}} |
-| sentences | no       | 10      | Integer in range 1-10       | if set, return the first number of sentences(can be no greater than 10) specified in this parameter.                              |
-
-
-## Return Values
-
-| Name       | Description                             | Type   | sample                                                                                                                              |
-|------------|-----------------------------------------|--------|-------------------------------------------------------------------------------------------------------------------------------------|
-| summary    | Plain text summary of the searched page | string |  Wikipedia is a collaboratively edited, multilingual, free Internet encyclopedia supported by the non-profit Wikimedia Foundation.. |
-| returncode | Error code. See bellow                  | string | SummaryFound                                                                                                                        |
-| may_refer  | List of pages that can refer the query  | list   | ['Marc Le Bot', 'Bot', 'Jean-Marc Bot', 'bot', 'pied bot', 'robot', 'Sam Bot', 'Famille Both', 'Yves Bot', 'Ben Bot', 'Botswana']   |
-
-
-| returncode          | Description                             |
-|---------------------|-----------------------------------------|
-| SummaryFound        | A summary hs been found from the querry |
-| DisambiguationError | The query match more than ony one page. |
-| PageError           | No Wikipedia matched a query            |
-
-## Synapses example
-
-This synapse will look for the {{ query }} spelt by the user on Wikipedia
-```
-- name: "wikipedia-search"
-  signals:
-    - order: "look on wikipedia {{ query }}"
-  neurons:
-    - wikipedia_searcher:
-        language: "en"
-        args:
-          - query
-        file_template: "wikipedia_returned_value.j2"
-
-```
-
-## Templates example 
-
-This template will simply make Kalliope speak out loud the summary section of the Wikipédia page of the query.
-If the query match more than one page, Kaliope will give the user all matched pages.
-If the query doesn't match any page on Wikipedia, kalliope will notify the user.
-```
-{% if returncode == "DisambiguationError" %}
-    The query match following pages    
-    {% if may_refer is not none %}
-        {% for page in may_refer %}
-            {{ page }}
-        {% endfor %}
-    {% endif %}
-{% elif returncode == "PageError" %}
-    I haven't  found anything on this
-{% else %}
-    {{ summary }}
-{% endif %}
-```
-
-## Notes
-
-Available languages in [the detailed list of the offical Wikipedia page](https://en.wikipedia.org/wiki/List_of_Wikipedias#Detailed_list). The column is called "Wiki". E.g: "en"

+ 0 - 1
kalliope/neurons/wikipedia_searcher/__init__.py

@@ -1 +0,0 @@
-from wikipedia_searcher import Wikipedia_searcher

+ 0 - 0
kalliope/neurons/wikipedia_searcher/tests/__init__.py


+ 0 - 67
kalliope/neurons/wikipedia_searcher/tests/test_wikipedia_searcher.py

@@ -1,67 +0,0 @@
-import unittest
-
-from kalliope.core.NeuronModule import InvalidParameterException
-from kalliope.neurons.wikipedia_searcher import Wikipedia_searcher
-
-
-class TestWikipediaSearcher(unittest.TestCase):
-    def setUp(self):
-        pass
-
-    # def test_parameters(self):
-    #     def run_test(parameters_to_test):
-    #         with self.assertRaises(InvalidParameterException):
-    #             Wikipedia_searcher(**parameters_to_test)
-    #
-    #     parameters = dict()
-    #     run_test(parameters)
-    #
-    #     # sentences must be an integer
-    #     parameters = {
-    #         "language": "en",
-    #         "query": "this is the query",
-    #         "sentences": "invalid"
-    #
-    #     }
-    #     run_test(parameters)
-    #
-    #     # test non existing language
-    #     parameters = {
-    #         "language": "foo",
-    #         "query": "this is the query",
-    #         "sentences": 1
-    #
-    #     }
-    #     run_test(parameters)
-
-    # def test_get_DisambiguationError(self):
-    #
-    #     parameters = {
-    #         "language": "fr",
-    #         "query": "bot",
-    #         "sentences": 1
-    #     }
-    #
-    #     wiki = Wikipedia_searcher(**parameters)
-    #     self.assertEqual(wiki.returncode, "DisambiguationError")
-    #
-    # def test_page_error(self):
-    #     parameters = {
-    #         "language": "fr",
-    #         "query": "fudu foo bar non exist",
-    #         "sentences": 1
-    #     }
-    #
-    #     wiki = Wikipedia_searcher(**parameters)
-    #     self.assertEqual(wiki.returncode, "PageError")
-    #
-    # def test_summary_found(self):
-    #     parameters = {
-    #         "language": "fr",
-    #         "query": "kalliope"
-    #     }
-    #     wiki = Wikipedia_searcher(**parameters)
-    #     self.assertEqual(wiki.returncode, "SummaryFound")
-
-if __name__ == '__main__':
-    unittest.main()

+ 0 - 80
kalliope/neurons/wikipedia_searcher/wikipedia_searcher.py

@@ -1,80 +0,0 @@
-import logging
-
-from kalliope.core.NeuronModule import NeuronModule, InvalidParameterException
-import wikipedia
-
-logging.basicConfig()
-logger = logging.getLogger("kalliope")
-
-
-class Wikipedia_searcher(NeuronModule):
-    def __init__(self, **kwargs):
-        # we don't need the TTS cache for this neuron
-        cache = kwargs.get('cache', None)
-        if cache is None:
-            cache = False
-            kwargs["cache"] = cache
-        super(Wikipedia_searcher, self).__init__(**kwargs)
-
-        # get parameters form the neuron
-        self.query = kwargs.get('query', None)
-        self.language = kwargs.get('language', None)
-        self.sentences = kwargs.get('sentences', None)
-
-        self.may_refer = None
-        self.returncode = None
-        self.message = None
-
-        # check parameters
-        if self._is_parameters_ok():
-            # set the language
-            wikipedia.set_lang(self.language)
-            # do the summary search
-            try:
-                summary = wikipedia.summary(self.query, auto_suggest=True, sentences=self.sentences)
-                # if we are here, no exception raised, we got a summary
-                self.returncode = "SummaryFound"
-            except wikipedia.exceptions.DisambiguationError, e:
-                # Exception raised when a page resolves to a Disambiguation page.
-                # The options property contains a list of titles of Wikipedia pages that the query may refer to.
-                self.may_refer = e.options
-                # Removing duplicates in lists.
-                self.may_refer = list(set(self.may_refer))
-                self.returncode = "DisambiguationError"
-                summary = ""
-            except wikipedia.exceptions.PageError:
-                # Exception raised when no Wikipedia matched a query.
-                self.returncode = "PageError"
-                summary = ""
-
-            self.message = {
-                "summary": summary,
-                "may_refer": self.may_refer,
-                "returncode": self.returncode
-            }
-            logger.debug("Wikipedia returned message: %s" % str(self.message))
-
-            self.say(self.message)
-
-    def _is_parameters_ok(self):
-        """
-        Check if received parameters are ok to perform operations in the neuron
-        :return: true if parameters are ok, raise an exception otherwise
-
-        .. raises:: InvalidParameterException
-        """
-
-        if self.query is None:
-            raise InvalidParameterException("Wikipedia needs a query")
-        if self.language is None:
-            raise InvalidParameterException("Wikipedia needs a language")
-
-        valid_language = wikipedia.languages().keys()
-        if self.language not in valid_language:
-            raise InvalidParameterException("Wikipedia needs a valid language: %s" % valid_language)
-
-        if self.sentences is not None:
-            if not isinstance(self.sentences, int):
-                raise InvalidParameterException("Number of sentences must be an integer")
-
-        return True

+ 2 - 1
setup.py

@@ -79,7 +79,8 @@ setup(
         'mock==2.0.0',
         'feedparser==5.2.1',
         'Flask-Testing==0.6.1',
-        'apscheduler==3.3.0'
+        'apscheduler==3.3.0',
+        'GitPython==2.1.1'
     ],