Changeset 3525 for pykota/branches

Show
Ignore:
Timestamp:
04/16/10 02:56:51 (15 years ago)
Author:
jerome
Message:

Backported the improvement of the fix for #52. References #52.

Location:
pykota/branches/1.26_fixes/pykota
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • pykota/branches/1.26_fixes/pykota/storage.py

    r3444 r3525  
    594594        """Ensures that the database connection is closed.""" 
    595595        self.close() 
     596 
     597    def hasWildCards(self, pattern) : 
     598        """Returns True if the pattern contains wildcards, else False.""" 
     599        specialchars = "*?[!" # no need to check for ] since [ would be there first 
     600        for specialchar in specialchars : 
     601            if specialchar in pattern : 
     602                return True 
     603        return False 
    596604 
    597605    def getFromCache(self, cachetype, key) : 
  • pykota/branches/1.26_fixes/pykota/storages/sql.py

    r3522 r3525  
    447447        return pgroups 
    448448 
    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  
    457     def getMatchingPrinters(self, printerpattern) : 
    458         """Returns the list of all printers for which name matches a certain pattern.""" 
    459         printers = [] 
    460         # We 'could' do a SELECT printername FROM printers WHERE printername LIKE ... 
    461         # but we don't because other storages semantics may be different, so every 
    462         # storage should use fnmatch to match patterns and be storage agnostic 
    463         result = self.doSearch("SELECT * FROM printers") 
    464         if result : 
    465             patterns = printerpattern.split(",") 
    466             patdict = {}.fromkeys(patterns) 
    467             for record in result : 
    468                 pname = self.databaseToUserCharset(record["printername"]) 
    469                 if patdict.has_key(pname) or self.tool.matchString(pname, patterns) : 
    470                     printer = self.storagePrinterFromRecord(pname, record) 
    471                     printers.append(printer) 
    472                     self.cacheEntry("PRINTERS", printer.Name, printer) 
    473         return printers 
    474  
    475     def getMatchingUsers(self, userpattern) : 
    476         """Returns the list of all users for which name matches a certain pattern.""" 
    477         users = [] 
    478         # We 'could' do a SELECT username FROM users WHERE username LIKE ... 
     449    def getMatchingStuff(self, pattern, tablename, entrytype, keyname) : 
     450        """Returns the list of all entries for which the name matches a certain pattern.""" 
     451        entries = [] 
     452        # We 'could' do a SELECT xxxxname FROM xxxx WHERE xxxxname LIKE ... 
    479453        # but we don't because other storages semantics may be different, so every 
    480454        # storage should use fnmatch to match patterns and be storage agnostic 
    481455        # 
    482456        # This doesn't prevent us from being smarter, thanks to bse@chalmers.se 
    483         userpattern = userpattern or "*" 
    484         patterns = userpattern.split(",") 
     457        pattern = pattern or "*" 
     458        patterns = pattern.split(",") 
    485459        patdict = {}.fromkeys(patterns) 
    486460        patterns = patdict.keys() # Ensures the uniqueness of each pattern, but we lose the cmd line ordering 
    487461        # BEWARE : if a single pattern contains wild cards, we'll still use the slow route. 
    488         if self.hasWildCards(userpattern) : 
     462        storageEntryFromRecord = getattr(self, "storage%sFromRecord" % entrytype) 
     463        cachename = tablename.upper() 
     464        if self.hasWildCards(pattern) : 
    489465            # Slow route 
    490             result = self.doSearch("SELECT * FROM users") 
     466            result = self.doSearch("SELECT * FROM %s" % tablename) 
    491467            if result : 
    492468                for record in result : 
    493                     uname = self.databaseToUserCharset(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) 
     469                    name = self.databaseToUserCharset(record[keyname]) 
     470                    if patdict.has_key(name) or self.tool.matchString(name, patterns) : 
     471                        entry = storageEntryFromRecord(name, record) 
     472                        entries.append(entry) 
     473                        self.cacheEntry(cachename, entry.Name, entry) 
     474        else : 
     475            # Fast route (probably not faster with a few entries) 
    500476            while patterns : 
    501477                subset = patterns[:MAXINNAMES] 
    502478                nbpatterns = len(subset) 
    503479                if nbpatterns == 1 : 
    504                     wherestmt = "username=%s" % self.doQuote(self.userCharsetToDatabase(subset[0])) 
     480                    wherestmt = "%s=%s" % (keyname, self.doQuote(self.userCharsetToDatabase(subset[0]))) 
    505481                else : 
    506                     wherestmt = "username IN (%s)" % ",".join([self.doQuote(self.userCharsetToDatabase(p)) for p in subset]) 
    507                 result = self.doSearch("SELECT * FROM users WHERE %s" % wherestmt) 
     482                    wherestmt = "%s IN (%s)" % (keyname, 
     483                                                ",".join([self.doQuote(self.userCharsetToDatabase(p)) for p in subset])) 
     484                result = self.doSearch("SELECT * FROM %s WHERE %s" % (tablename, 
     485                                                                      wherestmt)) 
    508486                if result : 
    509487                    for record in result : 
    510                         uname = self.databaseToUserCharset(record["username"]) 
    511                         user = self.storageUserFromRecord(uname, record) 
    512                         users.append(user) 
    513                         self.cacheEntry("USERS", user.Name, user) 
     488                        name = self.databaseToUserCharset(record[keyname]) 
     489                        entry = storageEntryFromRecord(name, record) 
     490                        entries.append(entry) 
     491                        self.cacheEntry(cachename, entry.Name, entry) 
    514492                patterns = patterns[MAXINNAMES:] 
    515         users.sort(key=lambda u : u.Name) # Adds some ordering, we've already lost the cmd line one anyway. 
    516         return users 
     493        entries.sort(key=lambda e : e.Name) # Adds some ordering, we've already lost the cmd line one anyway. 
     494        return entries 
     495 
     496    def getMatchingPrinters(self, pattern) : 
     497        """Returns the list of all printers for which name matches a certain pattern.""" 
     498        return self.getMatchingStuff(pattern, "printers", "Printer", "printername") 
     499 
     500    def getMatchingUsers(self, pattern) : 
     501        """Returns the list of all users for which name matches a certain pattern.""" 
     502        return self.getMatchingStuff(pattern, "users", "User", "username") 
     503 
     504    def getMatchingBillingCodes(self, pattern) : 
     505        """Returns the list of all billing codes for which the label matches a certain pattern.""" 
     506        return self.getMatchingStuff(pattern, "billingcodes", "BillingCode", "billingcode") 
    517507 
    518508    def getMatchingGroups(self, grouppattern) : 
     
    533523                    self.cacheEntry("GROUPS", group.Name, group) 
    534524        return groups 
    535  
    536     def getMatchingBillingCodes(self, billingcodepattern) : 
    537         """Returns the list of all billing codes for which the label matches a certain pattern.""" 
    538         codes = [] 
    539         result = self.doSearch("SELECT * FROM billingcodes") 
    540         if result : 
    541             patterns = billingcodepattern.split(",") 
    542             patdict = {}.fromkeys(patterns) 
    543             for record in result : 
    544                 codename = self.databaseToUserCharset(record["billingcode"]) 
    545                 if patdict.has_key(codename) or self.tool.matchString(codename, patterns) : 
    546                     code = self.storageBillingCodeFromRecord(codename, record) 
    547                     codes.append(code) 
    548                     self.cacheEntry("BILLINGCODES", code.BillingCode, code) 
    549         return codes 
    550525 
    551526    def getPrinterUsersAndQuotas(self, printer, names=["*"]) :