From f9c030fe46e3e43d9d69c951e740b6237b59b9fc Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Sat, 24 Jul 2004 02:28:43 +0000 Subject: [PATCH] Updated some stuff, refactoring for the the User.config command. --- src/Config.py | 50 ++++++++++++++++++++++++++++---------------------- src/User.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/src/Config.py b/src/Config.py index 38f760311..b609868bc 100644 --- a/src/Config.py +++ b/src/Config.py @@ -57,10 +57,9 @@ class InvalidRegistryName(callbacks.Error): def getWrapper(name): parts = name.split('.') - if not parts or parts[0] != 'supybot': + if not parts or parts[0] not in ('supybot', 'users'): raise InvalidRegistryName, name - group = conf.supybot - parts.pop(0) + group = getattr(conf, parts.pop(0)) while parts: try: group = group.get(parts.pop(0)) @@ -91,10 +90,28 @@ class Config(callbacks.Privmsg): irc.error(str(e)) def _canonicalizeName(self, name): - if not name.startswith('supybot'): + if not name.startswith('supybot') and not name.startswith('users'): name = 'supybot.' + name return name + def _list(self, name, groups=False): + name = self._canonicalizeName(name) + group = getWrapper(name) + if groups: + L = [] + for (vname, v) in group.children.iteritems(): + if v.added: + L.append(vname) + utils.sortBy(str.lower, L) + return L + else: + try: + L = zip(*group.getValues(fullNames=False))[0] + utils.sortBy(str.lower, L) + return L + except TypeError: + return [] + def list(self, irc, msg, args): """[--groups] @@ -108,24 +125,13 @@ class Config(callbacks.Privmsg): if name == '--groups': groups = True name = privmsgs.getArgs(rest) - name = self._canonicalizeName(name) - group = getWrapper(name) - if groups: - L = [] - for (vname, v) in group.children.iteritems(): - if v.added: - L.append(vname) - if L: - utils.sortBy(str.lower, L) - irc.reply(utils.commaAndify(L)) - else: - irc.reply('%s has no subgroups.' % name) + L = self._list(name, groups) + if L: + irc.reply(utils.commaAndify(L)) + elif groups: + irc.reply('%s has no subgroups.' % name) else: - try: - L = zip(*group.getValues(fullNames=False))[0] - irc.reply(utils.commaAndify(L)) - except TypeError: - irc.error('There don\'t seem to be any values in %s'%name) + irc.error('There don\'t seem to be any values in %s' % name) def search(self, irc, msg, args): """ @@ -227,7 +233,7 @@ class Config(callbacks.Privmsg): registry.open(world.registryFilename) irc.replySuccess() reload = privmsgs.checkCapability(reload, 'owner') - + Class = Config diff --git a/src/User.py b/src/User.py index b4a462b8d..7c9f12a99 100755 --- a/src/User.py +++ b/src/User.py @@ -51,6 +51,8 @@ import ircutils import privmsgs import callbacks +import Config + class User(callbacks.Privmsg): def _checkNotChannel(self, irc, msg, password=' '): if password and ircutils.isChannel(msg.args[0]): @@ -456,6 +458,41 @@ class User(callbacks.Privmsg): else: irc.error(conf.supybot.replies.incorrectAuthentication()) + def config(self, irc, msg, args): + """[--list] [] + + Sets the user configuration variable to , if given. If + is not given, returns the current value of for the user + giving the command. If --list is given, lists the values in . + """ + try: + id = ircdb.users.getUserId(msg.prefix) + except KeyError: + irc.errorNoUser() + return + list = False + while '--list' in args: + # XXX: Case-sensitive. + list = True + args.remove('--list') + if len(args) >= 2: + # We're setting. + pass + else: + # We're getting. + name = privmsgs.getArgs(args) + if not name.startswith('users.'): + name = 'users.' + name + try: + wrapper = Config.getWrapper(name) + wrapper = wrapper.get(str(id)) + except InvalidRegistryValue, e: + irc.error('%r is not a valid configuration variable.' % name) + return + if list: + pass + else: + irc.reply(str(wrapper)) Class = User