Giter Site home page Giter Site logo

macfsevents's Introduction

MacFSEvents is a Python library that provides thread-safe directory observation primitives using callbacks. It wraps the Mac OS X FSEvents API in a C-extension.

Requirements:

  • Mac OS X 10.5+ (Leopard)

This software was written by Malthe Borch <[email protected]>. The pyfsevents module by Nicolas Dumazet was used for reference.

At this time of writing there are three other libraries that integrate with the FSEvents API:

pyobjc-framework-FSEvents

These use the PyObjC bridge infrastructure which most applications do not need.

pyfsevents

Not thread-safe (API is not designed to support it).

fsevents

Obsolete bindings to the socket API by John Sutherland.

These issues have been addressed in MacFSEvents. The library provides a clean API and has full test coverage.

Note that pyfsevents has bindings to the file descriptor observation primitives. This is not currently implemented by the present library.

Made available as-is under the BSD License.

To observe a directory structure (recursively) under path, we set up an observer thread and schedule an event stream:

from fsevents import Observer
observer = Observer()
observer.start()

def callback(subpath, mask):
    ...

from fsevents import Stream
stream = Stream(callback, path)
observer.schedule(stream)

Streams can observe any number of paths; simply pass them as positional arguments (or using the * operator):

stream = Stream(callback, *paths)

To start the observer in its own thread, use the start method:

observer.starts()

To start the observer in the current thread, use the run method (it will block the thread until stopped from another thread):

observer.run()

The callback function will be called when an event occurs. The subpath parameter contains the path at which the event happened (may be a subdirectory) while mask parameter is the event mask [1].

To stop observation, simply unschedule the stream and stop the observer:

observer.unschedule(stream)
observer.stop()

While the observer thread will automatically join your main thread at this point, it doesn't hurt to be explicit about this:

observer.join()

We often want to know about events on a file level; to receive file events instead of path events, pass in file_events=True to the stream constructor:

def callback(event):
    ...

stream = Stream(callback, path, file_events=True)

The event object mimick the file events of the inotify kernel extension available in newer linux kernels. It has the following attributes:

mask
The mask field is a bitmask representing the event that occurred.
cookie
The cookie field is a unique identifier linking together two related but separate events. It is used to link together an IN_MOVED_FROM and an IN_MOVED_TO event.
name
The name field contains the name of the object to which the event occurred. This is the absolute filename.

Note that the logic to implement file events is implemented in Python; a snapshot of the observed file system hierarchies is maintained and used to monitor file events.

[1]See FSEventStreamEventFlags for a reference. To check for a particular mask, use the bitwise and operator &.

macfsevents's People

Contributors

axtl avatar malthe avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.