Changeset 2765 for pykota/trunk/bin

Show
Ignore:
Timestamp:
03/01/06 00:07:05 (18 years ago)
Author:
jerome
Message:

Optimized pkbcodes like edpykota.

Location:
pykota/trunk/bin
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/edpykota

    r2762 r2765  
    185185            getattr(self.storage, "deleteMany%sPQuotas" % suffix)(printers, entries) 
    186186            self.display("\n") 
    187             self.display("\r100.00%%\r        \r%s\n" % _("Done.")) 
     187            self.done() 
    188188        elif options["list"] : 
    189189            for printer in printers : 
     
    305305                            percent = 100.0 * float(i) / float(nbtotal) 
    306306                            self.display("\r%.02f%%" % percent) 
    307             self.display("\r100.00%%\r        \r%s\n" % _("Done.")) 
    308                       
     307            self.done()          
    309308if __name__ == "__main__" :  
    310309    retcode = 0 
  • pykota/trunk/bin/pkbcodes

    r2725 r2765  
    3030 
    3131from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_ 
     32from pykota.storage import StorageBillingCode 
    3233 
    3334__doc__ = N_("""pkbcodes v%(__version__)s (c) %(__years__)s %(__author__)s 
     
    8485class PKBcodes(PyKotaTool) :         
    8586    """A class for a billing codes manager.""" 
     87    def modifyBillingCode(self, billingcode, reset, description) : 
     88        """Modifies a billing code.""" 
     89        if reset : 
     90            billingcode.reset()     
     91        if description is not None : # NB : "" is allowed ! 
     92            billingcode.setDescription(description) 
     93         
    8694    def main(self, names, options) : 
    8795        """Manage billing codes.""" 
     
    8997            raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) 
    9098             
     99        if not options["add"] : 
     100            if not options["list"] : 
     101                self.display(_("Extracting datas...")) 
     102            if not names :      # NB : can't happen for --delete because it's catched earlier 
     103                names = ["*"] 
     104            billingcodes = self.storage.getMatchingBillingCodes(",".join(names)) 
     105            if not billingcodes : 
     106                raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names) 
     107                         
     108        reset = options["reset"] 
     109        description = options["description"] 
     110        if description : 
     111            description = options["description"].strip() 
     112                 
    91113        if options["delete"] :     
    92             self.display("%s...\n" % _("Deletion")) 
    93             todelete = self.storage.getMatchingBillingCodes(",".join(names)) 
    94             nbtotal = len(todelete) 
     114            self.display("\n%s..." % _("Deletion")) 
     115            self.storage.deleteManyBillingCodes(billingcodes) 
     116            self.display("\n") 
     117        elif options["add"] :     
     118            self.display("%s...\n" % _("Creation")) 
     119            nbtotal = len(names) 
    95120            for i in range(nbtotal) : 
    96                 entry = todelete[i] 
    97                 if entry.Exists : 
    98                     entry.delete() 
     121                bname = names[i] 
     122                billingcode = StorageBillingCode(self.storage, bname) 
     123                self.modifyBillingCode(billingcode, reset, description) 
     124                oldbillingcode = self.storage.addBillingCode(billingcode) 
     125                if oldbillingcode is not None : 
     126                    if options["skipexisting"] : 
     127                        self.printInfo(_("Billing code [%s] already exists, skipping.") % bname) 
     128                    else :     
     129                        self.printInfo(_("Billing code [%s] already exists, will be modified.") % bname) 
     130                        self.modifyBillingCode(oldbillingcode, reset, description) 
     131                        oldbillingcode.save() 
    99132                percent = 100.0 * float(i) / float(nbtotal) 
    100133                self.display("\r%.02f%%" % percent) 
    101         else : 
    102             if options["add"] :     
    103                 self.display("%s...\n" % _("Creation")) 
    104                 billingcodes = [] 
    105                 nbtotal = len(names) 
    106                 for i in range(nbtotal) : 
    107                     bname = names[i] 
    108                     billingcode = self.storage.getBillingCode(bname) 
    109                     if billingcode.Exists : 
    110                         if options["skipexisting"] : 
    111                             self.printInfo(_("Billing code [%s] already exists, skipping.") % billingcode.BillingCode) 
    112                         else :     
    113                             self.printInfo(_("Billing code [%s] already exists, will be modified.") % billingcode.BillingCode) 
    114                             billingcodes.append(billingcode) 
    115                     else : 
    116                         billingcode = self.storage.addBillingCode(bname) 
    117                         if not billingcode.Exists : 
    118                             raise PyKotaToolError, _("Impossible to add billingcode %s") % bname 
    119                         else :      
    120                             billingcodes.append(billingcode) 
    121                     percent = 100.0 * float(i) / float(nbtotal) 
    122                     self.display("\r%.02f%%" % percent) 
    123                 self.display("\r100.00%%\r        \r%s\n" % _("Done.")) 
    124             else :         
    125                 if not names : 
    126                     names = ["*"] 
    127                 billingcodes = self.storage.getMatchingBillingCodes(",".join(names)) 
    128                 if not billingcodes : 
    129                     raise PyKotaCommandLineError, _("There's no billingcode matching %s") % " ".join(names) 
    130                          
     134        else :         
    131135            if options["list"] : 
    132136                for billingcode in billingcodes : 
     
    140144                           _("credits")) 
    141145            else :                
    142                 self.display("%s...\n" % _("Modification")) 
    143                 reset = options["reset"] 
    144                 if reset or options["description"] : # optimize when nothing to do 
    145                     if options["description"] : 
    146                         description = options["description"].strip() 
    147                     nbtotal = len(billingcodes)             
    148                     for i in range(nbtotal) :         
    149                         billingcode = billingcodes[i] 
    150                         if reset : 
    151                             billingcode.reset()     
    152                         if description is not None : # NB : "" is allowed ! 
    153                             billingcode.setDescription(description) 
    154                         billingcode.save()     
    155                         percent = 100.0 * float(i) / float(nbtotal) 
    156                         self.display("\r%.02f%%" % percent) 
    157                          
     146                self.display("\n%s...\n" % _("Modification")) 
     147                nbtotal = len(billingcodes)             
     148                for i in range(nbtotal) :         
     149                    billingcode = billingcodes[i] 
     150                    self.modifyBillingCode(billingcode, reset, description) 
     151                    billingcode.save()     
     152                    percent = 100.0 * float(i) / float(nbtotal) 
     153                    self.display("\r%.02f%%" % percent) 
     154                     
    158155        if not options["list"] :                 
    159             self.display("\r100.00%%\r        \r%s\n" % _("Done.")) 
     156            self.done() 
    160157                      
    161158if __name__ == "__main__" :  
  • pykota/trunk/bin/pkusers

    r2762 r2765  
    226226                    percent = 100.0 * float(i) / float(nbtotal) 
    227227                    self.display("\r%.02f%%" % percent) 
    228                 self.display("\r100.00%%\r        \r%s\n" % _("Done.")) 
     228                self.done() 
    229229            else :         
    230230                entries = getattr(self.storage, "getMatching%ss" % suffix)(",".join(names)) 
     
    353353                                 
    354354        if not options["list"] :                 
    355             self.display("\r100.00%%\r        \r%s\n" % _("Done.")) 
     355            self.done() 
    356356                      
    357357if __name__ == "__main__" :