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