r97891 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97890‎ | r97891 | r97892 >
Date:07:16, 23 September 2011
Author:laner
Status:deferred
Tags:
Comment:
* Support reading from multiple files
* Echo content from specific files into specific channels
* Echo content to all channels for files that do not specify channels
Modified paths:
  • /trunk/debs/ircecho/ircecho (modified) (history)

Diff [purge]

Index: trunk/debs/ircecho/ircecho
@@ -9,26 +9,79 @@
1010 import sys
1111 import pyinotify
1212 import threading
 13+import random
 14+import string
 15+import re
1316 from optparse import OptionParser
1417 sys.path.append('/usr/ircecho/lib')
1518 from ircbot import SingleServerIRCBot
1619
 20+class EchoNotifier(threading.Thread):
 21+ def __init__(self, notifier):
 22+ threading.Thread.__init__(self)
 23+ self.notifier = notifier
 24+
 25+ def run(self):
 26+ self.notifier.loop()
 27+
1728 class EchoReader(threading.Thread):
18 - def __init__(self, infile=''):
 29+ def __init__(self, infile='', associatedchannel=''):
1930 threading.Thread.__init__(self)
2031 self.infile = infile
 32+ self.associatedchannel = associatedchannel
 33+ self.uniques = {';': "UNIQ_" + self.get_unique_string() + "_QINU",
 34+ ':': "UNIQ_" + self.get_unique_string() + "_QINU",
 35+ ',': "UNIQ_" + self.get_unique_string() + "_QINU"}
2136
 37+ def get_unique_string(self):
 38+ unique = ''
 39+ for i in range(15):
 40+ unique = unique + random.choice(string.letters)
 41+ return unique
 42+
 43+ def escape(self, string):
 44+ escaped_string = re.sub('\\\;', self.uniques[';'], string)
 45+ escaped_string = re.sub('\\\:', self.uniques[':'], escaped_string)
 46+ escaped_string = re.sub('\\\,', self.uniques[','], escaped_string)
 47+ return escaped_string
 48+
 49+ def unescape(self, string):
 50+ unescaped_string = re.sub(self.uniques[';'], ';', string)
 51+ unescaped_string = re.sub(self.uniques[':'], ':', unescaped_string)
 52+ unescaped_string = re.sub(self.uniques[','], ',', unescaped_string)
 53+ return unescaped_string
 54+
2255 def run(self):
2356 if self.infile:
2457 print "Using infile"
25 - self.f = open(self.infile)
26 - # Seek to the end of the file
27 - self.f.seek(0,2)
28 - wm = pyinotify.WatchManager()
29 - notifier = pyinotify.Notifier(wm)
30 - mask = pyinotify.IN_MODIFY | pyinotify.IN_CREATE
31 - wdd = wm.watch_transient_file(self.infile, mask, EventHandler)
32 - notifier.loop()
 58+ self.notifiers = []
 59+ self.associations = {}
 60+ self.files = {}
 61+ infiles = self.escape(self.infile)
 62+ for filechan in infiles.split(';'):
 63+ temparr = filechan.split(':')
 64+ filename = self.unescape(temparr[0])
 65+ try:
 66+ print "Opening: " + filename
 67+ f = open(filename)
 68+ f.seek(0,2)
 69+ self.files[filename] = f
 70+ except IOError:
 71+ print "Failed to open file: " + filename
 72+ self.files[filename] = None
 73+ pass
 74+ wm = pyinotify.WatchManager()
 75+ mask = pyinotify.IN_MODIFY | pyinotify.IN_CREATE
 76+ wm.watch_transient_file(filename, mask, EventHandler)
 77+ notifier = EchoNotifier(pyinotify.Notifier(wm))
 78+ self.notifiers.append(notifier)
 79+ # Does this file have channel associations?
 80+ if len(temparr) > 1:
 81+ chans = self.unescape(temparr[1])
 82+ self.associations[filename] = chans
 83+ for notifier in self.notifiers:
 84+ print "Starting notifier loop"
 85+ notifier.start()
3386 else:
3487 while True:
3588 try:
@@ -40,7 +93,18 @@
4194 break;
4295 except Exception:
4396 pass
 97+ def readfile(self, filename):
 98+ if self.files[filename]:
 99+ return self.files[filename].read()
 100+ else:
 101+ return
44102
 103+ def getchannels(self, filename):
 104+ if filename in self.associations:
 105+ return self.associations[filename]
 106+ else:
 107+ return bot.chans
 108+
45109 class EchoBot(SingleServerIRCBot):
46110 def __init__(self, chans, nickname, server):
47111 print "*** Connecting to IRC server %s..." % server
@@ -57,15 +121,17 @@
58122
59123 class EventHandler(pyinotify.ProcessEvent):
60124 def process_IN_MODIFY(self, event):
61 - s = reader.f.read()
62 - bot.connection.privmsg(bot.chans, s)
 125+ s = reader.readfile(event.pathname)
 126+ if s:
 127+ chans = reader.getchannels(event.pathname)
 128+ bot.connection.privmsg(chans, s)
63129
64130 def process_IN_CREATE(self, event):
65131 try:
66 - print "Reopening file"
67 - reader.f = open(reader.infile)
 132+ print "Reopening file: " + event.pathname
 133+ reader.files[event.pathname] = open(event.pathname)
68134 except IOError:
69 - print "Failed to reopen file"
 135+ print "Failed to reopen file: " + event.pathname
70136 pass
71137
72138 parser = OptionParser(conflict_handler="resolve")

Status & tagging log