Browse Source

[Fix] new version of the script Neuron

monf 8 years ago
parent
commit
5c1c7153b1
3 changed files with 20 additions and 23 deletions
  1. 1 1
      brains/script.yml
  2. 18 21
      neurons/script/script.py
  3. 1 1
      test.py

+ 1 - 1
brains/script.yml

@@ -2,7 +2,7 @@
   - name: "run-simple-script"
     neurons:
       - script:
-          path: "/home/nico/test.sh"
+          path: "/var/tmp/coucou.sh"
       - say:
           message: "Script lancé, monsieur"
     signals:

+ 18 - 21
neurons/script/script.py

@@ -1,33 +1,30 @@
 import subprocess
 import os
 
-from core.NeuronModule import NeuronModule
-
-
-class ScriptNotFound(Exception):
-    pass
-
-
-class ScriptNotExecutable(Exception):
-    pass
+from core.NeuronModule import NeuronModule, MissingParameterException, InvalidParameterException
 
 
 class Script(NeuronModule):
     def __init__(self, **kwargs):
         # get message to spell out loud
         super(Script, self).__init__(**kwargs)
-        script_path = kwargs.get('path', "")
+        self.path = kwargs.get("path", None)
 
-        # test that the file exist and is executable
-        if self.is_exe(script_path):
-            p = subprocess.Popen(script_path, stdout=subprocess.PIPE, shell=True)
+        # check parameters
+        if self._is_parameters_ok():
+            p = subprocess.Popen(self.path, stdout=subprocess.PIPE, shell=True)
             (output, err) = p.communicate()
 
-    def is_exe(self, fpath):
-        returned_value = True
-        if not os.path.isfile(fpath):
-            raise ScriptNotFound()
-        if not os.access(fpath, os.X_OK):
-            raise ScriptNotExecutable()
-
-        return returned_value
+    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
+        """
+        if self.path is None:
+            raise MissingParameterException("You must provide a script path.")
+        if not os.path.isfile(self.path):
+            raise InvalidParameterException("Script not found or is not a file.")
+        if not os.access(self.path, os.X_OK):
+            raise InvalidParameterException("Script not Executable.")
+
+        return True

+ 1 - 1
test.py

@@ -25,7 +25,7 @@ logger.setLevel(logging.DEBUG)
 #
 brain = BrainLoader.get_brain()
 
-order = "cherche sur Wikipédia bot"
+order = "lance le script"
 
 oa = OrderAnalyser(order=order, brain=brain)