Changeset 3441 for pykota

Show
Ignore:
Timestamp:
10/15/08 22:01:50 (16 years ago)
Author:
jerome
Message:

Intermediate commit. Doesn't work yet. Clearly marked as such when launched.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pknotify

    r3413 r3441  
    3636 
    3737import pykota.appinit 
    38 from pykota.utils import * 
    39  
    40 from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
     38from pykota.utils import run 
     39from pykota.commandline import PyKotaOptionParser, \ 
     40                               checkandset_positiveint 
     41from pykota.errors import PyKotaToolError, \ 
     42                          PyKotaCommandLineError, \ 
     43                          PyKotaTimeoutError 
    4144from pykota.tool import Tool 
    42  
    43 __doc__ = N_("""pknotify v%(__version__)s (c) %(__years__)s %(__author__)s 
    44  
    45 Notifies or ask questions to end users who launched the PyKotIcon application. 
    46  
    47 command line usage : 
    48  
    49   pknotify  [options]  [arguments] 
    50  
    51 options : 
    52  
    53   -v | --version             Prints pknotify's version number then exits. 
    54   -h | --help                Prints this message then exits. 
    55  
    56   -d | --destination h[:p]   Sets the destination hostname and optional 
    57                              port onto which contact the remote PyKotIcon 
    58                              application. This option is mandatory. 
    59                              When not specified, the port defaults to 7654. 
    60  
    61   -a | --ask                 Tells pknotify to ask something to the end 
    62                              user. Then pknotify will output the result. 
    63  
    64   -C | --checkauth           When --ask is used and both an 'username' and a 
    65                              'password' are asked to the end user, then 
    66                              pknotify will try to authenticate the user 
    67                              through PAM. If authentified, this program 
    68                              will print "AUTH=YES", else "AUTH=NO". 
    69                              If a field is missing, "AUTH=IMPOSSIBLE" will 
    70                              be printed. If the user is authenticated, then 
    71                              "USERNAME=xxxx" will be printed as well. 
    72  
    73   -c | --confirm             Tells pknotify to ask for either a confirmation 
    74                              or abortion. 
    75  
    76   -D | --denyafter N         With --checkauth above, makes pknotify loop 
    77                              up to N times if the password is incorrect. 
    78                              After having reached the limit, "DENY" will 
    79                              be printed, which effectively rejects the job. 
    80                              The default value of N is 1, meaning the job 
    81                              is denied after the first unsuccessful try. 
    82  
    83   -N | --noremote action     If it's impossible to connect to the remote 
    84                              PyKotIcon machine, do this action instead. 
    85                              Allowed actions are 'CONTINUE' and 'CANCEL', 
    86                              which will respectively allow the processing 
    87                              of the print job to continue, or the job to 
    88                              be cancelled. The default value is CANCEL. 
    89  
    90   -n | --notify              Tells pknotify to send an informational message 
    91                              to the end user. 
    92  
    93   -q | --quit                Tells pknotify to send a message asking the 
    94                              PyKotIcon application to exit. This option can 
    95                              be combined with the other ones to make PyKotIcon 
    96                              exit after having sent the answer from the dialog. 
    97  
    98   -t | --timeout T           Tells pknotify to ignore the end user's answer if 
    99                              it comes past T seconds after the dialog box being 
    100                              opened. The default value is 0 seconds, which 
    101                              tells pknotify to wait indefinitely. 
    102                              Use this option to avoid having an user who 
    103                              leaved his computer stall a whole print queue. 
    104  
    105   You MUST specify either --ask, --confirm, --notify or --quit. 
    106  
    107   arguments : 
    108  
    109     -a | --ask : Several arguments are accepted, of the form 
    110                  "label:varname:defaultvalue". The result will 
    111                  be printed to stdout in the following format : 
    112                  VAR1NAME=VAR1VALUE 
    113                  VAR2NAME=VAR2VALUE 
    114                  ... 
    115                  If the dialog was cancelled, nothing will be 
    116                  printed. If one of the varname is 'password' 
    117                  then this field is asked as a password (you won't 
    118                  see what you type in), and is NOT printed. Although 
    119                  it is not printed, it will be used to check if 
    120                  authentication is valid if you specify --checkauth. 
    121  
    122     -c | --confirm : A single argument is expected, representing the 
    123                      message to display. If the dialog is confirmed 
    124                      then pknotify will print OK, else CANCEL. 
    125  
    126     -n | --notify : A single argument is expected, representing the 
    127                     message to display. In this case pknotify will 
    128                     always print OK. 
    129  
    130 examples : 
    131  
    132   pknotify -d client:7654 --noremote CONTINUE --confirm "This job costs 10 credits" 
    133  
    134   Would display the cost of the print job and asks for confirmation. 
    135   If the end user doesn't have PyKotIcon running and accepting connections 
    136   from the print server, PyKota will consider that the end user accepted 
    137   to print this job. 
    138  
    139   pknotify --destination $PYKOTAJOBORIGINATINGHOSTNAME:7654 \\ 
    140            --checkauth --ask "Your name:username:" "Your password:password:" 
    141  
    142   Asks an username and password, and checks if they are valid. 
    143   NB : The PYKOTAJOBORIGINATINGHOSTNAME environment variable is 
    144   only set if you launch pknotify from cupspykota through a directive 
    145   in ~pykota/pykota.conf 
    146  
    147   The TCP port you'll use must be reachable on the client from the 
    148   print server. 
    149 """) 
    150  
    151 class TimeoutError(Exception) : 
    152     """An exception for timeouts.""" 
    153     def __init__(self, message = ""): 
    154         self.message = message 
    155         Exception.__init__(self, message) 
    156     def __repr__(self): 
    157         return self.message 
    158     __str__ = __repr__ 
    15945 
    16046class PyKotaNotify(Tool) : 
     
    216102    def alarmHandler(self, signum, frame) : 
    217103        """Alarm handler.""" 
    218         raise TimeoutError, _("The end user at %s:%i didn't answer within %i seconds. The print job will be cancelled.") % (self.destination, self.port, self.timeout) 
     104        raise PyKotaTimeoutError, _("The end user at %s:%i didn't answer within %i seconds. The print job will be cancelled.") % (self.destination, self.port, self.timeout) 
    219105 
    220106    def main(self, arguments, options) : 
     
    313199                #        raise PyKotaToolError, "%s : %s" % (str(msg), (_("Are you sure that PyKotIcon is running and accepting incoming connections on %s:%s ?") % (self.destination, self.port))) 
    314200                self.printInfo("%s : %s" % (_("Connection error"), str(msg)), "warn") 
    315             except TimeoutError, msg : 
     201            except PyKotaTimeoutError, msg : 
    316202                self.printInfo(msg, "warn") 
    317203                print "CANCEL"      # Timeout occured : job is cancelled. 
     
    321207 
    322208if __name__ == "__main__" : 
    323     retcode = 0 
    324     try : 
     209    sys.stderr.write("The pknotify command line tool is currently broken in this development tree. Please use a stable release instead.\n") 
     210    sys.exit(-1) 
     211    parser = PyKotaOptionParser(description=_("Notifies end users who have launched the PyKotIcon client side graphical desktop helper."), 
     212                                usage="pknotify [options] [arguments]") 
     213    parser.add_option("-a", "--ask", 
     214                            action="store_const", 
     215                            const="ask", 
     216                            dest="action", 
     217                            help=_("Ask something to the remote user, then print the result.")) 
     218    parser.add_option("-c", "--confirm", 
     219                            action="store_const", 
     220                            const="confirm", 
     221                            dest="action", 
     222                            help=_("Ask the remote user to confirm or abort, then print the result.")) 
     223    parser.add_option("-C", "--checkauth", 
     224                            dest="checkauth", 
     225                            help=_("When --ask is used, if both an username and password are asked to the end user, pknotify tries to authenticate these username and password through PAM. If this is successful, both 'AUTH=YES' and 'USERNAME=theusername' are printed. If unsuccessful, 'AUTH=NO' is printed. Finally, if one field is missing, 'AUTH=IMPOSSIBLE' is printed.")) 
     226    parser.add_option("-D", "--denyafter", 
     227                            type="int", 
     228                            action="callback", 
     229                            callback=checkandset_positiveint, 
     230                            default=1, 
     231                            dest="denyafter", 
     232                            help=_("When --checkauth is used, this option tell pknotify to loop up to this value times or until the password is correct for the returned username. If authentication was impossible with the username and password received from the remote user, 'DENY' is printed, rejecting the print job. The default value is %default, meaning pknotify asks a single time.")) 
     233    parser.add_option("-d", "--destination", 
     234                            dest="destination", 
     235                            help=_("Indicate the mandatory remote hostname or IP address and optional TCP port where PyKotIcon is listening for incoming connections from pknotify. If not specified, the port defaults to 7654.")) 
     236    parser.add_option("-n", "--notify", 
     237                            action="store_const", 
     238                            const="notify", 
     239                            dest="action", 
     240                            help=_("Send an informational message to the remote user.")) 
     241    parser.add_option("-N", "--noremote", 
     242                            dest="noremote", 
     243                            default="CANCEL", 
     244                            help=_("Tell pknotify what to print if it can't connect to a remote PyKotIcon application. The default value is 'CANCEL', which tells PyKota to cancel the print job. The only other supported value is 'CONTINUE', which tells PyKota to continue the processing of the current job.")) 
     245    parser.add_option("-q", "--quit", 
     246                            dest="quit", 
     247                            help=_("Ask the remote PyKotIcon application to quit. When combined with other command line options, any other action is performed first.")) 
     248    parser.add_option("-t", "--timeout", 
     249                            type="int", 
     250                            action="callback", 
     251                            callback=checkandset_positiveint, 
     252                            default=0, 
     253                            dest="timeout", 
     254                            help=_("Ensure that pknotify won't wait more than timeout seconds for an answer from the remote user. This avoids end users stalling a print queue because they don't answer in time. The default value is %default, making pknotify wait indefinitely.")) 
     255    run(parser, PyKotaNotify) 
     256 
     257""" 
     258  arguments : 
     259 
     260    -a | --ask : Several arguments are accepted, of the form 
     261                 "label:varname:defaultvalue". The result will 
     262                 be printed to stdout in the following format : 
     263                 VAR1NAME=VAR1VALUE 
     264                 VAR2NAME=VAR2VALUE 
     265                 ... 
     266                 If the dialog was cancelled, nothing will be 
     267                 printed. If one of the varname is 'password' 
     268                 then this field is asked as a password (you won't 
     269                 see what you type in), and is NOT printed. Although 
     270                 it is not printed, it will be used to check if 
     271                 authentication is valid if you specify --checkauth. 
     272 
     273    -c | --confirm : A single argument is expected, representing the 
     274                     message to display. If the dialog is confirmed 
     275                     then pknotify will print OK, else CANCEL. 
     276 
     277    -n | --notify : A single argument is expected, representing the 
     278                    message to display. In this case pknotify will 
     279                    always print OK. 
     280 
     281examples : 
     282 
     283  pknotify -d client:7654 --noremote CONTINUE --confirm "This job costs 10 credits" 
     284 
     285  Would display the cost of the print job and asks for confirmation. 
     286  If the end user doesn't have PyKotIcon running and accepting connections 
     287  from the print server, PyKota will consider that the end user accepted 
     288  to print this job. 
     289 
     290  pknotify --destination $PYKOTAJOBORIGINATINGHOSTNAME:7654 \\ 
     291           --checkauth --ask "Your name:username:" "Your password:password:" 
     292 
     293  Asks an username and password, and checks if they are valid. 
     294  NB : The PYKOTAJOBORIGINATINGHOSTNAME environment variable is 
     295  only set if you launch pknotify from cupspykota through a directive 
     296  in ~pykota/pykota.conf 
     297 
     298  The TCP port you'll use must be reachable on the client from the 
     299  print server. 
     300""" 
     301 
     302""" 
    325303        defaults = { \ 
    326304                     "timeout" : 0, 
     
    332310                        "quit", "noremote=" ] 
    333311 
    334         # Initializes the command line tool 
    335         notifier = PyKotaNotify(doc=__doc__) 
    336         notifier.deferredInit() 
    337  
    338         # parse and checks the command line 
    339         (options, args) = notifier.parseCommandline(sys.argv[1:], short_options, long_options) 
    340  
    341         # sets long options 
    342         options["help"] = options["h"] or options["help"] 
    343         options["version"] = options["v"] or options["version"] 
    344         options["destination"] = options["d"] or options["destination"] 
    345         options["ask"] = options["a"] or options["ask"] 
    346         options["confirm"] = options["c"] or options["confirm"] 
    347         options["notify"] = options["n"] or options["notify"] 
    348         options["quit"] = options["q"] or options["quit"] 
    349         options["checkauth"] = options["C"] or options["checkauth"] 
    350         options["denyafter"] = options["D"] or options["denyafter"] 
    351         options["timeout"] = options["t"] or options["timeout"] or defaults["timeout"] 
    352         options["noremote"] = (options["N"] or options["noremote"] or defaults["noremote"]).upper() 
    353  
    354         if options["help"] : 
    355             notifier.display_usage_and_quit() 
    356         elif options["version"] : 
    357             notifier.display_version_and_quit() 
    358312        elif (options["ask"] and (options["confirm"] or options["notify"])) \ 
    359313             or (options["confirm"] and (options["ask"] or options["notify"])) \ 
     
    368322        elif (not args) and (not options["quit"]) : 
    369323            raise PyKotaCommandLineError, _("some options require arguments, see help.") 
    370         else : 
    371             retcode = notifier.main(args, options) 
    372     except KeyboardInterrupt : 
    373         logerr("\nInterrupted with Ctrl+C !\n") 
    374         retcode = -3 
    375     except PyKotaCommandLineError, msg : 
    376         logerr("%s : %s\n" % (sys.argv[0], msg)) 
    377         print "CANCEL"  # Forces the cancellation of the print job if a command line switch is incorrect 
    378         retcode = -2 
    379     except SystemExit : 
    380         pass 
    381     except : 
    382         try : 
    383             notifier.crashed("%s failed" % sys.argv[0]) 
    384         except : 
    385             crashed("%s failed" % sys.argv[0]) 
    386         retcode = -1 
    387  
    388     sys.exit(retcode) 
     324"""