Wed, 31 Dec 2014 11:40:46 -0500
- added page system to prevent commands from printing too much output
mod_hgpoll.py | file | annotate | diff | comparison | revisions | |
mod_util.py | file | annotate | diff | comparison | revisions | |
modulecore.py | file | annotate | diff | comparison | revisions |
--- a/mod_hgpoll.py Wed Dec 31 10:27:57 2014 -0500 +++ b/mod_hgpoll.py Wed Dec 31 11:40:46 2014 -0500 @@ -4,6 +4,7 @@ import re import bt as Bt from configfile import Config +from modulecore import command_error ModuleData = { 'commands': @@ -41,7 +42,7 @@ def cmd_checkhg (bot, **rest): HgPoll.force_poll() -def cmd_cset (bot, args, replyto, **rest): +def cmd_cset (bot, args, reply, **rest): repo = Repo ('zandronum-everything') data = "" node = args['key'] @@ -58,13 +59,12 @@ node = data[0] break else: - bot.privmsg (replyto, 'couldn\'t find changset for date %s' % args['key']) + command_error ('couldn\'t find changeset for date %s' % args['key']) return except ValueError: pass - # The sandboxes contain all revisions in zandronum and zandronum-stable. - # Thus we only need to try find the revision in the sandbox repos. + # zandronum-everything contains all zandronum changesets, so look for changesets in that. try: data = repo.hg_command ("log", "-l1", "-r", node, "--template", "{node|short}@@@@@@@" + @@ -76,7 +76,7 @@ "{latesttagdistance}@@@@@@@" + "{latesttag}") except hgapi.HgException: - bot.privmsg (replyto, 'couldn\'t find changeset %s' % (node)) + command_error ('couldn\'t find changeset %s' % (node)) return try: @@ -87,7 +87,7 @@ diffstat = data[3] date = datetime.utcfromtimestamp (int (data[4].split (' ')[0])) bookmarks = data[5] - latesttagdistance = int( data[6] ) + latesttagdistance = int (data[6]) latesttag = data[7] delta = datetime.utcnow() - date datestring = '' @@ -157,34 +157,34 @@ else: versionstring = latesttag - bot.privmsg (replyto, 'changeset\0035 %s%s\003 (%s)\003: committed by\0032 %s\003 %s,\0032 %s' % \ + reply ('changeset\0035 %s%s\003 (%s)\003: committed by\0032 %s\003 %s,\0032 %s' % \ (node, bookmarks, versionstring, author, datestring, diffstat)) for line in message.split ('\n'): - bot.privmsg (replyto, ' ' + line) + reply (' ' + line) except hgapi.HgException as e: result = HgPoll.decipher_hgapi_error (e) if result[0]: - bot.privmsg (replyto, 'error: %s' % result[1]) + command_error (result[1]) else: - bot.privmsg (replyto, 'error: %s' % `e`) + command_error (`e`) def cmd_hg (bot, args, **rest): try: repo = hgapi.Repo (args['repo']) result = repo.hg_command (*args['command']) - self.privmsg (replyto, result) + reply (replyto, result) except hgapi.hgapi.HgException as e: result = HgPoll.decipher_hgapi_error (e) if result[0]: - self.privmsg (replyto, 'error: %s' % result[1]) + command_error (result[1]) else: - self.privmsg (replyto, 'error: %s' % `e`) + command_error (`e`) def cmd_resolves (bot, args, replyto, **rest): try: HgPoll.announce_ticket_resolved (args['ticket'], args['changeset']) except Exception as e: - bot.privmsg (replyto, 'Error: %s' % e) + command_error (str (e))
--- a/mod_util.py Wed Dec 31 10:27:57 2014 -0500 +++ b/mod_util.py Wed Dec 31 11:40:46 2014 -0500 @@ -43,11 +43,22 @@ 'args': '<expression...>', 'level': 'normal', }, + + { + 'name': 'more', + 'description': 'Prints subsequent command result pages', + 'args': None, + 'level': 'normal', + }, ] } -def cmd_convert (bot, args, replyto, **rest): - value = float (args['value']) +def cmd_convert (bot, args, reply, **rest): + try: + value = float (args['value']) + except Exception as e: + command_error (str (e)) + valuetype = args['valuetype'] if valuetype in ['radians', 'degrees']: @@ -58,8 +69,7 @@ radvalue = (value * math.pi) / 180. degvalue = value - bot.privmsg (replyto, '%s radians, %s degrees (%s)' % - (radvalue, degvalue, degvalue % 360.)) + reply ('%s radians, %s degrees (%s)' % (radvalue, degvalue, degvalue % 360.)) return if valuetype in ['celsius', 'fahrenheit']: @@ -70,13 +80,13 @@ celvalue = (value - 32) / 1.8 fahrvalue = value - bot.privmsg (replyto, '%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue)) + reply ('%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue)) return command_error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' + 'celsius, fahrenheit (temperature conversion)' % valuetype) -def cmd_ud (bot, args, replyto, **rest): +def cmd_ud (bot, args, reply, **rest): try: url = 'http://api.urbandictionary.com/v0/define?term=%s' % (args['term'].replace (' ', '%20')) response = urllib2.urlopen (url).read() @@ -92,11 +102,11 @@ definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ') up = data['list'][0]['thumbs_up'] down = data['list'][0]['thumbs_down'] - bot.privmsg (replyto, "\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down)) + reply ("\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down)) except Exception as e: command_error ('Urban dictionary lookup failed: %s' % e) -def cmd_commands (bot, replyto, ident, host, **rest): +def cmd_commands (bot, reply, ident, host, **rest): commandlist = ModuleCore.get_available_commands (ident, host) partitioned=[] @@ -105,9 +115,9 @@ commandlist = commandlist[15:] for part in partitioned: - bot.privmsg (replyto, '\002Available commands\002: %s' % (", ".join (part))) + reply ('\002Available commands\002: %s' % (", ".join (part))) -def cmd_help (bot, replyto, ident, host, args, **rest): +def cmd_help (bot, reply, ident, host, args, **rest): cmd = ModuleCore.get_command_by_name (args['command']) if not cmd: @@ -116,7 +126,7 @@ if not ModuleCore.is_available (cmd, ident, host): command_error ('you may not use %s' % cmd['name']) - bot.privmsg (replyto, '%s %s: %s' % (cmd['name'], cmd['args'], cmd['description'])) + reply ('%s %s: %s' % (cmd['name'], cmd['args'], cmd['description'])) def mathsubstitute (expr, token, value): rex = re.compile (r'^(.*)\b' + token + r'\b(.*)$') @@ -128,7 +138,7 @@ return expr -def cmd_calc (bot, replyto, args, **rest): +def cmd_calc (bot, reply, args, **rest): expr = args['expression'] try: @@ -147,8 +157,11 @@ command_error ('math error') return - bot.privmsg (replyto, 'Result: %s' % result) + reply ('Result: %s' % result) except subprocess.CalledProcessError as e: command_error (e.output.split('\n')[0]) except OSError as e: command_error ('failed to execute calc: ' + e.strerror) + +def cmd_more (bot, replyto, **rest): + ModuleCore.print_responses (bot, replyto)
--- a/modulecore.py Wed Dec 31 10:27:57 2014 -0500 +++ b/modulecore.py Wed Dec 31 11:40:46 2014 -0500 @@ -49,14 +49,10 @@ continue cmd['argnames'].append (argname) - #done - #done print "Loaded module %s" % fn - #done print 'Loaded %d commands in %d modules' % (len (Commands), len (Modules)) -#enddef # # command_error (message) @@ -79,11 +75,50 @@ return True # +# response_function +# +g_responsePages = [[]] +g_responsePageNum = 0 + +def response_function (message): + global g_responsePages + + if len (g_responsePages[-1]) > 4: + g_responsePages.append ([message]) + else: + g_responsePages[-1].append (message) + +def print_responses (bot, replyto): + global g_responsePages + global g_responsePageNum + + # Check bounds + if g_responsePageNum >= len (g_responsePages): + bot.privmsg (replyto, "No more messages.") + return + + # Print this page + for line in g_responsePages[g_responsePageNum]: + bot.privmsg (replyto, line) + + # Advance page cursor + g_responsePageNum += 1 + + # If this was not the last page, tell the user there's more + if g_responsePageNum != len (g_responsePages): + num = (len (g_responsePages) - g_responsePageNum) + bot.privmsg (replyto, "%d more page%s, use .more to continue output" \ + % (num, 's' if num != 1 else '')) + +# # call_command (bot, message, cmdname, **kvargs) # # Calls a cobalt command # def call_command (bot, message, cmdname, **kvargs): + global g_responsePages + global g_responsePageNum + try: cmd = Commands[cmdname] except KeyError: @@ -98,7 +133,13 @@ # didn't match print "regex: %s" % cmd['regex'] command_error ('invalid arguments\nusage: %s %s' % (cmd['name'], cmd['args'])) - + + # .more is special as it is an interface to the page system. + # Anything else resets it. + if cmdname != 'more': + g_responsePages = [[]] + g_responsePageNum = 0 + i = 1 args = {} @@ -106,7 +147,12 @@ args[argname] = match.group (i) i += 1 - getattr (cmd['module'], "cmd_%s" % cmdname) (bot=bot, cmdname=cmdname, args=args, **kvargs) + getattr (cmd['module'], "cmd_%s" % cmdname) (bot=bot, cmdname=cmdname, args=args, reply=response_function, **kvargs) + + # Print the first page of responses. + if cmdname != 'more': + print_responses (bot, kvargs['replyto']) + return True #