Changeset 3521

Show
Ignore:
Timestamp:
04/15/10 01:21:11 (15 years ago)
Author:
jerome
Message:

Removed some code specific to Python v2.2 and earlier.
Improved the work done by bse@… to fix #52.
IMPORTANT : the same optimisation is currently done for users only, not for
users groups, printers or billing codes. And more importantly the code
was not ported to the LDAP backend. I need more time to do all this.

Location:
pykota/trunk/pykota/storages
Files:
2 modified

Legend:

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

    r3489 r3521  
    704704        if result : 
    705705            patterns = printerpattern.split(",") 
    706             try : 
    707                 patdict = {}.fromkeys(patterns) 
    708             except AttributeError : 
    709                 # Python v2.2 or earlier 
    710                 patdict = {} 
    711                 for p in patterns : 
    712                     patdict[p] = None 
     706            patdict = {}.fromkeys(patterns) 
    713707            for (printerid, fields) in result : 
    714708                printername = databaseToUnicode(fields.get("pykotaPrinterName", [""])[0] or fields.get(self.info["printerrdn"], [""])[0]) 
     
    740734        if result : 
    741735            patterns = userpattern.split(",") 
    742             try : 
    743                 patdict = {}.fromkeys(patterns) 
    744             except AttributeError : 
    745                 # Python v2.2 or earlier 
    746                 patdict = {} 
    747                 for p in patterns : 
    748                     patdict[p] = None 
     736            patdict = {}.fromkeys(patterns) 
    749737            for (userid, fields) in result : 
    750738                username = databaseToUnicode(fields.get("pykotaUserName", [""])[0] or fields.get(self.info["userrdn"], [""])[0]) 
     
    807795        if result : 
    808796            patterns = grouppattern.split(",") 
    809             try : 
    810                 patdict = {}.fromkeys(patterns) 
    811             except AttributeError : 
    812                 # Python v2.2 or earlier 
    813                 patdict = {} 
    814                 for p in patterns : 
    815                     patdict[p] = None 
     797            patdict = {}.fromkeys(patterns) 
    816798            for (groupid, fields) in result : 
    817799                groupname = databaseToUnicode(fields.get("pykotaGroupName", [""])[0] or fields.get(self.info["grouprdn"], [""])[0]) 
     
    18231805        if result : 
    18241806            patterns = billingcodepattern.split(",") 
    1825             try : 
    1826                 patdict = {}.fromkeys(patterns) 
    1827             except AttributeError : 
    1828                 # Python v2.2 or earlier 
    1829                 patdict = {} 
    1830                 for p in patterns : 
    1831                     patdict[p] = None 
     1807            patdict = {}.fromkeys(patterns) 
    18321808            for (codeid, fields) in result : 
    18331809                codename = databaseToUnicode(fields.get("pykotaBillingCode", [""])[0]) 
  • pykota/trunk/pykota/storages/sql.py

    r3489 r3521  
    2828 
    2929from pykota.utils import * 
     30 
     31MAXINNAMES = 500 # Maximum number of non-patterns names to use in a single IN statement 
    3032 
    3133class SQLStorage : 
     
    445447        return pgroups 
    446448 
     449    def hasWildCards(self, pattern) : 
     450        """Returns True if the pattern contains wildcards, else False.""" 
     451        specialchars = "*?[!" # no need to check for ] since [ would be there first 
     452        for specialchar in specialchars : 
     453            if specialchar in pattern : 
     454                return True 
     455        return False 
     456 
    447457    def getMatchingPrinters(self, printerpattern) : 
    448458        """Returns the list of all printers for which name matches a certain pattern.""" 
     
    454464        if result : 
    455465            patterns = printerpattern.split(",") 
    456             try : 
    457                 patdict = {}.fromkeys(patterns) 
    458             except AttributeError : 
    459                 # Python v2.2 or earlier 
    460                 patdict = {} 
    461                 for p in patterns : 
    462                     patdict[p] = None 
     466            patdict = {}.fromkeys(patterns) 
    463467            for record in result : 
    464468                pname = databaseToUnicode(record["printername"]) 
     
    475479        # but we don't because other storages semantics may be different, so every 
    476480        # storage should use fnmatch to match patterns and be storage agnostic 
    477         result = self.doSearch("SELECT * FROM users") 
    478         if result : 
    479             patterns = userpattern.split(",") 
    480             try : 
    481                 patdict = {}.fromkeys(patterns) 
    482             except AttributeError : 
    483                 # Python v2.2 or earlier 
    484                 patdict = {} 
    485                 for p in patterns : 
    486                     patdict[p] = None 
    487             for record in result : 
    488                 uname = databaseToUnicode(record["username"]) 
    489                 if patdict.has_key(uname) or self.tool.matchString(uname, patterns) : 
    490                     user = self.storageUserFromRecord(uname, record) 
    491                     users.append(user) 
    492                     self.cacheEntry("USERS", user.Name, user) 
     481        # 
     482        # This doesn't prevent us from being smarter, thanks to bse@chalmers.se 
     483        userpattern = userpattern or "*" 
     484        patterns = userpattern.split(",") 
     485        patdict = {}.fromkeys(patterns) 
     486        patterns = patdict.keys() # Ensures the uniqueness of each pattern, but we lose the cmd line ordering 
     487        # BEWARE : if a single pattern contains wild cards, we'll still use the slow route. 
     488        if self.hasWildCards(userpattern) : 
     489            # Slow route 
     490            result = self.doSearch("SELECT * FROM users") 
     491            if result : 
     492                for record in result : 
     493                    uname = databaseToUnicode(record["username"]) 
     494                    if patdict.has_key(uname) or self.tool.matchString(uname, patterns) : 
     495                        user = self.storageUserFromRecord(uname, record) 
     496                        users.append(user) 
     497                        self.cacheEntry("USERS", user.Name, user) 
     498        else : 
     499            # Fast route (probably not faster with a few users) 
     500            while patterns : 
     501                subset = patterns[:MAXINNAMES] 
     502                nbpatterns = len(subset) 
     503                if nbpatterns == 1 : 
     504                    wherestmt = "username=%s" % self.doQuote(unicodeToDatabase(subset[0])) 
     505                else : 
     506                    wherestmt = "username IN (%s)" % ",".join([self.doQuote(unicodeToDatabase(p)) for p in subset]) 
     507                result = self.doSearch("SELECT * FROM users WHERE %s" % wherestmt) 
     508                if result : 
     509                    for record in result : 
     510                        uname = databaseToUnicode(record["username"]) 
     511                        user = self.storageUserFromRecord(uname, record) 
     512                        users.append(user) 
     513                        self.cacheEntry("USERS", user.Name, user) 
     514                patterns = patterns[MAXINNAMES:] 
     515        users.sort(key=lambda u : u.Name) # Adds some ordering, we've already lost the cmd line one anyway. 
    493516        return users 
    494517 
     
    502525        if result : 
    503526            patterns = grouppattern.split(",") 
    504             try : 
    505                 patdict = {}.fromkeys(patterns) 
    506             except AttributeError : 
    507                 # Python v2.2 or earlier 
    508                 patdict = {} 
    509                 for p in patterns : 
    510                     patdict[p] = None 
     527            patdict = {}.fromkeys(patterns) 
    511528            for record in result : 
    512529                gname = databaseToUnicode(record["groupname"]) 
     
    523540        if result : 
    524541            patterns = billingcodepattern.split(",") 
    525             try : 
    526                 patdict = {}.fromkeys(patterns) 
    527             except AttributeError : 
    528                 # Python v2.2 or earlier 
    529                 patdict = {} 
    530                 for p in patterns : 
    531                     patdict[p] = None 
     542            patdict = {}.fromkeys(patterns) 
    532543            for record in result : 
    533544                codename = databaseToUnicode(record["billingcode"])