1
# gozerbot/persistconfig.py
5
""" allow data to be pickled to disk .. creating the persisted object
9
!plug-cfg -> shows list of all config
10
!plug-cfg key value -> sets value to key
11
!plug-cfg key -> shows list of key
12
!plug-cfg key add value -> adds value to list
13
!plug-cfg key remove value -> removes value from list
14
!plug-cfg key clear -> clears entire list
15
!plug-cfgsave -> force save configuration to disk
18
- plugin api (more work needed?)
22
__copyright__ = 'this file is in the public domain'
23
__author__ = 'Bas van Oostveen'
26
from gozerbot.utils.log import rlog
27
from gozerbot.utils.trace import calledfrom
28
from gozerbot.commands import cmnds, Command
29
from gozerbot.examples import examples
30
from gozerbot.datadir import datadir
31
from gozerbot.persist.persist import Persist
32
from gozerbot.config import Config, config
35
import sys, os, types, time
37
class PersistConfigError(Exception): pass
39
class PersistConfig(Config):
41
""" persist plugin configuration and create default handlers. """
45
self.plugname = calledfrom(sys._getframe())
46
Config.__init__(self, datadir + os.sep + 'plugs' + os.sep + self.plugname, "config")
48
if self.plugname not in config['loadlist']:
51
cmndname = "%s-cfg" % self.plugname
52
rlog(-3, 'persistconfig', 'added command %s (%s)' % (cmndname, \
54
cmnds[cmndname] = Command(self.cmnd_cfg, 'OPER', self.plugname, \
56
examples.add(cmndname, "plugin configuration", cmndname)
57
cmndnamesave = cmndname + "save"
58
cmnds[cmndnamesave] = Command(self.cmnd_cfgsave, 'OPER', \
59
self.plugname, threaded=True)
60
examples.add(cmndnamesave, "save plugin configuration", cmndnamesave)
64
def show_cfg(self, bot, ievent):
66
""" show config options. """
70
for key, optionvalue in sorted(self.iteritems()):
74
if type(v) in [str, unicode]:
77
s.append("%s=%s" % (key, v))
79
ievent.reply("options: " + ' .. '.join(s))
81
def cmnd_cfgsave(self, bot, ievent):
86
ievent.reply("config saved")
88
def cmnd_cfg_edit(self, bot, ievent, args, key, optionvalue):
90
""" edit config values. """
92
if not self.has_key(key):
93
ievent.reply('option %s is not defined' % key)
99
if type(optionvalue) == types.ListType:
101
if args[0].startswith("[") and args[-1].endswith("]"):
104
for v in ' '.join(args)[1:-1].replace(", ", ",").split(","):
105
if v[0]=='"' and v[-1]=='"':
107
v = v.replace('"', '')
108
elif v[0]=="'" and v[-1]=="'":
110
v = v.replace("'", "")
116
ievent.reply("invalid long literal: %s" % v)
123
ievent.reply("invalid int literal: %s" % v)
127
self.set(key, values)
129
ievent.reply("%s set %s" % (key, values))
133
value = ' '.join(args[1:])
135
if command == "clear":
138
ievent.reply("list empty")
139
elif command == "add":
140
self.append(key, value)
142
ievent.reply("%s added %s" % (key, value))
143
elif command == "remove" or command == "del":
145
self.remove(key, value)
147
ievent.reply("%s removed" % str(value))
149
ievent.reply("%s is not in list" % str(value))
151
ievent.reply("invalid command")
155
value = ' '.join(args)
158
value = type(optionvalue)(value)
162
if type(value) == type(optionvalue):
165
ievent.reply("%s set" % key)
166
elif type(value) == types.LongType and \
167
type(option.value) == types.IntType:
168
# allow upscaling from int to long
171
ievent.reply("%s set" % key)
173
ievent.reply("value %s (%s) is not of the same type as %s \
174
(%s)" % (value, type(value), optionvalue, type(optionvalue)))
176
def cmnd_cfg(self, bot, ievent):
178
""" the config (cfg) command. """
181
self.show_cfg(bot, ievent)
184
argc = len(ievent.args)
188
optionvalue = self[key]
190
ievent.reply("%s option %s not found" % (self.plugname, key))
197
ievent.reply(str(optionvalue))
200
self.cmnd_cfg_edit(bot, ievent, ievent.args[1:], key, optionvalue)
202
def generic_cmnd(self, key):
204
""" command for editing config values. """
206
def func(bot, ievent):
209
optionvalue = self[key]
211
ievent.reply("%s not found" % key)
214
if not isinstance(option, Option):
215
rlog(5, 'persistconfig', 'Option %s is not a valid option' % \
220
value = ' '.join(ievent.args)
222
value = type(optionvalue)(value)
225
self.cmnd_cfg_edit(bot, ievent, ievent.args, key, optionvalue)
227
ievent.reply(str(optionvalue))
233
def define(self, key, value=None, desc="plugin option", perm='OPER', \
234
example="", name=None, exposed=True):
236
""" define initial value. """
242
self.hide.append(key)
244
if not self.has_key(key):
246
name = "%s-cfg-%s" % (self.plugname, str(key))
249
def undefine(self, key, throw=False):
251
""" remove a key. """
263
def set(self, key, value, throw=False):
265
""" set a key's value. """
269
def append(self, key, value):
271
""" append a value. """
273
self[key].append(value)
275
def remove(self, key, value):
277
""" remove a value. """
279
self[key].remove(value)
281
def clear(self, key):
283
""" clear a value. """
287
def get(self, key, default=None):
289
""" get value of key. """