Mercurial > p > roundup > code
annotate roundup/backends/sessions.py @ 968:07d8a4e296f8
Whee! It's not finished yet, but I can create a new instance...
...and play with it a little bit :)
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 22 Aug 2002 07:56:51 +0000 |
| parents | 76b783c69976 |
| children | 4af69ca380bd |
| rev | line source |
|---|---|
|
916
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1 #$Id: sessions.py,v 1.1 2002-07-30 08:22:38 richard Exp $ |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
2 ''' |
|
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 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
4 to store session information. |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
5 ''' |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
6 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
7 import anydbm, whichdb, os, marshal |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
8 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
9 class Sessions: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
10 ''' Back onto an anydbm store. |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
11 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
12 Keys are session id strings, values are marshalled data. |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
13 ''' |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
14 def __init__(self, config): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
15 self.config = config |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
16 self.dir = config.DATABASE |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
17 # 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
|
18 os.umask(0002) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
19 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
20 def clear(self): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
21 path = os.path.join(self.dir, 'sessions') |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
22 if os.path.exists(path): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
23 os.remove(path) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
24 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
|
25 os.remove(path+'.db') |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
26 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
27 def determine_db_type(self, path): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
28 ''' determine which DB wrote the class file |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
29 ''' |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
30 db_type = '' |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
31 if os.path.exists(path): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
32 db_type = whichdb.whichdb(path) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
33 if not db_type: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
34 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
|
35 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
|
36 # 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
|
37 # 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
|
38 db_type = 'dbm' |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
39 return db_type |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
40 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
41 def get(self, sessionid, value): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
42 db = self.opendb('c') |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
43 try: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
44 if db.has_key(sessionid): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
45 values = marshal.loads(db[sessionid]) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
46 else: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
47 return None |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
48 return values.get(value, None) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
49 finally: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
50 db.close() |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
51 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
52 def set(self, sessionid, **newvalues): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
53 db = self.opendb('c') |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
54 try: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
55 if db.has_key(sessionid): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
56 values = marshal.loads(db[sessionid]) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
57 else: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
58 values = {} |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
59 values.update(newvalues) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
60 db[sessionid] = marshal.dumps(values) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
61 finally: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
62 db.close() |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
63 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
64 def list(self): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
65 db = self.opendb('r') |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
66 try: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
67 return db.keys() |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
68 finally: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
69 db.close() |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
70 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
71 def destroy(self, sessionid): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
72 db = self.opendb('c') |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
73 try: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
74 if db.has_key(sessionid): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
75 del db[sessionid] |
|
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 opendb(self, mode): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
80 '''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
|
81 eccentricities. |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
82 ''' |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
83 # figure the class db type |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
84 path = os.path.join(os.getcwd(), self.dir, 'sessions') |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
85 db_type = self.determine_db_type(path) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
86 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
87 # 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
|
88 if not db_type: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
89 return anydbm.open(path, 'n') |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
90 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
91 # 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
|
92 dbm = __import__(db_type) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
93 return dbm.open(path, mode) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
94 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
95 def commit(self): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
96 pass |
|
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 # |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
99 #$Log: not supported by cvs2svn $ |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
100 # |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
101 # |
