annotate roundup/backends/sessions.py @ 2052:78e6a1e4984e

forward-port from maint branch
author Richard Jones <richard@users.sourceforge.net>
date Wed, 25 Feb 2004 23:27:54 +0000
parents bcb21e5722b8
children bd79245fc30c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2031
bcb21e5722b8 fix permission handling around rego
Richard Jones <richard@users.sourceforge.net>
parents: 2005
diff changeset
1 #$Id: sessions.py,v 1.8 2004-02-19 02:39:05 richard Exp $
2005
fc52d57c6c3e documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents: 1789
diff changeset
2 """This module defines a very basic store that's used by the CGI interface
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
3 to store session and one-time-key information.
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
4
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
5 Yes, it's called "sessions" - because originally it only defined a session
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
6 class. It's now also used for One Time Key handling too.
1789
8c05f8a93a36 Normalize multiline strings for emacs.
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents: 1701
diff changeset
7 """
2005
fc52d57c6c3e documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents: 1789
diff changeset
8 __docformat__ = 'restructuredtext'
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
9
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
10 import anydbm, whichdb, os, marshal
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
11
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
12 class BasicDatabase:
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
13 ''' Provide a nice encapsulation of an anydbm store.
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
14
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
15 Keys are id strings, values are automatically marshalled data.
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
16 '''
1701
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
17 _db_type = None
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
18
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
19 def __init__(self, config):
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
20 self.config = config
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
21 self.dir = config.DATABASE
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
22 # ensure files are group readable and writable
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
23 os.umask(0002)
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
24
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
25 def clear(self):
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
26 path = os.path.join(self.dir, self.name)
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
27 if os.path.exists(path):
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
28 os.remove(path)
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
29 elif os.path.exists(path+'.db'): # dbm appends .db
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
30 os.remove(path+'.db')
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
31
1701
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
32 def cache_db_type(self, path):
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
33 ''' determine which DB wrote the class file, and cache it as an
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
34 attribute of __class__ (to allow for subclassed DBs to be
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
35 different sorts)
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
36 '''
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
37 db_type = ''
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
38 if os.path.exists(path):
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
39 db_type = whichdb.whichdb(path)
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
40 if not db_type:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
41 raise hyperdb.DatabaseError, "Couldn't identify database type"
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
42 elif os.path.exists(path+'.db'):
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
43 # if the path ends in '.db', it's a dbm database, whether
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
44 # anydbm says it's dbhash or not!
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
45 db_type = 'dbm'
1701
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
46 self.__class__._db_type = db_type
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
47
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
48 def get(self, infoid, value):
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
49 db = self.opendb('c')
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
50 try:
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
51 if db.has_key(infoid):
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
52 values = marshal.loads(db[infoid])
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
53 else:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
54 return None
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
55 return values.get(value, None)
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
56 finally:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
57 db.close()
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
58
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
59 def getall(self, infoid):
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
60 db = self.opendb('c')
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
61 try:
2031
bcb21e5722b8 fix permission handling around rego
Richard Jones <richard@users.sourceforge.net>
parents: 2005
diff changeset
62 try:
bcb21e5722b8 fix permission handling around rego
Richard Jones <richard@users.sourceforge.net>
parents: 2005
diff changeset
63 return marshal.loads(db[infoid])
bcb21e5722b8 fix permission handling around rego
Richard Jones <richard@users.sourceforge.net>
parents: 2005
diff changeset
64 except KeyError:
bcb21e5722b8 fix permission handling around rego
Richard Jones <richard@users.sourceforge.net>
parents: 2005
diff changeset
65 raise KeyError, 'No such One Time Key "%s"'%infoid
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
66 finally:
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
67 db.close()
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
68
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
69 def set(self, infoid, **newvalues):
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
70 db = self.opendb('c')
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
71 try:
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
72 if db.has_key(infoid):
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
73 values = marshal.loads(db[infoid])
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
74 else:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
75 values = {}
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
76 values.update(newvalues)
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
77 db[infoid] = marshal.dumps(values)
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
78 finally:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
79 db.close()
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
80
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
81 def list(self):
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
82 db = self.opendb('r')
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
83 try:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
84 return db.keys()
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
85 finally:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
86 db.close()
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
87
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
88 def destroy(self, infoid):
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
89 db = self.opendb('c')
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
90 try:
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
91 if db.has_key(infoid):
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
92 del db[infoid]
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
93 finally:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
94 db.close()
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
95
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
96 def opendb(self, mode):
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
97 '''Low-level database opener that gets around anydbm/dbm
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
98 eccentricities.
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
99 '''
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
100 # figure the class db type
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
101 path = os.path.join(os.getcwd(), self.dir, self.name)
1701
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
102 if self._db_type is None:
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
103 self.cache_db_type(path)
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
104
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
105 db_type = self._db_type
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
106
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
107 # new database? let anydbm pick the best dbm
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
108 if not db_type:
1075
4af69ca380bd more 'n' -> 'c' :(
Richard Jones <richard@users.sourceforge.net>
parents: 916
diff changeset
109 return anydbm.open(path, 'c')
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
110
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
111 # open the database with the correct module
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
112 dbm = __import__(db_type)
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
113 return dbm.open(path, mode)
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
114
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
115 def commit(self):
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
116 pass
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
117
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
118 class Sessions(BasicDatabase):
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
119 name = 'sessions'
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
120
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
121 class OneTimeKeys(BasicDatabase):
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
122 name = 'otks'
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
123

Roundup Issue Tracker: http://roundup-tracker.org/