1- #$Id: sessions.py,v 1.3 2002-09-10 00:11:50 richard Exp $
1+ #$Id: sessions.py,v 1.4 2003-02-25 10:19:32 richard Exp $
22'''
33This module defines a very basic store that's used by the CGI interface
4- to store session information.
4+ to store session and one-time-key information.
5+
6+ Yes, it's called "sessions" - because originally it only defined a session
7+ class. It's now also used for One Time Key handling too.
8+
59'''
610
711import anydbm , whichdb , os , marshal
812
9- class Sessions :
10- ''' Back onto an anydbm store.
13+ class BasicDatabase :
14+ ''' Provide a nice encapsulation of an anydbm store.
1115
12- Keys are session id strings, values are marshalled data.
16+ Keys are id strings, values are automatically marshalled data.
1317 '''
1418 def __init__ (self , config ):
1519 self .config = config
@@ -18,7 +22,7 @@ def __init__(self, config):
1822 os .umask (0002 )
1923
2024 def clear (self ):
21- path = os .path .join (self .dir , 'sessions' )
25+ path = os .path .join (self .dir , self . name )
2226 if os .path .exists (path ):
2327 os .remove (path )
2428 elif os .path .exists (path + '.db' ): # dbm appends .db
@@ -38,26 +42,33 @@ def determine_db_type(self, path):
3842 db_type = 'dbm'
3943 return db_type
4044
41- def get (self , sessionid , value ):
45+ def get (self , infoid , value ):
4246 db = self .opendb ('c' )
4347 try :
44- if db .has_key (sessionid ):
45- values = marshal .loads (db [sessionid ])
48+ if db .has_key (infoid ):
49+ values = marshal .loads (db [infoid ])
4650 else :
4751 return None
4852 return values .get (value , None )
4953 finally :
5054 db .close ()
5155
52- def set (self , sessionid , ** newvalues ):
56+ def getall (self , infoid ):
57+ db = self .opendb ('c' )
58+ try :
59+ return marshal .loads (db [infoid ])
60+ finally :
61+ db .close ()
62+
63+ def set (self , infoid , ** newvalues ):
5364 db = self .opendb ('c' )
5465 try :
55- if db .has_key (sessionid ):
56- values = marshal .loads (db [sessionid ])
66+ if db .has_key (infoid ):
67+ values = marshal .loads (db [infoid ])
5768 else :
5869 values = {}
5970 values .update (newvalues )
60- db [sessionid ] = marshal .dumps (values )
71+ db [infoid ] = marshal .dumps (values )
6172 finally :
6273 db .close ()
6374
@@ -68,11 +79,11 @@ def list(self):
6879 finally :
6980 db .close ()
7081
71- def destroy (self , sessionid ):
82+ def destroy (self , infoid ):
7283 db = self .opendb ('c' )
7384 try :
74- if db .has_key (sessionid ):
75- del db [sessionid ]
85+ if db .has_key (infoid ):
86+ del db [infoid ]
7687 finally :
7788 db .close ()
7889
@@ -81,7 +92,7 @@ def opendb(self, mode):
8192 eccentricities.
8293 '''
8394 # figure the class db type
84- path = os .path .join (os .getcwd (), self .dir , 'sessions' )
95+ path = os .path .join (os .getcwd (), self .dir , self . name )
8596 db_type = self .determine_db_type (path )
8697
8798 # new database? let anydbm pick the best dbm
@@ -94,3 +105,10 @@ def opendb(self, mode):
94105
95106 def commit (self ):
96107 pass
108+
109+ class Sessions (BasicDatabase ):
110+ name = 'sessions'
111+
112+ class OneTimeKeys (BasicDatabase ):
113+ name = 'otks'
114+
0 commit comments