From 623ebb2d3570795c3d405a8c293a8cad64d9b708 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 11 Dec 2014 01:58:15 +0100 Subject: add inotify MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/plugins/inotify.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/plugins/inotify.py (limited to 'src') diff --git a/src/plugins/inotify.py b/src/plugins/inotify.py new file mode 100644 index 0000000..5cf2d73 --- /dev/null +++ b/src/plugins/inotify.py @@ -0,0 +1,59 @@ +# -*- python -*- +''' +xpybar – xmobar replacement written in python +Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +''' + +import os + +from util import * + +from plugins.linereader import LineReader + + + +class Inotify: + ''' + File access monitor + ''' + + def __init__(self, callback, *arguments, events = None): + ''' + Constructor + + @param callback:(str)→void Function that will be called everytime a line is read from + `inotifywait`'s standard output, the parameter will be that line, + note that this will not be asynchronously, you may want to specify + something like `lambda : async(fun)` for that, and use semaphores + in `fun`. + @param arguments:*str The files and directories you want to watch and any addition + argument you want to pass to `inotifywait` + @param event:itr? Event you want reported, see "EVENTS" under `inotifywait`'s man page + ''' + command = ['inotifywait', '-m'] + if events is not None: + if isinstance(events, str): + events = [events] + for event in events: + command.append('-e') + command.append(event) + command += list(arguments) + def start(): + with LineReader(spawn(*command)) as reader: + while True: + callback(reader.next()) + async(start) + -- cgit v1.2.3-70-g09d2