[TASK] Improve folder watch.

This commit is contained in:
Jan Philipp Timme 2014-05-14 22:57:30 +02:00
parent ec6413231c
commit 9a26c3f8cb
5 changed files with 43 additions and 11 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
settings.ini
*.pyc
*.cache

View File

@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
from twisted.internet import protocol
from twisted.python import log
from twisted.words.protocols import irc

View File

@ -1,8 +0,0 @@
[irc]
endpoint = tcp:host=irc.euirc.net:port=6667
nickName = ftpd
realName = bot: provides tracking of an ftp
channel = #Tonari.
[fsmonitor]
path=/home/kuzuru-ftp/

8
settings.ini.example Normal file
View File

@ -0,0 +1,8 @@
[irc]
endpoint = tcp:host=irc.euirc.net:port=6667
nickName = FDS-kun
realName = bot: provides tracking of an ftp folder
channel = #JPT
[fsmonitor]
path=/tmp/foobar/

View File

@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
from ConfigParser import ConfigParser
from twisted.application.service import IServiceMaker, Service
@ -19,7 +21,7 @@ class MonitorBotService(Service):
self._channel = channel
self._nickname = nickname
self._realname = realname
self._path = path
self._watch_path = filepath.FilePath(path)
def startService(self):
"""Construct a client & connect to server."""
@ -40,14 +42,41 @@ class MonitorBotService(Service):
self._realname
)
def humanReadableMask(mask):
flags_to_human = [
(inotify.IN_MODIFY, 'geändert'),
(inotify.IN_CREATE, 'erstellt'),
(inotify.IN_DELETE, 'gelöscht'),
(inotify.IN_MOVED_FROM, 'umbenannt von'),
(inotify.IN_MOVED_TO, 'umbenannt nach')
]
s = []
for k, v in flags_to_human:
if k & mask:
s.append(v)
return s
def fsnotify(ignored, filepath, mask):
msg = "event %s on %s" % (', '.join(inotify.humanReadableMask(mask)), filepath)
path_segments = filepath.segmentsFrom(self._watch_path)
new_path = '/'.join(path_segments)
msg = "ftp> /%s (%s)" % (new_path, ', '.join(humanReadableMask(mask)))
self._bot.msg(self._channel, msg)
pass
watchMask = ( inotify.IN_MODIFY
| inotify.IN_CREATE
| inotify.IN_DELETE
| inotify.IN_DELETE_SELF
| inotify.IN_MOVED_FROM
| inotify.IN_MOVED_TO
| inotify.IN_MOVE_SELF )
notifier = inotify.INotify()
notifier.startReading()
notifier.watch(filepath.FilePath(self._path), autoAdd=True, recursive=True, callbacks=[fsnotify])
notifier.watch(self._watch_path, autoAdd=True, recursive=True, callbacks=[fsnotify], mask=watchMask)
"""Attach defined callbacks."""
return client.connect(factory).addCallbacks(connected, failure)