diff options
author | Aldo Cortesi <aldo@corte.si> | 2017-12-19 10:09:14 +1300 |
---|---|---|
committer | Aldo Cortesi <aldo@corte.si> | 2017-12-19 10:19:08 +1300 |
commit | 6563feaf059f9c829ba6b57d312a0f1dbfb84e33 (patch) | |
tree | 6baebf45d24369be51964d6a628902e582a325c2 /mitmproxy/command.py | |
parent | cda14830d349f4c1c60af2d1ec563e4894b836c3 (diff) | |
download | mitmproxy-6563feaf059f9c829ba6b57d312a0f1dbfb84e33.tar.gz mitmproxy-6563feaf059f9c829ba6b57d312a0f1dbfb84e33.tar.bz2 mitmproxy-6563feaf059f9c829ba6b57d312a0f1dbfb84e33.zip |
types: use new type validation mechanism in commands
Diffstat (limited to 'mitmproxy/command.py')
-rw-r--r-- | mitmproxy/command.py | 48 |
1 files changed, 20 insertions, 28 deletions
diff --git a/mitmproxy/command.py b/mitmproxy/command.py index e93a0cfa..f978b25b 100644 --- a/mitmproxy/command.py +++ b/mitmproxy/command.py @@ -10,11 +10,18 @@ import textwrap import functools import sys -from mitmproxy.utils import typecheck from mitmproxy import exceptions import mitmproxy.types +def verify_arg_signature(f: typing.Callable, args: list, kwargs: dict) -> None: + sig = inspect.signature(f) + try: + sig.bind(*args, **kwargs) + except TypeError as v: + raise exceptions.CommandError("command argument mismatch: %s" % v.args[0]) + + def lexer(s): # mypy mis-identifies shlex.shlex as abstract lex = shlex.shlex(s) # type: ignore @@ -74,8 +81,7 @@ class Command: Call the command with a list of arguments. At this point, all arguments are strings. """ - if not self.has_positional and (len(self.paramtypes) != len(args)): - raise exceptions.CommandError("Usage: %s" % self.signature_help()) + verify_arg_signature(self.func, list(args), {}) remainder = [] # type: typing.Sequence[str] if self.has_positional: @@ -84,27 +90,21 @@ class Command: pargs = [] for arg, paramtype in zip(args, self.paramtypes): - if typecheck.check_command_type(arg, paramtype): - pargs.append(arg) - else: - pargs.append(parsearg(self.manager, arg, paramtype)) - - if remainder: - chk = typecheck.check_command_type( - remainder, - typing.Sequence[self.paramtypes[-1]] # type: ignore - ) - if chk: - pargs.extend(remainder) - else: - raise exceptions.CommandError("Invalid value type: %s - expected %s" % (remainder, self.paramtypes[-1])) + pargs.append(parsearg(self.manager, arg, paramtype)) + pargs.extend(remainder) with self.manager.master.handlecontext(): ret = self.func(*pargs) - if not typecheck.check_command_type(ret, self.returntype): - raise exceptions.CommandError("Command returned unexpected data") - + if ret is None and self.returntype is None: + return + typ = mitmproxy.types.CommandTypes.get(self.returntype) + if not typ.is_valid(self.manager, typ, ret): + raise exceptions.CommandError( + "%s returned unexpected data - expected %s" % ( + self.path, typ.display + ) + ) return ret @@ -210,14 +210,6 @@ def parsearg(manager: CommandManager, spec: str, argtype: type) -> typing.Any: raise exceptions.CommandError from e -def verify_arg_signature(f: typing.Callable, args: list, kwargs: dict) -> None: - sig = inspect.signature(f) - try: - sig.bind(*args, **kwargs) - except TypeError as v: - raise exceptions.CommandError("Argument mismatch: %s" % v.args[0]) - - def command(path): def decorator(function): @functools.wraps(function) |