Changeset 3524

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

Improved the retrival of printers and billingcodes as well as
users. References #52. For users groups it will be harder to do, next
time :-)

Location:
pykota/trunk/pykota
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/storage.py

    r3489 r3524  
    542542    def __init__(self, pykotatool) : 
    543543        """Opens the storage connection.""" 
    544         self.closed = 1 
     544        self.closed = True 
    545545        self.tool = pykotatool 
    546546        self.usecache = pykotatool.config.getCaching() 
     
    571571        """Logs a database query, where all queries are already UTF-8 encoded.""" 
    572572        self.tool.logdebug(qmsg.decode("UTF-8", "replace")) 
     573 
     574    def hasWildCards(self, pattern) : 
     575        """Returns True if the pattern contains wildcards, else False.""" 
     576        specialchars = "*?[!" # no need to check for ] since [ would be there first 
     577        for specialchar in specialchars : 
     578            if specialchar in pattern : 
     579                return True 
     580        return False 
    573581 
    574582    def getFromCache(self, cachetype, key) : 
  • pykota/trunk/pykota/storages/sql.py

    r3521 r3524  
    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 = databaseToUnicode(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 = 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) 
     469                    name = databaseToUnicode(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(unicodeToDatabase(subset[0])) 
     480                    wherestmt = "%s=%s" % (keyname, self.doQuote(unicodeToDatabase(subset[0]))) 
    505481                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) 
     482                    wherestmt = "%s IN (%s)" % (keyname, 
     483                                                ",".join([self.doQuote(unicodeToDatabase(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 = databaseToUnicode(record["username"]) 
    511                         user = self.storageUserFromRecord(uname, record) 
    512                         users.append(user) 
    513                         self.cacheEntry("USERS", user.Name, user) 
     488                        name = databaseToUnicode(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 = databaseToUnicode(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=["*"]) :