Changeset 3167 for pykota

Show
Ignore:
Timestamp:
04/17/07 00:13:44 (18 years ago)
Author:
jerome
Message:

dumpykota --orderby now works with an LDAP backend.

Location:
pykota/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/storages/ldapstorage.py

    r3165 r3167  
    15741574        self.doDelete(code.ident) 
    15751575         
     1576    def sortRecords(self, fields, records, default, ordering) :      
     1577        """Sort records based on list of fields prefixed with '+' (ASC) or '-' (DESC).""" 
     1578        fieldindexes = {} 
     1579        for i in range(len(fields)) : 
     1580            fieldindexes[fields[i]] = i 
     1581        if not ordering :     
     1582            ordering = default 
     1583        orderby = []     
     1584        for orderkey in ordering : 
     1585            if orderkey.startswith("-") : 
     1586                orderby.append((-1, fieldindexes[orderkey[1:]])) 
     1587            elif orderkey.startswith("+") : 
     1588                orderby.append((+1, fieldindexes[orderkey[1:]])) 
     1589            else :     
     1590                orderby.append((+1, fieldindexes[orderkey])) 
     1591                 
     1592        def compare(x, y, orderby=orderby) :     
     1593            """Compares two records.""" 
     1594            i = 0 
     1595            nbkeys = len(orderby) 
     1596            while i < nbkeys : 
     1597                (sign, index) = orderby[i] 
     1598                result = cmp(x[i], y[i]) 
     1599                if not result : 
     1600                    i += 1 
     1601                else :     
     1602                    return sign * result 
     1603            return 0 # identical keys         
     1604             
     1605        records.sort(compare) 
     1606        return records 
     1607         
    15761608    def extractPrinters(self, extractonly={}, ordering=[]) : 
    15771609        """Extracts all printer records.""" 
     
    15791611        entries = [p for p in [self.getPrinter(name) for name in self.getAllPrintersNames(pname)] if p.Exists] 
    15801612        if entries : 
    1581             result = [ ("dn", "printername", "priceperpage", "priceperjob", "description", "maxjobsize", "passthrough") ] 
     1613            fields = ("dn", "printername", "priceperpage", "priceperjob", "description", "maxjobsize", "passthrough") 
     1614            result = [] 
    15821615            for entry in entries : 
    15831616                if entry.PassThrough in (1, "1", "t", "true", "T", "TRUE", "True") : 
     
    15861619                    passthrough = "f" 
    15871620                result.append((entry.ident, entry.Name, entry.PricePerPage, entry.PricePerJob, entry.Description, entry.MaxJobSize, passthrough)) 
    1588             return result  
     1621            return [fields] + self.sortRecords(fields, result, ["+dn"], ordering)  
    15891622         
    15901623    def extractUsers(self, extractonly={}, ordering=[]) : 
     
    15931626        entries = [u for u in [self.getUser(name) for name in self.getAllUsersNames(uname)] if u.Exists] 
    15941627        if entries : 
    1595             result = [ ("dn", "username", "balance", "lifetimepaid", "limitby", "email", "description", "overcharge") ] 
     1628            fields = ("dn", "username", "balance", "lifetimepaid", "limitby", "email", "description", "overcharge") 
     1629            result = [] 
    15961630            for entry in entries : 
    15971631                result.append((entry.ident, entry.Name, entry.AccountBalance, entry.LifeTimePaid, entry.LimitBy, entry.Email, entry.Description, entry.OverCharge)) 
    1598             return result  
     1632            return [fields] + self.sortRecords(fields, result, ["+dn"], ordering) 
    15991633         
    16001634    def extractBillingcodes(self, extractonly={}, ordering=[]) : 
     
    16031637        entries = [b for b in [self.getBillingCode(label) for label in self.getAllBillingCodes(billingcode)] if b.Exists] 
    16041638        if entries : 
    1605             result = [ ("dn", "billingcode", "balance", "pagecounter", "description") ] 
     1639            fields = ("dn", "billingcode", "balance", "pagecounter", "description") 
     1640            result = [] 
    16061641            for entry in entries : 
    16071642                result.append((entry.ident, entry.BillingCode, entry.Balance, entry.PageCounter, entry.Description)) 
    1608             return result  
     1643            return [fields] + self.sortRecords(fields, result, ["+dn"], ordering) 
    16091644         
    16101645    def extractGroups(self, extractonly={}, ordering=[]) : 
     
    16131648        entries = [g for g in [self.getGroup(name) for name in self.getAllGroupsNames(gname)] if g.Exists] 
    16141649        if entries : 
    1615             result = [ ("dn", "groupname", "limitby", "balance", "lifetimepaid", "description") ] 
     1650            fields = ("dn", "groupname", "limitby", "balance", "lifetimepaid", "description") 
     1651            result = [] 
    16161652            for entry in entries : 
    16171653                result.append((entry.ident, entry.Name, entry.LimitBy, entry.AccountBalance, entry.LifeTimePaid, entry.Description)) 
    1618             return result  
     1654            return [fields] + self.sortRecords(fields, result, ["+dn"], ordering) 
    16191655         
    16201656    def extractPayments(self, extractonly={}, ordering=[]) : 
     
    16261662        entries = [u for u in [self.getUser(name) for name in self.getAllUsersNames(uname)] if u.Exists] 
    16271663        if entries : 
    1628             result = [ ("username", "amount", "date", "description") ] 
     1664            fields = ("username", "amount", "date", "description") 
     1665            result = [] 
    16291666            for entry in entries : 
    16301667                for (date, amount, description) in entry.Payments : 
     
    16341671                       ((date >= startdate) and (date <= enddate)) : 
    16351672                        result.append((entry.Name, amount, date, description)) 
    1636             return result         
     1673            return [fields] + self.sortRecords(fields, result, ["+date"], ordering) 
    16371674         
    16381675    def extractUpquotas(self, extractonly={}, ordering=[]) : 
     
    16411678        entries = [p for p in [self.getPrinter(name) for name in self.getAllPrintersNames(pname)] if p.Exists] 
    16421679        if entries : 
    1643             result = [ ("username", "printername", "dn", "userdn", "printerdn", "lifepagecounter", "pagecounter", "softlimit", "hardlimit", "datelimit") ] 
     1680            fields = ("username", "printername", "dn", "userdn", "printerdn", "lifepagecounter", "pagecounter", "softlimit", "hardlimit", "datelimit") 
     1681            result = [] 
    16441682            uname = extractonly.get("username") 
    16451683            for entry in entries : 
    16461684                for (user, userpquota) in self.getPrinterUsersAndQuotas(entry, names=[uname or "*"]) : 
    16471685                    result.append((user.Name, entry.Name, userpquota.ident, user.ident, entry.ident, userpquota.LifePageCounter, userpquota.PageCounter, userpquota.SoftLimit, userpquota.HardLimit, userpquota.DateLimit)) 
    1648             return result 
     1686            return [fields] + self.sortRecords(fields, result, ["+userdn"], ordering) 
    16491687         
    16501688    def extractGpquotas(self, extractonly={}, ordering=[]) : 
     
    16531691        entries = [p for p in [self.getPrinter(name) for name in self.getAllPrintersNames(pname)] if p.Exists] 
    16541692        if entries : 
    1655             result = [ ("groupname", "printername", "dn", "groupdn", "printerdn", "lifepagecounter", "pagecounter", "softlimit", "hardlimit", "datelimit") ] 
     1693            fields = ("groupname", "printername", "dn", "groupdn", "printerdn", "lifepagecounter", "pagecounter", "softlimit", "hardlimit", "datelimit") 
     1694            result = [] 
    16561695            gname = extractonly.get("groupname") 
    16571696            for entry in entries : 
    16581697                for (group, grouppquota) in self.getPrinterGroupsAndQuotas(entry, names=[gname or "*"]) : 
    16591698                    result.append((group.Name, entry.Name, grouppquota.ident, group.ident, entry.ident, grouppquota.LifePageCounter, grouppquota.PageCounter, grouppquota.SoftLimit, grouppquota.HardLimit, grouppquota.DateLimit)) 
    1660             return result 
     1699            return [fields] + self.sortRecords(fields, result, ["+groupdn"], ordering) 
    16611700         
    16621701    def extractUmembers(self, extractonly={}, ordering=[]) : 
     
    16651704        entries = [g for g in [self.getGroup(name) for name in self.getAllGroupsNames(gname)] if g.Exists] 
    16661705        if entries : 
    1667             result = [ ("groupname", "username", "groupdn", "userdn") ] 
     1706            fields = ("groupname", "username", "groupdn", "userdn") 
     1707            result = [] 
    16681708            uname = extractonly.get("username") 
    16691709            for entry in entries : 
     
    16711711                    if (uname is None) or (member.Name == uname) : 
    16721712                        result.append((entry.Name, member.Name, entry.ident, member.ident)) 
    1673             return result         
     1713            return [fields] + self.sortRecords(fields, result, ["+groupdn", "+userdn"], ordering) 
    16741714                 
    16751715    def extractPmembers(self, extractonly={}, ordering=[]) : 
     
    16781718        entries = [p for p in [self.getPrinter(name) for name in self.getAllPrintersNames(pname)] if p.Exists] 
    16791719        if entries : 
    1680             result = [ ("pgroupname", "printername", "pgroupdn", "printerdn") ] 
     1720            fields = ("pgroupname", "printername", "pgroupdn", "printerdn") 
     1721            result = [] 
    16811722            pgname = extractonly.get("pgroupname") 
    16821723            for entry in entries : 
     
    16841725                    if (pgname is None) or (parent.Name == pgname) : 
    16851726                        result.append((parent.Name, entry.Name, parent.ident, entry.ident)) 
    1686             return result         
     1727            return [fields] + self.sortRecords(fields, result, ["+pgroupdn", "+printerdn"], ordering) 
    16871728         
    16881729    def extractHistory(self, extractonly={}, ordering=[]) : 
     
    17031744        entries = self.retrieveHistory(user, printer, hostname=extractonly.get("hostname"), billingcode=extractonly.get("billingcode"), jobid=extractonly.get("jobid"), limit=None, start=startdate, end=enddate) 
    17041745        if entries : 
    1705             result = [ ("username", "printername", "dn", "jobid", "pagecounter", "jobsize", "action", "jobdate", "filename", "title", "copies", "options", "jobprice", "hostname", "jobsizebytes", "md5sum", "pages", "billingcode", "precomputedjobsize", "precomputedjobprice") ]  
     1746            fields = ("username", "printername", "dn", "jobid", "pagecounter", "jobsize", "action", "jobdate", "filename", "title", "copies", "options", "jobprice", "hostname", "jobsizebytes", "md5sum", "pages", "billingcode", "precomputedjobsize", "precomputedjobprice") 
     1747            result = [] 
    17061748            for entry in entries : 
    17071749                result.append((entry.UserName, entry.PrinterName, entry.ident, entry.JobId, entry.PrinterPageCounter, entry.JobSize, entry.JobAction, entry.JobDate, entry.JobFileName, entry.JobTitle, entry.JobCopies, entry.JobOptions, entry.JobPrice, entry.JobHostName, entry.JobSizeBytes, entry.JobMD5Sum, entry.JobPages, entry.JobBillingCode, entry.PrecomputedJobSize, entry.PrecomputedJobPrice))  
    1708             return result 
     1750            return [fields] + self.sortRecords(fields, result, ["+dn"], ordering) 
    17091751             
    17101752    def getBillingCodeFromBackend(self, label) : 
  • pykota/trunk/TODO

    r3165 r3167  
    2929          normal page accounting. 
    3030           
    31         - --orderby and --order ASC|DESC for dumpykota (or --asc | --desc) 
    32           Now works for SQL backends, not with LDAP yet. 
    33          
    3431        - Document the PYKOTA_HOME environment variable. 
    3532