root / pkipplib / trunk / bin / pksubscribe @ 3476

Revision 3476, 7.1 kB (checked in by jerome, 15 years ago)

Changed copyright years.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[29]1#! /usr/bin/env python
[3437]2# -*- coding: utf-8 -*-
[29]3#
4# pkipplib : IPP and CUPS support for Python
5#
[3476]6# (c) 2003-2009 Jerome Alet <alet@librelogiciel.com>
[40]7# This program is free software: you can redistribute it and/or modify
[29]8# it under the terms of the GNU General Public License as published by
[40]9# the Free Software Foundation, either version 3 of the License, or
[29]10# (at your option) any later version.
[3437]11#
[29]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#
[29]17# You should have received a copy of the GNU General Public License
[40]18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[29]19#
20# $Id$
21
[32]22"""pksubscribe is a command line tool which can be used to create
[30]23or delete IPP subscriptions."""
24
[29]25import sys
26import locale
27import gettext
28import optparse
29
[3437]30from pkipplib import pkipplib
31
32if __name__ == "__main__" :
[29]33    try :
34        locale.setlocale(locale.LC_ALL, "")
35    except (locale.Error, IOError) :
36        sys.stderr.write("Problem while setting locale.\n")
37    try :
[32]38        gettext.install("pksubscribe")
[29]39    except :
40        gettext.NullTranslations().install()
[32]41    parser = optparse.OptionParser(usage="pksubscribe [options] [subscriptions ids]")
[3437]42    parser.add_option("-v", "--version",
43                            action="store_true",
[29]44                            dest="version",
[32]45                            help=_("show pksubscribe's version number and exit."))
[3437]46    parser.add_option("-c", "--cups",
47                            default="http://localhost:631",
[29]48                            dest="cups",
49                            help=_("the CUPS server to connect to. Defaults to http://localhost:631"))
[3437]50    parser.add_option("-d", "--debug",
51                            action="store_true",
[29]52                            dest="debug",
53                            help=_("activate debug mode."))
[3437]54    parser.add_option("-X", "--delete",
55                            action="store_true",
[30]56                            dest="delete",
57                            help=_("deletes subscriptions."))
[3437]58    parser.add_option("-p", "--printer",
[29]59                            dest="printer",
60                            help=_("the printer's name for a printer subscription."))
[3437]61    parser.add_option("-j", "--job",
62                            type="int",
[29]63                            dest="job",
64                            help=_("the job's id for a job subscripition."))
[3437]65    parser.add_option("-r", "--recipient",
[29]66                            dest="recipient",
67                            help=_("the recipient's uri."))
[3437]68    parser.add_option("-C", "--charset",
[29]69                            dest="charset",
70                            help=_("the charset to use in notifications sent for this subscription."))
[3437]71    parser.add_option("-L", "--language",
[29]72                            dest="language",
73                            help=_("the language to use in notifications sent for this subscription."))
[3437]74    parser.add_option("-u", "--userdata",
[29]75                            dest="userdata",
76                            help=_("the user's data to use in notifications for this subscription."))
[3437]77    parser.add_option("-U", "--username",
[29]78                            dest="username",
79                            help=_("the user's name to use when connecting to the CUPS server."))
[3437]80    parser.add_option("-W", "--password",
[29]81                            dest="password",
82                            help=_("the user's password to use when connecting to the CUPS server."))
[3437]83    parser.add_option("-E", "--events",
[29]84                            dest="events",
85                            help=_("a comma separated list of events to subscribe to."))
[3437]86    parser.add_option("-P", "--pullmethod",
[29]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."))
[3437]93    parser.add_option("-I", "--interval",
[29]94                            type="int",
95                            dest="interval",
96                            help=_("the time interval of the subscription."))
[3437]97
[29]98    (options, arguments) = parser.parse_args()
99    if options.version :
[32]100        print "pksubscribe v%(__version__)s" % globals()
[29]101    else :
[30]102        if not options.events and not options.delete :
[29]103            sys.stderr.write(_("You MUST pass a list of events to subscribe to.\n"))
[3437]104        elif not options.recipient and not options.delete :
[29]105            sys.stderr.write(_("You MUST pass a recipient for the subscription.\n"))
[3437]106        elif options.delete and not arguments :
[30]107            sys.stderr.write(_("You MUST pass a subscriptions ids at the end of your command line.\n"))
[3437]108        else :
[29]109            cups = pkipplib.CUPS(options.cups,
110                                 options.username,
111                                 options.password,
112                                 debug=options.debug)
113            baseurl = options.cups.replace("http://", "ipp://")
[3437]114            if baseurl.endswith(":631") :
[29]115                baseurl = baseurl[:-4]
116            if options.printer :
117                url = "%s/printers/%s" % (baseurl, options.printer)
[3437]118            elif options.job :
[29]119                url = "%s/jobs/%i" % (baseurl, options.job)
[3437]120            else :
[29]121                url = baseurl
[3437]122            if not options.delete :
[30]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)
[3437]133                try :
[30]134                    subscriptionid = answer.subscription["notify-subscription-id"][0][1]
135                except KeyError :
136                    sys.stderr.write("%s\n" % answer.operation["status-message"][0][1])
[3437]137                else :
[44]138                    print _("Subscription %i registered.") % subscriptionid
[3437]139            else :
[30]140                for subid in [int(arg) for arg in arguments] :
141                    answer = cups.cancelSubscription(url, subid, options.job)
[44]142                    if answer :
143                        try :
144                            error = answer.operation["status-message"][0][1]
[3437]145                        except KeyError :
[44]146                            print _("Subscription %i cancelled.") % subid
147                        else :
148                            sys.stderr.write("%s\n" % error)
[3437]149                    else :
[44]150                        sys.stderr.write(_("Incorrect answer (None)\n"))
Note: See TracBrowser for help on using the browser.