2004-01-13 07:07:31 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
2004-01-13 16:56:58 +01:00
|
|
|
# Copyright (c) 2004, Jeremiah Fincher
|
2004-01-13 07:07:31 +01:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2004-01-20 18:09:57 +01:00
|
|
|
import re
|
2004-01-13 07:07:31 +01:00
|
|
|
import copy
|
2004-01-18 08:58:26 +01:00
|
|
|
import sets
|
2004-01-22 21:16:21 +01:00
|
|
|
import time
|
2004-01-18 08:58:26 +01:00
|
|
|
import types
|
2004-01-21 16:48:48 +01:00
|
|
|
import textwrap
|
2004-01-13 07:07:31 +01:00
|
|
|
|
2004-01-21 18:13:04 +01:00
|
|
|
import fix
|
2004-01-13 07:07:31 +01:00
|
|
|
import utils
|
|
|
|
|
|
|
|
class RegistryException(Exception):
|
|
|
|
pass
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
class InvalidRegistryFile(RegistryException):
|
|
|
|
pass
|
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
class InvalidRegistryValue(RegistryException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class NonExistentRegistryEntry(RegistryException):
|
|
|
|
pass
|
|
|
|
|
2004-01-20 12:41:53 +01:00
|
|
|
_cache = {}
|
2004-01-22 21:16:21 +01:00
|
|
|
_lastModified = 0
|
2004-01-14 07:05:58 +01:00
|
|
|
def open(filename):
|
|
|
|
"""Initializes the module by loading the registry file into memory."""
|
2004-01-22 21:16:21 +01:00
|
|
|
global _lastModified
|
2004-01-20 12:41:53 +01:00
|
|
|
_cache.clear()
|
2004-01-14 07:05:58 +01:00
|
|
|
fd = utils.nonCommentNonEmptyLines(file(filename))
|
2004-01-18 08:58:26 +01:00
|
|
|
for (i, line) in enumerate(fd):
|
|
|
|
line = line.rstrip('\r\n')
|
|
|
|
try:
|
2004-01-20 18:09:57 +01:00
|
|
|
(key, value) = re.split(r':\s*', line, 1)
|
2004-01-18 08:58:26 +01:00
|
|
|
except ValueError:
|
|
|
|
raise InvalidRegistryFile, 'Error unpacking line #%s' % (i+1)
|
2004-01-20 12:41:53 +01:00
|
|
|
_cache[key.lower()] = value
|
2004-01-22 21:16:21 +01:00
|
|
|
_lastModified = time.time()
|
2004-01-14 07:05:58 +01:00
|
|
|
|
2004-01-21 16:48:48 +01:00
|
|
|
def close(registry, filename, annotated=True):
|
2004-01-21 18:13:04 +01:00
|
|
|
first = True
|
|
|
|
helpCache = sets.Set()
|
2004-01-14 07:05:58 +01:00
|
|
|
fd = file(filename, 'w')
|
2004-01-18 08:58:26 +01:00
|
|
|
for (name, value) in registry.getValues(getChildren=True):
|
2004-01-21 20:12:51 +01:00
|
|
|
if annotated and \
|
|
|
|
hasattr(value,'help') and \
|
|
|
|
value.help and \
|
|
|
|
value.help not in helpCache:
|
2004-01-21 18:13:04 +01:00
|
|
|
helpCache.add(value.help)
|
2004-01-21 16:48:48 +01:00
|
|
|
lines = textwrap.wrap(value.help)
|
|
|
|
for (i, line) in enumerate(lines):
|
|
|
|
lines[i] = '# %s\n' % line
|
|
|
|
lines.insert(0, '###\n')
|
2004-01-21 18:13:04 +01:00
|
|
|
if first:
|
|
|
|
first = False
|
|
|
|
else:
|
|
|
|
lines.insert(0, '\n')
|
2004-01-21 16:48:48 +01:00
|
|
|
lines.append('###\n')
|
|
|
|
fd.writelines(lines)
|
2004-01-14 07:05:58 +01:00
|
|
|
fd.write('%s: %s\n' % (name, value))
|
|
|
|
fd.close()
|
2004-01-18 08:58:26 +01:00
|
|
|
|
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
class Value(object):
|
|
|
|
def __init__(self, default, help):
|
2004-01-18 08:58:26 +01:00
|
|
|
self.default = default
|
|
|
|
self.help = utils.normalizeWhitespace(help.strip())
|
|
|
|
self.setValue(default)
|
2004-01-13 07:07:31 +01:00
|
|
|
|
|
|
|
def set(self, s):
|
|
|
|
"""Override this with a function to convert a string to whatever type
|
2004-01-20 16:08:08 +01:00
|
|
|
you want, and call self.setValue to set the value."""
|
2004-01-13 07:07:31 +01:00
|
|
|
raise NotImplementedError
|
2004-01-15 13:17:50 +01:00
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
def setValue(self, v):
|
2004-01-20 16:08:08 +01:00
|
|
|
"""Check conditions on the actual value type here. I.e., if you're a
|
|
|
|
IntegerLessThanOneHundred (all your values must be integers less than
|
|
|
|
100) convert to an integer in set() and check that the integer is less
|
2004-01-22 21:16:21 +01:00
|
|
|
than 100 in this method. You *must* call this parent method in your
|
|
|
|
own setValue."""
|
|
|
|
self._lastModified = time.time()
|
2004-01-18 08:58:26 +01:00
|
|
|
self.value = v
|
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
def __str__(self):
|
2004-01-22 21:16:21 +01:00
|
|
|
return repr(self())
|
2004-01-13 07:07:31 +01:00
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
# This is simply prettier than naming this function get(self)
|
|
|
|
def __call__(self):
|
2004-01-22 21:16:21 +01:00
|
|
|
# TODO: Check _lastModified to see if stuff needs to be reloaded from
|
|
|
|
# the _cache.
|
2004-01-18 08:58:26 +01:00
|
|
|
return self.value
|
2004-01-25 09:22:50 +01:00
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
class Boolean(Value):
|
|
|
|
def set(self, s):
|
|
|
|
s = s.lower()
|
2004-01-21 16:48:48 +01:00
|
|
|
if s in ('true', 'on', 'enable', 'enabled'):
|
2004-01-20 16:08:08 +01:00
|
|
|
value = True
|
2004-01-21 16:48:48 +01:00
|
|
|
elif s in ('false', 'off', 'disable', 'disabled'):
|
2004-01-20 16:08:08 +01:00
|
|
|
value = False
|
2004-01-18 08:58:26 +01:00
|
|
|
elif s == 'toggle':
|
2004-01-20 16:08:08 +01:00
|
|
|
value = not self.value
|
2004-01-13 07:07:31 +01:00
|
|
|
else:
|
|
|
|
raise InvalidRegistryValue, 'Value must be True or False.'
|
2004-01-20 16:08:08 +01:00
|
|
|
self.setValue(value)
|
|
|
|
|
|
|
|
def setValue(self, v):
|
2004-01-22 21:16:21 +01:00
|
|
|
Value.setValue(self, bool(v))
|
2004-01-13 07:07:31 +01:00
|
|
|
|
|
|
|
class Integer(Value):
|
|
|
|
def set(self, s):
|
|
|
|
try:
|
2004-01-20 16:08:08 +01:00
|
|
|
self.setValue(int(s))
|
2004-01-13 07:07:31 +01:00
|
|
|
except ValueError:
|
|
|
|
raise InvalidRegistryValue, 'Value must be an integer.'
|
|
|
|
|
2004-01-20 00:42:50 +01:00
|
|
|
class PositiveInteger(Value):
|
|
|
|
def set(self, s):
|
|
|
|
try:
|
2004-01-20 16:08:08 +01:00
|
|
|
self.setValue(int(s))
|
2004-01-20 00:42:50 +01:00
|
|
|
except ValueError:
|
|
|
|
raise InvalidRegistryValue, 'Value must be a positive integer.'
|
|
|
|
|
2004-01-20 16:08:08 +01:00
|
|
|
def setValue(self, v):
|
|
|
|
if v <= 0:
|
2004-01-21 16:48:48 +01:00
|
|
|
raise InvalidRegistryValue, 'Value must be a positive integer.'
|
2004-01-22 21:16:21 +01:00
|
|
|
Value.setValue(self, v)
|
2004-01-20 16:08:08 +01:00
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
class Float(Value):
|
|
|
|
def set(self, s):
|
|
|
|
try:
|
2004-01-20 16:08:08 +01:00
|
|
|
self.setValue(float(s))
|
2004-01-18 08:58:26 +01:00
|
|
|
except ValueError:
|
|
|
|
raise InvalidRegistryValue, 'Value must be a float.'
|
|
|
|
|
2004-01-21 16:48:48 +01:00
|
|
|
def setValue(self, v):
|
|
|
|
try:
|
2004-01-22 21:16:21 +01:00
|
|
|
Value.setValue(self, float(v))
|
2004-01-21 16:48:48 +01:00
|
|
|
except ValueError:
|
|
|
|
raise InvalidRegistryValue, 'Value must be a float.'
|
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
class String(Value):
|
|
|
|
def set(self, s):
|
2004-01-21 16:48:48 +01:00
|
|
|
if not s:
|
|
|
|
s = '""'
|
|
|
|
elif s[0] != s[-1] or s[0] not in '\'"':
|
2004-01-13 07:07:31 +01:00
|
|
|
s = repr(s)
|
|
|
|
try:
|
|
|
|
v = utils.safeEval(s)
|
2004-01-20 05:36:49 +01:00
|
|
|
if not isinstance(v, basestring):
|
2004-01-13 07:07:31 +01:00
|
|
|
raise ValueError
|
|
|
|
self.value = v
|
|
|
|
except ValueError: # This catches utils.safeEval(s) errors too.
|
|
|
|
raise InvalidRegistryValue, 'Value must be a string.'
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
class NormalizedString(String):
|
|
|
|
def set(self, s):
|
|
|
|
s = utils.normalizeWhitespace(s.strip())
|
|
|
|
String.set(self, s)
|
|
|
|
|
2004-01-19 21:51:04 +01:00
|
|
|
def setValue(self, s):
|
|
|
|
s = utils.normalizeWhitespace(s.strip())
|
|
|
|
String.setValue(self, s)
|
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
class StringSurroundedBySpaces(String):
|
|
|
|
def set(self, s):
|
|
|
|
String.set(self, s)
|
2004-01-20 16:08:08 +01:00
|
|
|
self.setValue(self.value)
|
|
|
|
|
|
|
|
def setValue(self, v):
|
|
|
|
if v.lstrip() == v:
|
2004-01-21 16:48:48 +01:00
|
|
|
v= ' ' + v
|
2004-01-20 16:08:08 +01:00
|
|
|
if v.rstrip() == v:
|
|
|
|
v += ' '
|
2004-01-22 21:16:21 +01:00
|
|
|
String.setValue(self, v)
|
2004-01-13 07:07:31 +01:00
|
|
|
|
2004-01-23 14:28:53 +01:00
|
|
|
class StringWithSpaceOnRight(String):
|
|
|
|
def setValue(self, v):
|
|
|
|
if v.rstrip() == v:
|
|
|
|
v += ' '
|
|
|
|
String.setValue(self, v)
|
2004-01-27 19:14:44 +01:00
|
|
|
|
|
|
|
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
|
2004-01-23 14:28:53 +01:00
|
|
|
|
2004-01-21 17:15:31 +01:00
|
|
|
class SeparatedListOf(Value):
|
|
|
|
Value = Value
|
|
|
|
def splitter(self, s):
|
|
|
|
"""Override this with a function that takes a string and returns a list
|
|
|
|
of strings."""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def joiner(self, L):
|
|
|
|
"""Override this to join the internal list for output."""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
def set(self, s):
|
2004-01-21 17:15:31 +01:00
|
|
|
L = self.splitter(s)
|
|
|
|
for (i, s) in enumerate(L):
|
|
|
|
v = self.Value(s, 'help does not matter here')
|
|
|
|
L[i] = v()
|
|
|
|
self.setValue(L)
|
2004-01-18 08:58:26 +01:00
|
|
|
|
2004-01-27 12:25:36 +01:00
|
|
|
def setValue(self, v):
|
|
|
|
Value.setValue(self, list(v))
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
def __str__(self):
|
2004-01-21 17:15:31 +01:00
|
|
|
return self.joiner(self.value)
|
|
|
|
|
2004-01-21 18:13:04 +01:00
|
|
|
class SpaceSeparatedListOfStrings(SeparatedListOf):
|
|
|
|
Value = String
|
|
|
|
def splitter(self, s):
|
2004-01-21 20:12:51 +01:00
|
|
|
return s.split()
|
2004-01-21 18:13:04 +01:00
|
|
|
joiner = ' '.join
|
|
|
|
|
2004-01-21 17:15:31 +01:00
|
|
|
class CommaSeparatedListOfStrings(SeparatedListOf):
|
|
|
|
Value = String
|
|
|
|
def splitter(self, s):
|
|
|
|
return re.split(r'\s*,\s*', s)
|
2004-01-21 18:13:04 +01:00
|
|
|
joiner = ', '.join
|
2004-01-21 17:15:31 +01:00
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
class CommaSeparatedSetOfStrings(CommaSeparatedListOfStrings):
|
2004-01-20 16:08:08 +01:00
|
|
|
def setValue(self, v):
|
2004-01-22 21:16:21 +01:00
|
|
|
CommaSeparatedListOfStrings.setValue(self, sets.Set(v))
|
2004-01-13 07:07:31 +01:00
|
|
|
|
|
|
|
class Group(object):
|
|
|
|
def __init__(self):
|
2004-01-18 08:58:26 +01:00
|
|
|
self.name = 'unset'
|
|
|
|
self.values = {}
|
|
|
|
self.children = {}
|
|
|
|
self.originals = {}
|
2004-01-13 07:07:31 +01:00
|
|
|
|
2004-01-13 16:56:58 +01:00
|
|
|
def __nonExistentEntry(self, attr):
|
|
|
|
s = '%s is not a valid entry in %s' % (attr, self.name)
|
|
|
|
raise NonExistentRegistryEntry, s
|
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
def __getattr__(self, attr):
|
|
|
|
original = attr
|
|
|
|
attr = attr.lower()
|
|
|
|
if attr in self.values:
|
2004-01-18 08:58:26 +01:00
|
|
|
return self.values[attr]
|
2004-01-13 07:07:31 +01:00
|
|
|
elif attr in self.children:
|
|
|
|
return self.children[attr]
|
|
|
|
else:
|
2004-01-13 16:56:58 +01:00
|
|
|
self.__nonExistentEntry(original)
|
2004-01-13 07:07:31 +01:00
|
|
|
|
2004-01-19 21:51:04 +01:00
|
|
|
def get(self, attr):
|
|
|
|
return self.__getattr__(attr)
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
def getChild(self, attr):
|
|
|
|
return self.children[attr.lower()]
|
2004-01-13 16:56:58 +01:00
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
def setName(self, name):
|
2004-01-18 08:58:26 +01:00
|
|
|
self.name = name
|
2004-01-13 07:07:31 +01:00
|
|
|
|
|
|
|
def getName(self):
|
2004-01-18 08:58:26 +01:00
|
|
|
return self.name
|
2004-01-13 07:07:31 +01:00
|
|
|
|
|
|
|
def register(self, name, value):
|
|
|
|
original = name
|
|
|
|
name = name.lower()
|
|
|
|
self.values[name] = value
|
2004-01-13 16:56:58 +01:00
|
|
|
self.originals[name] = original
|
2004-01-20 12:41:53 +01:00
|
|
|
if _cache:
|
2004-01-14 07:05:58 +01:00
|
|
|
fullname = '%s.%s' % (self.name, name)
|
2004-01-20 12:41:53 +01:00
|
|
|
if fullname in _cache:
|
|
|
|
value.set(_cache[fullname])
|
2004-01-13 07:07:31 +01:00
|
|
|
|
|
|
|
def registerGroup(self, name, group=None):
|
|
|
|
original = name
|
|
|
|
name = name.lower()
|
2004-01-18 08:58:26 +01:00
|
|
|
if name in self.children:
|
|
|
|
return # Ignore redundant group inserts.
|
2004-01-13 07:07:31 +01:00
|
|
|
if group is None:
|
|
|
|
group = Group()
|
|
|
|
self.children[name] = group
|
2004-01-13 16:56:58 +01:00
|
|
|
self.originals[name] = original
|
2004-01-14 07:05:58 +01:00
|
|
|
fullname = '%s.%s' % (self.name, name)
|
|
|
|
group.setName(fullname)
|
2004-01-20 12:41:53 +01:00
|
|
|
if _cache and fullname in _cache:
|
|
|
|
group.set(_cache[fullname])
|
2004-01-13 07:07:31 +01:00
|
|
|
|
2004-01-21 16:48:48 +01:00
|
|
|
def getValues(self, getChildren=False, fullNames=True):
|
2004-01-13 07:07:31 +01:00
|
|
|
L = []
|
|
|
|
items = self.values.items()
|
2004-01-27 12:25:36 +01:00
|
|
|
# If getChildren=True, the group will insert itself into its getValues.
|
|
|
|
if not getChildren:
|
|
|
|
for (name, child) in self.children.items():
|
|
|
|
if hasattr(child, 'value'):
|
|
|
|
items.append((name, child))
|
2004-01-19 23:39:13 +01:00
|
|
|
utils.sortBy(lambda (k, _): (k.lower(), len(k), k), items)
|
2004-01-22 21:16:21 +01:00
|
|
|
for (name, value) in items:
|
|
|
|
if fullNames:
|
|
|
|
name = '%s.%s' % (self.getName(), self.originals[name])
|
|
|
|
else:
|
|
|
|
name = self.originals[name]
|
|
|
|
L.append((name, value))
|
2004-01-18 08:58:26 +01:00
|
|
|
if getChildren:
|
2004-01-14 07:05:58 +01:00
|
|
|
items = self.children.items()
|
2004-01-14 15:18:56 +01:00
|
|
|
utils.sortBy(lambda (k, _): (k.lower(), len(k), k), items)
|
2004-01-14 07:05:58 +01:00
|
|
|
for (_, child) in items:
|
2004-01-21 16:48:48 +01:00
|
|
|
L.extend(child.getValues(getChildren, fullNames))
|
2004-01-13 07:07:31 +01:00
|
|
|
return L
|
|
|
|
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
class GroupWithValue(Group):
|
2004-01-13 07:07:31 +01:00
|
|
|
def __init__(self, value):
|
|
|
|
Group.__init__(self)
|
2004-01-18 08:58:26 +01:00
|
|
|
self.value = value
|
|
|
|
self.help = value.help
|
2004-01-26 20:17:41 +01:00
|
|
|
self.default = value.default
|
2004-01-18 08:58:26 +01:00
|
|
|
|
|
|
|
def set(self, s):
|
|
|
|
self.value.set(s)
|
|
|
|
|
|
|
|
def setValue(self, v):
|
|
|
|
self.value.setValue(v)
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
return self.value()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.value)
|
|
|
|
|
2004-01-27 12:25:36 +01:00
|
|
|
def getValues(self, getChildren=False, fullNames=True):
|
|
|
|
L = Group.getValues(self, getChildren, fullNames)
|
|
|
|
if getChildren:
|
|
|
|
L.insert(0, (self.getName(), self))
|
|
|
|
return L
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
|
|
|
|
class GroupWithDefault(GroupWithValue):
|
|
|
|
def __init__(self, value):
|
2004-01-20 13:27:29 +01:00
|
|
|
class X(value.__class__):
|
2004-01-22 21:16:21 +01:00
|
|
|
"""This class exists to differentiate those values that have been
|
|
|
|
changed from their default to those that haven't."""
|
2004-01-20 13:27:29 +01:00
|
|
|
def set(self, *args):
|
|
|
|
self.__class__ = value.__class__
|
|
|
|
self.set(*args)
|
|
|
|
def setValue(self, *args):
|
|
|
|
self.__class__ = value.__class__
|
|
|
|
self.setValue(*args)
|
|
|
|
self.X = X
|
2004-01-18 08:58:26 +01:00
|
|
|
GroupWithValue.__init__(self, value)
|
2004-01-13 07:07:31 +01:00
|
|
|
|
2004-01-14 07:05:58 +01:00
|
|
|
def __makeChild(self, attr, s):
|
2004-01-27 12:25:36 +01:00
|
|
|
#print '***', attr, ':', repr(s)
|
2004-01-14 07:05:58 +01:00
|
|
|
v = copy.copy(self.value)
|
|
|
|
v.set(s)
|
2004-01-20 13:27:29 +01:00
|
|
|
v.__class__ = self.X
|
2004-01-14 07:05:58 +01:00
|
|
|
self.register(attr, v)
|
2004-01-20 13:27:29 +01:00
|
|
|
return v
|
2004-01-19 21:51:04 +01:00
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
def __getattr__(self, attr):
|
|
|
|
try:
|
2004-01-20 13:27:29 +01:00
|
|
|
v = GroupWithValue.__getattr__(self, attr)
|
|
|
|
if v.__class__ is self.X:
|
|
|
|
raise NonExistentRegistryEntry
|
2004-01-13 07:07:31 +01:00
|
|
|
except NonExistentRegistryEntry:
|
2004-01-20 13:27:29 +01:00
|
|
|
v = self.__makeChild(attr, str(self))
|
|
|
|
return v
|
2004-01-14 07:05:58 +01:00
|
|
|
|
|
|
|
def setName(self, name):
|
2004-01-19 21:51:04 +01:00
|
|
|
GroupWithValue.setName(self, name)
|
2004-01-20 12:41:53 +01:00
|
|
|
for (k, v) in _cache.iteritems():
|
2004-01-14 07:05:58 +01:00
|
|
|
if k.startswith(self.name):
|
|
|
|
(_, group) = rsplit(k, '.', 1)
|
|
|
|
self.__makeChild(group, v)
|
2004-01-13 07:07:31 +01:00
|
|
|
|
2004-01-21 16:48:48 +01:00
|
|
|
def getValues(self, getChildren=False, fullNames=True):
|
|
|
|
L = GroupWithValue.getValues(self, getChildren, fullNames)
|
2004-01-20 13:27:29 +01:00
|
|
|
L = [v for v in L if v[1].__class__ is not self.X]
|
2004-01-19 21:51:04 +01:00
|
|
|
return L
|
2004-01-13 07:07:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2004-01-18 08:58:26 +01:00
|
|
|
import sys
|
|
|
|
sys.setrecursionlimit(40)
|
2004-01-13 07:07:31 +01:00
|
|
|
supybot = Group()
|
|
|
|
supybot.setName('supybot')
|
2004-01-18 08:58:26 +01:00
|
|
|
supybot.register('throttleTime', Float(1, """Determines the minimum
|
2004-01-13 07:07:31 +01:00
|
|
|
number of seconds the bot will wait between sending messages to the server.
|
|
|
|
"""))
|
|
|
|
supybot.registerGroup('plugins')
|
|
|
|
supybot.plugins.registerGroup('topic')
|
|
|
|
supybot.plugins.topic.registerGroup('separator',
|
|
|
|
GroupWithDefault(StringSurroundedBySpaces(' || ',
|
|
|
|
'Determines what separator the bot uses to separate topic entries.')))
|
2004-01-22 21:16:21 +01:00
|
|
|
supybot.plugins.topic.separator.get('#supybot').set(' |||| ')
|
2004-01-13 07:26:35 +01:00
|
|
|
supybot.plugins.topic.separator.set(' <> ')
|
2004-01-13 07:07:31 +01:00
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
supybot.throttleTime.set(10)
|
|
|
|
|
|
|
|
supybot.registerGroup('log')
|
|
|
|
supybot.log.registerGroup('stdout',
|
|
|
|
GroupWithValue(Boolean(False,
|
|
|
|
"""Help for stdout.""")))
|
|
|
|
supybot.log.stdout.register('colorized', Boolean(False,
|
|
|
|
'Help colorized'))
|
|
|
|
supybot.log.stdout.setValue(True)
|
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
for (k, v) in supybot.getValues():
|
|
|
|
print '%s: %s' % (k, v)
|
2004-01-13 16:56:58 +01:00
|
|
|
|
2004-01-14 07:05:58 +01:00
|
|
|
print
|
|
|
|
print 'Asking children'
|
|
|
|
print
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
for (k, v) in supybot.getValues(getChildren=True):
|
2004-01-14 07:05:58 +01:00
|
|
|
print '%s: %s' % (k, v)
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
print supybot.throttleTime.help
|
|
|
|
print supybot.plugins.topic.separator.help
|
2004-01-13 07:07:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
|
|
|
|