Updated to make case insensitivity with preservation much easier (based on utils.InsensitivePreservingDict).

This commit is contained in:
Jeremy Fincher 2004-02-05 07:54:38 +00:00
parent 8eeb9e7221
commit 21ef041a58

View File

@ -53,7 +53,7 @@ class InvalidRegistryValue(RegistryException):
class NonExistentRegistryEntry(RegistryException): class NonExistentRegistryEntry(RegistryException):
pass pass
_cache = {} _cache = utils.InsensitivePreservingDict()
_lastModified = 0 _lastModified = 0
def open(filename): def open(filename):
"""Initializes the module by loading the registry file into memory.""" """Initializes the module by loading the registry file into memory."""
@ -66,35 +66,34 @@ def open(filename):
(key, value) = re.split(r':\s*', line, 1) (key, value) = re.split(r':\s*', line, 1)
except ValueError: except ValueError:
raise InvalidRegistryFile, 'Error unpacking line #%s' % (i+1) raise InvalidRegistryFile, 'Error unpacking line #%s' % (i+1)
_cache[key.lower()] = value _cache[key] = value
_lastModified = time.time() _lastModified = time.time()
def close(registry, filename, annotated=True): def close(registry, filename, annotated=True, helpOnceOnly=False):
first = True first = True
helpCache = sets.Set() helpCache = sets.Set()
fd = file(filename, 'w') fd = file(filename, 'w')
for (name, value) in registry.getValues(getChildren=True): for (name, value) in registry.getValues(getChildren=True):
if annotated and \ if annotated and hasattr(value,'help') and value.help:
hasattr(value,'help') and \ if not helpOnceOnly or value.help not in self.helpCache:
value.help and value.help not in helpCache: helpCache.add(value.help)
helpCache.add(value.help) lines = textwrap.wrap(value.help)
lines = textwrap.wrap(value.help) for (i, line) in enumerate(lines):
for (i, line) in enumerate(lines): lines[i] = '# %s\n' % line
lines[i] = '# %s\n' % line lines.insert(0, '###\n')
lines.insert(0, '###\n') if first:
if first: first = False
first = False else:
else: lines.insert(0, '\n')
lines.insert(0, '\n') lines.append('#\n')
lines.append('#\n') try:
try: original = value.value
original = value.value value.value = value.default
value.value = value.default lines.append('# Default value: %s\n' % value)
lines.append('# Default value: %s\n' % value) finally:
finally: value.value = original
value.value = original lines.append('###\n')
lines.append('###\n') fd.writelines(lines)
fd.writelines(lines)
fd.write('%s: %s\n' % (name, value)) fd.write('%s: %s\n' % (name, value))
fd.close() fd.close()
@ -103,8 +102,7 @@ class Group(object):
def __init__(self, supplyDefault=False): def __init__(self, supplyDefault=False):
self.name = 'unset' self.name = 'unset'
self.added = [] self.added = []
self.children = {} self.children = utils.InsensitivePreservingDict()
self.originals = {}
self._lastModified = 0 self._lastModified = 0
self.supplyDefault = supplyDefault self.supplyDefault = supplyDefault
OriginalClass = self.__class__ OriginalClass = self.__class__
@ -128,18 +126,17 @@ class Group(object):
v.set(s) v.set(s)
v.__class__ = self.X v.__class__ = self.X
v.supplyDefault = False v.supplyDefault = False
v.help = '' # Clear this so it doesn't print a bazillion times.
self.register(attr, v) self.register(attr, v)
return v return v
def __getattr__(self, attr): def __getattr__(self, attr):
original = attr
attr = attr.lower()
if attr in self.children: if attr in self.children:
return self.children[attr] return self.children[attr]
elif self.supplyDefault: elif self.supplyDefault:
return self.__makeChild(original, str(self)) return self.__makeChild(attr, str(self))
else: else:
self.__nonExistentEntry(original) self.__nonExistentEntry(attr)
def get(self, attr): def get(self, attr):
# Not getattr(self, attr) because some nodes might have groups that # Not getattr(self, attr) because some nodes might have groups that
@ -148,9 +145,8 @@ class Group(object):
def setName(self, name): def setName(self, name):
self.name = name self.name = name
lowered = name.lower() if name in _cache and self._lastModified < _lastModified:
if lowered in _cache and self._lastModified < _lastModified: self.set(_cache[name])
self.set(_cache[lowered])
if self.supplyDefault: if self.supplyDefault:
for (k, v) in _cache.iteritems(): for (k, v) in _cache.iteritems():
if k.startswith(self.name): if k.startswith(self.name):
@ -158,29 +154,24 @@ class Group(object):
self.__makeChild(group, v) self.__makeChild(group, v)
def register(self, name, node=None): def register(self, name, node=None):
original = name
name = name.lower()
if node is None: if node is None:
node = Group() node = Group()
if name not in self.children: # XXX Is this right? if name not in self.children: # XXX Is this right?
self.children[name] = node self.children[name] = node
self.added.append(original) self.added.append(name)
self.originals[name] = original fullname = '%s.%s' % (self.name, name)
fullname = '%s.%s' % (self.name, original)
node.setName(fullname) node.setName(fullname)
def unregister(self, name): def unregister(self, name):
original = name
name = name.lower()
try: try:
del self.children[name] del self.children[name]
self.added.remove(original) self.added.remove(name)
except KeyError: except KeyError:
self.__nonExistentEntry(original) self.__nonExistentEntry(name)
def getValues(self, getChildren=False, fullNames=True): def getValues(self, getChildren=False, fullNames=True):
L = [] L = []
for name in map(str.lower, self.added): for name in self.added:
node = self.children[name] node = self.children[name]
if hasattr(node, 'value'): if hasattr(node, 'value'):
if node.__class__ is not self.X: if node.__class__ is not self.X:
@ -225,8 +216,8 @@ class Value(Group):
# This is simply prettier than naming this function get(self) # This is simply prettier than naming this function get(self)
def __call__(self): def __call__(self):
if _lastModified > self._lastModified: if _lastModified > self._lastModified:
if self.name.lower() in _cache: if self.name in _cache:
self.set(_cache[self.name.lower()]) self.set(_cache[self.name])
return self.value return self.value
class Boolean(Value): class Boolean(Value):