Made Configurable store its data in human-readable/editable form.

This commit is contained in:
Jeremy Fincher 2003-11-22 16:10:45 +00:00
parent 1d95410f91
commit dfadfe7e34

View File

@ -41,7 +41,6 @@ import types
import random import random
import urllib2 import urllib2
import threading import threading
import cPickle as pickle
import fix import fix
import cdb import cdb
@ -53,6 +52,7 @@ import ircdb
import ircutils import ircutils
import privmsgs import privmsgs
import callbacks import callbacks
import structures
try: try:
import sqlite import sqlite
@ -242,15 +242,18 @@ class ConfigurableDictionary(object):
self.helps = {} self.helps = {}
self.types = {} self.types = {}
self.defaults = {} self.defaults = {}
self.originalNames = [] self.originalNames = {}
self.unparsedValues = {}
self.channels = ircutils.IrcDict() self.channels = ircutils.IrcDict()
for (name, type, default, help) in seq: for (name, type, default, help) in seq:
self.originalNames.append(name) if ',' in name:
raise ValueError, 'There can be no commas in the name.'
original = name
name = callbacks.canonicalName(name) name = callbacks.canonicalName(name)
self.originalNames[name] = original
self.helps[name] = utils.normalizeWhitespace(help) self.helps[name] = utils.normalizeWhitespace(help)
self.types[name] = type self.types[name] = type
self.defaults[name] = default self.defaults[name] = default
self.originalNames.sort()
def get(self, name, channel=None): def get(self, name, channel=None):
name = callbacks.canonicalName(name) name = callbacks.canonicalName(name)
@ -264,6 +267,11 @@ class ConfigurableDictionary(object):
def set(self, name, value, channel=None): def set(self, name, value, channel=None):
name = callbacks.canonicalName(name) name = callbacks.canonicalName(name)
if name not in self.originalNames:
raise KeyError, name
if ',' in name:
raise ValueError, 'There can be no commas in the name.'
self.unparsedValues[(channel, name)] = value
if channel is not None: if channel is not None:
d = self.channels.setdefault(channel, {}) d = self.channels.setdefault(channel, {})
d[name] = self.types[name](value) d[name] = self.types[name](value)
@ -271,11 +279,12 @@ class ConfigurableDictionary(object):
self.defaults[name] = self.types[name](value) self.defaults[name] = self.types[name](value)
def help(self, name): def help(self, name):
name = callbacks.canonicalName(name) return self.helps[callbacks.canonicalName(name)]
return self.helps[name]
def names(self): def names(self):
return self.originalNames L = self.originalNames.values()
L.sort()
return L
class ConfigurableTypeError(TypeError): class ConfigurableTypeError(TypeError):
pass pass
@ -322,17 +331,33 @@ class Configurable(object):
def __init__(self): def __init__(self):
className = self.__class__.__name__ className = self.__class__.__name__
self.filename = os.path.join(conf.confDir,'%s-configurable'%className) self.filename = os.path.join(conf.confDir,'%s-configurable'%className)
try:
if os.path.exists(self.filename): if os.path.exists(self.filename):
configurables = pickle.load(file(self.filename, 'rb')) fd = file(self.filename)
if configurables.names() == self.configurables.names(): for line in fd:
self.configurables = configurables line = line.rstrip()
except Exception, e: (channel, name, value) = line.split(',', 2)
debug.msg('%s raised when trying to unpickle %s configurables' % if channel == 'default':
(debug.exnToString(e), className)) channel = None
try:
self.configurables.set(name, eval(value), channel)
except ConfigurableTypeError, e:
name = '%s.%s' % (self.__class__.__name__, name)
s = 'Couldn\'t read configurable %s from file: %s'%(name,e)
debug.msg(s)
except KeyError, e:
s = 'Configurable variable %s doesn\'t exist anymore'%name
debug.msg(s)
def die(self): def die(self):
pickle.dump(self.configurables, file(self.filename, 'wb'), -1) fd = file(self.filename, 'w')
L = self.configurables.unparsedValues.items()
L.sort()
for ((channel, name), value) in L:
if channel is None:
channel = 'default'
name = self.configurables.originalNames[name]
fd.write('%s,%s,%r\n' % (channel, name, value))
fd.close()
def config(self, irc, msg, args): def config(self, irc, msg, args):
"""[<channel>] [<name>] [<value>] """[<channel>] [<name>] [<value>]