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