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

@ -212,6 +212,29 @@ class StringWithSpaceOnRight(String):
v += ' ' v += ' '
String.setValue(self, 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): class SeparatedListOf(Value):
Value = Value Value = Value
def splitter(self, s): def splitter(self, s):

View File

@ -31,6 +31,8 @@
from testsupport import * from testsupport import *
import re
import conf import conf
import registry import registry
@ -120,5 +122,15 @@ class ValuesTestCase(unittest.TestCase):
v.set('foo,bar') v.set('foo,bar')
self.assertEqual(v(), ['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: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: