root / pkipplib / trunk / notifiers / samplenotifier @ 1357

Revision 45, 2.1 kB (checked in by jerome, 17 years ago)

Changed copyright years.
Changed default source encoding from ISO-889-15 to UTF-8.

  • Property svn:executable set to *
Line 
1#! /usr/bin/env python
2# -*- coding: UTF-8 -*-
3#
4# pkipplib : IPP and CUPS support for Python
5#
6# (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com>
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21"""This command can be placed in /usr/lib/cups/notifier and
22used as the notifications' recipient when creating IPP subscriptions.
23
24It will automatically ensures that each time a printer is added or
25removed from CUPS it is also added or removed from PyKota's database.
26
27IMPORTANT : because of a bug in CUPS 1.2.1, this command only works
28the first time a notification is sent."""
29
30import sys
31import os
32import fcntl
33
34from pkipplib import pkipplib
35
36if __name__ == "__main__" :
37    #
38    # First thing we do is put stdin in non-blocking mode.
39    fd = sys.stdin.fileno()
40    fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, 
41                    fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)
42
43    # then we read the notification CUPS sent us to our stdin
44    notification = pkipplib.IPPRequest(sys.stdin.read())
45   
46    # now we parse it
47    notification.parse()
48   
49    # then we act one way or another, depending on the event received.
50    event = notification.event_notification["notify-subscribed-event"][0][1]
51    if event in ("printer-added", "printer-deleted") :
52        printername = notification.event_notification["printer-name"][0][1]
53        if event.endswith("-added") :
54            action = "add"
55        else :   
56            action = "delete"
57        os.system('/usr/bin/pkprinters --%s "%s"' % (action, printername))
Note: See TracBrowser for help on using the browser.