Show
Ignore:
Timestamp:
03/20/06 23:55:16 (18 years ago)
Author:
jerome
Message:

Added timeout option.
Added denyafter option, but no code to support it yet.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pknotify

    r2786 r2799  
    2929import popen2 
    3030import socket 
     31import signal 
    3132import xmlrpclib 
    3233 
     
    7374                             or abortion. 
    7475                              
     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                              
    7583  -n | --notify              Tells pknotify to send an informational message 
    7684                             message to the end user. 
     
    8088                             be combined with the other ones to make PyKotIcon 
    8189                             exit after having sent the answer from the dialog. 
     90                              
     91  -t | --timeout T           Tells pknotify to ignore the end user's answer if 
     92                             it comes pas T seconds after the dialog box being 
     93                             opened. The default value is 0 seconds, which  
     94                             tells pknotify to wait indefinitely. 
     95                             Use this option to avoid having an user who 
     96                             leaved his computer stall a whole print queue. 
    8297                              
    8398  You MUST specify either --ask, --confirm, --notify or --quit. 
     
    119134  only set if you launch pknotify from cupspykota through a directive 
    120135  in ~pykota/pykota.conf 
     136   
     137  The TCP port you'll use must be reachable on the client from the 
     138  print server. 
    121139""") 
    122140         
     141class TimeoutError(Exception) :         
     142    """An exception for timeouts.""" 
     143    def __init__(self, message = ""): 
     144        self.message = message 
     145        Exception.__init__(self, message) 
     146    def __repr__(self): 
     147        return self.message 
     148    __str__ = __repr__ 
     149     
    123150class PyKotaNotify(Tool) :         
    124151    """A class for pknotify.""" 
     
    165192            return retcode 
    166193             
     194    def alarmHandler(self, signum, frame) :         
     195        """Alarm handler.""" 
     196        raise TimeoutError, "The end user at %s:%i didn't answer after %i seconds !" % (self.destination, self.port, self.timeout) 
     197         
    167198    def main(self, arguments, options) : 
    168199        """Notifies or asks questions to end users through PyKotIcon.""" 
    169200        try : 
    170             (destination, port) = options["destination"].split(":") 
     201            (self.destination, self.port) = options["destination"].split(":") 
     202            self.port = int(self.port) 
    171203        except ValueError : 
    172             destination = options["destination"] 
    173             port = 7654 
     204            self.destination = options["destination"] 
     205            self.port = 7654 
     206             
     207        try : 
     208            denyafter = int(options["denyafter"]) 
     209            if denyafter < 1 : 
     210                raise ValueError 
     211        except (ValueError, TypeError) :         
     212            denyafter = 1 
     213             
    174214        try :     
    175             server = xmlrpclib.ServerProxy("http://%s:%s" % (destination, port)) 
     215            self.timeout = int(options["timeout"]) 
     216            if self.timeout < 0 : 
     217                raise ValueError 
     218        except (ValueError, TypeError) : 
     219            self.timeout = 0 
     220             
     221        if self.timeout : 
     222            signal.signal(signal.SIGALRM, self.alarmHandler) 
     223            signal.alarm(self.timeout) 
     224             
     225        try :     
     226            server = xmlrpclib.ServerProxy("http://%s:%s" % (self.destination, self.port)) 
    176227            if options["ask"] : 
    177228                labels = [] 
     
    200251                    for varname in varnames : 
    201252                        if (varname != "password") \ 
    202                            and ((varname != "username") or (authok == "AUTH=YES")) : 
     253                           and ((varname != "username") or (authok in (None, "AUTH=YES"))) : 
    203254                            print "%s=%s" % (varname.upper(), result[varname].data) 
    204255                    if authok is not None :         
     
    213264        except (socket.error, socket.gaierror), msg : 
    214265            raise PyKotaCommandLineError, "%s : %s" % (_("Connection error"), str(msg)) 
     266        except TimeoutError, msg :     
     267            self.printInfo(msg, "warn") 
     268             
     269        if self.timeout :     
     270            signal.alarm(0)     
    215271         
    216272if __name__ == "__main__" : 
     
    218274    try : 
    219275        defaults = { \ 
     276                     "denyafter" : 1, 
     277                     "timeout" : 0, 
    220278                   } 
    221         short_options = "vhd:acnqC" 
    222         long_options = ["help", "version", "destination=", \ 
    223                         "ask", "checkauth", "confirm", "notify", "quit" ] 
     279        short_options = "vhd:acnqCD:t:" 
     280        long_options = ["help", "version", "destination=", "denyafter=", \ 
     281                        "timeout=", "ask", "checkauth", "confirm", "notify", \ 
     282                        "quit" ] 
    224283         
    225284        # Initializes the command line tool 
     
    239298        options["quit"] = options["q"] or options["quit"] 
    240299        options["checkauth"] = options["C"] or options["checkauth"] 
     300        options["denyafter"] = options["D"] or options["denyafter"] or defaults["denyafter"] 
     301        options["timeout"] = options["t"] or options["timeout"] or defaults["timeout"] 
    241302         
    242303        if options["help"] : 
     
    246307        elif (options["ask"] and (options["confirm"] or options["notify"])) \ 
    247308             or (options["confirm"] and (options["ask"] or options["notify"])) \ 
    248              or (options["checkauth"] and not options["ask"]) \ 
     309             or ((options["checkauth"] or options["denyafter"]) and not options["ask"]) \ 
    249310             or (options["notify"] and (options["ask"] or options["confirm"])) : 
    250311            raise PyKotaCommandLineError, _("incompatible options, see help.")