Update FSF address.
[libguestfs.git] / python / t / 400-events.py
1 # libguestfs Python bindings
2 # Copyright (C) 2011 Red Hat Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 import os
19 import guestfs
20
21 g = guestfs.GuestFS()
22
23 def log_callback (ev,eh,buf,array):
24     if ev == guestfs.EVENT_APPLIANCE:
25         buf = buf.rstrip()
26
27     # Log what happened.
28     print ("python event logged: event=0x%x eh=%d buf='%s' array=%s" %
29            (ev, eh, buf, array))
30
31 close_invoked = 0
32
33 def close_callback (ev, eh, buf, array):
34     global close_invoked
35     close_invoked += 1
36     log_callback (ev, eh, buf, array)
37
38 # Register an event callback for all log messages.
39 events = guestfs.EVENT_APPLIANCE | guestfs.EVENT_LIBRARY | guestfs.EVENT_TRACE
40 g.set_event_callback (log_callback, events)
41
42 # Register a callback for the close event.
43 g.set_event_callback (close_callback, guestfs.EVENT_CLOSE)
44
45 # Now make sure we see some messages.
46 g.set_trace (1)
47 g.set_verbose (1)
48
49 # Do some stuff.
50 g.add_drive_ro ("/dev/null")
51 g.set_autosync (1)
52
53 # Close the handle.  The close callback should be invoked.
54 if close_invoked != 0:
55     raise "Error: close_invoked should be 0"
56 del g
57 if close_invoked != 1:
58     raise "Error: close_invoked should be 1"