http://www.mediawiki.org/wiki/Special:Code/MediaWiki/98010

Revision: 98010
Author:   laner
Date:     2011-09-24 17:45:22 +0000 (Sat, 24 Sep 2011)
Log Message:
-----------
Add comments, some more exception handling, and refactor names some.

Modified Paths:
--------------
    trunk/debs/ircecho/ircecho

Modified: trunk/debs/ircecho/ircecho
===================================================================
--- trunk/debs/ircecho/ircecho  2011-09-24 17:13:51 UTC (rev 98009)
+++ trunk/debs/ircecho/ircecho  2011-09-24 17:45:22 UTC (rev 98010)
@@ -15,6 +15,9 @@
 from optparse import OptionParser
 from ircbot import SingleServerIRCBot
 
+global bot
+global reader
+
 class EchoNotifier(threading.Thread):
        def __init__(self, notifier):
                threading.Thread.__init__(self)
@@ -56,6 +59,9 @@
                        self.notifiers = []
                        self.associations = {}
                        self.files = {}
+                       # infiles format:
+                       #   
file[:channel,channel,...;file[:channel,channel,...];...]
+                       # , ; : can be escaped with \
                        infiles = self.escape(self.infile)
                        for filechan in infiles.split(';'):
                                temparr = filechan.split(':')
@@ -71,7 +77,7 @@
                                        pass
                                wm = pyinotify.WatchManager()
                                mask = pyinotify.IN_MODIFY | pyinotify.IN_CREATE
-                               wm.watch_transient_file(filename, mask, 
EventHandler)
+                               wm.watch_transient_file(filename, mask, 
EchoHandler)
                                notifier = EchoNotifier(pyinotify.Notifier(wm))
                                self.notifiers.append(notifier)
                                # Does this file have channel associations?
@@ -82,6 +88,7 @@
                                print "Starting notifier loop"
                                notifier.start()
                else:
+                       # Read from stdin
                        while True:
                                try:
                                        s = raw_input()
@@ -92,6 +99,7 @@
                                        break;
                                except Exception:
                                        pass
+
        def readfile(self, filename):
                if self.files[filename]:
                        return self.files[filename].read()
@@ -102,6 +110,7 @@
                if filename in self.associations:
                        return self.associations[filename]
                else:
+                       # If file has no associated channels, return all 
channels
                        return bot.chans
 
 class EchoBot(SingleServerIRCBot):
@@ -118,14 +127,18 @@
                for chan in [self.chans]:
                        c.join(chan)
 
-class EventHandler(pyinotify.ProcessEvent):
+class EchoHandler(pyinotify.ProcessEvent):
        def process_IN_MODIFY(self, event):
                s = reader.readfile(event.pathname)
                if s:
                        chans = reader.getchannels(event.pathname)
-                       bot.connection.privmsg(chans, s)
+                       try:
+                               bot.connection.privmsg(chans, s)
+                       except Exception:
+                               pass
 
        def process_IN_CREATE(self, event):
+               # File has been deleted and re-created
                try:
                        print "Reopening file: " + event.pathname
                        reader.files[event.pathname] = open(event.pathname)
@@ -133,16 +146,18 @@
                        print "Failed to reopen file: " + event.pathname
                        pass
 
-parser = OptionParser(conflict_handler="resolve")
-parser.set_usage("ircecho [--infile=<filename>] <channel> <nickname> <server>")
-parser.add_option("--infile", dest="infile", help="Read input from the 
specific file instead of from stdin")
-(options, args) = parser.parse_args()
-chans = args[0]
-nickname = args[1]
-server = args[2]
-global bot
-bot = EchoBot(chans, nickname, server)
-global reader
-reader = EchoReader(options.infile)
-reader.start()
-bot.start()
+def main():
+       parser = OptionParser(conflict_handler="resolve")
+       parser.set_usage("ircecho [--infile=<filename>] <channel> <nickname> 
<server>")
+       parser.add_option("--infile", dest="infile", help="Read input from the 
specific file instead of from stdin")
+       (options, args) = parser.parse_args()
+       chans = args[0]
+       nickname = args[1]
+       server = args[2]
+       bot = EchoBot(chans, nickname, server)
+       reader = EchoReader(options.infile)
+       reader.start()
+       bot.start()
+
+if __name__ == "__main__":
+       main()


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to