mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-26 20:59:27 +01:00
Added a Regexp type.
This commit is contained in:
parent
64f4013a2f
commit
3f310efed7
@ -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
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user