r98010 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98009‎ | r98010 | r98011 >
Date:17:45, 24 September 2011
Author:laner
Status:deferred
Tags:
Comment:
Add comments, some more exception handling, and refactor names some.
Modified paths:
  • /trunk/debs/ircecho/ircecho (modified) (history)

Diff [purge]

Index: trunk/debs/ircecho/ircecho
@@ -15,6 +15,9 @@
1616 from optparse import OptionParser
1717 from ircbot import SingleServerIRCBot
1818
 19+global bot
 20+global reader
 21+
1922 class EchoNotifier(threading.Thread):
2023 def __init__(self, notifier):
2124 threading.Thread.__init__(self)
@@ -56,6 +59,9 @@
5760 self.notifiers = []
5861 self.associations = {}
5962 self.files = {}
 63+ # infiles format:
 64+ # file[:channel,channel,...;file[:channel,channel,...];...]
 65+ # , ; : can be escaped with \
6066 infiles = self.escape(self.infile)
6167 for filechan in infiles.split(';'):
6268 temparr = filechan.split(':')
@@ -71,7 +77,7 @@
7278 pass
7379 wm = pyinotify.WatchManager()
7480 mask = pyinotify.IN_MODIFY | pyinotify.IN_CREATE
75 - wm.watch_transient_file(filename, mask, EventHandler)
 81+ wm.watch_transient_file(filename, mask, EchoHandler)
7682 notifier = EchoNotifier(pyinotify.Notifier(wm))
7783 self.notifiers.append(notifier)
7884 # Does this file have channel associations?
@@ -82,6 +88,7 @@
8389 print "Starting notifier loop"
8490 notifier.start()
8591 else:
 92+ # Read from stdin
8693 while True:
8794 try:
8895 s = raw_input()
@@ -92,6 +99,7 @@
93100 break;
94101 except Exception:
95102 pass
 103+
96104 def readfile(self, filename):
97105 if self.files[filename]:
98106 return self.files[filename].read()
@@ -102,6 +110,7 @@
103111 if filename in self.associations:
104112 return self.associations[filename]
105113 else:
 114+ # If file has no associated channels, return all channels
106115 return bot.chans
107116
108117 class EchoBot(SingleServerIRCBot):
@@ -118,14 +127,18 @@
119128 for chan in [self.chans]:
120129 c.join(chan)
121130
122 -class EventHandler(pyinotify.ProcessEvent):
 131+class EchoHandler(pyinotify.ProcessEvent):
123132 def process_IN_MODIFY(self, event):
124133 s = reader.readfile(event.pathname)
125134 if s:
126135 chans = reader.getchannels(event.pathname)
127 - bot.connection.privmsg(chans, s)
 136+ try:
 137+ bot.connection.privmsg(chans, s)
 138+ except Exception:
 139+ pass
128140
129141 def process_IN_CREATE(self, event):
 142+ # File has been deleted and re-created
130143 try:
131144 print "Reopening file: " + event.pathname
132145 reader.files[event.pathname] = open(event.pathname)
@@ -133,16 +146,18 @@
134147 print "Failed to reopen file: " + event.pathname
135148 pass
136149
137 -parser = OptionParser(conflict_handler="resolve")
138 -parser.set_usage("ircecho [--infile=<filename>] <channel> <nickname> <server>")
139 -parser.add_option("--infile", dest="infile", help="Read input from the specific file instead of from stdin")
140 -(options, args) = parser.parse_args()
141 -chans = args[0]
142 -nickname = args[1]
143 -server = args[2]
144 -global bot
145 -bot = EchoBot(chans, nickname, server)
146 -global reader
147 -reader = EchoReader(options.infile)
148 -reader.start()
149 -bot.start()
 150+def main():
 151+ parser = OptionParser(conflict_handler="resolve")
 152+ parser.set_usage("ircecho [--infile=<filename>] <channel> <nickname> <server>")
 153+ parser.add_option("--infile", dest="infile", help="Read input from the specific file instead of from stdin")
 154+ (options, args) = parser.parse_args()
 155+ chans = args[0]
 156+ nickname = args[1]
 157+ server = args[2]
 158+ bot = EchoBot(chans, nickname, server)
 159+ reader = EchoReader(options.infile)
 160+ reader.start()
 161+ bot.start()
 162+
 163+if __name__ == "__main__":
 164+ main()

Status & tagging log