Skip to main content

PyNotify is an async Python interface to the Linux inotify API.

Project description

PyNotify is an async Python interface to the Linux inotify API.

See man inotify for more information regarding inotify details.

See the documentation!

Install

Install via pip:

pip install pynotify-mcriley821

To install manually, clone the repo and pip install:

git clone https://github.com/mcriley821/PyNotify.git
cd PyNotify && pip install .

Description

PyNotify uses the ctypes module to interface with the inotify API to allow the user to create ‘watches’ for monitoring filesystem events. These events are parsed into Event objects, which are then handled by EventHandler objects.

Any number of EventHandlers can be added to a Notifier instance to handle a specific watch. This is done when requesting a watch via Notifier.add_watch When an Event is emitted for the corresponding watch, each EventHandler is queried for capability of handling said Event. The Event is subsequently passed to the EventHandler if it is capable.

Usage

Simple use case

As an example, an EventHandler that handles all event types for a watch could be defined as so:

class AllHandler:
    def handle_event(self, event: Event) -> None:
        # just print out what is happening
        print(f"{event.type.name} at {event.file_path}")

    def can_handle_event_type(self, type: EventType) -> bool:
        return EventType.ALL & type != 0

The AllHandler can now be added to a watch via Notifier.add_watch:

async def main():
    with pynotify.Notifier() as notifier:
        notifier.add_watch(pathlib.Path.cwd(), AllHandler())
        await notifier.run()

A slightly more interesting example

class OpenHandler:
    def handle_event(self, event: Event) -> None:
        ...

    def can_handle_event_type(self, type: EventType) -> bool:
        return EventType.OPEN & type != 0

class CloseHandler:
    def handle_event(self, event: Event) -> None:
        ...

     def can_handle_event_type(self, type: EventType) -> bool:
        return EventType.CLOSE & type != 0

async def stop_loop(stop_event: asyncio.Event):
    await asyncio.sleep(10)
    stop_event.set()

async def main():
    with pynotify.Notifier() as notifier:
        path = pathlib.Path.cwd()
        stop_event = asyncio.Event()

        notifier.add_watch(path, OpenHandler(), CloseHandler(),
                           only_event_types=EventType.OPEN | EventType.CLOSE)
        await asyncio.gather(
               notifier.run(stop_event=stop_event),
               stop_loop(stop_event))

The above example will run the Notifier run-loop for 10 seconds, generating only open and close Events for the watch on the current working directory.

Adding/Modifying/Removing watches

Watches can be added as simply as we’ve seen above. There are a few more options that can be specified when adding a watch:

async def main():
    with pynotify.Notifier() as notifier:
        path = pathlib.Path.cwd()
        notifier.add_watch(
            path,  # path to add a watch on

            # any number of handlers for the watch
            AllHandler(), OpenHandler(), CloseHandler(),

            # restrict EventTypes generated by the watch
            only_event_types=EventTypes.OPEN,

            # raises if False and path is a symlink
            follow_symlinks=False,

            # raises if True and path is not a directory
            if_directory_only=True,

            # if True, generate a single event then remove the watch
            oneshot=False,

            # See the docs for more info on this flag
            exclude_unlinks=True)

EventTypes for a watch can be modified after it has been added to a Notifier:

async def main():
   with pynotify.Notifier() as notifier:
       path = pathlib.Path.cwd()
       notifier.add_watch(path)  # generates all EventTypes by default
       ...
       # generate only CLOSE Events
       notifier.modify_watch_event_type(path, EventType.CLOSE)

       # merge EventTypes to generate both CLOSE and OPEN Events
       notifier.modify_watch_event_type(path, EventType.OPEN, merge=True)

Watches are easily removed:

async def main():
    with pynotify.Notifier() as notifier:
        path = pathlib.Path.cwd()
        notifier.add_watch(path)
        ...
        notifier.remove_watch(path)
        # notifier.remove_watch(path)  # raises, since path not being watched
        notifier.remove_watch(path, raises=False)  # don't raise

Adding/Removing/Clearing EventHandlers

EventHandlers can be added when adding a watch, and can be added or removed after a watch has already been established:

async def main():
    with pynotify.Notifier() as notifier:
        path = pathlib.Path.cwd()
        open_handler = OpenHandler()
        notifier.add_watch(path, open_handler)  # add open_handler to watch

        all_handler = AllHandler()
        # add all_handler and a CloseHandler
        notifier.add_handlers(path, all_handler, CloseHandler())

        # remove only the all_handler
        notifier.remove_handlers(path, all_handler)

        # clear all handlers on the watch
        notifier.clear_handlers(path)

Note in the above example that the Notifier.add_watches and Notifier.remove_handlers method can take any number of EventHandlers to add or remove. Also, duplicate handlers for a watch are not possible, and removing a handler that isn’t on a watch will do nothing:

async def main():
    with pynotify.Notifier() as notifier:
        path = pathlib.Path.cwd()
        open_handler = OpenHandler()

        notifier.add_watch(path, open_handler)

        # does nothing, since open_handler already on the watch!
        notifier.add_handlers(path, open_handlers)

        notifier.remove_handlers(path, open_handler)  # no more handlers

        # does nothing, since open_handler isn't on the watch
        notifier.remove_handlers(path, open_handler)

FAQ

To be filled as questions arise…

License

The UNLICENSE. See https://www.unlicense.org for more info.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynotify_mcriley821-0.0.2.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pynotify_mcriley821-0.0.2-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file pynotify_mcriley821-0.0.2.tar.gz.

File metadata

  • Download URL: pynotify_mcriley821-0.0.2.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pynotify_mcriley821-0.0.2.tar.gz
Algorithm Hash digest
SHA256 4ab6df2667f2f98f2d88d7fa88b0892000756bcc7c4f84a92ef857cf45e8c087
MD5 1f6328e47659a21c1de3e4689fb720e9
BLAKE2b-256 fa9606054ba992e54bd5a4318618d7b96a169abc73eb9ebff3fd66968bf187c1

See more details on using hashes here.

File details

Details for the file pynotify_mcriley821-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pynotify_mcriley821-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 52efbfe3449bb76157456873dfedf43faf09c6c0a5ce6b523c1b752733f67503
MD5 eebfc6aa45e4b9d5d084c588fb92b661
BLAKE2b-256 796b1618e9c46323e4cabfbb51b42c5f3ebbebd6b75246a62485fe81dae103a0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page