optmanager: tweaks and cleanups

This commit is contained in:
Aldo Cortesi 2018-05-27 10:43:14 +12:00
parent e59ba13417
commit ec2ae19e22
2 changed files with 15 additions and 15 deletions

View File

@ -91,7 +91,7 @@ class OptManager:
mutation doesn't change the option state inadvertently. mutation doesn't change the option state inadvertently.
""" """
def __init__(self): def __init__(self):
self._deferred: typing.Dict[str, str] = {} self.deferred: typing.Dict[str, str] = {}
self.changed = blinker.Signal() self.changed = blinker.Signal()
self.errored = blinker.Signal() self.errored = blinker.Signal()
# Options must be the last attribute here - after that, we raise an # Options must be the last attribute here - after that, we raise an
@ -219,7 +219,7 @@ class OptManager:
def update_defer(self, **kwargs): def update_defer(self, **kwargs):
unknown = self.update_known(**kwargs) unknown = self.update_known(**kwargs)
self._deferred.update(unknown) self.deferred.update(unknown)
def update(self, **kwargs): def update(self, **kwargs):
u = self.update_known(**kwargs) u = self.update_known(**kwargs)
@ -307,7 +307,7 @@ class OptManager:
else: else:
unknown[optname] = optval unknown[optname] = optval
if defer: if defer:
self._deferred.update(unknown) self.deferred.update(unknown)
elif unknown: elif unknown:
raise exceptions.OptionsError("Unknown options: %s" % ", ".join(unknown.keys())) raise exceptions.OptionsError("Unknown options: %s" % ", ".join(unknown.keys()))
self.update(**vals) self.update(**vals)
@ -318,12 +318,12 @@ class OptManager:
have since been added. have since been added.
""" """
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(self._options[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, o: _Option, optstr: typing.Optional[str]) -> typing.Any: def parse_setval(self, o: _Option, optstr: typing.Optional[str]) -> typing.Any:
""" """

View File

@ -270,12 +270,12 @@ def test_serialize():
t = "# a comment" t = "# a comment"
optmanager.load(o2, t) optmanager.load(o2, t)
optmanager.load(o2, "foobar: '123'") optmanager.load(o2, "foobar: '123'")
assert o2._deferred == {"foobar": "123"} assert o2.deferred == {"foobar": "123"}
t = "" t = ""
optmanager.load(o2, t) optmanager.load(o2, t)
optmanager.load(o2, "foobar: '123'") optmanager.load(o2, "foobar: '123'")
assert o2._deferred == {"foobar": "123"} assert o2.deferred == {"foobar": "123"}
def test_serialize_defaults(): def test_serialize_defaults():
@ -300,7 +300,7 @@ def test_saving(tmpdir):
with open(dst, 'a') as f: with open(dst, 'a') as f:
f.write("foobar: '123'") f.write("foobar: '123'")
optmanager.load_paths(o, dst) optmanager.load_paths(o, dst)
assert o._deferred == {"foobar": "123"} assert o.deferred == {"foobar": "123"}
with open(dst, 'a') as f: with open(dst, 'a') as f:
f.write("'''") f.write("'''")
@ -429,13 +429,13 @@ def test_set():
assert opts.seqstr == [] assert opts.seqstr == []
with pytest.raises(exceptions.OptionsError): with pytest.raises(exceptions.OptionsError):
opts.set("deferred=wobble") opts.set("deferredoption=wobble")
opts.set("deferred=wobble", defer=True) opts.set("deferredoption=wobble", defer=True)
assert "deferred" in opts._deferred assert "deferredoption" in opts.deferred
opts.process_deferred() opts.process_deferred()
assert "deferred" in opts._deferred assert "deferredoption" in opts.deferred
opts.add_option("deferred", str, "default", "help") opts.add_option("deferredoption", str, "default", "help")
opts.process_deferred() opts.process_deferred()
assert "deferred" not in opts._deferred assert "deferredoption" not in opts.deferred
assert opts.deferred == "wobble" assert opts.deferredoption == "wobble"