• Stars
    star
    116
  • Rank 302,163 (Top 6 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created over 8 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple Python wrapper around inotify. No fancy bells and whistles, just a literal wrapper with ctypes.

inotify_simple 1.3

inotify_simple is a simple Python wrapper around inotify. No fancy bells and whistles, just a literal wrapper with ctypes. Only ~100 lines of code!

inotify_init1() is wrapped as a file-like object, INotify(), holding the inotify file descriptor. INotify().read() reads available data from the file descriptor and returns events as namedtuple objects after unpacking them with the struct module. inotify_add_watch() and inotify_rm_watch() are wrapped with no changes at all, taking and returning watch descriptor integers that calling code is expected to keep track of itself, just as one would use inotify from C. Works with Python 2.7 and Python >= 3.2.

View on PyPI | Fork me on github | Read the docs

Project status: stable

This project does not require much maintenance to continue to to meet its goals. Do not panic if there have been no commits in a long time, this is normal.

Installation

to install inotify_simple, run:

$ pip3 install inotify_simple

or to install from source:

$ python3 setup.py install

Note: If on Python < 3.4, you'll need the backported enum34 module.

Introduction

There are many inotify python wrappers out there. I found them all unsatisfactory. Most are far too high-level for my tastes, and the supposed convenience they provide actually limits one from using inotify in ways other than those the author imagined. Others are C extensions, requiring compilation for different platforms and Python versions, rather than a pure python module using ctypes. This one is pretty low-level and really just does what inotify itself does and nothing more. So hopefully if I've written it right, it will remain functional well into the future with no changes, recompilation or attention on my part.

Example usage

import os
from inotify_simple import INotify, flags

os.mkdir('/tmp/inotify_test')

inotify = INotify()
watch_flags = flags.CREATE | flags.DELETE | flags.MODIFY | flags.DELETE_SELF
wd = inotify.add_watch('/tmp/inotify_test', watch_flags)

# Now create, delete and modify some files in the directory being monitored:
os.chdir('/tmp/inotify_test')
# CREATE event for a directory:
os.system('mkdir foo')
# CREATE event for a file:
os.system('echo hello > test.txt')
# MODIFY event for the file:
os.system('echo world >> test.txt')
# DELETE event for the file
os.system('rm test.txt')
# DELETE event for the directory
os.system('rmdir foo')
os.chdir('/tmp')
# DELETE_SELF on the original directory. # Also generates an IGNORED event
# indicating the watch was removed.
os.system('rmdir inotify_test')

# And see the corresponding events:
for event in inotify.read():
    print(event)
    for flag in flags.from_mask(event.mask):
        print('    ' + str(flag))

This outputs the following:

Event(wd=1, mask=1073742080, cookie=0, name=u'foo')
    flags.CREATE
    flags.ISDIR
Event(wd=1, mask=256, cookie=0, name=u'test.txt')
    flags.CREATE
Event(wd=1, mask=2, cookie=0, name=u'test.txt')
    flags.MODIFY
Event(wd=1, mask=512, cookie=0, name=u'test.txt')
    flags.DELETE
Event(wd=1, mask=1073742336, cookie=0, name=u'foo')
    flags.DELETE
    flags.ISDIR
Event(wd=1, mask=1024, cookie=0, name=u'')
    flags.DELETE_SELF
Event(wd=1, mask=32768, cookie=0, name=u'')
    flags.IGNORED

Note that the flags, since they are defined with an enum.IntEnum, print as what they are called rather than their integer values. However they are still just integers and so can be bitwise-ANDed and ORed etc with masks etc. The flags.from_mask() method bitwise-ANDs a mask with all possible flags and returns a list of matches. This is for convenience and useful for debugging which events are coming through, but performance critical code should generally bitwise-AND masks with flags of interest itself so as to not do unnecessary checks.

See here for more.

More Repositories

1

gil_load

Utility for measuring the fraction of time the CPython GIL is held
Cython
104
star
2

chrisjbillington.github.io

My personal website on github pages (WIP)
Python
56
star
3

git-nautilus-icons

A nautilus Python extension to overlay icons on files in git repositories
Python
39
star
4

setuptools-conda

Build a conda package from a setuptools project
Python
30
star
5

hg-export-tool

A tool to convert mercurial repositories to git ones locally, working around some deficiencies in github's importer and in `hg-fast-export`
Python
20
star
6

starship_telemetry

Starship telemetry data extraction, analysis and plots
Python
9
star
7

fugue-2x-icons

The Fugue icon set, upscaled to 32x32.
Python
6
star
8

DElauncher4Kodi

A launcher for Kodi to make media keys and system volume behave better when running Kodi from a desktop environment such as gnome-shell
Python
6
star
9

desktop-app

OS menu shortcuts, correct taskbar behaviour, and environment activation for Python GUI apps
Python
5
star
10

interminal

Utility to run a command in a graphical terminal emulator
Python
4
star
11

tubes

A colour-coded appindicator to show whether your internet is working
Python
2
star
12

zprocess

A collection of utilities for multiprocessing using zeromq.
Python
2
star
13

spinapi

Python
1
star
14

arch_install

The script I made to install Arch Linux. Already out of date given removal of the base group, but a starting point for next time.
Python
1
star
15

lansound

Pulseaudio sink and server program to stream audio with low latency across a LAN
Python
1
star
16

alkali

Atomic physics calculations and constants for hydrogen-like atoms
Python
1
star