@@ -17,11 +17,11 @@ import urllib import struct import decimal import unicodedata from cStringIO import StringIO -from utils import simple_hash, hmac_hash +from utils import simple_hash, hmac_hash, web2py_uuid __all__ = [ 'CLEANUP', 'CRYPT', 'IS_ALPHANUMERIC', @@ -119,24 +119,45 @@ the argument of IS_MATCH is a regular expression:: >>> IS_MATCH('.+')('hello') ('hello', None) + + >>> IS_MATCH('hell')('hello') + ('hello', 'invalid expression') + + >>> IS_MATCH('hell.*', strict=False)('hello') + ('hello', None) + + >>> IS_MATCH('hello')('shello') + ('shello', 'invalid expression') + + >>> IS_MATCH('hello', search=True)('shello') + ('hello', None) + + >>> IS_MATCH('hello', search=True, strict=False)('shellox') + ('hello', None) + + >>> IS_MATCH('.*hello.*', search=True, strict=False)('shellox') + ('shellox', None) >>> IS_MATCH('.+')('') ('', 'invalid expression') """ - def __init__(self, expression, error_message='invalid expression', strict=True): + def __init__(self, expression, error_message='invalid expression', strict=True, search=False): if strict: if not expression.endswith('$'): expression = '(%s)$' % expression + if not search: + if not expression.startswith('^'): + expression = '^(%s)' % expression self.regex = re.compile(expression) self.error_message = error_message def __call__(self, value): - match = self.regex.match(value) + match = self.regex.search(value) if match: return (match.group(), None) return (value, translate(self.error_message)) @@ -316,11 +337,11 @@ items = [(k, k) for (i, k) in enumerate(self.theset)] else: items = [(k, self.labels[i]) for (i, k) in enumerate(self.theset)] if self.sort: items.sort(options_sorter) - if zero and self.zero != None and not self.multiple: + if zero and not self.zero is None and not self.multiple: items.insert(0,('',self.zero)) return items def __call__(self, value): if self.multiple: @@ -333,11 +354,11 @@ values = [] else: values = [value] failures = [x for x in values if not x in self.theset] if failures and self.theset: - if self.multiple and (value == None or value == ''): + if self.multiple and (value is None or value == ''): return ([], None) return (value, translate(self.error_message)) if self.multiple: if isinstance(self.multiple,(tuple,list)) and \ not self.multiple[0]<=len(values)0: + value = web2py_uuid() + elif len(value) 0)): ok = False - if not (self.is_automatic == None or self.is_automatic == \ + if not (self.is_automatic is None or self.is_automatic == \ (self.automatic[0] <= number <= self.automatic[1])): ok = False if ok: return (value, None) return (value, translate(self.error_message)) if __name__ == '__main__': import doctest doctest.testmod() + +