[35] | 1 | #! /usr/bin/env python |
---|
[3437] | 2 | # -*- coding: utf-8 -*- |
---|
[35] | 3 | # |
---|
[40] | 4 | # pkipplib : IPP and CUPS support for Python |
---|
| 5 | # |
---|
[45] | 6 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
[40] | 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. |
---|
[3437] | 11 | # |
---|
[40] | 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. |
---|
[3437] | 16 | # |
---|
[40] | 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 | # |
---|
[35] | 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() |
---|
[3437] | 40 | fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, |
---|
[35] | 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()) |
---|
[3437] | 45 | |
---|
[35] | 46 | # now we parse it |
---|
| 47 | notification.parse() |
---|
[3437] | 48 | |
---|
[35] | 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" |
---|
[3437] | 55 | else : |
---|
[35] | 56 | action = "delete" |
---|
| 57 | os.system('/usr/bin/pkprinters --%s "%s"' % (action, printername)) |
---|