Line wrapping, and now more showDefault for long things (since I can't figure out how to wrap it), and line-wrapping of serialized NormalizedString things.

This commit is contained in:
Jeremy Fincher 2004-09-30 15:22:50 +00:00
parent 43fbd5ac77
commit 6ce21297de
1 changed files with 23 additions and 4 deletions

View File

@ -30,6 +30,7 @@
__revision__ = "$Id$" __revision__ = "$Id$"
import re import re
import os
import sets import sets
import time import time
import string import string
@ -62,15 +63,20 @@ def open(filename, clear=False):
_cache.clear() _cache.clear()
_fd = file(filename) _fd = file(filename)
fd = utils.nonCommentNonEmptyLines(_fd) fd = utils.nonCommentNonEmptyLines(_fd)
for (i, line) in enumerate(fd): acc = ''
for line in fd:
line = line.rstrip('\r\n') line = line.rstrip('\r\n')
if line.endswith('\\'):
acc += line[:-1]
continue
else:
acc = line
try: try:
#print '***', repr(line) (key, value) = re.split(r'(?<!\\):', acc, 1)
(key, value) = re.split(r'(?<!\\):', line, 1)
key = key.strip() key = key.strip()
value = value.strip() value = value.strip()
except ValueError: except ValueError:
raise InvalidRegistryFile, 'Error unpacking line %r' % line raise InvalidRegistryFile, 'Error unpacking line %r' % acc
_cache[key] = value _cache[key] = value
_lastModified = time.time() _lastModified = time.time()
_fd.close() _fd.close()
@ -447,6 +453,7 @@ class NormalizedString(String):
default = self.normalize(default) default = self.normalize(default)
self.__parent = super(NormalizedString, self) self.__parent = super(NormalizedString, self)
self.__parent.__init__(default, *args, **kwargs) self.__parent.__init__(default, *args, **kwargs)
self.showDefault = False
def normalize(self, s): def normalize(self, s):
return utils.normalizeWhitespace(s.strip()) return utils.normalizeWhitespace(s.strip())
@ -459,6 +466,18 @@ class NormalizedString(String):
s = self.normalize(s) s = self.normalize(s)
self.__parent.setValue(s) self.__parent.setValue(s)
def serialize(self):
s = str(self)
prefixLen = len(self._name) + 2
lines = textwrap.wrap(s, width=76-prefixLen)
first = True
for (i, line) in enumerate(lines):
if not first:
line = ' '*prefixLen + line
lines[i] = line + '\\'
first = False
return os.linesep.join(lines)
class StringSurroundedBySpaces(String): class StringSurroundedBySpaces(String):
def setValue(self, v): def setValue(self, v):
if v.lstrip() == v: if v.lstrip() == v: