tlslite.basedb

Package tlslite :: Module basedb |
[hide private]
[frames] | no frames]

# Source Code for Module tlslite.basedb

      1  # Author: Trevor Perrin
      2  # See the LICENSE file for legal information regarding use of this file.
      3
      4  """Base class for SharedKeyDB and VerifierDB."""
      5
      6  import anydbm
      7  import thread
      8
      9 -class BaseDB:
     10 -    def __init__(self, filename, type):
     11          self.type = type
     12          self.filename = filename
     13          if self.filename:
     14              self.db = None
     15          else:
     16              self.db = {}
     17          self.lock = thread.allocate_lock()
     18
     19 -    def create(self):
     20          """Create a new on-disk database.
     21
     22          @raise anydbm.error: If there's a problem creating the database.
     23          """
     24          if self.filename:
     25              self.db = anydbm.open(self.filename, "n") #raises anydbm.error
     26              self.db["--Reserved--type"] = self.type
     27              self.db.sync()
     28          else:
     29              self.db = {}
     30
     31 -    def open(self):
     32          """Open a pre-existing on-disk database.
     33
     34          @raise anydbm.error: If there's a problem opening the database.
     35          @raise ValueError: If the database is not of the right type.
     36          """
     37          if not self.filename:
     38              raise ValueError("Can only open on-disk databases")
     39          self.db = anydbm.open(self.filename, "w") #raises anydbm.error
     40          try:
     41              if self.db["--Reserved--type"] != self.type:
     42                  raise ValueError("Not a %s database" % self.type)
     43          except KeyError:
     44              raise ValueError("Not a recognized database")
     45
     46 -    def __getitem__(self, username):
     47          if self.db == None:
     48              raise AssertionError("DB not open")
     49
     50          self.lock.acquire()
     51          try:
     52              valueStr = self.db[username]
     53          finally:
     54              self.lock.release()
     55
     56          return self._getItem(username, valueStr)
     57
     58 -    def __setitem__(self, username, value):
     59          if self.db == None:
     60              raise AssertionError("DB not open")
     61
     62          valueStr = self._setItem(username, value)
     63
     64          self.lock.acquire()
     65          try:
     66              self.db[username] = valueStr
     67              if self.filename:
     68                  self.db.sync()
     69          finally:
     70              self.lock.release()
     71
     72 -    def __delitem__(self, username):
     73          if self.db == None:
     74              raise AssertionError("DB not open")
     75
     76          self.lock.acquire()
     77          try:
     78              del(self.db[username])
     79              if self.filename:
     80                  self.db.sync()
     81          finally:
     82              self.lock.release()
     83
     84 -    def __contains__(self, username):
     85          """Check if the database contains the specified username.
     86
     87          @type username: str
     88          @param username: The username to check for.
     89
     90          @rtype: bool
     91          @return: True if the database contains the username, False
     92          otherwise.
     93
     94          """
     95          if self.db == None:
     96              raise AssertionError("DB not open")
     97
     98          self.lock.acquire()
     99          try:
    100              return self.db.has_key(username)
    101          finally:
    102              self.lock.release()
    103
    104 -    def check(self, username, param):
    105          value = self.__getitem__(username)
    106          return self._checkItem(value, username, param)
    107
    108 -    def keys(self):
    109          """Return a list of usernames in the database.
    110
    111          @rtype: list
    112          @return: The usernames in the database.
    113          """
    114          if self.db == None:
    115              raise AssertionError("DB not open")
    116
    117          self.lock.acquire()
    118          try:
    119              usernames = self.db.keys()
    120          finally:
    121              self.lock.release()
    122          usernames = [u for u in usernames if not u.startswith("--Reserved--")]
    123          return usernames
    124