Changeset 3437 for pkipplib/trunk/bin/pksubscribe
- Timestamp:
- 10/06/08 00:24:42 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pkipplib/trunk/bin/pksubscribe
r45 r3437 1 1 #! /usr/bin/env python 2 # -*- coding: UTF-8 -*-2 # -*- coding: utf-8 -*- 3 3 # 4 4 # pkipplib : IPP and CUPS support for Python … … 9 9 # the Free Software Foundation, either version 3 of the License, or 10 10 # (at your option) any later version. 11 # 11 # 12 12 # This program is distributed in the hope that it will be useful, 13 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 15 # GNU General Public License for more details. 16 # 16 # 17 17 # You should have received a copy of the GNU General Public License 18 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 28 28 import optparse 29 29 30 from pkipplib import pkipplib 31 32 if __name__ == "__main__" : 30 from pkipplib import pkipplib 31 32 if __name__ == "__main__" : 33 33 try : 34 34 locale.setlocale(locale.LC_ALL, "") … … 40 40 gettext.NullTranslations().install() 41 41 parser = optparse.OptionParser(usage="pksubscribe [options] [subscriptions ids]") 42 parser.add_option("-v", "--version", 43 action="store_true", 42 parser.add_option("-v", "--version", 43 action="store_true", 44 44 dest="version", 45 45 help=_("show pksubscribe's version number and exit.")) 46 parser.add_option("-c", "--cups", 47 default="http://localhost:631", 46 parser.add_option("-c", "--cups", 47 default="http://localhost:631", 48 48 dest="cups", 49 49 help=_("the CUPS server to connect to. Defaults to http://localhost:631")) 50 parser.add_option("-d", "--debug", 51 action="store_true", 50 parser.add_option("-d", "--debug", 51 action="store_true", 52 52 dest="debug", 53 53 help=_("activate debug mode.")) 54 parser.add_option("-X", "--delete", 55 action="store_true", 54 parser.add_option("-X", "--delete", 55 action="store_true", 56 56 dest="delete", 57 57 help=_("deletes subscriptions.")) 58 parser.add_option("-p", "--printer", 58 parser.add_option("-p", "--printer", 59 59 dest="printer", 60 60 help=_("the printer's name for a printer subscription.")) 61 parser.add_option("-j", "--job", 62 type="int", 61 parser.add_option("-j", "--job", 62 type="int", 63 63 dest="job", 64 64 help=_("the job's id for a job subscripition.")) 65 parser.add_option("-r", "--recipient", 65 parser.add_option("-r", "--recipient", 66 66 dest="recipient", 67 67 help=_("the recipient's uri.")) 68 parser.add_option("-C", "--charset", 68 parser.add_option("-C", "--charset", 69 69 dest="charset", 70 70 help=_("the charset to use in notifications sent for this subscription.")) 71 parser.add_option("-L", "--language", 71 parser.add_option("-L", "--language", 72 72 dest="language", 73 73 help=_("the language to use in notifications sent for this subscription.")) 74 parser.add_option("-u", "--userdata", 74 parser.add_option("-u", "--userdata", 75 75 dest="userdata", 76 76 help=_("the user's data to use in notifications for this subscription.")) 77 parser.add_option("-U", "--username", 77 parser.add_option("-U", "--username", 78 78 dest="username", 79 79 help=_("the user's name to use when connecting to the CUPS server.")) 80 parser.add_option("-W", "--password", 80 parser.add_option("-W", "--password", 81 81 dest="password", 82 82 help=_("the user's password to use when connecting to the CUPS server.")) 83 parser.add_option("-E", "--events", 83 parser.add_option("-E", "--events", 84 84 dest="events", 85 85 help=_("a comma separated list of events to subscribe to.")) 86 parser.add_option("-P", "--pullmethod", 86 parser.add_option("-P", "--pullmethod", 87 87 dest="pullmethod", 88 88 help=_("the optional pull method's name.")) … … 91 91 dest="duration", 92 92 help=_("the duration of the subscription.")) 93 parser.add_option("-I", "--interval", 93 parser.add_option("-I", "--interval", 94 94 type="int", 95 95 dest="interval", 96 96 help=_("the time interval of the subscription.")) 97 97 98 98 (options, arguments) = parser.parse_args() 99 99 if options.version : … … 102 102 if not options.events and not options.delete : 103 103 sys.stderr.write(_("You MUST pass a list of events to subscribe to.\n")) 104 elif not options.recipient and not options.delete : 104 elif not options.recipient and not options.delete : 105 105 sys.stderr.write(_("You MUST pass a recipient for the subscription.\n")) 106 elif options.delete and not arguments : 106 elif options.delete and not arguments : 107 107 sys.stderr.write(_("You MUST pass a subscriptions ids at the end of your command line.\n")) 108 else : 108 else : 109 109 cups = pkipplib.CUPS(options.cups, 110 110 options.username, … … 112 112 debug=options.debug) 113 113 baseurl = options.cups.replace("http://", "ipp://") 114 if baseurl.endswith(":631") : 114 if baseurl.endswith(":631") : 115 115 baseurl = baseurl[:-4] 116 116 if options.printer : 117 117 url = "%s/printers/%s" % (baseurl, options.printer) 118 elif options.job : 118 elif options.job : 119 119 url = "%s/jobs/%i" % (baseurl, options.job) 120 else : 120 else : 121 121 url = baseurl 122 if not options.delete : 122 if not options.delete : 123 123 answer = cups.createSubscription(url, 124 124 [e.strip() for e in options.events.split(",")], … … 131 131 timeinterval=options.interval, 132 132 jobid=options.job) 133 try : 133 try : 134 134 subscriptionid = answer.subscription["notify-subscription-id"][0][1] 135 135 except KeyError : 136 136 sys.stderr.write("%s\n" % answer.operation["status-message"][0][1]) 137 else : 137 else : 138 138 print _("Subscription %i registered.") % subscriptionid 139 else : 139 else : 140 140 for subid in [int(arg) for arg in arguments] : 141 141 answer = cups.cancelSubscription(url, subid, options.job) … … 143 143 try : 144 144 error = answer.operation["status-message"][0][1] 145 except KeyError : 145 except KeyError : 146 146 print _("Subscription %i cancelled.") % subid 147 147 else : 148 148 sys.stderr.write("%s\n" % error) 149 else : 149 else : 150 150 sys.stderr.write(_("Incorrect answer (None)\n")) 151 151