]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
python: getopt: move validators logic to parent class
authorRob van der Linde <rob@catalyst.net.nz>
Thu, 5 Oct 2023 01:17:01 +0000 (14:17 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 24 Oct 2023 23:31:29 +0000 (23:31 +0000)
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>
python/samba/getopt.py
python/samba/netcmd/__init__.py

index 1d6f8d11229a69b64481aab115b940f6249a69f1..ea1f338d04c6aac26287e478b414b3c0b192530e 100644 (file)
@@ -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."""
index edd254145f542c4e7ebe9a1412a675dc090eefe8..a1c070b618cb665ad37e0591bc5e7338d2c28098 100644 (file)
@@ -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."""