Skip to main content

A simple optionally-async python inotify library, focused on simplicity of use and operation, and leveraging modern Python features

Project description

An async python inotify package. Kept as simple and easy-to-understand as possible, while still being flexible and powerful. This is built on no external dependencies, and works through ctypes in a very obvious fashion.

This depends on Python 3.6+ features, and will not work with prior versions.

This works without any other external dependencies.

The code is available on GitHub and the documentation is available on ReadTheDocs. The package itself is available on PyPi.

Installation

You know the drill:

pip install asyncinotify

Usage

The core of this package is asyncinotify.Inotify. Most important Classes may be imported directly from the asyncinotify package.

from pathlib import Path
from asyncinotify import Inotify, Mask
import asyncio

async def main():
    # Context manager to close the inotify handle after use
    with Inotify() as inotify:
        # Adding the watch can also be done outside of the context manager.
        # __enter__ doesn't actually do anything except return self.
        # This returns an asyncinotify.inotify.Watch instance
        inotify.add_watch('/tmp', Mask.ACCESS | Mask.MODIFY | Mask.OPEN | Mask.CREATE | Mask.DELETE | Mask.ATTRIB | Mask.CLOSE | Mask.MOVE | Mask.ONLYDIR)
        # Iterate events forever, yielding them one at a time
        async for event in inotify:
            # Events have a helpful __repr__.  They also have a reference to
            # their Watch instance.
            print(event)

            # the contained path may or may not be valid UTF-8.  See the note
            # below
            print(repr(event.path))

asyncio.run(main())

This will asynchronously watch the /tmp directory and report events it encounters.

This library also supports synchronous operation, using the asyncinotify.inotify.Inotify.sync_get` method, or simply using synchronous iteration.

Motivation

There are a few different python inotify packages. Most of them either have odd conventions, expose too much of the underlying C API in a way that I personally don’t like, are badly documented, they work with paths in a non-idiomatic way, are not asynchronous, or are overengineered compared to the API they are wrapping. I find that the last one is true for the majority of them.

I encourage everybody to read the sources of this package. They are quite simple and easy to understand.

This library

  • Works in a very simple way. It does not have add-ons or extra features beyond presenting a very Python interface to the raw inotify functionality.

  • Grabs events in bulk and caches them for minor performance gains.

  • Leverages IntFlag for all masks and flags, allowing the user to use the features of IntFlag, such as seeing individual applied flags in the repr, checking for flag set bits with in.

  • Exposes all paths via python’s pathlib

  • Exposes all the functionality of inotify without depending on the user having to interact with any of the underlying mechanics of Inotify. You should never have to touch the inotify or watch descriptors for any reason.

The primary motivation is that this is written to be a Python inotify module that I would feel comfortable using.

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

asyncinotify-4.2.0.tar.gz (27.6 kB view details)

Uploaded Source

Built Distribution

asyncinotify-4.2.0-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file asyncinotify-4.2.0.tar.gz.

File metadata

  • Download URL: asyncinotify-4.2.0.tar.gz
  • Upload date:
  • Size: 27.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for asyncinotify-4.2.0.tar.gz
Algorithm Hash digest
SHA256 dac1d75e16a4919c6eab84a90ff51218db622c5524a84a5c501a0b62ea7ec7ea
MD5 0f7a6b3557e8846cfbeb600689f63fea
BLAKE2b-256 ab3465ef5a24fc286a1cb9b1293873e0d8d3d6a2048886820fcae062f0b3a7da

See more details on using hashes here.

File details

Details for the file asyncinotify-4.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncinotify-4.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 23cbcb0704cc65a2009d5ddc5a70dc5be6560708d8a684bba82e03e384c6295f
MD5 83e73cd20ead40301d8a9b9569655999
BLAKE2b-256 f005cddd66212c7b6e5e4d010f061f21081a5a76639ac49c29d18458f3b5fb55

See more details on using hashes here.

Supported by

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