Package konval :: Module stringval
[hide private]
[frames] | no frames]

Source Code for Module konval.stringval

  1  """ 
  2  Validators that clean or transform strings. 
  3   
  4  """ 
  5   
  6  __docformat__ = "restructuredtext en" 
  7   
  8   
  9  ### IMPORTS 
 10   
 11  import re 
 12   
 13  import impl 
 14  from basevalidator import BaseValidator 
 15   
 16   
 17  ### CONSTANTS & DEFINES 
 18   
 19  ### IMPLEMENTATION ### 
 20   
21 -class Strip (BaseValidator):
22 """ 23 Transform strings by stripping flanking space. 24 25 Note that this does not explicitly throw errors. 26 27 For example:: 28 29 >>> v = Strip() 30 >>> v (' abc ') 31 'abc' 32 33 """
34 - def convert_value (self, value):
35 return value.strip()
36 37
38 -class ToLower (BaseValidator):
39 """ 40 Transform strings by converting to lower case. 41 42 Note that this does not explicitly throw errors. 43 44 For example:: 45 46 >>> v = ToLower() 47 >>> v ('aBcD') 48 'abcd' 49 50 """
51 - def convert_value (self, value):
52 return value.lower()
53 54
55 -class ToUpper (BaseValidator):
56 """ 57 Transform strings by converting to upper case. 58 59 Note that this does not explicitly throw errors. 60 61 For example:: 62 63 >>> v = ToUpper() 64 >>> v ('aBcD') 65 'ABCD' 66 67 """
68 - def convert_value (self, value):
69 return value.upper()
70 71
72 -class IsNonblank (BaseValidator):
73 """ 74 Only allow non-blank strings (i.e. those with a length more than 0). 75 76 For example:: 77 78 >>> v = IsNonblank() 79 >>> v ('abcd') 80 'abcd' 81 >>> v ('') 82 Traceback (most recent call last): 83 ... 84 ValueError: can't validate '' 85 86 """
87 - def validate_value (self, value):
88 assert isinstance (value, basestring) 89 assert 0 < len(value), "can't be a blank string" 90 return True
91 92
93 -class IsRegexMatch (BaseValidator):
94 """ 95 Only allow values that match a certain regular expression. 96 97 For example:: 98 99 >>> v = IsRegexMatch('[a-z]+') 100 >>> v ('abcd') 101 'abcd' 102 >>> v ('') 103 Traceback (most recent call last): 104 ... 105 ValueError: '' does not match the pattern '[a-z]+' 106 107 """ 108 # TODO: compile flags?
109 - def __init__ (self, patt):
110 self.re = re.compile (patt) 111 self.patt = patt
112
113 - def validate_value (self, value):
114 return self.re.match (value)
115
116 - def make_validation_error_msg (self, bad_val, err):
117 """ 118 Generate an meaningful error message for an empty value. 119 """ 120 return "'%s' does not match the pattern '%s'" % (bad_val, self.patt)
121 122
123 -class IsPlainText(IsRegexMatch):
124 """ 125 Check the value only contains alphanumerics, underscores & hyphen. 126 127 Useful for checking identifiers. 128 129 For example:: 130 131 >>> v = IsPlainText() 132 >>> v ('abcd') 133 'abcd' 134 >>> v ('ab cd') 135 Traceback (most recent call last): 136 ... 137 ValueError: 'ab cd' is not plain text 138 >>> v ('ab!cd') 139 Traceback (most recent call last): 140 ... 141 ValueError: 'ab!cd' is not plain text 142 143 Idea flinched from FormEncode. 144 """
145 - def __init__ (self):
146 IsRegexMatch.__init__ (self, r'^[a-zA-Z_\-0-9]*$')
147
148 - def make_validation_error_msg (self, bad_val, err):
149 """ 150 Generate an meaningful error message for an empty value. 151 """ 152 return "'%s' is not plain text" % (bad_val)
153 154
155 -class ToCanonical (BaseValidator):
156 """ 157 Reduce strings to a canonical form. 158 159 A common problem in cleaning user input is to catch trivial variants, e.g. 160 how to recognise 'foo-bar', 'Foo-bar', ' foo-bar ' and 'foo_bar' as being 161 the same value. This function achieves that by stripping flanking spaces, 162 converting letters to uppercase and converting internal stretches of spaces, 163 underscores and hyphens to a single underscore. Thus, all of the previous 164 values would be converted to 'FOO_BAR'. 165 166 For example:: 167 168 >>> v = ToCanonical() 169 >>> v ('aBcD') 170 'ABCD' 171 >>> v (' ab cd_') 172 'AB_CD_' 173 >>> v ('AB-_ CD') 174 'AB_CD' 175 176 """
177 - def convert_value (self, value):
178 return impl.make_canonical (value)
179 180 181 182 ## DEBUG & TEST ### 183 184 if __name__ == "__main__": 185 import doctest 186 doctest.testmod() 187 188 189 190 191 192 ### END ####################################################################### 193