Improve PEP8 gluon/validators.py

This commit is contained in:
Richard Vézina
2015-03-31 14:03:38 -04:00
parent f9c755bc18
commit bbd6a081eb
+50 -40
View File
@@ -190,9 +190,9 @@ class IS_MATCH(Validator):
if not expression.endswith('$'): if not expression.endswith('$'):
expression = '(%s)$' % expression expression = '(%s)$' % expression
if is_unicode: if is_unicode:
if not isinstance(expression,unicode): if not isinstance(expression, unicode):
expression = expression.decode('utf8') expression = expression.decode('utf8')
self.regex = re.compile(expression,re.UNICODE) self.regex = re.compile(expression, re.UNICODE)
else: else:
self.regex = re.compile(expression) self.regex = re.compile(expression)
self.error_message = error_message self.error_message = error_message
@@ -200,7 +200,7 @@ class IS_MATCH(Validator):
self.is_unicode = is_unicode self.is_unicode = is_unicode
def __call__(self, value): def __call__(self, value):
if self.is_unicode and not isinstance(value,unicode): if self.is_unicode and not isinstance(value, unicode):
match = self.regex.search(str(value).decode('utf8')) match = self.regex.search(str(value).decode('utf8'))
else: else:
match = self.regex.search(str(value)) match = self.regex.search(str(value))
@@ -348,6 +348,7 @@ class IS_LENGTH(Validator):
return (value, translate(self.error_message) return (value, translate(self.error_message)
% dict(min=self.minsize, max=self.maxsize)) % dict(min=self.minsize, max=self.maxsize))
class IS_JSON(Validator): class IS_JSON(Validator):
""" """
Example: Example:
@@ -377,7 +378,7 @@ class IS_JSON(Validator):
except JSONErrors: except JSONErrors:
return (value, translate(self.error_message)) return (value, translate(self.error_message))
def formatter(self,value): def formatter(self, value):
if value is None: if value is None:
return None return None
if self.native_json: if self.native_json:
@@ -669,7 +670,7 @@ class IS_NOT_IN_DB(Validator):
self.record_id = id self.record_id = id
def __call__(self, value): def __call__(self, value):
if isinstance(value,unicode): if isinstance(value, unicode):
value = value.encode('utf8') value = value.encode('utf8')
else: else:
value = str(value) value = str(value)
@@ -696,7 +697,7 @@ class IS_NOT_IN_DB(Validator):
def range_error_message(error_message, what_to_enter, minimum, maximum): def range_error_message(error_message, what_to_enter, minimum, maximum):
"build the error message for the number range validators" """build the error message for the number range validators"""
if error_message is None: if error_message is None:
error_message = 'Enter ' + what_to_enter error_message = 'Enter ' + what_to_enter
if minimum is not None and maximum is not None: if minimum is not None and maximum is not None:
@@ -938,7 +939,7 @@ class IS_DECIMAL_IN_RANGE(Validator):
def is_empty(value, empty_regex=None): def is_empty(value, empty_regex=None):
"test empty field" """test empty field"""
if isinstance(value, (str, unicode)): if isinstance(value, (str, unicode)):
value = value.strip() value = value.strip()
if empty_regex is not None and empty_regex.match(value): if empty_regex is not None and empty_regex.match(value):
@@ -1153,6 +1154,7 @@ class IS_EMAIL(Validator):
return (value, None) return (value, None)
return (value, translate(self.error_message)) return (value, translate(self.error_message))
class IS_LIST_OF_EMAILS(object): class IS_LIST_OF_EMAILS(object):
""" """
Example: Example:
@@ -1166,7 +1168,8 @@ class IS_LIST_OF_EMAILS(object):
) )
""" """
split_emails = re.compile('[^,;\s]+') split_emails = re.compile('[^,;\s]+')
def __init__(self, error_message = 'Invalid emails: %s'):
def __init__(self, error_message='Invalid emails: %s'):
self.error_message = error_message self.error_message = error_message
def __call__(self, value): def __call__(self, value):
@@ -1182,7 +1185,7 @@ class IS_LIST_OF_EMAILS(object):
return (value, return (value,
translate(self.error_message) % ', '.join(bad_emails)) translate(self.error_message) % ', '.join(bad_emails))
def formatter(self,value,row=None): def formatter(self, value, row=None):
return ', '.join(value or []) return ', '.join(value or [])
@@ -1334,7 +1337,7 @@ label_split_regex = re.compile(u'[\u002e\u3002\uff0e\uff61]')
def escape_unicode(string): def escape_unicode(string):
''' """
Converts a unicode string into US-ASCII, using a simple conversion scheme. Converts a unicode string into US-ASCII, using a simple conversion scheme.
Each unicode character that does not have a US-ASCII equivalent is Each unicode character that does not have a US-ASCII equivalent is
converted into a URL escaped form based on its hexadecimal value. converted into a URL escaped form based on its hexadecimal value.
@@ -1348,7 +1351,7 @@ def escape_unicode(string):
string: the US-ASCII escaped form of the inputted string string: the US-ASCII escaped form of the inputted string
@author: Jonathan Benn @author: Jonathan Benn
''' """
returnValue = StringIO() returnValue = StringIO()
for character in string: for character in string:
@@ -1363,7 +1366,7 @@ def escape_unicode(string):
def unicode_to_ascii_authority(authority): def unicode_to_ascii_authority(authority):
''' """
Follows the steps in RFC 3490, Section 4 to convert a unicode authority Follows the steps in RFC 3490, Section 4 to convert a unicode authority
string into its ASCII equivalent. string into its ASCII equivalent.
For example, u'www.Alliancefran\xe7aise.nu' will be converted into For example, u'www.Alliancefran\xe7aise.nu' will be converted into
@@ -1382,19 +1385,19 @@ def unicode_to_ascii_authority(authority):
authority authority
@author: Jonathan Benn @author: Jonathan Benn
''' """
#RFC 3490, Section 4, Step 1 # RFC 3490, Section 4, Step 1
#The encodings.idna Python module assumes that AllowUnassigned == True # The encodings.idna Python module assumes that AllowUnassigned == True
#RFC 3490, Section 4, Step 2 # RFC 3490, Section 4, Step 2
labels = label_split_regex.split(authority) labels = label_split_regex.split(authority)
#RFC 3490, Section 4, Step 3 # RFC 3490, Section 4, Step 3
#The encodings.idna Python module assumes that UseSTD3ASCIIRules == False # The encodings.idna Python module assumes that UseSTD3ASCIIRules == False
#RFC 3490, Section 4, Step 4 # RFC 3490, Section 4, Step 4
#We use the ToASCII operation because we are about to put the authority # We use the ToASCII operation because we are about to put the authority
#into an IDN-unaware slot # into an IDN-unaware slot
asciiLabels = [] asciiLabels = []
try: try:
import encodings.idna import encodings.idna
@@ -1402,18 +1405,18 @@ def unicode_to_ascii_authority(authority):
if label: if label:
asciiLabels.append(encodings.idna.ToASCII(label)) asciiLabels.append(encodings.idna.ToASCII(label))
else: else:
#encodings.idna.ToASCII does not accept an empty string, but # encodings.idna.ToASCII does not accept an empty string, but
#it is necessary for us to allow for empty labels so that we # it is necessary for us to allow for empty labels so that we
#don't modify the URL # don't modify the URL
asciiLabels.append('') asciiLabels.append('')
except: except:
asciiLabels = [str(label) for label in labels] asciiLabels = [str(label) for label in labels]
#RFC 3490, Section 4, Step 5 # RFC 3490, Section 4, Step 5
return str(reduce(lambda x, y: x + unichr(0x002E) + y, asciiLabels)) return str(reduce(lambda x, y: x + unichr(0x002E) + y, asciiLabels))
def unicode_to_ascii_url(url, prepend_scheme): def unicode_to_ascii_url(url, prepend_scheme):
''' """
Converts the inputed unicode url into a US-ASCII equivalent. This function Converts the inputed unicode url into a US-ASCII equivalent. This function
goes a little beyond RFC 3490, which is limited in scope to the domain name goes a little beyond RFC 3490, which is limited in scope to the domain name
(authority) only. Here, the functionality is expanded to what was observed (authority) only. Here, the functionality is expanded to what was observed
@@ -1443,23 +1446,23 @@ def unicode_to_ascii_url(url, prepend_scheme):
string: a US-ASCII equivalent of the inputed url string: a US-ASCII equivalent of the inputed url
@author: Jonathan Benn @author: Jonathan Benn
''' """
#convert the authority component of the URL into an ASCII punycode string, # convert the authority component of the URL into an ASCII punycode string,
#but encode the rest using the regular URI character encoding # but encode the rest using the regular URI character encoding
groups = url_split_regex.match(url).groups() groups = url_split_regex.match(url).groups()
#If no authority was found # If no authority was found
if not groups[3]: if not groups[3]:
#Try appending a scheme to see if that fixes the problem # Try appending a scheme to see if that fixes the problem
scheme_to_prepend = prepend_scheme or 'http' scheme_to_prepend = prepend_scheme or 'http'
groups = url_split_regex.match( groups = url_split_regex.match(
unicode(scheme_to_prepend) + u'://' + url).groups() unicode(scheme_to_prepend) + u'://' + url).groups()
#if we still can't find the authority # if we still can't find the authority
if not groups[3]: if not groups[3]:
raise Exception('No authority component found, ' + raise Exception('No authority component found, ' +
'could not decode unicode to US-ASCII') 'could not decode unicode to US-ASCII')
#We're here if we found an authority, let's rebuild the URL # We're here if we found an authority, let's rebuild the URL
scheme = groups[1] scheme = groups[1]
authority = groups[3] authority = groups[3]
path = groups[4] or '' path = groups[4] or ''
@@ -2047,12 +2050,12 @@ class IS_URL(Validator):
try: try:
asciiValue = unicode_to_ascii_url(value, self.prepend_scheme) asciiValue = unicode_to_ascii_url(value, self.prepend_scheme)
except Exception: except Exception:
#If we are not able to convert the unicode url into a # If we are not able to convert the unicode url into a
# US-ASCII URL, then the URL is not valid # US-ASCII URL, then the URL is not valid
return (value, translate(self.error_message)) return (value, translate(self.error_message))
methodResult = subMethod(asciiValue) methodResult = subMethod(asciiValue)
#if the validation of the US-ASCII version of the value failed # if the validation of the US-ASCII version of the value failed
if not methodResult[1] is None: if not methodResult[1] is None:
# then return the original input value, not the US-ASCII version # then return the original input value, not the US-ASCII version
return (value, methodResult[1]) return (value, methodResult[1])
@@ -2120,7 +2123,7 @@ class IS_TIME(Validator):
if not value.group('s') is None: if not value.group('s') is None:
s = int(value.group('s')) s = int(value.group('s'))
if value.group('d') == 'pm' and 0 < h < 12: if value.group('d') == 'pm' and 0 < h < 12:
h = h + 12 h += 12
if value.group('d') == 'am' and h == 12: if value.group('d') == 'am' and h == 12:
h = 0 h = 0
if not (h in range(24) and m in range(60) and s if not (h in range(24) and m in range(60) and s
@@ -2134,18 +2137,23 @@ class IS_TIME(Validator):
pass pass
return (ivalue, translate(self.error_message)) return (ivalue, translate(self.error_message))
# A UTC class. # A UTC class.
class UTC(datetime.tzinfo): class UTC(datetime.tzinfo):
"""UTC""" """UTC"""
ZERO = datetime.timedelta(0) ZERO = datetime.timedelta(0)
def utcoffset(self, dt): def utcoffset(self, dt):
return UTC.ZERO return UTC.ZERO
def tzname(self, dt): def tzname(self, dt):
return "UTC" return "UTC"
def dst(self, dt): def dst(self, dt):
return UTC.ZERO return UTC.ZERO
utc = UTC() utc = UTC()
class IS_DATE(Validator): class IS_DATE(Validator):
""" """
Examples: Examples:
@@ -2159,7 +2167,7 @@ class IS_DATE(Validator):
def __init__(self, format='%Y-%m-%d', def __init__(self, format='%Y-%m-%d',
error_message='Enter date as %(format)s', error_message='Enter date as %(format)s',
timezone = None): timezone=None):
self.format = translate(format) self.format = translate(format)
self.error_message = str(error_message) self.error_message = str(error_message)
self.timezone = timezone self.timezone = timezone
@@ -2402,7 +2410,7 @@ class IS_LIST_OF(Validator):
new_value = [] new_value = []
other = self.other other = self.other
if self.other: if self.other:
if not isinstance(other, (list,tuple)): if not isinstance(other, (list, tuple)):
other = [other] other = [other]
for item in ivalue: for item in ivalue:
v = item v = item
@@ -2625,7 +2633,7 @@ class IS_EMPTY_OR(Validator):
return self.other.formatter(value) return self.other.formatter(value)
return value return value
IS_NULL_OR = IS_EMPTY_OR # for backward compatibility IS_NULL_OR = IS_EMPTY_OR # for backward compatibility
class CLEANUP(Validator): class CLEANUP(Validator):
@@ -2735,6 +2743,7 @@ class LazyCrypt(object):
def __ne__(self, other): def __ne__(self, other):
return not self.__eq__(other) return not self.__eq__(other)
class CRYPT(object): class CRYPT(object):
""" """
Examples: Examples:
@@ -2852,7 +2861,7 @@ otherset = frozenset(
def calc_entropy(string): def calc_entropy(string):
" calculates a simple entropy for a given string " """ calculates a simple entropy for a given string """
import math import math
alphabet = 0 # alphabet size alphabet = 0 # alphabet size
other = set() other = set()
@@ -3374,6 +3383,7 @@ class IS_IPV4(Validator):
return (value, None) return (value, None)
return (value, translate(self.error_message)) return (value, translate(self.error_message))
class IS_IPV6(Validator): class IS_IPV6(Validator):
""" """
Checks if field's value is an IP version 6 address. First attempts to Checks if field's value is an IP version 6 address. First attempts to