diff --git a/src/registry.py b/src/registry.py index 4fa7bc873..8dc3c3504 100644 --- a/src/registry.py +++ b/src/registry.py @@ -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 diff --git a/test/test_registry.py b/test/test_registry.py index 5747dbe06..ce23fa9a1 100644 --- a/test/test_registry.py +++ b/test/test_registry.py @@ -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: