[node-patches] Change in ovirt-node[master]: validator: Extend and add doctests

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


Fabian Deutsch has uploaded a new change for review.

Change subject: validator: Extend and add doctests
......................................................................

validator: Extend and add doctests

Change-Id: Iea6c63e9406319e3463b6bea648c60186f75c467
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M scripts/tui/src/ovirt/node/plugins/example.py
M scripts/tui/src/ovirt/node/valid.py
2 files changed, 121 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/73/9873/1

diff --git a/scripts/tui/src/ovirt/node/plugins/example.py b/scripts/tui/src/ovirt/node/plugins/example.py
index 36bb83a..0a4104d 100644
--- a/scripts/tui/src/ovirt/node/plugins/example.py
+++ b/scripts/tui/src/ovirt/node/plugins/example.py
@@ -35,7 +35,7 @@
 
         return {
                 "foo.hostname": ovirt.node.valid.FQDN(),
-                "foo.port": ovirt.node.valid.Number(),
+                "foo.port": ovirt.node.valid.Port(),
                 "foo.password": nospace
             }
 
diff --git a/scripts/tui/src/ovirt/node/valid.py b/scripts/tui/src/ovirt/node/valid.py
index 602c03f..99a8687 100644
--- a/scripts/tui/src/ovirt/node/valid.py
+++ b/scripts/tui/src/ovirt/node/valid.py
@@ -1,8 +1,10 @@
-#!/bin/env python
-
+"""
+A module with several validators for common user inputs.
+"""
 import re
 import logging
 import socket
+import urlparse
 
 import ovirt.node.plugins
 
@@ -12,11 +14,21 @@
 
 
 class Validator(object):
+    """This class is used to validate user inputs
+    Basically an exception is raised if an invalid value was given. The value
+    is validated using the self.validate(value) method raises an exception.
+    This is just a base class because this class doesn't implement the actual
+    validation method.
+
+    Further below you will find an regex validator which uses regular
+    expressions to validate the input.
+    """
     __exception_msg = "The field must contain {description}."
     description = None
 
     def validate(self, value):
         """Validate a given value, return true if valid.
+        This method shall just return True or False and not exception.
 
         Args:
             value: The value to be validated
@@ -38,15 +50,25 @@
         raise ovirt.node.plugins.InvalidData(msg)
 
 class RegexValidator(Validator):
+    """A validator which uses a regular expression to validate a value.
+    """
     # pattern defined by subclass
 
     def validate(self, value):
         if type(self.pattern) in [str, unicode]:
             self.pattern = (self.pattern, )
-        return re.compile(*self.pattern).search(value) != None
+        return re.compile(*self.pattern).search(str(value)) != None
 
 
 class Text(RegexValidator):
+    """Quite anything
+
+    >>> Text()("anything")
+    True
+    >>> Text()("")
+    True
+    """
+
     description = "anything"
     pattern = ".*"
 
@@ -57,44 +79,99 @@
 
 
 class Number(RegexValidator):
+    """A number
+
+    >>> Number()(42)
+    True
+    >>> Number()(-42)
+    True
+    >>> Number()("42")
+    True
+    >>> Number().validate("4 2")
+    False
+    """
+
     description = "a number"
-    pattern = "^\d+$"
-    minmax = None
+    pattern = "^[-]?\d+$"
+    minmax = (None, None)
 
     def __init__(self, min=None, max=None):
         if min or max:
             self.minmax = (min, max)
-            self.description = "a number (%s<%s)" % (min, max)
+            self.description = "%s (%s - %s)" % (self.description, min, max)
 
     def validate(self, value):
-        valid = True
-        RegexValidator.validate(self, value)
-        try:
-            value = int(value)
-            LOGGER.debug("val %d" % value)
+        valid = RegexValidator.validate(self, value)
+        if valid:
             min, max = self.minmax
-            if min and value < min:
-                LOGGER.debug("min %s" % min)
-                self.raise_exception()
-            if max and value > max:
-                LOGGER.debug("max %s" % max)
-                self.raise_exception()
-        except:
-            valid = False
+            value = int(value)
+            if (min and value < min) or (max and value > max):
+                valid = False
         return valid
 
+
+class Port(Number):
+    """An TCP/UDP port number
+
+    >>> Port()(42)
+    True
+    >>> Port().validate(-42)
+    False
+    >>> Port().validate(12345678)
+    False
+    """
+
+    description = "a port number"
+    def __init__(self):
+        super(Port, self).__init__(1, 65535)
+
+
 class NoSpaces(RegexValidator):
+    """A string, but without any space character
+
+    >>> NoSpaces()("abc")
+    True
+    >>> NoSpaces().validate("a b c")
+    False
+    """
+
     description = "a string without spaces"
     pattern = "^\S*$"
 
 
 class FQDN(RegexValidator):
+    """Matches a FQDN
+
+    >>> FQDN()("example.com")
+    True
+    >>> FQDN().validate("example.com.")
+    False
+    >>> FQDN().validate("")
+    False
+    """
+
     description = "a valid FQDN"
     pattern = ("^(([a-z]|[a-z][a-z0-9\-]*[a-z0-9])\.)" +
                "*([a-z]|[a-z][a-z0-9\-]*[a-z0-9])$", re.I)
 
 
 class IPv4Address(Validator):
+    """Matches IPv4 addresses
+
+    >>> IPv4Address()("127.0.0.1")
+    True
+    >>> IPv4Address()("1.2.3.4")
+    True
+    >>> IPv4Address().validate("127.0.0.1.2")
+    False
+    >>> IPv4Address().validate("127.0.0.")
+    False
+    >>> IPv4Address().validate("")
+    False
+    >>> IPv4Address().validate("999.99.9.0")
+    False
+    """
+
     description = "a valid IPv4 address"
     family = socket.AF_INET
     def validate(self, value):
@@ -107,13 +184,37 @@
 
 
 class IPv6Address(IPv4Address):
+    """Validates IPv6 addresses
+
+    >>> IPv6Address()("::1")
+    True
+    >>> IPv6Address()("::")
+    True
+    >>> IPv6Address()("0::0")
+    True
+    >>> IPv6Address().validate("0:::0")
+    False
+    >>> IPv6Address().validate("0::0::0")
+    False
+    """
+
     description = "a valid IPv6 address"
     family = socket.AF_INET6
 
 
 class FQDNOrIPAddress(Validator):
     """Allows any FQDN, IPv4 or IPv6 address
+
+    >>> FQDNOrIPAddress()("example.com")
+    True
+    >>> FQDNOrIPAddress()("127.0.0.1")
+    True
+    >>> FQDNOrIPAddress()("::1")
+    True
+    >>> FQDNOrIPAddress().validate("")
+    False
     """
+
     description = " or ".join([FQDN.description, IPv4Address.description,
                                IPv6Address.description])
 


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iea6c63e9406319e3463b6bea648c60186f75c467
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