Statistics
| Branch: | Tag: | Revision:

root / lib / asyncnotifier.py @ a8a76bc2

History | View | Annotate | Download (1.8 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 a8a76bc2 Guido Trotter
  def __init__(self, watch_manager,
35 a8a76bc2 Guido Trotter
               default_proc_fun=None,
36 a8a76bc2 Guido Trotter
               map=None):
37 a8a76bc2 Guido Trotter
    """
38 a8a76bc2 Guido Trotter
    Constructor for AsyncNotifier, a special asyncore file_dispatcher that
39 a8a76bc2 Guido Trotter
    actually wraps a pyinotify Notifier, making it asyncronous.
40 a8a76bc2 Guido Trotter

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