Statistics
| Branch: | Tag: | Revision:

root / lib / asyncnotifier.py @ 39292d3a

History | View | Annotate | Download (1.9 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 asyncore
26 a8a76bc2 Guido Trotter
27 ad54f3d2 Guido Trotter
try:
28 7260cfbe Iustin Pop
  # pylint: disable-msg=E0611
29 6af8a903 Iustin Pop
  from pyinotify import pyinotify
30 ad54f3d2 Guido Trotter
except ImportError:
31 6af8a903 Iustin Pop
  import pyinotify
32 ad54f3d2 Guido Trotter
33 a8a76bc2 Guido Trotter
34 a8a76bc2 Guido Trotter
class AsyncNotifier(asyncore.file_dispatcher):
35 a8a76bc2 Guido Trotter
  """An asyncore dispatcher for inotify events.
36 a8a76bc2 Guido Trotter

37 a8a76bc2 Guido Trotter
  """
38 7260cfbe Iustin Pop
  # pylint: disable-msg=W0622,W0212
39 69b99987 Michael Hanselmann
  def __init__(self, watch_manager, default_proc_fun=None, map=None):
40 69b99987 Michael Hanselmann
    """Initializes this class.
41 69b99987 Michael Hanselmann

42 69b99987 Michael Hanselmann
    This is a a special asyncore file_dispatcher that actually wraps a
43 69b99987 Michael Hanselmann
    pyinotify Notifier, making it asyncronous.
44 a8a76bc2 Guido Trotter

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