from twisted.internet.protocol import Protocol, ClientFactory from sys import stdout class Echo(Protocol): def dataReceived(self, data): stdout.write(data) self.transport.write(data) def announceNewFile(self, watch_obj, path, mask): # problem!!! the inotify callback sends three args (watch_obj, path, mask) # but we require a reference to self. halp? self.transport.write(msg) class EchoClientFactory(ClientFactory): def startedConnecting(self, connector): print 'Started to connect.' def buildProtocol(self, addr): print 'Connected.' return Echo() def clientConnectionLost(self, connector, reason): print 'Lost connection. Reason:', reason def clientConnectionFailed(self, connector, reason): print 'Connection failed. Reason:', reason if __name__ == "__main__": from twisted.internet import reactor, inotify from twisted.python import filepath import sys reactor.connectTCP("localhost", int(sys.argv[1]), EchoClientFactory()) notifier = inotify.INotify() notifier.startReading() watch_mask = inotify.IN_CREATE notifier.watch(filepath.FilePath("/tmp"), mask=watch_mask, callbacks=[Echo.announceNewFile]) # again, there is no way for notifier to actually call the announceNewFile # method on an Echo() object, because it doesn't have an Echo() object. lolwut? # speaking generally, the protocol/client/factory situation is all object-oriented, # but the inotify stuff if procedure (ie, just a callback function). how does one # reconcile these two realms? reactor.run()