mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-27 02:24:18 +00:00
optmanager: refactor for coverage and clarity
This commit is contained in:
parent
76a7883c8b
commit
717fbaa990
@ -298,7 +298,7 @@ class OptManager:
|
|||||||
else:
|
else:
|
||||||
optname, optval = parts[0], parts[1]
|
optname, optval = parts[0], parts[1]
|
||||||
if optname in self._options:
|
if optname in self._options:
|
||||||
vals[optname] = self.parse_setval(optname, optval)
|
vals[optname] = self.parse_setval(self._options[optname], optval)
|
||||||
else:
|
else:
|
||||||
unknown[optname] = optval
|
unknown[optname] = optval
|
||||||
if defer:
|
if defer:
|
||||||
@ -315,19 +315,15 @@ class OptManager:
|
|||||||
update = {}
|
update = {}
|
||||||
for optname, optval in self._deferred.items():
|
for optname, optval in self._deferred.items():
|
||||||
if optname in self._options:
|
if optname in self._options:
|
||||||
update[optname] = self.parse_setval(optname, optval)
|
update[optname] = self.parse_setval(self._options[optname], optval)
|
||||||
self.update(**update)
|
self.update(**update)
|
||||||
for k in update.keys():
|
for k in update.keys():
|
||||||
del self._deferred[k]
|
del self._deferred[k]
|
||||||
|
|
||||||
def parse_setval(self, optname: str, optstr: typing.Optional[str]) -> typing.Any:
|
def parse_setval(self, o: _Option, optstr: typing.Optional[str]) -> typing.Any:
|
||||||
"""
|
"""
|
||||||
Convert a string to a value appropriate for the option type.
|
Convert a string to a value appropriate for the option type.
|
||||||
"""
|
"""
|
||||||
if optname not in self._options:
|
|
||||||
raise exceptions.OptionsError("No such option %s" % optname)
|
|
||||||
o = self._options[optname]
|
|
||||||
|
|
||||||
if o.typespec in (str, typing.Optional[str]):
|
if o.typespec in (str, typing.Optional[str]):
|
||||||
return optstr
|
return optstr
|
||||||
elif o.typespec in (int, typing.Optional[int]):
|
elif o.typespec in (int, typing.Optional[int]):
|
||||||
@ -337,7 +333,7 @@ class OptManager:
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
raise exceptions.OptionsError("Not an integer: %s" % optstr)
|
raise exceptions.OptionsError("Not an integer: %s" % optstr)
|
||||||
elif o.typespec == int:
|
elif o.typespec == int:
|
||||||
raise exceptions.OptionsError("Option is required: %s" % optname)
|
raise exceptions.OptionsError("Option is required: %s" % o.name)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
elif o.typespec == bool:
|
elif o.typespec == bool:
|
||||||
@ -355,7 +351,7 @@ class OptManager:
|
|||||||
if not optstr:
|
if not optstr:
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
return getattr(self, optname) + [optstr]
|
return getattr(self, o.name) + [optstr]
|
||||||
raise NotImplementedError("Unsupported option type: %s", o.typespec)
|
raise NotImplementedError("Unsupported option type: %s", o.typespec)
|
||||||
|
|
||||||
def make_parser(self, parser, optname, metavar=None, short=None):
|
def make_parser(self, parser, optname, metavar=None, short=None):
|
||||||
|
@ -70,7 +70,7 @@ def test_defaults():
|
|||||||
def test_required_int():
|
def test_required_int():
|
||||||
o = TO()
|
o = TO()
|
||||||
with pytest.raises(exceptions.OptionsError):
|
with pytest.raises(exceptions.OptionsError):
|
||||||
o.parse_setval("required_int", None)
|
o.parse_setval(o._options["required_int"], None)
|
||||||
|
|
||||||
|
|
||||||
def test_deepcopy():
|
def test_deepcopy():
|
||||||
|
Loading…
Reference in New Issue
Block a user