mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-24 19:14:09 +01:00
src/registry.py, test/test_registry.py: Better handling of backslashes in config values.
This commit is contained in:
parent
76deae4b1d
commit
5b3ca9bd2d
@ -68,13 +68,18 @@ def open(filename, clear=False):
|
|||||||
_fd = file(filename)
|
_fd = file(filename)
|
||||||
fd = utils.file.nonCommentNonEmptyLines(_fd)
|
fd = utils.file.nonCommentNonEmptyLines(_fd)
|
||||||
acc = ''
|
acc = ''
|
||||||
|
slashEnd = re.compile(r'\\*$')
|
||||||
for line in fd:
|
for line in fd:
|
||||||
line = line.rstrip('\r\n')
|
line = line.rstrip('\r\n')
|
||||||
# XXX There should be some way to determine whether or not we're
|
# XXX There should be some way to determine whether or not we're
|
||||||
# starting a new variable or not. As it is, if there's a backslash
|
# starting a new variable or not. As it is, if there's a backslash
|
||||||
# at the end of every line in a variable, it won't be read, and
|
# at the end of every line in a variable, it won't be read, and
|
||||||
# worse, the error will pass silently.
|
# worse, the error will pass silently.
|
||||||
if line.endswith('\\'):
|
#
|
||||||
|
# If the line ends in an odd number of backslashes, then there is a
|
||||||
|
# line-continutation.
|
||||||
|
m = slashEnd.search(line)
|
||||||
|
if m and len(m.group(0)) % 2:
|
||||||
acc += line[:-1]
|
acc += line[:-1]
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
@ -82,7 +87,7 @@ def open(filename, clear=False):
|
|||||||
try:
|
try:
|
||||||
(key, value) = re.split(r'(?<!\\):', acc, 1)
|
(key, value) = re.split(r'(?<!\\):', acc, 1)
|
||||||
key = key.strip()
|
key = key.strip()
|
||||||
value = value.strip()
|
value = value.strip().replace('\\\\', '\\')
|
||||||
acc = ''
|
acc = ''
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise InvalidRegistryFile, 'Error unpacking line %r' % acc
|
raise InvalidRegistryFile, 'Error unpacking line %r' % acc
|
||||||
@ -119,7 +124,7 @@ def close(registry, filename, private=True):
|
|||||||
value._name)
|
value._name)
|
||||||
lines.append('###\n')
|
lines.append('###\n')
|
||||||
fd.writelines(lines)
|
fd.writelines(lines)
|
||||||
if hasattr(value, 'value'): # This lets us print help for non-valued.
|
if hasattr(value, 'value'): # This lets us print help for non-values.
|
||||||
try:
|
try:
|
||||||
if private or not value._private:
|
if private or not value._private:
|
||||||
s = value.serialize()
|
s = value.serialize()
|
||||||
@ -340,7 +345,7 @@ class Value(Group):
|
|||||||
return repr(self())
|
return repr(self())
|
||||||
|
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
return str(self)
|
return str(self).replace('\\', '\\\\')
|
||||||
|
|
||||||
# We tried many, *many* different syntactic methods here, and this one was
|
# We tried many, *many* different syntactic methods here, and this one was
|
||||||
# simply the best -- not very intrusive, easily overridden by subclasses,
|
# simply the best -- not very intrusive, easily overridden by subclasses,
|
||||||
@ -496,7 +501,7 @@ class NormalizedString(String):
|
|||||||
self.__parent.setValue(s)
|
self.__parent.setValue(s)
|
||||||
|
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
s = str(self)
|
s = str(self).replace('\\', '\\\\')
|
||||||
prefixLen = len(self._name) + 2
|
prefixLen = len(self._name) + 2
|
||||||
lines = textwrap.wrap(s, width=76-prefixLen)
|
lines = textwrap.wrap(s, width=76-prefixLen)
|
||||||
last = len(lines)-1
|
last = len(lines)-1
|
||||||
|
@ -167,5 +167,11 @@ class ValuesTestCase(SupyTestCase):
|
|||||||
self.assertRaises(registry.InvalidRegistryValue,
|
self.assertRaises(registry.InvalidRegistryValue,
|
||||||
v.setValue, re.compile(r'foo'))
|
v.setValue, re.compile(r'foo'))
|
||||||
|
|
||||||
|
def testBackslashes(self):
|
||||||
|
conf.supybot.reply.whenAddressedBy.chars.set('\\')
|
||||||
|
filename = conf.supybot.directories.conf.dirize('backslashes.conf')
|
||||||
|
registry.close(conf.supybot, filename)
|
||||||
|
registry.open(filename)
|
||||||
|
self.assertEqual(conf.supybot.reply.whenAddressedBy.chars(), '\\')
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
||||||
|
Loading…
Reference in New Issue
Block a user