mirror of
https://github.com/samba-team/samba.git
synced 2025-01-11 05:18:09 +03:00
python: getopt: move validators logic to parent class
Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
parent
bdad257a31
commit
29c9991594
@ -75,10 +75,27 @@ def check_bytes(option, opt, value):
|
||||
|
||||
|
||||
class SambaOption(optparse.Option):
|
||||
ATTRS = optparse.Option.ATTRS + ["validators"]
|
||||
TYPES = optparse.Option.TYPES + ("bytes",)
|
||||
TYPE_CHECKER = copy(optparse.Option.TYPE_CHECKER)
|
||||
TYPE_CHECKER["bytes"] = check_bytes
|
||||
|
||||
def run_validators(self, opt, value):
|
||||
"""Runs the list of validators on the current option."""
|
||||
validators = getattr(self, "validators") or []
|
||||
for validator in validators:
|
||||
validator(opt, value)
|
||||
|
||||
def convert_value(self, opt, value):
|
||||
"""Override convert_value to run validators just after.
|
||||
|
||||
This can also be done in process() but there we would have to
|
||||
replace the entire method.
|
||||
"""
|
||||
value = super().convert_value(opt, value)
|
||||
self.run_validators(opt, value)
|
||||
return value
|
||||
|
||||
|
||||
class SambaOptions(optparse.OptionGroup):
|
||||
"""General Samba-related command line options."""
|
||||
|
@ -34,25 +34,8 @@ from .encoders import JSONEncoder
|
||||
|
||||
|
||||
class Option(SambaOption):
|
||||
ATTRS = SambaOption.ATTRS + ["validators"]
|
||||
SUPPRESS_HELP = optparse.SUPPRESS_HELP
|
||||
|
||||
def run_validators(self, opt, value):
|
||||
"""Runs the list of validators on the current option."""
|
||||
validators = getattr(self, "validators") or []
|
||||
for validator in validators:
|
||||
validator(opt, value)
|
||||
|
||||
def convert_value(self, opt, value):
|
||||
"""Override convert_value to run validators just after.
|
||||
|
||||
This can also be done in process() but there we would have to
|
||||
replace the entire method.
|
||||
"""
|
||||
value = super().convert_value(opt, value)
|
||||
self.run_validators(opt, value)
|
||||
return value
|
||||
|
||||
|
||||
class PlainHelpFormatter(optparse.IndentedHelpFormatter):
|
||||
"""This help formatter does text wrapping and preserves newlines."""
|
||||
|
Loading…
Reference in New Issue
Block a user