From 717fbaa99076545d11f554187759005dce1aa67b Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 8 May 2018 14:24:41 +1200 Subject: [PATCH] optmanager: refactor for coverage and clarity --- mitmproxy/optmanager.py | 14 +++++--------- test/mitmproxy/test_optmanager.py | 2 +- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/mitmproxy/optmanager.py b/mitmproxy/optmanager.py index 7dc4bf6c5..a473e9dfb 100644 --- a/mitmproxy/optmanager.py +++ b/mitmproxy/optmanager.py @@ -298,7 +298,7 @@ class OptManager: else: optname, optval = parts[0], parts[1] if optname in self._options: - vals[optname] = self.parse_setval(optname, optval) + vals[optname] = self.parse_setval(self._options[optname], optval) else: unknown[optname] = optval if defer: @@ -315,19 +315,15 @@ class OptManager: update = {} for optname, optval in self._deferred.items(): if optname in self._options: - update[optname] = self.parse_setval(optname, optval) + update[optname] = self.parse_setval(self._options[optname], optval) self.update(**update) for k in update.keys(): 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. """ - 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]): return optstr elif o.typespec in (int, typing.Optional[int]): @@ -337,7 +333,7 @@ class OptManager: except ValueError: raise exceptions.OptionsError("Not an integer: %s" % optstr) elif o.typespec == int: - raise exceptions.OptionsError("Option is required: %s" % optname) + raise exceptions.OptionsError("Option is required: %s" % o.name) else: return None elif o.typespec == bool: @@ -355,7 +351,7 @@ class OptManager: if not optstr: return [] else: - return getattr(self, optname) + [optstr] + return getattr(self, o.name) + [optstr] raise NotImplementedError("Unsupported option type: %s", o.typespec) def make_parser(self, parser, optname, metavar=None, short=None): diff --git a/test/mitmproxy/test_optmanager.py b/test/mitmproxy/test_optmanager.py index 77bcc0078..4356434b9 100644 --- a/test/mitmproxy/test_optmanager.py +++ b/test/mitmproxy/test_optmanager.py @@ -70,7 +70,7 @@ def test_defaults(): def test_required_int(): o = TO() with pytest.raises(exceptions.OptionsError): - o.parse_setval("required_int", None) + o.parse_setval(o._options["required_int"], None) def test_deepcopy():