[TASK] Tabs to spaces.

This commit is contained in:
Jan Philipp Timme 2015-10-20 15:26:33 +02:00
parent b500639653
commit 0319cc12d8
1 changed files with 114 additions and 114 deletions

228
fdskun.py
View File

@ -10,150 +10,150 @@ from ConfigParser import ConfigParser
import time, sys import time, sys
class MonitorBot(irc.IRCClient): class MonitorBot(irc.IRCClient):
"""A (FTP)-FileSystem Monitoring IRC bot.""" """A (FTP)-FileSystem Monitoring IRC bot."""
def __init__(self, nickname): def __init__(self, nickname):
self.nickname = nickname self.nickname = nickname
def connectionMade(self): def connectionMade(self):
irc.IRCClient.connectionMade(self) irc.IRCClient.connectionMade(self)
print("[connected at %s]" % time.asctime(time.localtime(time.time()))) print("[connected at %s]" % time.asctime(time.localtime(time.time())))
def connectionLost(self, reason): def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason) irc.IRCClient.connectionLost(self, reason)
print("[disconnected at %s]" % time.asctime(time.localtime(time.time()))) print("[disconnected at %s]" % time.asctime(time.localtime(time.time())))
# callbacks for events # callbacks for events
def signedOn(self): def signedOn(self):
"""Called when bot has succesfully signed on to server.""" """Called when bot has succesfully signed on to server."""
print("[signed in]") print("[signed in]")
self.mode(self.nickname, False, 'x') self.mode(self.nickname, False, 'x')
self.mode(self.nickname, True, 'B') self.mode(self.nickname, True, 'B')
self.msg('NickServ', 'IDENTIFY %s' % self.factory.nickserv_pw) self.msg('NickServ', 'IDENTIFY %s' % self.factory.nickserv_pw)
self.join(self.factory.channel) self.join(self.factory.channel)
def joined(self, channel): def joined(self, channel):
"""This will get called when the bot joins the channel.""" """This will get called when the bot joins the channel."""
print("[I have joined %s]" % channel) print("[I have joined %s]" % channel)
def privmsg(self, user, channel, msg): def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message.""" """This will get called when the bot receives a message."""
pass pass
class MonitorBotFactory(protocol.ClientFactory): class MonitorBotFactory(protocol.ClientFactory):
"""A factory for MonitorBots. """A factory for MonitorBots.
A new protocol instance will be created each time we connect to the server. A new protocol instance will be created each time we connect to the server.
""" """
def __init__(self, nickname, channel, nickserv_pw, fsmon): def __init__(self, nickname, channel, nickserv_pw, fsmon):
self.nickname = nickname self.nickname = nickname
self.channel = channel self.channel = channel
self.nickserv_pw = nickserv_pw self.nickserv_pw = nickserv_pw
self.fsmon = fsmon self.fsmon = fsmon
def buildProtocol(self, addr): def buildProtocol(self, addr):
p = MonitorBot(self.nickname) p = MonitorBot(self.nickname)
self.fsmon.setBot(p) self.fsmon.setBot(p)
p.factory = self p.factory = self
return p return p
def clientConnectionLost(self, connector, reason): def clientConnectionLost(self, connector, reason):
"""If we get disconnected, reconnect to server.""" """If we get disconnected, reconnect to server."""
connector.connect() connector.connect()
def clientConnectionFailed(self, connector, reason): def clientConnectionFailed(self, connector, reason):
print "connection failed:", reason print "connection failed:", reason
reactor.stop() reactor.stop()
class Options(usage.Options): class Options(usage.Options):
optParameters = [ optParameters = [
['config', 'c', 'settings.ini', 'Configuration file.'], ['config', 'c', 'settings.ini', 'Configuration file.'],
] ]
class FSMonitor(): class FSMonitor():
def __init__(self, path, channel): def __init__(self, path, channel):
self._messages = [] self._messages = []
self._callid = None self._callid = None
self._bot = None self._bot = None
self._channel = channel self._channel = channel
self._watch_path = filepath.FilePath(path) self._watch_path = filepath.FilePath(path)
self._watchMask = ( inotify.IN_MODIFY self._watchMask = ( inotify.IN_MODIFY
| inotify.IN_CREATE | inotify.IN_CREATE
| inotify.IN_DELETE | inotify.IN_DELETE
| inotify.IN_MOVED_FROM | inotify.IN_MOVED_FROM
| inotify.IN_MOVED_TO | inotify.IN_MOVED_TO
) )
notifier = inotify.INotify() notifier = inotify.INotify()
notifier.startReading() notifier.startReading()
notifier.watch(self._watch_path, autoAdd=True, recursive=True, callbacks=[self.fsnotify], mask=self._watchMask) notifier.watch(self._watch_path, autoAdd=True, recursive=True, callbacks=[self.fsnotify], mask=self._watchMask)
def setBot(self, bot): def setBot(self, bot):
self._bot = bot self._bot = bot
def humanReadableMask(self, mask): def humanReadableMask(self, mask):
flags_to_human = [ flags_to_human = [
(inotify.IN_MODIFY, 'geändert'), (inotify.IN_MODIFY, 'geändert'),
(inotify.IN_CREATE, 'erstellt'), (inotify.IN_CREATE, 'erstellt'),
(inotify.IN_DELETE, 'gelöscht'), (inotify.IN_DELETE, 'gelöscht'),
(inotify.IN_MOVED_FROM, 'umbenannt von'), (inotify.IN_MOVED_FROM, 'umbenannt von'),
(inotify.IN_MOVED_TO, 'umbenannt nach') (inotify.IN_MOVED_TO, 'umbenannt nach')
] ]
"""In Maske enthaltene Flags als String zusammenbauen""" """In Maske enthaltene Flags als String zusammenbauen"""
s = [] s = []
for k, v in flags_to_human: for k, v in flags_to_human:
if k & mask: if k & mask:
s.append(v) s.append(v)
return s return s
def fsnotify(self, ignored, filepath, mask): def fsnotify(self, ignored, filepath, mask):
"""Actually called by the notifier in case of any event.""" """Actually called by the notifier in case of any event."""
if self._callid != None and self._callid.active(): if self._callid != None and self._callid.active():
self._callid.cancel() self._callid.cancel()
path_segments = filepath.segmentsFrom(self._watch_path) path_segments = filepath.segmentsFrom(self._watch_path)
new_path = '/'.join(path_segments) new_path = '/'.join(path_segments)
msg = "ftp> /%s (%s)" % (new_path, ', '.join(self.humanReadableMask(mask))) msg = "ftp> /%s (%s)" % (new_path, ', '.join(self.humanReadableMask(mask)))
if msg not in self._messages: if msg not in self._messages:
self._messages.append(msg) self._messages.append(msg)
self._callid = reactor.callLater(10.0, self.sendQueuedMessages) self._callid = reactor.callLater(10.0, self.sendQueuedMessages)
def sendQueuedMessages(self): def sendQueuedMessages(self):
if self._bot == None: if self._bot == None:
print("No Bot given, dropping messages!") print("No Bot given, dropping messages!")
return return
if len(self._messages) > 3: if len(self._messages) > 3:
self._bot.msg(self._channel, "ftp> %i Events übersprungen. Letzter Event:" % (len(self._messages)-1)) self._bot.msg(self._channel, "ftp> %i Events übersprungen. Letzter Event:" % (len(self._messages)-1))
self._bot.msg(self._channel, self._messages[len(self._messages)-1]) self._bot.msg(self._channel, self._messages[len(self._messages)-1])
else: else:
for msg in self._messages: for msg in self._messages:
self._bot.msg(self._channel, msg) self._bot.msg(self._channel, msg)
self._messages = [] self._messages = []
if __name__ == '__main__': if __name__ == '__main__':
options = Options() options = Options()
config = ConfigParser() config = ConfigParser()
config.read([options['config']]) config.read([options['config']])
host = config.get('irc', 'host') host = config.get('irc', 'host')
port = int(config.get('irc', 'port')) port = int(config.get('irc', 'port'))
channel = config.get('irc', 'channel') channel = config.get('irc', 'channel')
nickname = config.get('irc', 'nickname') nickname = config.get('irc', 'nickname')
nickserv_pw = config.get('irc', 'nickserv_pw') nickserv_pw = config.get('irc', 'nickserv_pw')
realname = config.get('irc', 'realname') realname = config.get('irc', 'realname')
path = config.get('fsmonitor', 'path') path = config.get('fsmonitor', 'path')
fsmon = FSMonitor(path, channel) fsmon = FSMonitor(path, channel)
# create factory protocol and application # create factory protocol and application
f = MonitorBotFactory(nickname, channel, nickserv_pw, fsmon) f = MonitorBotFactory(nickname, channel, nickserv_pw, fsmon)
# connect factory to this host and port # connect factory to this host and port
reactor.connectTCP(host, port, f) reactor.connectTCP(host, port, f)
# run bot # run bot
reactor.run() reactor.run()