Commit 978084f7 by Őry Máté

properly check command arguments

fixes #6
parent 50c7b745
...@@ -15,6 +15,7 @@ import tarfile ...@@ -15,6 +15,7 @@ import tarfile
from os.path import expanduser, join, exists from os.path import expanduser, join, exists
from os import mkdir, environ from os import mkdir, environ
from glob import glob from glob import glob
from inspect import getargspec
from StringIO import StringIO from StringIO import StringIO
from base64 import decodestring from base64 import decodestring
from shutil import rmtree, move from shutil import rmtree, move
...@@ -346,24 +347,29 @@ class SerialLineReceiver(SerialLineReceiverBase): ...@@ -346,24 +347,29 @@ class SerialLineReceiver(SerialLineReceiverBase):
self.send_response(response='status', self.send_response(response='status',
args=args) args=args)
def handle_command(self, command, args): def _get_command(self, command, args):
if not isinstance(command, basestring) or command.startswith('_'): if not isinstance(command, basestring) or command.startswith('_'):
raise Exception(u'Invalid command: %s' % command) raise AttributeError(u'Invalid command: %s' % command)
for k, v in args.iteritems():
if not (isinstance(v, int) or isinstance(v, float) or
isinstance(v, basestring)):
raise Exception(u'Invalid argument: %s' % k)
try: try:
func = getattr(Context, command) func = getattr(Context, command)
retval = func(**args) except AttributeError as e:
except (AttributeError, TypeError) as e: raise AttributeError(u'Command not found: %s (%s)' % (command, e))
raise Exception(u'Command not found: %s (%s)' % (command, e))
else: # check for unexpected keyword arguments
self.send_response( argspec = getargspec(func)
response=func.__name__, if argspec.keywords is None: # _operation doesn't take ** args
args={'retval': retval, 'uuid': args.get('uuid', None)}) unexpected_kwargs = set(args) - set(argspec.args)
if unexpected_kwargs:
raise TypeError("Command got unexpected keyword arguments: "
"%s" % ", ".join(unexpected_kwargs))
return func
def handle_command(self, command, args):
func = self._get_command(command, args)
retval = func(**args)
self.send_response(
response=func.__name__,
args={'retval': retval, 'uuid': args.get('uuid', None)})
def handle_response(self, response, args): def handle_response(self, response, args):
pass pass
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment