Show
Ignore:
Timestamp:
02/21/06 01:08:55 (18 years ago)
Author:
jerome
Message:

"edpykota --delete" and "edpykota --list" now work fine.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/edpykota

    r2712 r2728  
    5454                       Users or groups are never deleted, you have 
    5555                       to use the pkusers command to delete them. 
     56                       The history will be purge from all matching 
     57                       jobs, unless -g | --groups is used. 
    5658   
    5759  -P | --printer p     Edit quotas on printer p only. Actually p can 
     
    6567                       users print quota entries. 
    6668                           
     69  -L | --list          Lists users or groups print quota entries. 
     70   
    6771  -n | --noquota       Sets both soft and hard limits to None for users 
    6872                       or groups print quota entries. 
     
    7579                       for the user or group to zero on the specified  
    7680                       printers. This is a shortcut for '--used 0'. 
     81                        
     82  -s | --skipexisting  In combination with the --add option above, tells 
     83                       edpykota to not modify existing print quota entries. 
    7784                        
    7885  -S | --softlimit sl  Sets the quota soft limit to sl pages.                        
     
    153160         
    154161        if options["delete"] :     
    155             self.display("%s...\n" % _("Processing")) 
    156             todelete = getattr(self.storage, "getMatching%ss" % suffix)(",".join(names)) 
    157             nbtotal = len(todelete) 
     162            self.display("%s...\n" % _("Deletion")) 
     163            (printers, entries, pqentries) = getattr(self.storage, "getPrinters%ssAndPQuotas" % suffix)(options["printer"].split(","), names) 
     164            nbtotal = len(pqentries) 
    158165            for i in range(nbtotal) : 
    159                 todelete[i].delete() 
     166                pqentry = pqentries[i] 
     167                if pqentry.Exists : 
     168                    pqentry.delete() 
    160169                percent = 100.0 * float(i) / float(nbtotal) 
    161170                self.display("\r%.02f%%" % percent) 
    162             self.display("\r100.00%%\r        ") 
    163             self.display("\r%s\n" % _("Done.")) 
    164171        else : 
    165             softlimit = hardlimit = None 
    166              
    167             used = options["used"] 
    168             if used : 
    169                 used = used.strip() 
    170                 try : 
    171                     int(used) 
    172                 except ValueError : 
    173                     raise PyKotaCommandLineError, _("Invalid used value %s.") % used 
     172            if options["add"] : 
     173                raise PyKotaCommandLineError, "Not implemented yet." 
     174            else :         
     175                if not names : 
     176                    names = ["*"] 
     177                (printers, entries, pqentries) = getattr(self.storage, "getPrinters%ssAndPQuotas" % suffix)(options["printer"].split(","), names) 
     178                if not pqentries : 
     179                    raise PyKotaCommandLineError, _("There's no %s print quota entry matching %s") % (suffix.lower(), " ".join(names)) 
    174180                     
    175             increase = options["increase"] 
    176             if increase : 
    177                 try : 
    178                     increase = int(increase.strip()) 
    179                 except ValueError : 
    180                     raise PyKotaCommandLineError, _("Invalid increase value %s.") % increase 
    181              
    182             if not options["noquota"] : 
    183                 if options["softlimit"] : 
     181            if options["list"] : 
     182                for (name, entry) in pqentries.items() : 
     183                    if entry.Exists : 
     184                        print name 
     185                        print "    %s" % (_("Page counter : %s") % entry.PageCounter) 
     186                        print "    %s" % (_("Lifetime page counter : %s") % entry.LifePageCounter) 
     187                        print "    %s" % (_("Soft limit : %s") % entry.SoftLimit) 
     188                        print "    %s" % (_("Hard limit : %s") % entry.HardLimit) 
     189                        print "    %s" % (_("Date limit : %s") % entry.DateLimit) 
     190                        # TODO : print "    %s" % (_("Maximum job size : %s") % ((entry.MaxJobSize and (_("%s pages") % entry.MaxJobSize)) or _("Unlimited"))) 
     191                        if hasattr(entry, "WarnCount") : 
     192                            print "    %s" % (_("Warning banners printed : %s") % entry.WarnCount) 
     193                        print 
     194            else : 
     195                self.display("%s...\n" % _("Modification")) 
     196                raise PyKotaCommandLineError, "Not implemented yet." 
     197                softlimit = hardlimit = None 
     198                 
     199                used = options["used"] 
     200                if used : 
     201                    used = used.strip() 
    184202                    try : 
    185                         softlimit = int(options["softlimit"].strip()) 
    186                         if softlimit < 0 : 
    187                             raise ValueError 
    188                     except ValueError :     
    189                         raise PyKotaCommandLineError, _("Invalid softlimit value %s.") % options["softlimit"] 
    190                 if options["hardlimit"] : 
     203                        int(used) 
     204                    except ValueError : 
     205                        raise PyKotaCommandLineError, _("Invalid used value %s.") % used 
     206                         
     207                increase = options["increase"] 
     208                if increase : 
    191209                    try : 
    192                         hardlimit = int(options["hardlimit"].strip()) 
    193                         if hardlimit < 0 : 
    194                             raise ValueError 
    195                     except ValueError :     
    196                         raise PyKotaCommandLineError, _("Invalid hardlimit value %s.") % options["hardlimit"] 
    197                 if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) :         
    198                     # error, exchange them 
    199                     self.printInfo(_("Hard limit %i is less than soft limit %i, values will be exchanged.") % (hardlimit, softlimit)) 
    200                     (softlimit, hardlimit) = (hardlimit, softlimit) 
     210                        increase = int(increase.strip()) 
     211                    except ValueError : 
     212                        raise PyKotaCommandLineError, _("Invalid increase value %s.") % increase 
    201213                 
    202             sys.stdout.write(_("Managing print quotas... (this may take a lot of time)")) 
    203             sys.stdout.flush() 
    204             missingusers = {} 
    205             missinggroups = {}     
    206             changed = {} # tracks changes made at the user/group level 
    207             for printer in printers : 
    208                 if not options["noquota"] :     
    209                     if hardlimit is None :     
    210                         hardlimit = softlimit 
    211                         if hardlimit is not None : 
    212                             self.printInfo(_("Undefined hard limit set to soft limit (%s) on printer %s.") % (str(hardlimit), printer.Name)) 
    213                     if softlimit is None :     
    214                         softlimit = hardlimit 
    215                         if softlimit is not None : 
    216                             self.printInfo(_("Undefined soft limit set to hard limit (%s) on printer %s.") % (str(softlimit), printer.Name)) 
     214                if not options["noquota"] : 
     215                    if options["softlimit"] : 
     216                        try : 
     217                            softlimit = int(options["softlimit"].strip()) 
     218                            if softlimit < 0 : 
     219                                raise ValueError 
     220                        except ValueError :     
     221                            raise PyKotaCommandLineError, _("Invalid softlimit value %s.") % options["softlimit"] 
     222                    if options["hardlimit"] : 
     223                        try : 
     224                            hardlimit = int(options["hardlimit"].strip()) 
     225                            if hardlimit < 0 : 
     226                                raise ValueError 
     227                        except ValueError :     
     228                            raise PyKotaCommandLineError, _("Invalid hardlimit value %s.") % options["hardlimit"] 
     229                    if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) :         
     230                        # error, exchange them 
     231                        self.printInfo(_("Hard limit %i is less than soft limit %i, values will be exchanged.") % (hardlimit, softlimit)) 
     232                        (softlimit, hardlimit) = (hardlimit, softlimit) 
     233                     
     234                sys.stdout.write(_("Managing print quotas... (this may take a lot of time)")) 
     235                sys.stdout.flush() 
     236                missingusers = {} 
     237                missinggroups = {}     
     238                changed = {} # tracks changes made at the user/group level 
     239                for printer in printers : 
     240                    if not options["noquota"] :     
     241                        if hardlimit is None :     
     242                            hardlimit = softlimit 
     243                            if hardlimit is not None : 
     244                                self.printInfo(_("Undefined hard limit set to soft limit (%s) on printer %s.") % (str(hardlimit), printer.Name)) 
     245                        if softlimit is None :     
     246                            softlimit = hardlimit 
     247                            if softlimit is not None : 
     248                                self.printInfo(_("Undefined soft limit set to hard limit (%s) on printer %s.") % (str(softlimit), printer.Name)) 
     249                                 
     250                    if options["add"] :     
     251                        allentries = []     
     252                        for name in names : 
     253                            email = "" 
     254                            if not options["groups"] : 
     255                                splitname = name.split('/', 1)     # username/email 
     256                                if len(splitname) == 1 : 
     257                                    splitname.append("") 
     258                                (name, email) = splitname 
     259                                if email and (email.count('@') != 1) : 
     260                                    self.printInfo(_("Invalid email address %s") % email) 
     261                                    email = "" 
     262                            entry = getattr(self.storage, "get%s" % suffix)(name) 
     263                            if email and not options["groups"] : 
     264                                entry.Email = email 
     265                            entrypquota = getattr(self.storage, "get%sPQuota" % suffix)(entry, printer) 
     266                            allentries.append((entry, entrypquota)) 
     267                    else :    
     268                        allentries = getattr(self.storage, "getPrinter%ssAndQuotas" % suffix)(printer, names) 
     269                         
     270                    for (entry, entrypquota) in allentries : 
     271                        if not changed.has_key(entry.Name) : 
     272                            changed[entry.Name] = {} 
     273                            if not options["groups"] : 
     274                                changed[entry.Name]["ingroups"] = [] 
     275                                 
     276                        if entry.Exists and (not entrypquota.Exists) : 
     277                            # not found 
     278                            if options["add"] : 
     279                                entrypquota = getattr(self.storage, "add%sPQuota" % suffix)(entry, printer) 
     280                                 
     281                        if not entrypquota.Exists :      
     282                            self.printInfo(_("Quota not found for object %s on printer %s.") % (entry.Name, printer.Name)) 
     283                        else :     
     284                            if options["noquota"] \ 
     285                               or ((softlimit is not None) and (hardlimit is not None)) : 
     286                                entrypquota.setLimits(softlimit, hardlimit) 
     287                                 
     288                            if increase : 
     289                               if (entrypquota.SoftLimit is None) \ 
     290                                   or (entrypquota.HardLimit is None) : 
     291                                   self.printInfo(_("You can't increase limits by %s when no limit is set.") % increase, "error") 
     292                               else : 
     293                                   newsoft = entrypquota.SoftLimit + increase          
     294                                   newhard = entrypquota.HardLimit + increase          
     295                                   if (newsoft >= 0) and (newhard >= 0) : 
     296                                       entrypquota.setLimits(newsoft, newhard) 
     297                                   else :     
     298                                       self.printInfo(_("You can't set negative limits."), "error") 
    217299                             
    218                 if options["add"] :     
    219                     allentries = []     
    220                     for name in names : 
    221                         email = "" 
    222                         if not options["groups"] : 
    223                             splitname = name.split('/', 1)     # username/email 
    224                             if len(splitname) == 1 : 
    225                                 splitname.append("") 
    226                             (name, email) = splitname 
    227                             if email and (email.count('@') != 1) : 
    228                                 self.printInfo(_("Invalid email address %s") % email) 
    229                                 email = "" 
    230                         entry = getattr(self.storage, "get%s" % suffix)(name) 
    231                         if email and not options["groups"] : 
    232                             entry.Email = email 
    233                         entrypquota = getattr(self.storage, "get%sPQuota" % suffix)(entry, printer) 
    234                         allentries.append((entry, entrypquota)) 
    235                 else :    
    236                     allentries = getattr(self.storage, "getPrinter%ssAndQuotas" % suffix)(printer, names) 
    237                      
    238                 for (entry, entrypquota) in allentries : 
    239                     if not changed.has_key(entry.Name) : 
    240                         changed[entry.Name] = {} 
    241                         if not options["groups"] : 
    242                             changed[entry.Name]["ingroups"] = [] 
    243                              
    244                     if entry.Exists and (not entrypquota.Exists) : 
    245                         # not found 
    246                         if options["add"] : 
    247                             entrypquota = getattr(self.storage, "add%sPQuota" % suffix)(entry, printer) 
    248                              
    249                     if not entrypquota.Exists :      
    250                         self.printInfo(_("Quota not found for object %s on printer %s.") % (entry.Name, printer.Name)) 
    251                     else :     
    252                         if options["noquota"] \ 
    253                            or ((softlimit is not None) and (hardlimit is not None)) : 
    254                             entrypquota.setLimits(softlimit, hardlimit) 
    255                              
    256                         if increase : 
    257                            if (entrypquota.SoftLimit is None) \ 
    258                                or (entrypquota.HardLimit is None) : 
    259                                self.printInfo(_("You can't increase limits by %s when no limit is set.") % increase, "error") 
    260                            else : 
    261                                newsoft = entrypquota.SoftLimit + increase          
    262                                newhard = entrypquota.HardLimit + increase          
    263                                if (newsoft >= 0) and (newhard >= 0) : 
    264                                    entrypquota.setLimits(newsoft, newhard) 
    265                                else :     
    266                                    self.printInfo(_("You can't set negative limits."), "error") 
    267                          
    268                         if options["reset"] : 
    269                             entrypquota.reset() 
    270                              
    271                         if options["hardreset"] :     
    272                             entrypquota.hardreset() 
    273                              
    274                         if not options["groups"] : 
    275                             if used : 
    276                                 entrypquota.setUsage(used) 
    277                                  
    278             sys.stdout.write("\nDone.\n")     
     300                            if options["reset"] : 
     301                                entrypquota.reset() 
     302                                 
     303                            if options["hardreset"] :     
     304                                entrypquota.hardreset() 
     305                                 
     306                            if not options["groups"] : 
     307                                if used : 
     308                                    entrypquota.setUsage(used) 
     309                                 
     310        if not options["list"] :                 
     311            self.display("\r100.00%%\r        \r%s\n" % _("Done.")) 
    279312                      
    280313if __name__ == "__main__" :  
    281314    retcode = 0 
    282315    try : 
    283         raise PyKotaCommandLineError, "This version is not stable, download an earlier one. Operation aborted." 
    284316        defaults = { \ 
    285317                     "printer" : "*", \ 
    286318                   } 
    287         short_options = "vhdnagrP:S:H:G:RU:I:" 
     319        short_options = "vhdnagrLP:S:H:G:RU:I:s" 
    288320        long_options = ["help", "version", \ 
    289                         "delete", \ 
     321                        "delete", "list", \ 
    290322                        "noquota", "add", \ 
    291323                        "groups", "reset", "hardreset", \ 
    292324                        "printer=", "softlimit=", "hardlimit=", \ 
    293                         "increase=", "used="] 
     325                        "increase=", "used=", "skipexisting"] 
    294326         
    295327        # Initializes the command line tool 
     
    314346        options["used"] = options["U"] or options["used"] 
    315347        options["increase"] = options["I"] or options["increase"] 
     348        options["list"] = options["L"] or options["list"] 
     349        options["skipexisting"] = options["s"] or options["skipexisting"] 
    316350         
    317351        if options["help"] : 
     
    319353        elif options["version"] : 
    320354            manager.display_version_and_quit() 
    321         elif options["add"] and options["delete"] :     
    322             raise PyKotaCommandLineError, _("incompatible options, see help.") 
    323         elif options["noquota"] and (options["hardlimit"] or options["softlimit"]) : 
    324             raise PyKotaCommandLineError, _("incompatible options, see help.") 
    325         elif options["groups"] and options["used"] : 
     355        elif (options["add"] and options["delete"]) \ 
     356             or (options["noquota"] and (options["hardlimit"] or options["softlimit"])) \ 
     357             or (options["groups"] and options["used"]) \ 
     358             or (options["skipexisting"] and not options["add"]) : 
    326359            raise PyKotaCommandLineError, _("incompatible options, see help.") 
    327360        elif options["delete"] and not args :