root / lib / asyncnotifier.py @ a2c965ea
History | View | Annotate | Download (2.1 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2009 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Asynchronous pyinotify implementation"""
|
23 |
|
24 |
|
25 |
import asyncore |
26 |
|
27 |
try:
|
28 |
# pylint: disable-msg=E0611
|
29 |
from pyinotify import pyinotify |
30 |
except ImportError: |
31 |
import pyinotify |
32 |
|
33 |
|
34 |
# We contributed the AsyncNotifier class back to python-pyinotify, and it's
|
35 |
# part of their codebase since version 0.8.7. This code can be removed once
|
36 |
# we'll be ready to depend on python-pyinotify >= 0.8.7
|
37 |
class AsyncNotifier(asyncore.file_dispatcher): |
38 |
"""An asyncore dispatcher for inotify events.
|
39 |
|
40 |
"""
|
41 |
# pylint: disable-msg=W0622,W0212
|
42 |
def __init__(self, watch_manager, default_proc_fun=None, map=None): |
43 |
"""Initializes this class.
|
44 |
|
45 |
This is a a special asyncore file_dispatcher that actually wraps a
|
46 |
pyinotify Notifier, making it asyncronous.
|
47 |
|
48 |
"""
|
49 |
if default_proc_fun is None: |
50 |
default_proc_fun = pyinotify.ProcessEvent() |
51 |
|
52 |
self.notifier = pyinotify.Notifier(watch_manager, default_proc_fun)
|
53 |
|
54 |
# here we need to steal the file descriptor from the notifier, so we can
|
55 |
# use it in the global asyncore select, and avoid calling the
|
56 |
# check_events() function of the notifier (which doesn't allow us to select
|
57 |
# together with other file descriptors)
|
58 |
self.fd = self.notifier._fd |
59 |
asyncore.file_dispatcher.__init__(self, self.fd, map) |
60 |
|
61 |
def handle_read(self): |
62 |
self.notifier.read_events()
|
63 |
self.notifier.process_events()
|