1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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)
64 self._complete = complete
65 else:
66 raise ValueError, 'invalid number of arguments'
67
68
70 result = self.__dict__.copy()
71 del result['_pattern']
72 return result
73
74
76 self.__dict__ = d
77 self._pattern = re.compile(d.get('_regexp'))
78
79
81 if self._complete:
82 return self._pattern.match(value)
83 else:
84 return self._pattern.search(value)
85