Statistics
| Branch: | Tag: | Revision:

root / lib / asyncnotifier.py @ 78411c60

History | View | Annotate | Download (1.7 kB)

1 a8a76bc2 Guido Trotter
#
2 a8a76bc2 Guido Trotter
#
3 a8a76bc2 Guido Trotter
4 a8a76bc2 Guido Trotter
# Copyright (C) 2009 Google Inc.
5 a8a76bc2 Guido Trotter
#
6 a8a76bc2 Guido Trotter
# This program is free software; you can redistribute it and/or modify
7 a8a76bc2 Guido Trotter
# it under the terms of the GNU General Public License as published by
8 a8a76bc2 Guido Trotter
# the Free Software Foundation; either version 2 of the License, or
9 a8a76bc2 Guido Trotter
# (at your option) any later version.
10 a8a76bc2 Guido Trotter
#
11 a8a76bc2 Guido Trotter
# This program is distributed in the hope that it will be useful, but
12 a8a76bc2 Guido Trotter
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8a76bc2 Guido Trotter
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8a76bc2 Guido Trotter
# General Public License for more details.
15 a8a76bc2 Guido Trotter
#
16 a8a76bc2 Guido Trotter
# You should have received a copy of the GNU General Public License
17 a8a76bc2 Guido Trotter
# along with this program; if not, write to the Free Software
18 a8a76bc2 Guido Trotter
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8a76bc2 Guido Trotter
# 02110-1301, USA.
20 a8a76bc2 Guido Trotter
21 a8a76bc2 Guido Trotter
22 a8a76bc2 Guido Trotter
"""Asynchronous pyinotify implementation"""
23 a8a76bc2 Guido Trotter
24 a8a76bc2 Guido Trotter
25 a8a76bc2 Guido Trotter
import pyinotify
26 a8a76bc2 Guido Trotter
import asyncore
27 a8a76bc2 Guido Trotter
28 a8a76bc2 Guido Trotter
29 a8a76bc2 Guido Trotter
class AsyncNotifier(asyncore.file_dispatcher):
30 a8a76bc2 Guido Trotter
  """An asyncore dispatcher for inotify events.
31 a8a76bc2 Guido Trotter

32 a8a76bc2 Guido Trotter
  """
33 a8a76bc2 Guido Trotter
34 69b99987 Michael Hanselmann
  def __init__(self, watch_manager, default_proc_fun=None, map=None):
35 69b99987 Michael Hanselmann
    """Initializes this class.
36 69b99987 Michael Hanselmann

37 69b99987 Michael Hanselmann
    This is a a special asyncore file_dispatcher that actually wraps a
38 69b99987 Michael Hanselmann
    pyinotify Notifier, making it asyncronous.
39 a8a76bc2 Guido Trotter

40 a8a76bc2 Guido Trotter
    """
41 a8a76bc2 Guido Trotter
    if default_proc_fun is None:
42 69b99987 Michael Hanselmann
      default_proc_fun = pyinotify.ProcessEvent()
43 69b99987 Michael Hanselmann
44 a8a76bc2 Guido Trotter
    self.notifier = pyinotify.Notifier(watch_manager, default_proc_fun)
45 69b99987 Michael Hanselmann
46 a8a76bc2 Guido Trotter
    # here we need to steal the file descriptor from the notifier, so we can
47 a8a76bc2 Guido Trotter
    # use it in the global asyncore select, and avoid calling the
48 a8a76bc2 Guido Trotter
    # check_events() function of the notifier (which doesn't allow us to select
49 a8a76bc2 Guido Trotter
    # together with other file descriptors)
50 a8a76bc2 Guido Trotter
    self.fd = self.notifier._fd
51 a8a76bc2 Guido Trotter
    asyncore.file_dispatcher.__init__(self, self.fd, map)
52 a8a76bc2 Guido Trotter
53 a8a76bc2 Guido Trotter
  def handle_read(self):
54 a8a76bc2 Guido Trotter
    self.notifier.read_events()
55 a8a76bc2 Guido Trotter
    self.notifier.process_events()