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-18 08:58:26 +01:00
|
|
|
import sets
|
2004-01-22 21:16:21 +01:00
|
|
|
import time
|
2004-08-17 08:36:51 +02:00
|
|
|
import string
|
2004-01-21 16:48:48 +01:00
|
|
|
import textwrap
|
2004-01-13 07:07:31 +01:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.fix as fix
|
|
|
|
import supybot.utils as utils
|
2004-01-13 07:07:31 +01:00
|
|
|
|
|
|
|
class RegistryException(Exception):
|
|
|
|
pass
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
class InvalidRegistryFile(RegistryException):
|
|
|
|
pass
|
|
|
|
|
2004-04-29 13:49:24 +02:00
|
|
|
class InvalidRegistryName(RegistryException):
|
|
|
|
pass
|
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
class InvalidRegistryValue(RegistryException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class NonExistentRegistryEntry(RegistryException):
|
|
|
|
pass
|
|
|
|
|
2004-02-05 08:54:38 +01:00
|
|
|
_cache = utils.InsensitivePreservingDict()
|
2004-01-22 21:16:21 +01:00
|
|
|
_lastModified = 0
|
2004-04-30 20:24:35 +02:00
|
|
|
def open(filename, clear=False):
|
2004-01-14 07:05:58 +01:00
|
|
|
"""Initializes the module by loading the registry file into memory."""
|
2004-01-22 21:16:21 +01:00
|
|
|
global _lastModified
|
2004-04-30 20:24:35 +02:00
|
|
|
if clear:
|
|
|
|
_cache.clear()
|
|
|
|
_fd = file(filename)
|
|
|
|
fd = utils.nonCommentNonEmptyLines(_fd)
|
2004-01-18 08:58:26 +01:00
|
|
|
for (i, line) in enumerate(fd):
|
|
|
|
line = line.rstrip('\r\n')
|
|
|
|
try:
|
2004-08-22 08:24:45 +02:00
|
|
|
#print '***', repr(line)
|
2004-08-02 12:47:05 +02:00
|
|
|
(key, value) = re.split(r'(?<!\\):', line, 1)
|
|
|
|
key = key.strip()
|
|
|
|
value = value.strip()
|
2004-01-18 08:58:26 +01:00
|
|
|
except ValueError:
|
2004-08-17 08:36:51 +02:00
|
|
|
raise InvalidRegistryFile, 'Error unpacking line %r' % line
|
2004-02-05 08:54:38 +01:00
|
|
|
_cache[key] = value
|
2004-01-22 21:16:21 +01:00
|
|
|
_lastModified = time.time()
|
2004-04-30 20:24:35 +02:00
|
|
|
_fd.close()
|
2004-01-14 07:05:58 +01:00
|
|
|
|
2004-02-05 08:54:38 +01:00
|
|
|
def close(registry, filename, annotated=True, helpOnceOnly=False):
|
2004-01-21 18:13:04 +01:00
|
|
|
first = True
|
|
|
|
helpCache = sets.Set()
|
2004-08-01 14:46:03 +02:00
|
|
|
fd = utils.transactionalFile(filename)
|
2004-01-18 08:58:26 +01:00
|
|
|
for (name, value) in registry.getValues(getChildren=True):
|
2004-02-05 08:54:38 +01:00
|
|
|
if annotated and hasattr(value,'help') and value.help:
|
2004-08-11 09:30:43 +02:00
|
|
|
if not helpOnceOnly or value.help not in helpCache:
|
2004-02-05 08:54:38 +01:00
|
|
|
helpCache.add(value.help)
|
|
|
|
lines = textwrap.wrap(value.help)
|
|
|
|
for (i, line) in enumerate(lines):
|
|
|
|
lines[i] = '# %s\n' % line
|
|
|
|
lines.insert(0, '###\n')
|
|
|
|
if first:
|
|
|
|
first = False
|
|
|
|
else:
|
|
|
|
lines.insert(0, '\n')
|
2004-02-08 00:35:42 +01:00
|
|
|
if hasattr(value, 'value'):
|
2004-02-10 04:15:31 +01:00
|
|
|
if value.showDefault:
|
2004-02-16 09:41:26 +01:00
|
|
|
lines.append('#\n')
|
2004-02-10 04:15:31 +01:00
|
|
|
try:
|
|
|
|
original = value.value
|
2004-07-31 07:00:43 +02:00
|
|
|
value.value = value._default
|
2004-08-22 08:24:45 +02:00
|
|
|
lines.append('# Default value: %s\n' % value)
|
2004-02-10 04:15:31 +01:00
|
|
|
finally:
|
|
|
|
value.value = original
|
2004-02-05 08:54:38 +01:00
|
|
|
lines.append('###\n')
|
|
|
|
fd.writelines(lines)
|
2004-02-08 00:35:42 +01:00
|
|
|
if hasattr(value, 'value'): # This lets us print help for non-valued.
|
2004-08-22 08:24:45 +02:00
|
|
|
fd.write('%s: %s\n' % (name, value))
|
2004-01-14 07:05:58 +01:00
|
|
|
fd.close()
|
2004-01-18 08:58:26 +01:00
|
|
|
|
2004-04-29 13:49:24 +02:00
|
|
|
def isValidRegistryName(name):
|
2004-08-02 12:47:05 +02:00
|
|
|
# Now we can have . and : in names. I'm still gonna call shenanigans on
|
|
|
|
# anyone who tries to have spaces (though technically I can't see any
|
2004-09-09 00:51:00 +02:00
|
|
|
# reason why it wouldn't work). We also reserve all names starting with
|
|
|
|
# underscores for internal use.
|
|
|
|
return len(name.split()) == 1 and not name.startswith('_')
|
2004-04-29 13:49:24 +02:00
|
|
|
|
2004-07-29 11:51:38 +02:00
|
|
|
def escape(name):
|
2004-08-02 12:47:05 +02:00
|
|
|
name = name.replace('\\', '\\\\')
|
|
|
|
name = name.replace(':', '\\:')
|
|
|
|
name = name.replace('.', '\\.')
|
2004-07-29 11:51:38 +02:00
|
|
|
return name
|
|
|
|
|
2004-08-02 12:47:05 +02:00
|
|
|
def unescape(name):
|
|
|
|
name = name.replace('\\.', '.')
|
|
|
|
name = name.replace('\\:', ':')
|
|
|
|
name = name.replace('\\\\', '\\')
|
|
|
|
return name
|
|
|
|
|
|
|
|
_splitRe = re.compile(r'(?<!\\)\.')
|
2004-07-24 23:40:47 +02:00
|
|
|
def split(name):
|
2004-08-02 12:47:05 +02:00
|
|
|
return map(unescape, _splitRe.split(name))
|
2004-07-24 23:40:47 +02:00
|
|
|
|
2004-07-28 04:56:44 +02:00
|
|
|
def join(names):
|
2004-08-02 12:47:05 +02:00
|
|
|
return '.'.join(map(escape, names))
|
2004-07-28 04:56:44 +02:00
|
|
|
|
2004-02-03 17:43:22 +01:00
|
|
|
class Group(object):
|
2004-08-17 22:43:25 +02:00
|
|
|
"""A group; it doesn't hold a value unless handled by a subclass."""
|
2004-09-11 18:17:40 +02:00
|
|
|
def __init__(self,help='', supplyDefault=False, orderAlphabetically=False):
|
|
|
|
self.help = help
|
2004-07-31 07:00:43 +02:00
|
|
|
self._name = 'unset'
|
2004-08-20 08:58:25 +02:00
|
|
|
self._added = []
|
2004-08-11 19:05:33 +02:00
|
|
|
self._children = utils.InsensitivePreservingDict()
|
2004-02-03 17:43:22 +01:00
|
|
|
self._lastModified = 0
|
2004-08-17 22:43:25 +02:00
|
|
|
self._supplyDefault = supplyDefault
|
2004-09-09 00:51:00 +02:00
|
|
|
self._orderAlphabetically = orderAlphabetically
|
2004-02-03 17:43:22 +01:00
|
|
|
OriginalClass = self.__class__
|
|
|
|
class X(OriginalClass):
|
|
|
|
"""This class exists to differentiate those values that have
|
|
|
|
been changed from their default from those that haven't."""
|
2004-08-22 08:24:45 +02:00
|
|
|
def set(self, *args):
|
|
|
|
self.__class__ = OriginalClass
|
|
|
|
self.set(*args)
|
2004-02-03 17:43:22 +01:00
|
|
|
def setValue(self, *args):
|
|
|
|
self.__class__ = OriginalClass
|
|
|
|
self.setValue(*args)
|
|
|
|
self.X = X
|
|
|
|
|
2004-07-25 13:06:56 +02:00
|
|
|
def __call__(self):
|
|
|
|
raise ValueError, 'Groups have no value.'
|
|
|
|
|
2004-02-03 17:43:22 +01:00
|
|
|
def __nonExistentEntry(self, attr):
|
2004-07-31 07:00:43 +02:00
|
|
|
s = '%s is not a valid entry in %s' % (attr, self._name)
|
2004-02-03 17:43:22 +01:00
|
|
|
raise NonExistentRegistryEntry, s
|
|
|
|
|
|
|
|
def __makeChild(self, attr, s):
|
2004-07-31 07:00:43 +02:00
|
|
|
v = self.__class__(self._default, self.help)
|
2004-02-03 17:43:22 +01:00
|
|
|
v.set(s)
|
|
|
|
v.__class__ = self.X
|
2004-08-22 08:24:45 +02:00
|
|
|
v._supplyDefault = False
|
2004-02-05 08:54:38 +01:00
|
|
|
v.help = '' # Clear this so it doesn't print a bazillion times.
|
2004-02-03 17:43:22 +01:00
|
|
|
self.register(attr, v)
|
|
|
|
return v
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
2004-08-11 19:05:33 +02:00
|
|
|
if attr in self._children:
|
|
|
|
return self._children[attr]
|
2004-08-17 22:43:25 +02:00
|
|
|
elif self._supplyDefault:
|
2004-02-05 08:54:38 +01:00
|
|
|
return self.__makeChild(attr, str(self))
|
2004-02-03 17:43:22 +01:00
|
|
|
else:
|
2004-02-05 08:54:38 +01:00
|
|
|
self.__nonExistentEntry(attr)
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-02-03 17:43:22 +01:00
|
|
|
def get(self, attr):
|
|
|
|
# Not getattr(self, attr) because some nodes might have groups that
|
|
|
|
# are named the same as their methods.
|
|
|
|
return self.__getattr__(attr)
|
|
|
|
|
|
|
|
def setName(self, name):
|
2004-08-22 08:24:45 +02:00
|
|
|
#print '***', name
|
2004-07-31 07:00:43 +02:00
|
|
|
self._name = name
|
2004-02-05 08:54:38 +01:00
|
|
|
if name in _cache and self._lastModified < _lastModified:
|
2004-08-22 08:24:45 +02:00
|
|
|
#print '***>', _cache[name]
|
2004-02-05 08:54:38 +01:00
|
|
|
self.set(_cache[name])
|
2004-08-17 22:43:25 +02:00
|
|
|
if self._supplyDefault:
|
2004-02-03 17:43:22 +01:00
|
|
|
for (k, v) in _cache.iteritems():
|
2004-08-22 08:24:45 +02:00
|
|
|
if k.startswith(self._name):
|
|
|
|
rest = k[len(self._name)+1:] # +1 is for .
|
|
|
|
parts = split(rest)
|
|
|
|
if len(parts) == 1 and parts[0] == name:
|
2004-08-20 08:58:25 +02:00
|
|
|
try:
|
|
|
|
self.__makeChild(group, v)
|
|
|
|
except InvalidRegistryValue:
|
|
|
|
# It's probably supposed to be registered later.
|
|
|
|
pass
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-02-03 17:43:22 +01:00
|
|
|
def register(self, name, node=None):
|
2004-04-29 13:49:24 +02:00
|
|
|
if not isValidRegistryName(name):
|
2004-08-22 08:24:45 +02:00
|
|
|
raise InvalidRegistryName, name
|
2004-02-03 17:43:22 +01:00
|
|
|
if node is None:
|
2004-08-20 08:58:25 +02:00
|
|
|
node = Group()
|
2004-08-23 14:27:47 +02:00
|
|
|
# We tried in any number of horrible ways to make it so that
|
|
|
|
# re-registering something would work. It doesn't, plain and simple.
|
|
|
|
# For the longest time, we had an "Is this right?" comment here, but
|
|
|
|
# from experience, we now know that it most definitely *is* right.
|
|
|
|
if name not in self._children:
|
2004-08-22 08:24:45 +02:00
|
|
|
self._children[name] = node
|
2004-08-20 08:58:25 +02:00
|
|
|
self._added.append(name)
|
2004-08-22 08:24:45 +02:00
|
|
|
names = split(self._name)
|
|
|
|
names.append(name)
|
|
|
|
fullname = join(names)
|
|
|
|
node.setName(fullname)
|
2004-08-27 22:10:17 +02:00
|
|
|
else:
|
|
|
|
# We do this so the return value from here is at least useful;
|
|
|
|
# otherwise, we're just returning a useless, unattached node
|
|
|
|
# that's simply a waste of space.
|
|
|
|
node = self._children[name]
|
2004-07-20 07:39:58 +02:00
|
|
|
return node
|
2004-02-03 17:43:22 +01:00
|
|
|
|
|
|
|
def unregister(self, name):
|
|
|
|
try:
|
2004-08-22 08:24:45 +02:00
|
|
|
node = self._children[name]
|
|
|
|
del self._children[name]
|
2004-09-11 18:17:40 +02:00
|
|
|
# We do this because we need to remove case-insensitively.
|
|
|
|
name = name.lower()
|
|
|
|
for elt in reversed(self._added):
|
|
|
|
if elt.lower() == name:
|
|
|
|
self._added.remove(elt)
|
2004-08-05 20:47:03 +02:00
|
|
|
if node._name in _cache:
|
|
|
|
del _cache[node._name]
|
2004-04-13 03:01:52 +02:00
|
|
|
return node
|
2004-02-03 17:43:22 +01:00
|
|
|
except KeyError:
|
2004-02-05 08:54:38 +01:00
|
|
|
self.__nonExistentEntry(name)
|
2004-02-03 17:43:22 +01:00
|
|
|
|
2004-04-13 03:01:52 +02:00
|
|
|
def rename(self, old, new):
|
|
|
|
node = self.unregister(old)
|
|
|
|
self.register(new, node)
|
|
|
|
|
2004-02-03 17:43:22 +01:00
|
|
|
def getValues(self, getChildren=False, fullNames=True):
|
|
|
|
L = []
|
2004-09-09 00:51:00 +02:00
|
|
|
if self._orderAlphabetically:
|
|
|
|
self._added.sort()
|
2004-08-20 08:58:25 +02:00
|
|
|
for name in self._added:
|
2004-08-11 19:05:33 +02:00
|
|
|
node = self._children[name]
|
2004-02-08 00:35:42 +01:00
|
|
|
if hasattr(node, 'value') or hasattr(node, 'help'):
|
2004-02-03 17:43:22 +01:00
|
|
|
if node.__class__ is not self.X:
|
2004-07-31 07:00:43 +02:00
|
|
|
L.append((node._name, node))
|
2004-02-03 17:43:22 +01:00
|
|
|
if getChildren:
|
|
|
|
L.extend(node.getValues(getChildren, fullNames))
|
|
|
|
if not fullNames:
|
2004-07-28 04:56:44 +02:00
|
|
|
L = [(split(s)[-1], node) for (s, node) in L]
|
2004-02-03 17:43:22 +01:00
|
|
|
return L
|
|
|
|
|
|
|
|
|
|
|
|
class Value(Group):
|
2004-04-13 03:01:52 +02:00
|
|
|
"""Invalid registry value. If you're getting this message, report it,
|
2004-04-30 20:24:35 +02:00
|
|
|
because we forgot to put a proper help string here."""
|
2004-09-09 21:13:46 +02:00
|
|
|
def __init__(self, default, help, setDefault=True,
|
2004-02-14 01:47:21 +01:00
|
|
|
private=False, showDefault=True, **kwargs):
|
2004-09-09 17:34:13 +02:00
|
|
|
self.__parent = super(Value, self)
|
2004-09-11 18:17:40 +02:00
|
|
|
self.__parent.__init__(help, **kwargs)
|
2004-07-31 07:00:43 +02:00
|
|
|
self._default = default
|
|
|
|
self._private = private
|
2004-02-10 04:15:31 +01:00
|
|
|
self.showDefault = showDefault
|
2004-01-18 08:58:26 +01:00
|
|
|
self.help = utils.normalizeWhitespace(help.strip())
|
2004-09-09 21:13:46 +02:00
|
|
|
if setDefault:
|
|
|
|
self.setValue(default)
|
2004-01-13 07:07:31 +01:00
|
|
|
|
2004-04-13 03:01:52 +02:00
|
|
|
def error(self):
|
2004-04-30 20:24:35 +02:00
|
|
|
if self.__doc__:
|
|
|
|
s = self.__doc__
|
|
|
|
else:
|
|
|
|
s = """Invalid registry value. If you're getting this message,
|
|
|
|
report it, because we forgot to put a proper help string here."""
|
|
|
|
raise InvalidRegistryValue, utils.normalizeWhitespace(s)
|
2004-04-13 03:01:52 +02:00
|
|
|
|
2004-02-03 17:43:22 +01:00
|
|
|
def setName(self, *args):
|
2004-07-31 07:00:43 +02:00
|
|
|
if self._name == 'unset':
|
2004-02-03 17:43:22 +01:00
|
|
|
self._lastModified = 0
|
2004-09-09 17:34:13 +02:00
|
|
|
self.__parent.setName(*args)
|
2004-02-03 17:43:22 +01:00
|
|
|
self._lastModified = time.time()
|
|
|
|
|
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-08-27 06:32:50 +02:00
|
|
|
self.setValue(s)
|
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-08-17 22:43:25 +02:00
|
|
|
if self._supplyDefault:
|
2004-08-11 19:05:33 +02:00
|
|
|
for (name, v) in self._children.items():
|
2004-02-09 17:32:00 +01:00
|
|
|
if v.__class__ is self.X:
|
|
|
|
self.unregister(name)
|
2004-07-21 21:36:35 +02:00
|
|
|
|
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-09-10 08:31:39 +02:00
|
|
|
# We tried many, *many* different syntactic methods here, and this one was
|
|
|
|
# simply the best -- not very intrusive, easily overridden by subclasses,
|
|
|
|
# etc.
|
2004-01-18 08:58:26 +01:00
|
|
|
def __call__(self):
|
2004-02-03 17:43:22 +01:00
|
|
|
if _lastModified > self._lastModified:
|
2004-07-31 07:00:43 +02:00
|
|
|
if self._name in _cache:
|
|
|
|
self.set(_cache[self._name])
|
2004-01-18 08:58:26 +01:00
|
|
|
return self.value
|
2004-01-30 20:27:02 +01:00
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
class Boolean(Value):
|
2004-04-13 03:01:52 +02:00
|
|
|
"""Value must be either True or False (or On or Off)."""
|
2004-01-13 07:07:31 +01:00
|
|
|
def set(self, s):
|
2004-03-21 20:40:13 +01:00
|
|
|
s = s.strip().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:
|
2004-04-13 03:01:52 +02:00
|
|
|
self.error()
|
2004-01-20 16:08:08 +01:00
|
|
|
self.setValue(value)
|
|
|
|
|
|
|
|
def setValue(self, v):
|
2004-09-09 17:34:13 +02:00
|
|
|
super(Boolean, self).setValue(bool(v))
|
2004-01-13 07:07:31 +01:00
|
|
|
|
|
|
|
class Integer(Value):
|
2004-04-13 03:01:52 +02:00
|
|
|
"""Value must be an integer."""
|
2004-01-13 07:07:31 +01:00
|
|
|
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:
|
2004-04-13 03:01:52 +02:00
|
|
|
self.error()
|
2004-01-13 07:07:31 +01:00
|
|
|
|
2004-07-31 14:09:52 +02:00
|
|
|
class NonNegativeInteger(Integer):
|
2004-09-03 15:51:06 +02:00
|
|
|
"""Value must be a non-negative integer."""
|
2004-01-20 16:08:08 +01:00
|
|
|
def setValue(self, v):
|
2004-07-31 14:09:52 +02:00
|
|
|
if v < 0:
|
2004-04-13 03:01:52 +02:00
|
|
|
self.error()
|
2004-09-09 17:34:13 +02:00
|
|
|
super(NonNegativeInteger, self).setValue(v)
|
2004-01-20 16:08:08 +01:00
|
|
|
|
2004-08-17 08:36:51 +02:00
|
|
|
class PositiveInteger(NonNegativeInteger):
|
|
|
|
"""Value must be positive (non-zero) integer."""
|
|
|
|
def setValue(self, v):
|
|
|
|
if not v:
|
|
|
|
self.error()
|
2004-09-09 17:34:13 +02:00
|
|
|
super(PositiveInteger, self).setValue(v)
|
2004-08-17 08:36:51 +02:00
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
class Float(Value):
|
2004-04-13 03:01:52 +02:00
|
|
|
"""Value must be a floating-point number."""
|
2004-01-18 08:58:26 +01:00
|
|
|
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:
|
2004-04-13 03:01:52 +02:00
|
|
|
self.error()
|
2004-01-18 08:58:26 +01:00
|
|
|
|
2004-01-21 16:48:48 +01:00
|
|
|
def setValue(self, v):
|
|
|
|
try:
|
2004-09-09 17:34:13 +02:00
|
|
|
super(Float, self).setValue(float(v))
|
2004-01-21 16:48:48 +01:00
|
|
|
except ValueError:
|
2004-04-13 03:01:52 +02:00
|
|
|
self.error()
|
2004-01-21 16:48:48 +01:00
|
|
|
|
2004-08-17 08:36:51 +02:00
|
|
|
class PositiveFloat(Float):
|
|
|
|
"""Value must be a float-point number greater than zero."""
|
|
|
|
def setValue(self, v):
|
|
|
|
if v <= 0:
|
|
|
|
self.error()
|
|
|
|
else:
|
2004-09-09 17:34:13 +02:00
|
|
|
super(PositiveFloat, self).setValue(v)
|
2004-08-17 08:36:51 +02:00
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
class String(Value):
|
2004-04-16 10:27:27 +02:00
|
|
|
"""Value is not a valid Python string."""
|
2004-01-13 07:07:31 +01:00
|
|
|
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
|
2004-02-03 19:21:19 +01:00
|
|
|
self.setValue(v)
|
2004-01-13 07:07:31 +01:00
|
|
|
except ValueError: # This catches utils.safeEval(s) errors too.
|
2004-04-13 03:01:52 +02:00
|
|
|
self.error()
|
2004-01-13 07:07:31 +01:00
|
|
|
|
2004-08-17 08:36:51 +02:00
|
|
|
_printable = string.printable[:-4]
|
|
|
|
def _needsQuoting(self, s):
|
|
|
|
return s.translate(string.ascii, self._printable) and s.strip() != s
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
s = self.value
|
|
|
|
if self._needsQuoting(s):
|
|
|
|
s = repr(s)
|
|
|
|
return s
|
|
|
|
|
2004-03-30 10:27:05 +02:00
|
|
|
class OnlySomeStrings(String):
|
|
|
|
validStrings = ()
|
2004-04-08 12:59:13 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
assert self.validStrings, 'There must be some valid strings. ' \
|
|
|
|
'This is a bug.'
|
2004-09-09 17:34:13 +02:00
|
|
|
self.__parent = super(OnlySomeStrings, self)
|
|
|
|
self.__parent.__init__(*args, **kwargs)
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-04-16 10:27:27 +02:00
|
|
|
def error(self):
|
|
|
|
raise InvalidRegistryValue, \
|
|
|
|
'That is not a valid value. Valid values include %s.' % \
|
|
|
|
utils.commaAndify(map(repr, self.validStrings))
|
|
|
|
|
2004-04-08 13:13:03 +02:00
|
|
|
def normalize(self, s):
|
|
|
|
lowered = s.lower()
|
|
|
|
L = list(map(str.lower, self.validStrings))
|
|
|
|
try:
|
|
|
|
i = L.index(lowered)
|
|
|
|
except ValueError:
|
|
|
|
return s # This is handled in setValue.
|
|
|
|
return self.validStrings[i]
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-03-30 10:27:05 +02:00
|
|
|
def setValue(self, s):
|
2004-04-08 04:18:35 +02:00
|
|
|
s = self.normalize(s)
|
2004-03-30 10:27:05 +02:00
|
|
|
if s in self.validStrings:
|
2004-09-09 17:34:13 +02:00
|
|
|
self.__parent.setValue(s)
|
2004-03-30 10:27:05 +02:00
|
|
|
else:
|
2004-04-13 03:01:52 +02:00
|
|
|
self.error()
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
class NormalizedString(String):
|
2004-09-09 17:34:13 +02:00
|
|
|
def __init__(self, default, *args, **kwargs):
|
2004-08-17 08:36:51 +02:00
|
|
|
default = self.normalize(default)
|
2004-09-09 17:34:13 +02:00
|
|
|
self.__parent = super(NormalizedString, self)
|
|
|
|
self.__parent.__init__(default, *args, **kwargs)
|
2004-08-17 08:36:51 +02:00
|
|
|
|
2004-07-29 11:51:38 +02:00
|
|
|
def normalize(self, s):
|
|
|
|
return utils.normalizeWhitespace(s.strip())
|
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
def set(self, s):
|
2004-07-29 11:51:38 +02:00
|
|
|
s = self.normalize(s)
|
2004-09-09 17:34:13 +02:00
|
|
|
self.__parent.set(s)
|
2004-01-18 08:58:26 +01:00
|
|
|
|
2004-01-19 21:51:04 +01:00
|
|
|
def setValue(self, s):
|
2004-07-29 11:51:38 +02:00
|
|
|
s = self.normalize(s)
|
2004-09-09 17:34:13 +02:00
|
|
|
self.__parent.setValue(s)
|
2004-01-19 21:51:04 +01:00
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
class StringSurroundedBySpaces(String):
|
2004-01-20 16:08:08 +01:00
|
|
|
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-09-09 17:34:13 +02:00
|
|
|
super(StringSurroundedBySpaces, self).setValue(v)
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-01-23 14:28:53 +01:00
|
|
|
class StringWithSpaceOnRight(String):
|
|
|
|
def setValue(self, v):
|
|
|
|
if v.rstrip() == v:
|
|
|
|
v += ' '
|
2004-09-09 17:34:13 +02:00
|
|
|
super(StringWithSpaceOnRight, self).setValue(v)
|
2004-01-27 19:14:44 +01:00
|
|
|
|
|
|
|
class Regexp(Value):
|
2004-08-17 22:43:25 +02:00
|
|
|
"""Value must be a valid regular expression."""
|
2004-09-09 21:13:46 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs['setDefault'] = False
|
|
|
|
super(Regexp, self).__init__(*args, **kwargs)
|
2004-09-13 03:21:48 +02:00
|
|
|
self.value = None
|
2004-09-09 21:13:46 +02:00
|
|
|
|
2004-04-13 03:01:52 +02:00
|
|
|
def error(self, e):
|
2004-08-17 22:43:25 +02:00
|
|
|
raise InvalidRegistryValue, 'Value must be a regexp of the form %s' % e
|
2004-04-13 03:01:52 +02:00
|
|
|
|
2004-01-27 19:14:44 +01:00
|
|
|
def set(self, s):
|
|
|
|
try:
|
|
|
|
if s:
|
2004-08-01 20:08:55 +02:00
|
|
|
self.setValue(utils.perlReToPythonRe(s), sr=s)
|
2004-01-27 19:14:44 +01:00
|
|
|
else:
|
2004-02-03 19:21:19 +01:00
|
|
|
self.setValue(None)
|
2004-01-27 19:14:44 +01:00
|
|
|
except ValueError, e:
|
2004-04-13 03:01:52 +02:00
|
|
|
self.error(e)
|
2004-01-27 19:14:44 +01:00
|
|
|
|
2004-08-01 20:08:55 +02:00
|
|
|
def setValue(self, v, sr=None):
|
2004-09-09 17:34:13 +02:00
|
|
|
parent = super(Regexp, self)
|
2004-01-27 19:14:44 +01:00
|
|
|
if v is None:
|
|
|
|
self.sr = ''
|
2004-09-09 17:34:13 +02:00
|
|
|
parent.setValue(None)
|
2004-08-01 20:08:55 +02:00
|
|
|
elif sr is not None:
|
|
|
|
self.sr = sr
|
2004-09-09 17:34:13 +02:00
|
|
|
parent.setValue(v)
|
2004-01-27 19:14:44 +01:00
|
|
|
else:
|
2004-04-18 04:47:12 +02:00
|
|
|
raise InvalidRegistryValue, \
|
2004-04-30 20:24:35 +02:00
|
|
|
'Can\'t setValue a regexp, there would be an inconsistency '\
|
2004-01-27 19:14:44 +01:00
|
|
|
'between the regexp and the recorded string value.'
|
|
|
|
|
|
|
|
def __str__(self):
|
2004-02-17 07:30:12 +01:00
|
|
|
self() # Gotta update if we've been reloaded.
|
2004-01-27 19:14:44 +01:00
|
|
|
return self.sr
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-01-21 17:15:31 +01:00
|
|
|
class SeparatedListOf(Value):
|
2004-01-30 23:15:39 +01:00
|
|
|
List = list
|
2004-01-21 17:15:31 +01:00
|
|
|
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-07-21 21:36:35 +02:00
|
|
|
|
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):
|
2004-08-16 20:47:23 +02:00
|
|
|
v = self.Value(s, '')
|
2004-01-21 17:15:31 +01:00
|
|
|
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):
|
2004-09-09 17:34:13 +02:00
|
|
|
super(SeparatedListOf, self).setValue(self.List(v))
|
2004-01-27 12:25:36 +01:00
|
|
|
|
2004-01-18 08:58:26 +01:00
|
|
|
def __str__(self):
|
2004-08-17 08:36:51 +02:00
|
|
|
values = self()
|
|
|
|
if values:
|
|
|
|
return self.joiner(values)
|
2004-02-11 07:33:05 +01:00
|
|
|
else:
|
|
|
|
# We must return *something* here, otherwise down along the road we
|
|
|
|
# can run into issues showing users the value if they've disabled
|
|
|
|
# nick prefixes in any of the numerous ways possible. Since the
|
|
|
|
# config parser doesn't care about this space, we'll use it :)
|
2004-07-21 21:36:35 +02:00
|
|
|
return ' '
|
|
|
|
|
2004-07-21 20:49:27 +02:00
|
|
|
class SpaceSeparatedListOf(SeparatedListOf):
|
2004-04-16 10:27:27 +02:00
|
|
|
def splitter(self, s):
|
|
|
|
return s.split()
|
2004-01-21 18:13:04 +01:00
|
|
|
joiner = ' '.join
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-07-21 20:49:27 +02:00
|
|
|
class SpaceSeparatedListOfStrings(SpaceSeparatedListOf):
|
|
|
|
Value = String
|
2004-05-07 13:41:32 +02:00
|
|
|
|
2004-08-02 14:34:06 +02:00
|
|
|
class SpaceSeparatedSetOfStrings(SpaceSeparatedListOfStrings):
|
|
|
|
List = sets.Set
|
|
|
|
|
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-07-21 21:36:35 +02:00
|
|
|
|
2004-01-13 07:07:31 +01:00
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
|
|
|
|