Added a Regexp type.

This commit is contained in:
Jeremy Fincher 2004-01-27 18:14:44 +00:00
parent 64f4013a2f
commit 3f310efed7
2 changed files with 35 additions and 0 deletions

View File

@ -211,6 +211,29 @@ class StringWithSpaceOnRight(String):
if v.rstrip() == v:
v += ' '
String.setValue(self, v)
class Regexp(Value):
def set(self, s):
try:
if s:
self.value = utils.perlReToPythonRe(s)
else:
self.value = None
self.sr = s
except ValueError, e:
raise InvalidRegistryValue, 'Value must be a valid regexp: %s' % e
def setValue(self, v):
if v is None:
self.sr = ''
self.value = None
else:
raise InvalidRegistryValue, \
'Can\'t set to a regexp, there would be an inconsistency ' \
'between the regexp and the recorded string value.'
def __str__(self):
return self.sr
class SeparatedListOf(Value):
Value = Value

View File

@ -31,6 +31,8 @@
from testsupport import *
import re
import conf
import registry
@ -119,6 +121,16 @@ class ValuesTestCase(unittest.TestCase):
self.assertEqual(v(), ['foo', 'bar', 'baz'])
v.set('foo,bar')
self.assertEqual(v(), ['foo', 'bar'])
def testRegexp(self):
v = registry.Regexp(None, 'help')
self.assertEqual(v(), None)
v.set('m/foo/')
self.failUnless(v().match('foo'))
v.set('')
self.assertEqual(v(), None)
self.assertRaises(registry.InvalidRegistryValue,
v.setValue, re.compile(r'foo'))
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: