I'm writing an application that has to be able to monitor filesystem directories for changes in python, in order to do that i use MacFSEvents, a python wrapper over the FSEvents C API, as for obvious efficiency reasons polling can't be a choice since this code could be in charge of monitoring recursively a lot of files, the module however does work and a code like this produces the expected result
from fsevents import Observer, Stream
def callback(FSEvent, mask=0, cookie=0):
"""
the callback that gets called when an event occurs
:param FSEvent: file or dir that originated the event
:param mask: a bitMask that specifies the type of the event
:param cookie: contains info to bind a moved_to to a moved_from event
"""
print(FSEvent)
print('\n')
print(mask)
print('\n')
if __name__ == "__main__":
try:
obs = Observer()
obs.start()
str = Stream(callback, "/Users/filipposavi/ass", file_events=True)
obs.schedule(str)
except KeyboardInterrupt:
obs.stop()
obs.join
however when I put that into the class that should manage (relatively) low level filesystem details the callback is not called, here below you can find the code that doesn't work
import hashlib
import os
import fsevents
from database import Db
BUFSIZE = 2 ** 3
chunksize = 1024
IN_CREATE = 0x00000100
IN_ATTRIB = 0x00000004
IN_MODIFY = 0x00000002
IN_DELETE = 0x00000200
IN_MOVED_FROM = 0x00000040
IN_MOVED_TO = 0x00000080
class FsCore(object):
def __init__(self):
self.db = Db()
def callback(self, FSEvent, mask, cookie=0):
"""
the callback that gets called when an event occurs
:param FSEvent: file or dir that originated the event
:param mask: a bitMask that specifies the type of the event
:param cookie: contains info to bind a moved_to to a moved_from event
"""
print('ciao')
if (mask & IN_CREATE) == IN_CREATE:
self.on_created(FSEvent)
if (mask & IN_ATTRIB) == IN_ATTRIB:
self.on_attrib(FSEvent)
if (mask & IN_DELETE) == IN_DELETE:
self.on_deleted(FSEvent)
if (mask & IN_MODIFY) == IN_MODIFY:
self.on_modified(FSEvent)
if (mask & IN_MOVED_FROM) == IN_MOVED_FROM:
self.on_moved(FSEvent, cookie, False)
if (mask & IN_MOVED_TO) == IN_MOVED_TO:
def startFSEvents(self, path):
"""
:param path:
"""
self.observer = fsevents.Observer()
self.observer.start()
self.stream = fsevents.Stream(self.callback, path, file_events=True)
self.observer.schedule(self.stream)
def stopFSEvents(self):
"""
"""
self.observer.unschedule(self.stream)
self.observer.join()
self.observer.stop()
and here is the code that's should be triggering the event/callback(part of a unit test)
self.fs = FsCore()
self.fs.startFSEvents(self.path)
self.database = Db()
path = self.path + "/tmp"
file = open(path, mode='a')
file.write("ciao")
file.flush()
file.close()
if not self.database.getFile(path):
self.fail("onCreated callback doesn't works or it's not called at all")
Hope you guys can hint me the right direction since i'm a bit of a novice with python so i'm pretty stuck on that