arg_validator: make ArgValidatorDeprecated a subclass of ArgValidator

ArgValidatorDeprecated is used as a nested validator, it also should
subtype ArgValidator base class. Technially, in python you can do duck
typing and get away with a different class that merely behaves
sufficiently similar. But why?

That way, we also can set default_value=ArgValidator.MISSING.
As a result, we no longer need to special case ArgValidatorDeprecated
at various places, because the default value will indicate that
it should not be present.

Co-authored-by: Till Maas <opensource@till.name>

Signed-off-by: Thomas Haller <thaller@redhat.com>
This commit is contained in:
Thomas Haller 2021-07-15 14:55:31 +02:00 committed by Gris Ge
parent a262c77c26
commit e0a1b6af68

View file

@ -311,11 +311,22 @@ class ArgValidatorBool(ArgValidator):
raise ValidationError(name, "must be an boolean but is '%s'" % (value))
class ArgValidatorDeprecated:
class ArgValidatorDeprecated(ArgValidator):
"""
ArgValidatorDeprecated is only used as a marker to indicate that a setting is deprecated
by another setting. The validator that contains a deprecated setting is responsible for
processing this and the replacement setting needs to perform the validation.
"""
def __init__(self, name, deprecated_by):
self.name = name
ArgValidator.__init__(self, name, default_value=ArgValidator.MISSING)
self.deprecated_by = deprecated_by
def _validate_impl(self, value, name):
raise MyError(
"Deprecated settings need to be validated by the replacement setting."
)
class ArgValidatorDict(ArgValidator):
def __init__(
@ -359,8 +370,6 @@ class ArgValidatorDict(ArgValidator):
for (setting, validator) in self.nested.items():
if setting in seen_keys:
continue
if isinstance(validator, ArgValidatorDeprecated):
continue
if validator.required:
raise ValidationError(name, "missing required key '%s'" % (setting))
if not self.all_missing_during_validate:
@ -1717,11 +1726,9 @@ class ArgValidator_DictConnection(ArgValidatorDict):
for name in self.VALID_FIELDS:
if name in result:
continue
validator = self.nested[name]
if not isinstance(validator, ArgValidatorDeprecated):
value = validator.get_default_value()
if value is not ArgValidator.MISSING:
result[name] = value
value = self.nested[name].get_default_value()
if value is not ArgValidator.MISSING:
result[name] = value
return result