Mercurial > p > roundup > code
view roundup/backends/sessions.py @ 1496:e6ac4e074acb
relaxed CVS importing (feature [SF#693277])
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 06 Mar 2003 07:33:29 +0000 |
| parents | 378081f066cc |
| children | bee5f62ea214 |
line wrap: on
line source
#$Id: sessions.py,v 1.4 2003-02-25 10:19:32 richard Exp $ ''' This module defines a very basic store that's used by the CGI interface to store session and one-time-key information. Yes, it's called "sessions" - because originally it only defined a session class. It's now also used for One Time Key handling too. ''' import anydbm, whichdb, os, marshal class BasicDatabase: ''' Provide a nice encapsulation of an anydbm store. Keys are id strings, values are automatically marshalled data. ''' def __init__(self, config): self.config = config self.dir = config.DATABASE # ensure files are group readable and writable os.umask(0002) def clear(self): path = os.path.join(self.dir, self.name) if os.path.exists(path): os.remove(path) elif os.path.exists(path+'.db'): # dbm appends .db os.remove(path+'.db') def determine_db_type(self, path): ''' determine which DB wrote the class file ''' db_type = '' if os.path.exists(path): db_type = whichdb.whichdb(path) if not db_type: raise hyperdb.DatabaseError, "Couldn't identify database type" elif os.path.exists(path+'.db'): # if the path ends in '.db', it's a dbm database, whether # anydbm says it's dbhash or not! db_type = 'dbm' return db_type def get(self, infoid, value): db = self.opendb('c') try: if db.has_key(infoid): values = marshal.loads(db[infoid]) else: return None return values.get(value, None) finally: db.close() def getall(self, infoid): db = self.opendb('c') try: return marshal.loads(db[infoid]) finally: db.close() def set(self, infoid, **newvalues): db = self.opendb('c') try: if db.has_key(infoid): values = marshal.loads(db[infoid]) else: values = {} values.update(newvalues) db[infoid] = marshal.dumps(values) finally: db.close() def list(self): db = self.opendb('r') try: return db.keys() finally: db.close() def destroy(self, infoid): db = self.opendb('c') try: if db.has_key(infoid): del db[infoid] finally: db.close() def opendb(self, mode): '''Low-level database opener that gets around anydbm/dbm eccentricities. ''' # figure the class db type path = os.path.join(os.getcwd(), self.dir, self.name) db_type = self.determine_db_type(path) # new database? let anydbm pick the best dbm if not db_type: return anydbm.open(path, 'c') # open the database with the correct module dbm = __import__(db_type) return dbm.open(path, mode) def commit(self): pass class Sessions(BasicDatabase): name = 'sessions' class OneTimeKeys(BasicDatabase): name = 'otks'
