Package muntjac :: Package data :: Package validators :: Module regexp_validator
[hide private]
[frames] | no frames]

Source Code for Module muntjac.data.validators.regexp_validator

 1  # Copyright (C) 2012 Vaadin Ltd.  
 2  # Copyright (C) 2012 Richard Lincoln 
 3  #  
 4  # Licensed under the Apache License, Version 2.0 (the "License");  
 5  # you may not use this file except in compliance with the License.  
 6  # You may obtain a copy of the License at  
 7  #  
 8  #     http://www.apache.org/licenses/LICENSE-2.0  
 9  #  
10  # Unless required by applicable law or agreed to in writing, software  
11  # distributed under the License is distributed on an "AS IS" BASIS,  
12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
13  # See the License for the specific language governing permissions and  
14  # limitations under the License. 
15   
16  """String validator comparing the string against a regular expression.""" 
17   
18  import re 
19   
20  from muntjac.data.validators.abstract_string_validator import \ 
21      AbstractStringValidator 
22   
23   
24 -class RegexpValidator(AbstractStringValidator):
25 """String validator comparing the string against a regular expression. Both 26 complete matches and substring matches are supported. 27 28 See L{AbstractStringValidator} for more information. 29 30 @author: Vaadin Ltd. 31 @author: Richard Lincoln 32 @version: 1.1.2 33 """ 34
35 - def __init__(self, *args):
36 """Creates a validator for checking that the regular expression matches 37 the string to validate. 38 39 @param args: tuple of the form 40 - (regexp, errorMessage) 41 1. a regular expression 42 2. the message to display in case the value does not validate. 43 - (regexp, complete, errorMessage) 44 1. a regular expression 45 2. true to use check for a complete match, false to look for a 46 matching substring 47 3. the message to display in case the value does not validate. 48 """ 49 self._pattern = None 50 self._regexp = '' 51 self._complete = None 52 self._matcher = None 53 54 nargs = len(args) 55 if nargs == 2: 56 regexp, errorMessage = args 57 self._regexp = regexp 58 RegexpValidator.__init__(self, regexp, True, errorMessage) 59 elif nargs == 3: 60 regexp, complete, errorMessage = args 61 super(RegexpValidator, self).__init__(errorMessage) 62 self._regexp = regexp 63 self._pattern = re.compile(regexp) # FIXME: check re use 64 self._complete = complete 65 else: 66 raise ValueError, 'invalid number of arguments'
67 68
69 - def __getstate__(self):
70 result = self.__dict__.copy() 71 del result['_pattern'] 72 return result
73 74
75 - def __setstate__(self, d):
76 self.__dict__ = d 77 self._pattern = re.compile(d.get('_regexp'))
78 79
80 - def isValidString(self, value):
81 if self._complete: 82 return self._pattern.match(value) 83 else: 84 return self._pattern.search(value) # FIXME: check re use
85