[2778] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | |
---|
| 4 | # A notifier for PyKota |
---|
| 5 | # |
---|
| 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 7 | # |
---|
| 8 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
| 9 | # This program is free software; you can redistribute it and/or modify |
---|
| 10 | # it under the terms of the GNU General Public License as published by |
---|
| 11 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 12 | # (at your option) any later version. |
---|
| 13 | # |
---|
| 14 | # This program is distributed in the hope that it will be useful, |
---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | # GNU General Public License for more details. |
---|
| 18 | # |
---|
| 19 | # You should have received a copy of the GNU General Public License |
---|
| 20 | # along with this program; if not, write to the Free Software |
---|
| 21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
| 25 | # |
---|
| 26 | |
---|
| 27 | import sys |
---|
| 28 | import os |
---|
| 29 | import popen2 |
---|
[2780] | 30 | import socket |
---|
[2779] | 31 | import xmlrpclib |
---|
[2778] | 32 | |
---|
| 33 | from pykota.tool import Tool, PyKotaToolError, PyKotaCommandLineError, crashed, N_ |
---|
| 34 | |
---|
| 35 | __doc__ = N_("""pknotify v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
| 36 | |
---|
| 37 | Notifies or ask questions to end users who launched the PyKotIcon application. |
---|
| 38 | |
---|
| 39 | command line usage : |
---|
| 40 | |
---|
| 41 | pknotify [options] [arguments] |
---|
| 42 | |
---|
| 43 | options : |
---|
| 44 | |
---|
[2779] | 45 | -v | --version Prints pkbanner's version number then exits. |
---|
| 46 | -h | --help Prints this message then exits. |
---|
[2778] | 47 | |
---|
[2779] | 48 | -d | --destination h[:p] Sets the destination hostname and optional |
---|
| 49 | port onto which contact the remote PyKotIcon |
---|
| 50 | application. This option is mandatory. |
---|
| 51 | When not specified, the port defaults to 7654. |
---|
| 52 | |
---|
| 53 | -a | --ask Tells pknotify to ask something to the end |
---|
| 54 | user. Then pknotify will output the result. |
---|
| 55 | |
---|
| 56 | -c | --confirm Tells pknotify to ask for either a confirmation |
---|
| 57 | or abortion. |
---|
| 58 | |
---|
| 59 | -n | --notify Tells pknotify to send an informational message |
---|
| 60 | message to the end user. |
---|
| 61 | |
---|
| 62 | -q | --quit Tells pknotify to send a message asking the |
---|
| 63 | PyKotIcon application to exit. This option can |
---|
| 64 | be combined with the other ones to make PyKotIcon |
---|
| 65 | exit after having sent the answer from the dialog. |
---|
| 66 | |
---|
| 67 | You MUST specify either --ask, --confirm, --notify or --quit. |
---|
| 68 | |
---|
| 69 | arguments : |
---|
| 70 | |
---|
| 71 | -a | --ask : Several arguments are accepted, or the form |
---|
| 72 | "label:varname:defaultvalue". The result will |
---|
| 73 | be printed to stdout in the following format : |
---|
| 74 | VAR1NAME=VAR1VALUE |
---|
| 75 | VAR2NAME=VAR2VALUE |
---|
| 76 | ... |
---|
| 77 | If the dialog was cancelled, nothing will be |
---|
| 78 | printed. If one of the varname is 'password' |
---|
| 79 | then this field is asked as a password (you won't |
---|
| 80 | see what you type in). |
---|
| 81 | |
---|
| 82 | -c | --confirm : A single argument is expected, representing the |
---|
| 83 | message to display. If the dialog is confirmed |
---|
| 84 | then pknotify will print OK, else CANCEL. |
---|
| 85 | |
---|
| 86 | -n | --notify : A single argument is expected, representing the |
---|
| 87 | message to display. In this case pknotify will |
---|
| 88 | always print OK. |
---|
| 89 | |
---|
| 90 | examples : |
---|
| 91 | |
---|
| 92 | pknotify -d client:7654 --confirm "This job costs :\n10 credits !" |
---|
| 93 | |
---|
| 94 | would display the cost of a print job and asks for confirmation. |
---|
[2778] | 95 | """) |
---|
| 96 | |
---|
| 97 | class PyKotaNotify(Tool) : |
---|
| 98 | """A class for pknotify.""" |
---|
[2779] | 99 | def sanitizeMessage(self, msg) : |
---|
| 100 | """Replaces \\n and returns a messagee in xmlrpclib Binary format.""" |
---|
| 101 | return xmlrpclib.Binary(msg.replace("\\n", "\n")) |
---|
| 102 | |
---|
[2778] | 103 | def main(self, arguments, options) : |
---|
| 104 | """Notifies or asks questions to end users through PyKotIcon.""" |
---|
[2779] | 105 | try : |
---|
| 106 | (destination, port) = options["destination"].split(":") |
---|
| 107 | except ValueError : |
---|
| 108 | destination = options["destination"] |
---|
| 109 | port = 7654 |
---|
[2780] | 110 | try : |
---|
| 111 | server = xmlrpclib.ServerProxy("http://%s:%s" % (destination, port)) |
---|
| 112 | if options["ask"] : |
---|
| 113 | labels = [] |
---|
| 114 | varnames = [] |
---|
| 115 | varvalues = {} |
---|
| 116 | for arg in arguments : |
---|
| 117 | try : |
---|
| 118 | (label, varname, varvalue) = arg.split(":", 2) |
---|
| 119 | except ValueError : |
---|
| 120 | raise PyKotaCommandLineError, "argument '%s' is invalid !" % arg |
---|
| 121 | labels.append(self.sanitizeMessage(label)) |
---|
| 122 | varname = varname.lower() |
---|
| 123 | varnames.append(varname) |
---|
| 124 | varvalues[varname] = self.sanitizeMessage(varvalue) |
---|
| 125 | result = server.askDatas(labels, varnames, varvalues) |
---|
| 126 | if result["isValid"] : |
---|
| 127 | for varname in varnames : |
---|
| 128 | print "%s=%s" % (varname.upper(), result[varname]) |
---|
| 129 | elif options["confirm"] : |
---|
| 130 | print server.showDialog(self.sanitizeMessage(arguments[0]), True) |
---|
| 131 | elif options["notify"] : |
---|
| 132 | print server.showDialog(self.sanitizeMessage(arguments[0]), False) |
---|
| 133 | |
---|
| 134 | if options["quit"] : |
---|
| 135 | server.quitApplication() |
---|
| 136 | except (socket.error, socket.gaierror), msg : |
---|
| 137 | raise PyKotaCommandLineError, "%s : %s" % (_("Connexion error"), str(msg)) |
---|
[2779] | 138 | |
---|
[2778] | 139 | if __name__ == "__main__" : |
---|
| 140 | retcode = 0 |
---|
| 141 | try : |
---|
| 142 | defaults = { \ |
---|
| 143 | } |
---|
[2779] | 144 | short_options = "vhd:acnq" |
---|
| 145 | long_options = ["help", "version", "destination=", \ |
---|
| 146 | "ask", "confirm", "notify", "quit" ] |
---|
[2778] | 147 | |
---|
| 148 | # Initializes the command line tool |
---|
| 149 | notifier = PyKotaNotify(doc=__doc__) |
---|
| 150 | notifier.deferredInit() |
---|
| 151 | |
---|
| 152 | # parse and checks the command line |
---|
[2779] | 153 | (options, args) = notifier.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
[2778] | 154 | |
---|
| 155 | # sets long options |
---|
| 156 | options["help"] = options["h"] or options["help"] |
---|
| 157 | options["version"] = options["v"] or options["version"] |
---|
[2779] | 158 | options["destination"] = options["d"] or options["destination"] |
---|
| 159 | options["ask"] = options["a"] or options["ask"] |
---|
| 160 | options["confirm"] = options["c"] or options["confirm"] |
---|
| 161 | options["notify"] = options["n"] or options["notify"] |
---|
| 162 | options["quit"] = options["q"] or options["quit"] |
---|
[2778] | 163 | |
---|
| 164 | if options["help"] : |
---|
| 165 | notifier.display_usage_and_quit() |
---|
| 166 | elif options["version"] : |
---|
| 167 | notifier.display_version_and_quit() |
---|
[2779] | 168 | elif (options["ask"] and (options["confirm"] or options["notify"])) \ |
---|
| 169 | or (options["confirm"] and (options["ask"] or options["notify"])) \ |
---|
| 170 | or (options["notify"] and (options["ask"] or options["confirm"])) : |
---|
| 171 | raise PyKotaCommandLineError, _("incompatible options, see help.") |
---|
| 172 | elif (not options["destination"]) \ |
---|
| 173 | or not (options["quit"] or options["ask"] or options["confirm"] or options["notify"]) : |
---|
| 174 | raise PyKotaCommandLineError, _("some options are mandatory, see help.") |
---|
| 175 | elif (not args) and (not options["quit"]) : |
---|
| 176 | raise PyKotaCommandLineError, _("some options require arguments, see help.") |
---|
[2778] | 177 | else : |
---|
| 178 | retcode = notifier.main(args, options) |
---|
| 179 | except KeyboardInterrupt : |
---|
| 180 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
| 181 | retcode = -3 |
---|
| 182 | except PyKotaCommandLineError, msg : |
---|
| 183 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
| 184 | retcode = -2 |
---|
| 185 | except SystemExit : |
---|
| 186 | pass |
---|
| 187 | except : |
---|
| 188 | try : |
---|
| 189 | notifier.crashed("%s failed" % sys.argv[0]) |
---|
| 190 | except : |
---|
| 191 | crashed("%s failed" % sys.argv[0]) |
---|
| 192 | retcode = -1 |
---|
| 193 | |
---|
| 194 | sys.exit(retcode) |
---|