Merge#2
This commit is contained in:
parent
0724c49ae7
commit
9507cd4677
4 changed files with 79 additions and 4 deletions
|
@ -44,3 +44,4 @@
|
|||
- shell:
|
||||
cmd: "firefox http://fr-fr.facebook.com/&"
|
||||
async: True
|
||||
|
||||
|
|
4
resources/neurons/.gitignore
vendored
4
resources/neurons/.gitignore
vendored
|
@ -1,4 +0,0 @@
|
|||
# Ignore everything in this directory
|
||||
*
|
||||
# Except this file
|
||||
!.gitignore
|
1
resources/neurons/run/__init__.py
Normal file
1
resources/neurons/run/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from run import Run
|
77
resources/neurons/run/run.py
Normal file
77
resources/neurons/run/run.py
Normal file
|
@ -0,0 +1,77 @@
|
|||
import logging
|
||||
import subprocess
|
||||
import threading
|
||||
from kalliope.core.NeuronModule import NeuronModule, MissingParameterException
|
||||
|
||||
logging.basicConfig()
|
||||
logger = logging.getLogger("kalliope")
|
||||
|
||||
|
||||
class AsyncRun(threading.Thread):
|
||||
"""
|
||||
Class used to run an asynchrone Shell command
|
||||
|
||||
.. notes:: Impossible to get the success code of the command
|
||||
"""
|
||||
def __init__(self, cmd):
|
||||
self.stdout = None
|
||||
self.stderr = None
|
||||
self.cmd = cmd
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
def run(self):
|
||||
p = subprocess.Popen(self.cmd,
|
||||
shell=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
|
||||
self.stdout, self.stderr = p.communicate()
|
||||
|
||||
|
||||
class Run(NeuronModule):
|
||||
"""
|
||||
Run a shell command in a synchron mode
|
||||
"""
|
||||
def __init__(self, **kwargs):
|
||||
super(Run, self).__init__(**kwargs)
|
||||
|
||||
# get the command
|
||||
self.cmd = kwargs.get('application', None)
|
||||
# get if the user select a blocking command or not
|
||||
self.async = kwargs.get('async', False)
|
||||
self.query = kwargs.get('query', None)
|
||||
|
||||
if self.query is not None:
|
||||
self.cmd = self.cmd + "\"" + self.query +"\""
|
||||
|
||||
# check parameters
|
||||
if self._is_parameters_ok():
|
||||
# run the command
|
||||
self.cmd = "%s&" % (self.cmd.lower())
|
||||
print self.cmd
|
||||
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.returncode = p.returncode
|
||||
message = {
|
||||
"output": self.output,
|
||||
"returncode": self.returncode
|
||||
}
|
||||
self.say(message)
|
||||
|
||||
else:
|
||||
async_shell = AsyncRun(cmd=self.cmd)
|
||||
async_shell.start()
|
||||
|
||||
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:: MissingParameterException
|
||||
"""
|
||||
if self.cmd is None:
|
||||
raise MissingParameterException("cmd parameter required")
|
||||
|
||||
return True
|
Loading…
Reference in a new issue