1 | #! /usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # pkipplib : IPP and CUPS support for Python |
---|
5 | # |
---|
6 | # (c) 2003-2013 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 |
---|
22 | used as the notifications' recipient when creating IPP subscriptions. |
---|
23 | |
---|
24 | It will automatically ensures that each time a printer is added or |
---|
25 | removed from CUPS it is also added or removed from PyKota's database. |
---|
26 | |
---|
27 | IMPORTANT : because of a bug in CUPS 1.2.1, this command only works |
---|
28 | the first time a notification is sent.""" |
---|
29 | |
---|
30 | import sys |
---|
31 | import os |
---|
32 | import fcntl |
---|
33 | |
---|
34 | from pkipplib import pkipplib |
---|
35 | |
---|
36 | if __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)) |
---|