Show
Ignore:
Timestamp:
06/07/06 23:59:26 (18 years ago)
Author:
jerome
Message:

Implemented a caching mechanism for input forms.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykoticon/trunk/bin/pykoticon

    r143 r146  
    9999        self.frame = frame 
    100100        self.debug = options.debug 
     101        self.cacheduration = options.cache 
     102        self.cache = {} 
    101103        self.printServers = [ socket.gethostbyname(arg) for arg in arguments ] 
    102104        if "127.0.0.1" not in self.printServers : 
     
    110112            sys.stderr.write("%s\n" % message) 
    111113             
     114    def getAnswerFromCache(self, key) : 
     115        """Tries to extract a value from the cache and returns it if still valid.""" 
     116        cacheentry = self.cache.get(key) 
     117        if cacheentry is not None : 
     118            (birth, value) = cacheentry  
     119            if (time.time() - birth) < self.cacheduration : 
     120                self.logDebug("Cache hit for %s ---> %s" % (str(key), value)) 
     121                return value # NB : we don't extend the life of this entry 
     122            else :     
     123                self.logDebug("Cache expired for %s" % str(key)) 
     124        else :         
     125            self.logDebug("Cache miss for %s" % str(key)) 
     126        return None 
     127         
     128    def storeAnswerInCache(self, key, value) :     
     129        """Stores an entry in the cache.""" 
     130        self.cache[key] = (time.time(), value) 
     131        self.logDebug("Cache store for %s ---> %s" % (str(key), value)) 
     132         
     133    def export_askDatas(self, labels, varnames, varvalues) : 
     134        """Asks some textual datas defined by a list of labels, a list of variables' names and a list of variables values in a mapping.""" 
     135        values = {} 
     136        for (key, value) in varvalues.items() : 
     137            values[key] = self.frame.UTF8ToUserCharset(value.data) 
     138        cachekey = tuple(values.items())   
     139        retcode = self.getAnswerFromCache(cachekey) 
     140        if retcode is None : 
     141            wx.CallAfter(self.frame.askDatas, [ self.frame.UTF8ToUserCharset(label.data) for label in labels ], \ 
     142                                              varnames, \ 
     143                                              values) 
     144            # ugly, isn't it ? 
     145            while self.frame.dialogAnswer is None : 
     146                time.sleep(0.1) 
     147            retcode = self.frame.dialogAnswer     
     148            for (key, value) in retcode.items() : 
     149                if key != "isValid" : 
     150                    retcode[key] = xmlrpclib.Binary(self.frame.userCharsetToUTF8(value)) 
     151            self.frame.dialogAnswer = None # prepare for next call, just in case 
     152            self.storeAnswerInCache(cachekey, retcode) 
     153        return retcode 
     154         
    112155    def export_quitApplication(self) :     
    113156        """Makes the application quit.""" 
     
    115158        wx.CallAfter(self.frame.OnClose, None) 
    116159        return True 
    117          
    118     def export_askDatas(self, labels, varnames, varvalues) : 
    119         """Asks some textual datas defined by a list of labels, a list of variables' names and a list of variables values in a mapping.""" 
    120         values = {} 
    121         for (key, value) in varvalues.items() : 
    122             values[key] = self.frame.UTF8ToUserCharset(value.data) 
    123         wx.CallAfter(self.frame.askDatas, [ self.frame.UTF8ToUserCharset(label.data) for label in labels ], \ 
    124                                           varnames, \ 
    125                                           values) 
    126         # ugly, isn't it ? 
    127         while self.frame.dialogAnswer is None : 
    128             time.sleep(0.1) 
    129         retcode = self.frame.dialogAnswer     
    130         for (key, value) in retcode.items() : 
    131             if key != "isValid" : 
    132                 retcode[key] = xmlrpclib.Binary(self.frame.userCharsetToUTF8(value)) 
    133         self.frame.dialogAnswer = None # prepare for next call, just in case 
    134         return retcode 
    135160         
    136161    def export_showDialog(self, message, yesno) : 
     
    252277        wx.EVT_ICONIZE(self, self.OnIconify) 
    253278        wx.EVT_CLOSE(self, self.OnClose) 
    254         self.Show(True) 
     279        self.Show(False) 
    255280         
    256281    def getCurrentUserName(self) : 
     
    448473                            action="store_true",  
    449474                            dest="version", 
    450                             help=_("show PyKotIcon's version number and exit")) 
     475                            help=_("show PyKotIcon's version number and exit.")) 
     476    parser.add_option("-c", "--cache",  
     477                            type="int",  
     478                            default=0,  
     479                            dest="cache", 
     480                            help=_("the duration of the cache in seconds to keep input forms' datas in memory. Defaults to 0 second, meaning no cache.")) 
    451481    parser.add_option("-d", "--debug",  
    452482                            action="store_true",  
    453483                            dest="debug", 
    454                             help=_("activate debug mode")) 
     484                            help=_("activate debug mode.")) 
    455485    parser.add_option("-p", "--port",  
    456486                            type="int",  
    457487                            default=7654,  
    458488                            dest="port", 
    459                             help=_("the TCP port PyKotIcon will listen to, default is 7654")) 
     489                            help=_("the TCP port PyKotIcon will listen to, default is 7654.")) 
    460490    parser.add_option("-q", "--allowquit",  
    461491                            action="store_true",  
    462492                            dest="allowquit", 
    463                             help=_("allow the end user to close the application")) 
     493                            help=_("allow the end user to close the application.")) 
    464494    (options, arguments) = parser.parse_args() 
    465495    if options.version : 
    466496        print "PyKotIcon v%(__version__)s" % globals() 
    467497    else : 
    468         app = PyKotIconApp() 
    469         app.postInit(charset, options, arguments) 
    470         app.MainLoop() 
     498        if not (1024 <= options.port <= 65535) : 
     499            sys.stderr.write(_("The TCP port number specified for --port must be between 1024 and 65535.\n")) 
     500        elif not (0 <= options.cache <= 86400) :     
     501            sys.stderr.write(_("The duration specified for --cache must be between 0 and 86400 seconds.\n")) 
     502        else :     
     503            app = PyKotIconApp() 
     504            app.postInit(charset, options, arguments) 
     505            app.MainLoop() 
    471506     
    472507