registry: Get rid of the 'class X'-based hack.

This commit is contained in:
Valentin Lorentz 2018-09-25 18:55:23 +02:00
parent 296d44354f
commit fa2c11eec1

View File

@ -202,16 +202,7 @@ class Group(object):
self._supplyDefault = supplyDefault self._supplyDefault = supplyDefault
self._orderAlphabetically = orderAlphabetically self._orderAlphabetically = orderAlphabetically
OriginalClass = self.__class__ OriginalClass = self.__class__
class X(OriginalClass): self._wasSet = True
"""This class exists to differentiate those values that have
been changed from their default from those that haven't."""
def set(self, *args):
self.__class__ = OriginalClass
self.set(*args)
def setValue(self, *args):
self.__class__ = OriginalClass
self.setValue(*args)
self.X = X
def __call__(self): def __call__(self):
raise ValueError('Groups have no value.') raise ValueError('Groups have no value.')
@ -223,7 +214,7 @@ class Group(object):
def __makeChild(self, attr, s): def __makeChild(self, attr, s):
v = self.__class__(self._default, self._help) v = self.__class__(self._default, self._help)
v.set(s) v.set(s)
v.__class__ = self.X self._wasSet = False
v._supplyDefault = False v._supplyDefault = False
v._help = '' # Clear this so it doesn't print a bazillion times. v._help = '' # Clear this so it doesn't print a bazillion times.
self.register(attr, v) self.register(attr, v)
@ -320,7 +311,7 @@ class Group(object):
for name in self._added: for name in self._added:
node = self._children[name] node = self._children[name]
if hasattr(node, 'value') or hasattr(node, 'help'): if hasattr(node, 'value') or hasattr(node, 'help'):
if node.__class__ is not self.X: if self._wasSet:
L.append((node._name, node)) L.append((node._name, node))
if getChildren: if getChildren:
L.extend(node.getValues(getChildren, fullNames)) L.extend(node.getValues(getChildren, fullNames))
@ -372,6 +363,7 @@ class Value(Group):
"""Override this with a function to convert a string to whatever type """Override this with a function to convert a string to whatever type
you want, and call self.setValue to set the value.""" you want, and call self.setValue to set the value."""
self.setValue(s) self.setValue(s)
self._wasSet = True
def setValue(self, v): def setValue(self, v):
"""Check conditions on the actual value type here. I.e., if you're a """Check conditions on the actual value type here. I.e., if you're a
@ -383,11 +375,12 @@ class Value(Group):
self.value = v self.value = v
if self._supplyDefault: if self._supplyDefault:
for (name, v) in list(self._children.items()): for (name, v) in list(self._children.items()):
if v.__class__ is self.X: if not self._wasSet:
self.unregister(name) self.unregister(name)
# We call the callback once everything is clean # We call the callback once everything is clean
for callback, args, kwargs in self._callbacks: for callback, args, kwargs in self._callbacks:
callback(*args, **kwargs) callback(*args, **kwargs)
self._wasSet = True
def context(self, value): def context(self, value):
"""Return a context manager object, which sets this variable to a """Return a context manager object, which sets this variable to a