- Timestamp:
- 04/15/10 01:21:11 (15 years ago)
- Location:
- pykota/trunk/pykota/storages
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/storages/ldapstorage.py
r3489 r3521 704 704 if result : 705 705 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) 713 707 for (printerid, fields) in result : 714 708 printername = databaseToUnicode(fields.get("pykotaPrinterName", [""])[0] or fields.get(self.info["printerrdn"], [""])[0]) … … 740 734 if result : 741 735 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) 749 737 for (userid, fields) in result : 750 738 username = databaseToUnicode(fields.get("pykotaUserName", [""])[0] or fields.get(self.info["userrdn"], [""])[0]) … … 807 795 if result : 808 796 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) 816 798 for (groupid, fields) in result : 817 799 groupname = databaseToUnicode(fields.get("pykotaGroupName", [""])[0] or fields.get(self.info["grouprdn"], [""])[0]) … … 1823 1805 if result : 1824 1806 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) 1832 1808 for (codeid, fields) in result : 1833 1809 codename = databaseToUnicode(fields.get("pykotaBillingCode", [""])[0]) -
pykota/trunk/pykota/storages/sql.py
r3489 r3521 28 28 29 29 from pykota.utils import * 30 31 MAXINNAMES = 500 # Maximum number of non-patterns names to use in a single IN statement 30 32 31 33 class SQLStorage : … … 445 447 return pgroups 446 448 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 447 457 def getMatchingPrinters(self, printerpattern) : 448 458 """Returns the list of all printers for which name matches a certain pattern.""" … … 454 464 if result : 455 465 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) 463 467 for record in result : 464 468 pname = databaseToUnicode(record["printername"]) … … 475 479 # but we don't because other storages semantics may be different, so every 476 480 # 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. 493 516 return users 494 517 … … 502 525 if result : 503 526 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) 511 528 for record in result : 512 529 gname = databaseToUnicode(record["groupname"]) … … 523 540 if result : 524 541 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) 532 543 for record in result : 533 544 codename = databaseToUnicode(record["billingcode"])