1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import logging
- import subprocess
- import threading
- from core.NeuronModule import NeuronModule, MissingParameterException
- logging.basicConfig()
- logger = logging.getLogger("kalliope")
- class AsyncShell(threading.Thread):
- 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 Shell(NeuronModule):
- def __init__(self, **kwargs):
- super(Shell, self).__init__(**kwargs)
- # get the command
- self.cmd = kwargs.get('cmd', None)
- # get if the user select a blocking command or not
- self.async = kwargs.get('async', False)
- # check parameters
- if self._is_parameters_ok():
- # run the command
- if not self.async:
- p = subprocess.Popen(self.cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
- (output, err) = p.communicate()
- message = {
- "output": output,
- "returncode": p.returncode
- }
- self.say(message)
- else:
- async_shell = AsyncShell(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
- """
- if self.cmd is None:
- raise MissingParameterException("cmd parameter required")
- return True
|