~ubuntu-branches/ubuntu/trusty/pyinotify/trusty-proposed

« back to all changes in this revision

Viewing changes to src/examples/close.py

  • Committer: Bazaar Package Importer
  • Author(s): Hans Ulrich Niedermann
  • Date: 2006-04-02 02:35:31 UTC
  • Revision ID: james.westby@ubuntu.com-20060402023531-3fj27jbd4x9s4752
Tags: upstream-0.5.2
Import upstream version 0.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: iso-8859-1 -*-
 
3
#
 
4
# close.py - process several events with the same method
 
5
# Copyright (C) 2006  S�bastien Martini <sebastien.martini@gmail.com>
 
6
#
 
7
# This program is free software; you can redistribute it and/or
 
8
# modify it under the terms of the GNU General Public License
 
9
# as published by the Free Software Foundation.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU 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., 59 Temple Place - Suite 330, Boston, MA
 
19
# 02111-1307, USA.
 
20
 
 
21
# usage: ./close.py '/path-to-watch'
 
22
 
 
23
import os
 
24
 
 
25
# please read src/examples/README
 
26
try:
 
27
    # import local build
 
28
    import autopath
 
29
    from src.pyinotify.pyinotify import SimpleINotify, ProcessEvent, \
 
30
         EventsCodes
 
31
except ImportError:
 
32
    # import global (installed) pyinotify
 
33
    from pyinotify import SimpleINotify, ProcessEvent, EventsCodes
 
34
 
 
35
 
 
36
 
 
37
class PClose(ProcessEvent):
 
38
    def process_IN_CLOSE(self, event_k):
 
39
        """
 
40
        process 'IN_CLOSE_*' events
 
41
        """
 
42
        if event_k.name:
 
43
            f = "%s" % os.path.join(event_k.path, event_k.name)
 
44
        else:
 
45
            f = "%s" % event_k.path
 
46
        print "%s: closed" % f
 
47
 
 
48
 
 
49
 
 
50
if __name__ == '__main__':
 
51
    #
 
52
    # - watch for IN_CLOSE_* events and process them with
 
53
    #   the same processing method.
 
54
    # - The watched path is '/tmp' (by default) or the first
 
55
    #   command line argument if given.
 
56
    #
 
57
    import sys
 
58
 
 
59
    path = '/tmp' # default watched path
 
60
    if sys.argv[1:]:
 
61
        path = sys.argv[1]
 
62
 
 
63
    # only watch those events
 
64
    mask = EventsCodes.IN_CLOSE_WRITE | EventsCodes.IN_CLOSE_NOWRITE
 
65
 
 
66
    # class instance and init
 
67
    ino = SimpleINotify()
 
68
 
 
69
    print 'start monitoring %s with mask %d' % (path, mask)
 
70
 
 
71
    added_flag = False
 
72
    # read and process events
 
73
    while True:
 
74
        try:
 
75
            if not added_flag:
 
76
                # on first iteration, add a watch on path:
 
77
                # watch path for events handled by mask and give an
 
78
                # instance of PClose as processing function.
 
79
                ino.add_watch(path, mask, PClose())
 
80
                added_flag = True
 
81
            ino.process_events()
 
82
            if ino.event_check():
 
83
                ino.read_events()
 
84
        except KeyboardInterrupt:
 
85
            # ...until c^c signal
 
86
            print 'stop monitoring...'
 
87
            # close inotify's instance
 
88
            ino.close()
 
89
            break
 
90
        except Exception, err:
 
91
            # otherwise keep on watching
 
92
            print err
 
93