Show
Ignore:
Timestamp:
06/25/03 16:10:01 (21 years ago)
Author:
jalet
Message:

Hey, it may work (edpykota --reset excepted) !

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/edpykota

    r1031 r1041  
    2323# 
    2424# $Log$ 
     25# Revision 1.47  2003/06/25 14:10:01  jalet 
     26# Hey, it may work (edpykota --reset excepted) ! 
     27# 
    2528# Revision 1.46  2003/06/16 11:59:09  jalet 
    2629# More work on LDAP 
     
    330333    def main(self, names, options) : 
    331334        """Edit user or group quotas.""" 
    332         printeradded = 0 
    333         printers = self.storage.getMatchingPrinters(options["printer"]) 
    334         if not printers : 
    335             pname = options["printer"] 
    336             if options["add"] and pname : 
    337                 if self.isValidName(pname) : 
    338                     printerid = self.storage.addPrinter(pname) 
    339                     printers = [ (printerid, pname) ] 
    340                     printeradded = 1 
    341                 else :     
    342                     raise PyKotaToolError, _("Invalid printer name %s") % pname 
    343             else : 
    344                 raise PyKotaToolError, _("There's no printer matching %s") % pname 
    345                  
     335         
     336        suffix = (options["groups"] and "Group") or "User"         
     337         
    346338        softlimit = hardlimit = None     
    347         if options["softlimit"] : 
    348             try : 
    349                 softlimit = int(options["softlimit"].strip()) 
    350             except ValueError :     
    351                 raise PyKotaToolError, _("Invalid softlimit value %s.") % options["softlimit"] 
    352         if options["hardlimit"] : 
    353             try : 
    354                 hardlimit = int(options["hardlimit"].strip()) 
    355             except ValueError :     
    356                 raise PyKotaToolError, _("Invalid hardlimit value %s.") % options["hardlimit"] 
    357         if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) :         
    358             # error, exchange them 
    359             self.logger.log_message(_("Hard limit %i is less than soft limit %i, values will be exchanged.") % (hardlimit, softlimit)) 
    360             (softlimit, hardlimit) = (hardlimit, softlimit) 
     339        if not options["noquota"] : 
     340            if options["softlimit"] : 
     341                try : 
     342                    softlimit = int(options["softlimit"].strip()) 
     343                except ValueError :     
     344                    raise PyKotaToolError, _("Invalid softlimit value %s.") % options["softlimit"] 
     345            if options["hardlimit"] : 
     346                try : 
     347                    hardlimit = int(options["hardlimit"].strip()) 
     348                except ValueError :     
     349                    raise PyKotaToolError, _("Invalid hardlimit value %s.") % options["hardlimit"] 
     350            if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) :         
     351                # error, exchange them 
     352                self.logger.log_message(_("Hard limit %i is less than soft limit %i, values will be exchanged.") % (hardlimit, softlimit)) 
     353                (softlimit, hardlimit) = (hardlimit, softlimit) 
    361354             
    362         if not names :     
    363             if options["add"] and not printeradded : 
    364                 raise PyKotaToolError, _("You have to pass user or group names on the command line") 
    365             else :     
    366                 names = [ "*" ] # all users 
    367                  
    368355        balance = options["balance"] 
    369356        if balance : 
     
    394381            groupnames = [] 
    395382             
    396         uwaschanged = {}         # tracks changed made at the user level 
    397         gwaschanged = {}         # tracks changed made at the group level 
    398         for (printerid, printer) in printers : 
     383        if options["prototype"] :     
     384            protoentry = getattr(self.storage, "get%s" % suffix)(options["prototype"]) 
     385             
     386        printeradded = 0 
     387        printers = self.storage.getMatchingPrinters(options["printer"]) 
     388        if not printers : 
     389            pname = options["printer"] 
     390            if options["add"] and pname : 
     391                if self.isValidName(pname) : 
     392                    printers = [ self.storage.addPrinter(pname) ] 
     393                    if printers[0].Exists : 
     394                        printeradded = 1 
     395                    else :     
     396                        raise PyKotaToolError, _("Impossible to add printer %s") % pname 
     397                else :     
     398                    raise PyKotaToolError, _("Invalid printer name %s") % pname 
     399            else : 
     400                raise PyKotaToolError, _("There's no printer matching %s") % pname 
     401        if not names :     
     402            if options["add"] and not printeradded : 
     403                raise PyKotaToolError, _("You have to pass user or group names on the command line") 
     404            else :     
     405                names = [ "*" ] # all users 
     406                 
     407        changed = {} # tracks changes made at the user/group level 
     408        for printer in printers : 
    399409            if options["charge"] : 
    400410                (perpage, perjob) = charges 
    401                 if perjob is None : 
    402                     # we don't want to change this one, get the old value 
    403                     (dummy, perjob) = self.storage.getPrinterPrices(printerid) 
    404                 self.storage.setPrinterPrices(printerid, perpage, perjob)     
     411                printer.setPrices(perpage, perjob)     
     412                 
    405413            if options["prototype"] : 
    406                 if options["groups"] : 
    407                     prototype = self.storage.getGroupPQuota(self.storage.getGroupId(options["prototype"]), printerid) 
    408                 else :      
    409                     # default is user quota edition 
    410                     prototype = self.storage.getUserPQuota(self.storage.getUserId(options["prototype"]), printerid) 
    411                 if prototype is None : 
    412                     self.logger.log_message(_("Prototype %s not found in Quota Storage for printer %s.") % (options["prototype"], printer)) 
    413                     continue    # skip this printer 
    414                 else :     
    415                     (softlimit, hardlimit) = (prototype["softlimit"], prototype["hardlimit"]) 
    416             if hardlimit is None :     
    417                 hardlimit = softlimit 
    418                 if hardlimit is not None : 
    419                     self.logger.log_message(_("Undefined hard limit set to soft limit (%s) on printer %s.") % (str(hardlimit), printer)) 
    420             if softlimit is None :     
    421                 softlimit = hardlimit 
    422                 if softlimit is not None : 
    423                     self.logger.log_message(_("Undefined soft limit set to hard limit (%s) on printer %s.") % (str(softlimit), printer)) 
    424             if (not options["ingroups"] and not options["reset"] and not options["noquota"] and not options["prototype"] and not options["limitby"] and not options["balance"] and not options["delete"] and not options["charge"]) and ((hardlimit is None) or (softlimit is None)) : 
    425                 raise PyKotaToolError, _("Both hard and soft limits must be set ! Aborting.") 
     414                if protoentry.Exists : 
     415                    protoquota = getattr(self.storage, "get%PQuota" % suffix)(protoentry, printer) 
     416                    if not protoquota.Exists : 
     417                        self.logger.log_message(_("Prototype %s not found in Quota Storage for printer %s.") % (protoentry.Name, printer.Name)) 
     418                        continue    # skip this printer 
     419                    else :     
     420                        (softlimit, hardlimit) = (protoquota.SoftLimit, protoquota.HardLimit) 
     421                else :         
     422                    self.logger.log_message(_("Prototype object %s not found in Quota Storage.") % protoentry.Name) 
     423            if not options["noquota"] :     
     424                if hardlimit is None :     
     425                    hardlimit = softlimit 
     426                    if hardlimit is not None : 
     427                        self.logger.log_message(_("Undefined hard limit set to soft limit (%s) on printer %s.") % (str(hardlimit), printer.Name)) 
     428                if softlimit is None :     
     429                    softlimit = hardlimit 
     430                    if softlimit is not None : 
     431                        self.logger.log_message(_("Undefined soft limit set to hard limit (%s) on printer %s.") % (str(softlimit), printer.Name)) 
     432                         
    426433            if options["add"] :     
    427                 if options["groups"] :     
    428                     allidnames = [(self.storage.getGroupId(n), n) for n in names] 
    429                 else : 
    430                     allidnames = [(self.storage.getUserId(n), n) for n in names] 
     434                allentries = []     
     435                for name in names : 
     436                    entry = getattr(self.storage, "get%s" % suffix)(name) 
     437                    entrypquota = getattr(self.storage, "get%sPQuota" % suffix)(entry, printer) 
     438                    allentries.append((entry, entrypquota)) 
    431439            else :    
    432                 if options["groups"] :     
    433                     allidnames = self.storage.getPrinterGroups(printerid) or [] 
    434                 else :     
    435                     allidnames = self.storage.getPrinterUsers(printerid) or [] 
    436             for (ident, name) in [(i, n) for (i, n) in allidnames if self.matchString(n, names)]: 
    437                 if options["groups"] : 
    438                     if not gwaschanged.has_key(name) : 
    439                         gwaschanged[name] = {} 
    440                     quota = self.storage.getGroupPQuota(ident, printerid) 
    441                 else : 
    442                     if not uwaschanged.has_key(name) : 
    443                         uwaschanged[name] = {"ingroups": []} 
    444                     quota = self.storage.getUserPQuota(ident, printerid) 
    445                 if quota is None : 
     440                allentries = getattr(self.storage, "getPrinter%ssAndQuotas" % suffix)(printer) 
     441                 
     442            for (entry, entrypquota) in [(e, q) for (e, q) in allentries if self.matchString(e.Name, names)] : 
     443                if not changed.has_key(entry.Name) : 
     444                    changed[entry.Name] = {} 
     445                if not entrypquota.Exists : 
    446446                    # not found 
    447447                    if options["add"] : 
     
    450450                        # like /etc/passwd because users may be defined  
    451451                        # only remotely 
    452                         if options["groups"] : 
    453                             if self.isValidName(name) : 
    454                                 (ident, printerid) = self.storage.addGroupPQuota(name, printerid) 
    455                                 quota = self.storage.getGroupPQuota(ident, printerid) 
     452                        if self.isValidName(entry.Name) : 
     453                            if not entry.Exists : 
     454                                entry = getattr(self.storage, "add%s" % suffix)(entry) 
     455                            entrypquota = getattr(self.storage, "add%sPQuota" % suffix)(entry, printer) 
     456                        else :     
     457                            if options["groups"] : 
     458                                self.logger.log_message(_("Invalid group name %s") % entry.Name) 
    456459                            else :     
    457                                 self.logger.log_message(_("Invalid group name %s") % name) 
    458                         else : 
    459                             if self.isValidName(name) : 
    460                                 (ident, printerid) = self.storage.addUserPQuota(name, printerid) 
    461                                 quota = self.storage.getUserPQuota(ident, printerid) 
    462                             else :     
    463                                 self.logger.log_message(_("Invalid user name %s") % name) 
    464                 if quota is None :      
    465                     self.logger.log_message(_("Quota not found for object %s on printer %s.") % (name, printer)) 
     460                                self.logger.log_message(_("Invalid user name %s") % entry.Name) 
     461                if not entrypquota.Exists :      
     462                    self.logger.log_message(_("Quota not found for object %s on printer %s.") % (entry.Name, printer.Name)) 
    466463                else :     
    467464                    if options["delete"] : 
    468                         if options["groups"] : 
    469                             self.storage.deleteGroup(ident) 
    470                         else : 
    471                             self.storage.deleteUser(ident) 
     465                        entry.delete() 
    472466                    else : 
    473                         if options["groups"] : 
    474                             if options["noquota"] or options["prototype"] or ((softlimit is not None) and (hardlimit is not None)) : 
    475                                 self.storage.setGroupPQuota(ident, printerid, softlimit, hardlimit) 
     467                        if options["noquota"] or options["prototype"] or ((softlimit is not None) and (hardlimit is not None)) : 
     468                            entrypquota.setLimits(softlimit, hardlimit) 
     469                        if limitby : 
     470                            if changed[entry.Name].get("limitby") is None : 
     471                                entry.setLimitBy(limitby) 
     472                                changed[entry.Name]["limitby"] = limitby 
     473                         
     474                        if not options["groups"] : 
    476475                            if options["reset"] : 
    477                                 self.storage.resetGroupPQuota(ident, printerid) 
    478                             if limitby : 
    479                                 if gwaschanged[name].get("limitby") is None : 
    480                                     self.storage.limitGroupBy(ident, limitby) 
    481                                     gwaschanged[name]["limitby"] = limitby 
    482                             self.warnGroupPQuota(name, printer)     
    483                         else : 
    484                             if options["noquota"] or options["prototype"] or ((softlimit is not None) and (hardlimit is not None)) : 
    485                                 self.storage.setUserPQuota(ident, printerid, softlimit, hardlimit) 
    486                             if options["reset"] : 
    487                                 self.storage.resetUserPQuota(ident, printerid) 
    488                             if limitby : 
    489                                 if uwaschanged[name].get("limitby") is None : 
    490                                     self.storage.limitUserBy(ident, limitby) 
    491                                     uwaschanged[name]["limitby"] = limitby 
     476                                entrypquota.reset() 
     477                                 
    492478                            if balance : 
    493                                 if uwaschanged[name].get("balance") is None : 
     479                                if changed[entry.Name].get("balance") is None : 
    494480                                    if balance.startswith("+") or balance.startswith("-") : 
    495                                         self.storage.increaseUserBalance(ident, balancevalue) 
     481                                        entry.consumeAccountBalance(-balancevalue) 
    496482                                    else : 
    497                                         self.storage.setUserBalance(ident, balancevalue) 
    498                                     uwaschanged[name]["balance"] = balance 
     483                                        entry.consumeAccountBalance(float(entry.AccountBalance or 0.0) - balancevalue) 
     484                                    changed[entry.Name]["balance"] = balance 
    499485                            for groupname in groupnames :         
    500                                 if groupname not in uwaschanged[name]["ingroups"] : 
    501                                     groupid = self.storage.getGroupId(groupname) 
    502                                     if groupid is not None : 
    503                                         self.storage.addUserToGroup(ident, groupid) 
    504                                         uwaschanged[name]["ingroups"].append(groupname) 
     486                                if groupname not in changed[name]["ingroups"] : 
     487                                    group = self.storage.getGroup(groupname) 
     488                                    if group.Exists : 
     489                                        self.storage.addUserToGroup(entry, group) 
     490                                        changed[entry.Name]["ingroups"].append(groupname) 
    505491                                    else : 
    506492                                        self.logger.log_message(_("Group %s not found in the PyKota Storage.") % groupname) 
    507                             self.warnUserPQuota(name, printer)     
     493                                         
     494                        getattr(self, "warn%sPQuota" % suffix)(entrypquota)     
    508495                      
    509496if __name__ == "__main__" :