Show
Ignore:
Timestamp:
01/21/05 18:32:28 (19 years ago)
Author:
jerome
Message:

Now accept a configuration file on the command line

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykoticon/trunk/bin/pykoticon

    r80 r81  
    5252    raise ImportError, "wxPython is missing. Please install it." 
    5353     
    54 #DUMPYKOTA_URL = "http://cgi.librelogiciel.com/cgi-bin/dumpykota.cgi" 
    55 DUMPYKOTA_URL = "http://servmediup.unice.fr/cgi-bin/dumpykota.cgi" 
    56  
    5754def getCurrentUserName() : 
    5855    """Retrieves the current user's name.""" 
     
    137134        """Initialize CGI connection datas.""" 
    138135        self.cgiurl = cgiurl 
    139         self.authname = authname 
     136        self.authname = authname        # TODO : at least do basic auth 
    140137        self.authpw = authpw 
    141138         
     
    321318        wxPython.wx.EVT_TIMER(self, self.TBTIMER, self.OnChronoTimer) 
    322319         
     320    def postInit(self, configFile) :     
     321        """Loads the configuration file and starts processing.""" 
    323322        self.User = None 
    324         self.networkInterface = CGINetworkInterface(DUMPYKOTA_URL) 
     323        url = None 
     324        try : 
     325            # try a file on disk first 
     326            u = open(configFile) 
     327        except IOError :     
     328            try : 
     329                # then try through the web 
     330                u = urllib2.urlopen(configFile) 
     331            except (ValueError, IOError), msg :     
     332                raise IOError, _("Impossible to read configuration file %s : %s") % (configFile, msg) 
     333        url = u.readline().strip() 
     334        u.close() 
     335         
     336        if not url : 
     337            raise ValueError, _("Configuration file %s is incorrect.") % configFile 
     338        self.networkInterface = CGINetworkInterface(url) 
    325339        self.inTimer = False 
    326340        self.chrono.Start(250) # first time in 0.25 second 
     
    333347        if self.inTimer is False : # avoids re-entrance 
    334348            self.inTimer = True 
    335             self.User = self.networkInterface.getUserInfo(getCurrentUserName()) 
    336             if self.User.LimitBy == "balance" : 
    337                 if self.User.Balance <= 0.0 : 
    338                     self.SetIcon(self.redicon) 
    339                     if self.tbicon is not None : 
    340                         self.tbicon.SetIcon(self.redicon, "PyKotIcon") 
    341                 else :     
    342                     self.SetIcon(self.greenicon) 
    343                     if self.tbicon is not None : 
    344                         self.tbicon.SetIcon(self.greenicon, "PyKotIcon") 
    345             else :         
    346                 isRed = False 
    347                 for q in self.User.Quotas.keys() : 
    348                     quota = self.User.Quotas[q] 
    349                     if quota.SoftLimit is not None : 
    350                         if quota.PageCounter >= quota.SoftLimit : 
    351                             isRed = True 
    352                             break 
    353                     elif quota.HardLimit is not None :         
    354                         if quota.PageCounter >= quota.HardLimit : 
    355                             isRed = True 
    356                             break 
    357                 if isRed is True : 
    358                     self.SetIcon(self.redicon) 
    359                     if self.tbicon is not None : 
    360                         self.tbicon.SetIcon(self.redicon, "PyKotIcon") 
    361                 else :     
    362                     self.SetIcon(self.greenicon) 
    363                     if self.tbicon is not None : 
    364                         self.tbicon.SetIcon(self.greenicon, "PyKotIcon") 
    365             if hasattr(self, "quotasgrid") :     
    366                 self.quotasgrid.Close() 
    367                 self.quotasgrid.Destroy() 
    368                 del self.quotasgrid 
    369             self.quotasgrid = PyKotIconGrid(self, self.User)     
     349            try : 
     350                self.User = self.networkInterface.getUserInfo(getCurrentUserName()) 
     351            except IOError, msg :     
     352                sys.stderr.write("ERROR : %s\n" % msg) 
     353            else : 
     354                if self.User.LimitBy == "balance" : 
     355                    if self.User.Balance <= 0.0 : 
     356                        self.SetIcon(self.redicon) 
     357                        if self.tbicon is not None : 
     358                            self.tbicon.SetIcon(self.redicon, "PyKotIcon") 
     359                    else :     
     360                        self.SetIcon(self.greenicon) 
     361                        if self.tbicon is not None : 
     362                            self.tbicon.SetIcon(self.greenicon, "PyKotIcon") 
     363                else :         
     364                    isRed = False 
     365                    for q in self.User.Quotas.keys() : 
     366                        quota = self.User.Quotas[q] 
     367                        if quota.SoftLimit is not None : 
     368                            if quota.PageCounter >= quota.SoftLimit : 
     369                                isRed = True 
     370                                break 
     371                        elif quota.HardLimit is not None :         
     372                            if quota.PageCounter >= quota.HardLimit : 
     373                                isRed = True 
     374                                break 
     375                    if isRed is True : 
     376                        self.SetIcon(self.redicon) 
     377                        if self.tbicon is not None : 
     378                            self.tbicon.SetIcon(self.redicon, "PyKotIcon") 
     379                    else :     
     380                        self.SetIcon(self.greenicon) 
     381                        if self.tbicon is not None : 
     382                            self.tbicon.SetIcon(self.greenicon, "PyKotIcon") 
     383                isiconized = self.IsIconized()             
     384                if isiconized is False : 
     385                    self.Iconize(True) 
     386                isshown = self.IsShown()     
     387                if isshown is True : 
     388                    self.Hide() 
     389                if hasattr(self, "quotasgrid") :     
     390                    self.quotasgrid.Close() 
     391                    self.quotasgrid.Destroy() 
     392                    del self.quotasgrid 
     393                self.quotasgrid = PyKotIconGrid(self, self.User)     
     394                if isiconized is False : 
     395                    self.Iconize(False) 
     396                if isshown is True :     
     397                    self.Show(True) 
     398                self.Refresh() 
    370399            self.inTimer = False 
    371400        # Now we want it every 3 minutes     
     
    408437    def OnInit(self) : 
    409438        try : 
    410             frame = PyKotIcon(None, -1) 
     439            self.frame = PyKotIcon(None, -1) 
    411440        except :     
    412441            crashed() 
    413         else :     
    414             frame.Show(True) 
    415442        return True 
    416443         
    417 def main(): 
     444    def postInit(self, configFile) :     
     445        """Continues processing.""" 
     446        self.frame.postInit(configFile) 
     447        self.frame.Show(True) 
     448         
     449def main(conffile): 
    418450    """Program's entry point.""" 
    419451    try : 
     
    426458        gettext.NullTranslations().install() 
    427459    app = PyKotIconApp() 
     460    app.postInit(conffile) 
    428461    app.MainLoop() 
    429462     
     
    438471    sys.stderr.flush() 
    439472     
    440 def test() : 
    441     """Runs in test mode (console).""" 
    442     if len(sys.argv) >= 3 : 
    443         username = sys.argv[2]  
     473# def test() : 
     474#     """Runs in test mode (console).""" 
     475#     print "Configuration file is ignored." 
     476#     if len(sys.argv) >= 3 : 
     477#         username = sys.argv[2]  
     478#     else :     
     479#         username = getCurrentUserName() 
     480#     net = CGINetworkInterface(DUMPYKOTA_URL) 
     481#     user = net.getUserInfo(username) 
     482#     print "UserName : ", user.UserName 
     483#     print "LimitBy : ", user.LimitBy 
     484#     print "Balance : ", user.Balance 
     485#     for printername in user.Quotas.keys() : 
     486#         quota = user.Quotas[printername] 
     487#         print "\tPrinterName : ", printername 
     488#         print "\tPageCounter : ", quota.PageCounter 
     489#         print "\tSoftLimit : ", quota.SoftLimit 
     490#         print "\tHardLimit : ", quota.HardLimit 
     491#         print "\tDateLimit : ", quota.DateLimit 
     492#         print 
     493#     for printername in user.Printers.keys() : 
     494#         printer = user.Printers[printername] 
     495#         print "\tPrinterName : ", printername 
     496#         print "\tPrice per Page : ", printer.PricePerPage 
     497#         print "\tPrice per Job : ", printer.PricePerJob 
     498#         print 
     499 
     500if __name__ == '__main__': 
     501    if len(sys.argv) >= 2 : 
     502        arg = sys.argv[1] 
     503        if arg in ("-v", "--version") :     
     504            print "0.1" 
     505        elif arg in ("-h", "--help") :     
     506            print "usage : pykoticon configuration_file" 
     507        #elif arg == "--test" : 
     508        #    test() 
     509        else : 
     510            main(arg) 
    444511    else :     
    445         username = getCurrentUserName() 
    446     net = CGINetworkInterface(DUMPYKOTA_URL) 
    447     user = net.getUserInfo(username) 
    448     print "UserName : ", user.UserName 
    449     print "LimitBy : ", user.LimitBy 
    450     print "Balance : ", user.Balance 
    451     for printername in user.Quotas.keys() : 
    452         quota = user.Quotas[printername] 
    453         print "\tPrinterName : ", printername 
    454         print "\tPageCounter : ", quota.PageCounter 
    455         print "\tSoftLimit : ", quota.SoftLimit 
    456         print "\tHardLimit : ", quota.HardLimit 
    457         print "\tDateLimit : ", quota.DateLimit 
    458         print 
    459     for printername in user.Printers.keys() : 
    460         printer = user.Printers[printername] 
    461         print "\tPrinterName : ", printername 
    462         print "\tPrice per Page : ", printer.PricePerPage 
    463         print "\tPrice per Job : ", printer.PricePerJob 
    464         print 
    465  
    466 if __name__ == '__main__': 
    467     if (len(sys.argv) >= 2) and (sys.argv[1] == "--test") : 
    468         test() 
    469     else :     
    470         main() 
    471      
     512        main("pykoticon.conf")