Code Monkey home page Code Monkey logo

asyncinotify's Introduction

asyncinotify

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.

asyncinotify's People

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.