1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # pkipplib : IPP and CUPS support for Python |
---|
5 | # |
---|
6 | # (c) 2003, 2004, 2005, 2006 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 2 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, write to the Free Software |
---|
19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | # |
---|
21 | # $Id$ |
---|
22 | |
---|
23 | """cupssubscribe is a command line tool which can be used to create |
---|
24 | or delete IPP subscriptions.""" |
---|
25 | |
---|
26 | import sys |
---|
27 | import locale |
---|
28 | import gettext |
---|
29 | import optparse |
---|
30 | |
---|
31 | from pkipplib import pkipplib |
---|
32 | |
---|
33 | if __name__ == "__main__" : |
---|
34 | try : |
---|
35 | locale.setlocale(locale.LC_ALL, "") |
---|
36 | except (locale.Error, IOError) : |
---|
37 | sys.stderr.write("Problem while setting locale.\n") |
---|
38 | try : |
---|
39 | gettext.install("cupssubscribe") |
---|
40 | except : |
---|
41 | gettext.NullTranslations().install() |
---|
42 | parser = optparse.OptionParser(usage="cupssubscribe [options] [subscriptions ids]") |
---|
43 | parser.add_option("-v", "--version", |
---|
44 | action="store_true", |
---|
45 | dest="version", |
---|
46 | help=_("show cupssubscribe's version number and exit.")) |
---|
47 | parser.add_option("-c", "--cups", |
---|
48 | default="http://localhost:631", |
---|
49 | dest="cups", |
---|
50 | help=_("the CUPS server to connect to. Defaults to http://localhost:631")) |
---|
51 | parser.add_option("-d", "--debug", |
---|
52 | action="store_true", |
---|
53 | dest="debug", |
---|
54 | help=_("activate debug mode.")) |
---|
55 | parser.add_option("-X", "--delete", |
---|
56 | action="store_true", |
---|
57 | dest="delete", |
---|
58 | help=_("deletes subscriptions.")) |
---|
59 | parser.add_option("-p", "--printer", |
---|
60 | dest="printer", |
---|
61 | help=_("the printer's name for a printer subscription.")) |
---|
62 | parser.add_option("-j", "--job", |
---|
63 | type="int", |
---|
64 | dest="job", |
---|
65 | help=_("the job's id for a job subscripition.")) |
---|
66 | parser.add_option("-r", "--recipient", |
---|
67 | dest="recipient", |
---|
68 | help=_("the recipient's uri.")) |
---|
69 | parser.add_option("-C", "--charset", |
---|
70 | dest="charset", |
---|
71 | help=_("the charset to use in notifications sent for this subscription.")) |
---|
72 | parser.add_option("-L", "--language", |
---|
73 | dest="language", |
---|
74 | help=_("the language to use in notifications sent for this subscription.")) |
---|
75 | parser.add_option("-u", "--userdata", |
---|
76 | dest="userdata", |
---|
77 | help=_("the user's data to use in notifications for this subscription.")) |
---|
78 | parser.add_option("-U", "--username", |
---|
79 | dest="username", |
---|
80 | help=_("the user's name to use when connecting to the CUPS server.")) |
---|
81 | parser.add_option("-W", "--password", |
---|
82 | dest="password", |
---|
83 | help=_("the user's password to use when connecting to the CUPS server.")) |
---|
84 | parser.add_option("-E", "--events", |
---|
85 | dest="events", |
---|
86 | help=_("a comma separated list of events to subscribe to.")) |
---|
87 | parser.add_option("-P", "--pullmethod", |
---|
88 | dest="pullmethod", |
---|
89 | help=_("the optional pull method's name.")) |
---|
90 | parser.add_option("-D", "--duration", |
---|
91 | type="int", |
---|
92 | dest="duration", |
---|
93 | help=_("the duration of the subscription.")) |
---|
94 | parser.add_option("-I", "--interval", |
---|
95 | type="int", |
---|
96 | dest="interval", |
---|
97 | help=_("the time interval of the subscription.")) |
---|
98 | |
---|
99 | (options, arguments) = parser.parse_args() |
---|
100 | if options.version : |
---|
101 | print "cupssubscribe v%(__version__)s" % globals() |
---|
102 | else : |
---|
103 | if not options.events and not options.delete : |
---|
104 | sys.stderr.write(_("You MUST pass a list of events to subscribe to.\n")) |
---|
105 | elif not options.recipient and not options.delete : |
---|
106 | sys.stderr.write(_("You MUST pass a recipient for the subscription.\n")) |
---|
107 | elif options.delete and not arguments : |
---|
108 | sys.stderr.write(_("You MUST pass a subscriptions ids at the end of your command line.\n")) |
---|
109 | else : |
---|
110 | cups = pkipplib.CUPS(options.cups, |
---|
111 | options.username, |
---|
112 | options.password, |
---|
113 | debug=options.debug) |
---|
114 | baseurl = options.cups.replace("http://", "ipp://") |
---|
115 | if baseurl.endswith(":631") : |
---|
116 | baseurl = baseurl[:-4] |
---|
117 | if options.printer : |
---|
118 | url = "%s/printers/%s" % (baseurl, options.printer) |
---|
119 | elif options.job : |
---|
120 | url = "%s/jobs/%i" % (baseurl, options.job) |
---|
121 | else : |
---|
122 | url = baseurl |
---|
123 | if not options.delete : |
---|
124 | answer = cups.createSubscription(url, |
---|
125 | [e.strip() for e in options.events.split(",")], |
---|
126 | userdata=options.userdata, |
---|
127 | recipient=options.recipient, |
---|
128 | pullmethod=options.pullmethod, |
---|
129 | charset=options.charset, |
---|
130 | naturallanguage=options.language, |
---|
131 | leaseduration=options.duration, |
---|
132 | timeinterval=options.interval, |
---|
133 | jobid=options.job) |
---|
134 | try : |
---|
135 | subscriptionid = answer.subscription["notify-subscription-id"][0][1] |
---|
136 | except KeyError : |
---|
137 | sys.stderr.write("%s\n" % answer.operation["status-message"][0][1]) |
---|
138 | else : |
---|
139 | print "Subscription %i registered." % subscriptionid |
---|
140 | else : |
---|
141 | for subid in [int(arg) for arg in arguments] : |
---|
142 | answer = cups.cancelSubscription(url, subid, options.job) |
---|
143 | try : |
---|
144 | error = answer.operation["status-message"][0][1] |
---|
145 | except KeyError : |
---|
146 | print "Subscription %i cancelled." % subid |
---|
147 | else : |
---|
148 | sys.stderr.write("%s\n" % error) |
---|