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