commands: add bytes type

This commit is contained in:
Maximilian Hils 2021-06-23 13:46:57 +02:00
parent a19e95e6c9
commit 91bb757660

View File

@ -4,7 +4,7 @@ import typing
from mitmproxy import exceptions
from mitmproxy import flow
from mitmproxy.utils import emoji
from mitmproxy.utils import emoji, strutils
if typing.TYPE_CHECKING: # pragma: no cover
from mitmproxy.command import CommandManager
@ -114,6 +114,23 @@ class _StrType(_BaseType):
return isinstance(val, str)
class _BytesType(_BaseType):
typ = bytes
display = "bytes"
def completion(self, manager: "CommandManager", t: type, s: str) -> typing.Sequence[str]:
return []
def parse(self, manager: "CommandManager", t: type, s: str) -> bytes:
try:
return strutils.escaped_str_to_bytes(s)
except ValueError as e:
raise exceptions.TypeError(str(e))
def is_valid(self, manager: "CommandManager", typ: typing.Any, val: typing.Any) -> bool:
return isinstance(val, bytes)
class _UnknownType(_BaseType):
typ = Unknown
display = "unknown"
@ -460,4 +477,5 @@ CommandTypes = TypeManager(
_PathType,
_StrType,
_StrSeqType,
_BytesType,
)