From 6eb0066dec5a58cf98bbb167c13e04bb243fd8e9 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Tue, 19 Oct 2004 11:58:04 +0000 Subject: [PATCH] Converted to commands.wrap, added capitalize and title. --- plugins/Format.py | 111 +++++++++++++++++++++----------------------- test/test_Format.py | 8 ++++ 2 files changed, 60 insertions(+), 59 deletions(-) diff --git a/plugins/Format.py b/plugins/Format.py index c58be6654..10e7aaf13 100644 --- a/plugins/Format.py +++ b/plugins/Format.py @@ -42,8 +42,8 @@ import string import supybot.conf as conf import supybot.utils as utils +from supybot.commands import * import supybot.ircutils as ircutils -import supybot.privmsgs as privmsgs import supybot.callbacks as callbacks @@ -57,137 +57,131 @@ def configure(advanced): class Format(callbacks.Privmsg): - def bold(self, irc, msg, args): + def bold(self, irc, msg, args, text): """ Returns bolded. """ - text = privmsgs.getArgs(args) irc.reply(ircutils.bold(text)) + bold = wrap(bold, ['text']) - def reverse(self, irc, msg, args): + def reverse(self, irc, msg, args, text): """ Returns in reverse-video. """ - text = privmsgs.getArgs(args) irc.reply(ircutils.reverse(text)) + reverse = wrap(reverse, ['text']) - def underline(self, irc, msg, args): + def underline(self, irc, msg, args, text): """ Returns underlined. """ - text = privmsgs.getArgs(args) irc.reply(ircutils.underline(text)) + underline = wrap(underline, ['text']) - def color(self, irc, msg, args): + def color(self, irc, msg, args, fg, bg, text): """ [] Returns with foreground color and background color (if given) """ - try: - fg = args.pop(0) - if args[0] in ircutils.mircColors: - bg = args.pop(0) - else: - bg = None - except IndexError: - raise callbacks.ArgumentError - text = privmsgs.getArgs(args) - try: - fg = ircutils.mircColors[fg] - except KeyError: - irc.errorInvalid('foreground color', fg, Raise=True) - if bg is not None: - try: - bg = ircutils.mircColors[bg] - except KeyError: - irc.errorInvalid('background color', bg, Raise=True) irc.reply(ircutils.mircColor(text, fg=fg, bg=bg)) + color = wrap(color, ['color', optional('color'), 'text']) - def join(self, irc, msg, args): + def join(self, irc, msg, args, sep): """ [ ...] Joins all the arguments together with . """ - sep = args.pop(0) irc.reply(sep.join(args)) + join = wrap(join, ['something'], allowExtra=True) - def translate(self, irc, msg, args): + def translate(self, irc, msg, args, bad, good, text): """ Replaces with in . The first and second arguments must necessarily be the same length. """ - (bad, good, text) = privmsgs.getArgs(args, required=3) + if len(bad) != len(good): + irc.error(' must be the same length as ' + '.', Raise=True) irc.reply(text.translate(string.maketrans(bad, good))) + translate = wrap(translate, ['something', 'something', 'text']) - def upper(self, irc, msg, args): + def upper(self, irc, msg, args, text): """ Returns uppercased. """ - irc.reply(privmsgs.getArgs(args).upper()) + irc.reply(text.upper()) + upper = wrap(upper, ['text']) - def lower(self, irc, msg, args): + def lower(self, irc, msg, args, text): """ Returns lowercased. """ - irc.reply(privmsgs.getArgs(args).lower()) + irc.reply(text.lower()) + lower = wrap(lower, ['text']) - def repr(self, irc, msg, args): + def capitalize(self, irc, msg, args, text): + """ + + Returns lowercased. + """ + irc.reply(text.capitalize()) + capitalize = wrap(capitalize, ['text']) + + def title(self, irc, msg, args, text): + """ + + Returns lowercased. + """ + irc.reply(text.title()) + title = wrap(title, ['text']) + + def repr(self, irc, msg, args, text): """ Returns the text surrounded by double quotes. """ - text = privmsgs.getArgs(args) irc.reply(utils.dqrepr(text)) + repr = wrap(repr, ['text']) - def concat(self, irc, msg, args): + def concat(self, irc, msg, args, first, second): """ Concatenates two strings. Do keep in mind that this is *not* the same thing as join "", since if contains spaces, they won't be removed by concat. """ - (first, second) = privmsgs.getArgs(args, required=2) irc.reply(first+second) + concat = wrap(concat, ['text', 'text']) - def cut(self, irc, msg, args): + def cut(self, irc, msg, args, size, text): """ Cuts down to by chopping off the rightmost characters in excess of . If is a negative number, it chops that many characters off the end of . """ - (size, text) = privmsgs.getArgs(args, required=2) - try: - size = int(size) - except ValueError: - irc.errorInvalid('integer', size, Raise=True) irc.reply(text[:size]) + cut = wrap(cut, ['int', 'text']) - def field(self, irc, msg, args): + def field(self, irc, msg, args, index, text): """ Returns the th space-separated field of . I.e., if text is "foo bar baz" and is 2, "bar" is returned. """ - (number, text) = privmsgs.getArgs(args, required=2) - try: - index = int(number) - if index > 0: - index -= 1 - except ValueError: - irc.errorInvalid('integer', number, Raise=True) try: irc.reply(text.split()[index]) except IndexError: irc.errorInvalid('field') + field = wrap(field, ['index', 'text']) def format(self, irc, msg, args): """ [ ...] @@ -196,16 +190,15 @@ class Format(callbacks.Privmsg): sure always to use %s, not %d or %f or whatever, because all the args are strings. """ - try: - s = args.pop(0) - except IndexError: + if not args: raise callbacks.ArgumentError + s = args.pop(0) try: s %= tuple(args) - except TypeError: - irc.error('Not enough arguments for the format string.') - return - irc.reply(s) + irc.reply(s) + except TypeError, e: + self.log.debug(utils.exnToString(e)) + irc.error('Not enough arguments for the format string.',Raise=True) Class = Format diff --git a/test/test_Format.py b/test/test_Format.py index e597e04d7..8d707c4a6 100644 --- a/test/test_Format.py +++ b/test/test_Format.py @@ -62,6 +62,14 @@ class UtilitiesTestCase(PluginTestCase): self.assertResponse('lower foo', 'foo') self.assertResponse('lower FOO', 'foo') + def testCapitalize(self): + self.assertResponse('capitalize foo', 'Foo') + self.assertResponse('capitalize foo bar', 'Foo bar') + + def testTitle(self): + self.assertResponse('title foo', 'Foo') + self.assertResponse('title foo bar', 'Foo Bar') + def testRepr(self): self.assertResponse('repr foo bar baz', '"foo bar baz"')