annotate roundup/backends/sessions.py @ 1789:8c05f8a93a36

Normalize multiline strings for emacs.
author Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
date Fri, 05 Sep 2003 21:05:18 +0000
parents bee5f62ea214
children fc52d57c6c3e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1789
8c05f8a93a36 Normalize multiline strings for emacs.
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents: 1701
diff changeset
1 #$Id: sessions.py,v 1.6 2003-09-05 21:05:18 jlgijsbers Exp $
8c05f8a93a36 Normalize multiline strings for emacs.
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents: 1701
diff changeset
2 """
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
3 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
4 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
5
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
6 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
7 class. It's now also used for One Time Key handling too.
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
8
1789
8c05f8a93a36 Normalize multiline strings for emacs.
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents: 1701
diff changeset
9 """
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
10
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
11 import anydbm, whichdb, os, marshal
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
12
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
13 class BasicDatabase:
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
14 ''' 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
15
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
16 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
17 '''
1701
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
18 _db_type = None
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
19
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
20 def __init__(self, config):
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
21 self.config = config
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
22 self.dir = config.DATABASE
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
23 # 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
24 os.umask(0002)
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
25
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
26 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
27 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
28 if os.path.exists(path):
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
29 os.remove(path)
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
30 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
31 os.remove(path+'.db')
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
32
1701
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
33 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
34 ''' 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
35 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
36 different sorts)
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
37 '''
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
38 db_type = ''
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
39 if os.path.exists(path):
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
40 db_type = whichdb.whichdb(path)
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
41 if not db_type:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
42 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
43 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
44 # 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
45 # 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
46 db_type = 'dbm'
1701
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
47 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
48
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
49 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
50 db = self.opendb('c')
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
51 try:
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
52 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
53 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
54 else:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
55 return None
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
56 return values.get(value, None)
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
57 finally:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
58 db.close()
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
59
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
60 def getall(self, infoid):
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
61 db = self.opendb('c')
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
62 try:
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
63 return marshal.loads(db[infoid])
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
64 finally:
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
65 db.close()
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
66
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
67 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
68 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
69 try:
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
70 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
71 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
72 else:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
73 values = {}
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
74 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
75 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
76 finally:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
77 db.close()
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
78
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
79 def list(self):
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
80 db = self.opendb('r')
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
81 try:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
82 return db.keys()
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
83 finally:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
84 db.close()
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
85
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
86 def destroy(self, infoid):
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
87 db = self.opendb('c')
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
88 try:
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
89 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
90 del db[infoid]
916
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
91 finally:
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
92 db.close()
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
93
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
94 def opendb(self, mode):
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
95 '''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
96 eccentricities.
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
97 '''
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
98 # 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
99 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
100 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
101 self.cache_db_type(path)
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
102
bee5f62ea214 forward-port patch to cache session-database-type.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents: 1467
diff changeset
103 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
104
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
105 # 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
106 if not db_type:
1075
4af69ca380bd more 'n' -> 'c' :(
Richard Jones <richard@users.sourceforge.net>
parents: 916
diff changeset
107 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
108
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
109 # 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
110 dbm = __import__(db_type)
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
111 return dbm.open(path, mode)
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
112
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
113 def commit(self):
76b783c69976 Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff changeset
114 pass
1467
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
115
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
116 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
117 name = 'sessions'
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
118
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
119 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
120 name = 'otks'
378081f066cc registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents: 1088
diff changeset
121

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