[node-patches] Change in ovirt-node[master]: Support & and | for Validators

fabiand at fedoraproject.org fabiand at fedoraproject.org
Tue Dec 11 20:09:35 UTC 2012


Fabian Deutsch has uploaded a new change for review.

Change subject: Support & and | for Validators
......................................................................

Support & and | for Validators

Allows to combine validators using math. & and |:
IPAddress() | FQDN() | Empty()

Change-Id: Ifc64140ddaec93b60f88ac5612c85be45a4119d2
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M scripts/tui/src/ovirt/node/valid.py
1 file changed, 52 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/97/9897/1

diff --git a/scripts/tui/src/ovirt/node/valid.py b/scripts/tui/src/ovirt/node/valid.py
index 5953525..5965987 100644
--- a/scripts/tui/src/ovirt/node/valid.py
+++ b/scripts/tui/src/ovirt/node/valid.py
@@ -69,6 +69,26 @@
         msg = self.__exception_msg.format(description=self.description)
         raise ovirt.node.exceptions.InvalidData(msg)
 
+    def __or__(self, other):
+        """This allows to combin validators using |
+        """
+        validator = Validator()
+        validator.description = " or ".join([self.description,
+                                             other.description])
+        validator.validate = lambda value: (self.validate(value) or \
+                                            other.validate(value))
+        return validator
+
+    def __and__(self, other):
+        """This allows to combin validators using &
+        """
+        validator = Validator()
+        validator.description = " and ".join([self.description,
+                                             other.description])
+        validator.validate = lambda value: (self.validate(value) and \
+                                            other.validate(value))
+        return validator
+
 
 class RegexValidator(Validator):
     """A validator which uses a regular expression to validate a value.
@@ -225,6 +245,27 @@
     family = socket.AF_INET6
 
 
+class IPAddress(Validator):
+    """Allows any IPv4 or IPv6 address
+
+    >>> FQDNOrIPAddress()("127.0.0.1")
+    True
+    >>> FQDNOrIPAddress()("::1")
+    True
+    >>> FQDNOrIPAddress().validate("example.com")
+    False
+    >>> FQDNOrIPAddress().validate("")
+    False
+    """
+
+    def __init__(self):
+        self._validator = IPv4Address() | IPv6Address()
+        self.description = self._validator.description
+
+    def validate(self, value):
+        return self._validator.validate(value)
+
+
 class FQDNOrIPAddress(Validator):
     """Allows any FQDN, IPv4 or IPv6 address
 
@@ -238,13 +279,12 @@
     False
     """
 
-    description = " or ".join([FQDN.description, IPv4Address.description,
-                               IPv6Address.description])
+    def __init__(self):
+        self._validator = FQDN() | IPAddress()
+        self.description = self._validator.description
 
     def validate(self, value):
-        return (FQDN().validate(value) or \
-                IPv4Address().validate(value) or \
-                IPv6Address().validate(value))
+        return self._validator.validate(value)
 
 
 class Options(Validator):
@@ -258,3 +298,10 @@
     def validate(self, value):
         self.description = self.description % self.options
         return value in self.options
+
+
+class Empty(Validator):
+    description = "an empty string"
+
+    def validate(self, value):
+        return value == ""


--
To view, visit http://gerrit.ovirt.org/9897
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifc64140ddaec93b60f88ac5612c85be45a4119d2
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Fabian Deutsch <fabiand at fedoraproject.org>



More information about the node-patches mailing list