| OLD | NEW |
| 1 # Authors: |
| 2 # Trevor Perrin |
| 3 # Martin von Loewis - python 3 port |
| 4 # |
| 5 # See the LICENSE file for legal information regarding use of this file. |
| 6 |
| 1 """Base class for SharedKeyDB and VerifierDB.""" | 7 """Base class for SharedKeyDB and VerifierDB.""" |
| 2 | 8 |
| 3 import anydbm | 9 try: |
| 4 import thread | 10 import anydbm |
| 11 except ImportError: |
| 12 # Python 3 |
| 13 import dbm as anydbm |
| 14 import threading |
| 5 | 15 |
| 6 class BaseDB: | 16 class BaseDB(object): |
| 7 def __init__(self, filename, type): | 17 def __init__(self, filename, type): |
| 8 self.type = type | 18 self.type = type |
| 9 self.filename = filename | 19 self.filename = filename |
| 10 if self.filename: | 20 if self.filename: |
| 11 self.db = None | 21 self.db = None |
| 12 else: | 22 else: |
| 13 self.db = {} | 23 self.db = {} |
| 14 self.lock = thread.allocate_lock() | 24 self.lock = threading.Lock() |
| 15 | 25 |
| 16 def create(self): | 26 def create(self): |
| 17 """Create a new on-disk database. | 27 """Create a new on-disk database. |
| 18 | 28 |
| 19 @raise anydbm.error: If there's a problem creating the database. | 29 @raise anydbm.error: If there's a problem creating the database. |
| 20 """ | 30 """ |
| 21 if self.filename: | 31 if self.filename: |
| 22 self.db = anydbm.open(self.filename, "n") #raises anydbm.error | 32 self.db = anydbm.open(self.filename, "n") #raises anydbm.error |
| 23 self.db["--Reserved--type"] = self.type | 33 self.db["--Reserved--type"] = self.type |
| 24 self.db.sync() | 34 self.db.sync() |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 """ | 120 """ |
| 111 if self.db == None: | 121 if self.db == None: |
| 112 raise AssertionError("DB not open") | 122 raise AssertionError("DB not open") |
| 113 | 123 |
| 114 self.lock.acquire() | 124 self.lock.acquire() |
| 115 try: | 125 try: |
| 116 usernames = self.db.keys() | 126 usernames = self.db.keys() |
| 117 finally: | 127 finally: |
| 118 self.lock.release() | 128 self.lock.release() |
| 119 usernames = [u for u in usernames if not u.startswith("--Reserved--")] | 129 usernames = [u for u in usernames if not u.startswith("--Reserved--")] |
| 120 return usernames | 130 return usernames |
| OLD | NEW |