[node-patches] Change in ovirt-node[master]: Use a better URL validator. RegexValidator adds flags
rbarry at redhat.com
rbarry at redhat.com
Mon Jun 15 16:19:10 UTC 2015
Ryan Barry has uploaded a new change for review.
Change subject: Use a better URL validator. RegexValidator adds flags
......................................................................
Use a better URL validator. RegexValidator adds flags
valid.url relied on urlparse very heavily, but urlparse did not
throw an exception on some invalid addresses. urllib3 does this
better, but is not available in EL6.
Use the URL validator from django instead, and validate with a
regex.
To support this, give RegexValidator easier-to-set flags as part
of the class constructor.
Change-Id: I13429d0cc600446c8fd4c1be5e5a8752d6d992f7
Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=1231029
Signed-off-by: Ryan Barry <rbarry at redhat.com>
---
M src/ovirt/node/valid.py
1 file changed, 28 insertions(+), 30 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/94/42394/1
diff --git a/src/ovirt/node/valid.py b/src/ovirt/node/valid.py
index 7b1bb45..8f6dc9d 100644
--- a/src/ovirt/node/valid.py
+++ b/src/ovirt/node/valid.py
@@ -141,11 +141,13 @@
"""
# pattern defined by subclass
pattern = None
+ flags = None
- def __init__(self, pattern=None, description=None):
+ def __init__(self, pattern=None, description=None, flags=0):
super(RegexValidator, self).__init__()
self.pattern = self.pattern or pattern
self.description = self.description or description
+ self.flags = self.flags or flags
def validate(self, value):
if type(self.pattern) in [str, unicode]:
@@ -156,8 +158,11 @@
pass
else:
self.logger.warning("Unknown type: %s %s" % (value, type(value)))
+ if len(self.pattern) > 1:
+ print self.pattern
return value is not None and \
- re.compile(*self.pattern).search(value) is not None
+ re.compile(*self.pattern, flags=self.flags).search(value) \
+ is not None
class Text(RegexValidator):
@@ -311,7 +316,8 @@
description = "a valid FQDN"
pattern = ("^(?!(\d+\.){3}\d+)(([a-z0-9\-]*[a-z0-9])\.)*" +
- "(([a-z0-9\-]*[a-z0-9])\.?)$", re.I)
+ "(([a-z0-9\-]*[a-z0-9])\.?)$")
+ flags = re.IGNORECASE
def validate(self, value):
is_valid = super(FQDN, self).validate(value)
@@ -547,44 +553,36 @@
return value == "" or (self.or_none and value is None)
-class URL(Validator):
+class URL(RegexValidator):
"""Allows any FQDN, IPv4 or IPv6 address
>>> URL().validate("")
False
>>> URL().validate("https://1.2.3.4/abc")
True
- >>> URL(True, True, False).validate("https://1.2.3.4/")
+ >>> URL().validate("https://1.2.3.4/")
True
- >>> URL(True, True, False).validate("https://1.2.3.4")
+ >>> URL().validate("https://1.2.3.4")
True
- >>> URL(True, False, False).validate("https:///")
- True
+ >>> URL().validate("https:///")
+ False
+ >>> URL().validate("http://1.2.3./")
+ False
+ >>> URL().validate("http://1.2.3.4:")
+ False
+ >>> URL().validate("http://1.2.3.4::")
+ False
"""
description = "a valid URL"
-
- requires_scheme = True
- requires_netloc = True
- requires_path = False
-
- def __init__(self, scheme=True, netloc=True, path=False):
- self.requires_scheme = scheme
- self.requires_netloc = netloc
- self.requires_path = path
-
- def validate(self, value):
- p = urlparse.urlparse(value)
- is_valid = True
- # pylint: disable-msg=E1101
- if self.requires_scheme:
- is_valid &= p.scheme != ""
- if self.requires_netloc:
- is_valid &= p.netloc != ""
- if self.requires_path:
- is_valid &= p.path != ""
- # pylint: enable-msg=E1101
- return is_valid
+ pattern = r"""^(?:http|ftp)s?:// # protocol
+ (?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}
+ [A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)| # domain
+ localhost| # or localhost...
+ \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) # or ip
+ (?::\d+)? # and port
+ (?:/?|[/?]\S+)$ # path"""
+ flags = re.IGNORECASE | re.VERBOSE
class Boolean(Validator):
--
To view, visit https://gerrit.ovirt.org/42394
To unsubscribe, visit https://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I13429d0cc600446c8fd4c1be5e5a8752d6d992f7
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Ryan Barry <rbarry at redhat.com>
More information about the node-patches
mailing list