Mercurial > p > roundup > code
annotate 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 |
| rev | line source |
|---|---|
|
1467
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
1 #$Id: sessions.py,v 1.4 2003-02-25 10:19:32 richard Exp $ |
|
916
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 |
|
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 |
|
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 |
|
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 ''' |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
18 def __init__(self, config): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
19 self.config = config |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
20 self.dir = config.DATABASE |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
21 # 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
|
22 os.umask(0002) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
23 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
24 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
|
25 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
|
26 if os.path.exists(path): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
27 os.remove(path) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
28 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
|
29 os.remove(path+'.db') |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
30 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
31 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
|
32 ''' 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
|
33 ''' |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
34 db_type = '' |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
35 if os.path.exists(path): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
36 db_type = whichdb.whichdb(path) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
37 if not db_type: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
38 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
|
39 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
|
40 # 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
|
41 # 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
|
42 db_type = 'dbm' |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
43 return db_type |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
44 |
|
1467
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
45 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
|
46 db = self.opendb('c') |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
47 try: |
|
1467
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
48 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
|
49 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
|
50 else: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
51 return None |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
52 return values.get(value, None) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
53 finally: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
54 db.close() |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
55 |
|
1467
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
56 def getall(self, infoid): |
|
916
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
57 db = self.opendb('c') |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
58 try: |
|
1467
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
59 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
|
60 finally: |
|
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
61 db.close() |
|
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
62 |
|
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
63 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
|
64 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
|
65 try: |
|
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
66 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
|
67 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
|
68 else: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
69 values = {} |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
70 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
|
71 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
|
72 finally: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
73 db.close() |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
74 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
75 def list(self): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
76 db = self.opendb('r') |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
77 try: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
78 return db.keys() |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
79 finally: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
80 db.close() |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
81 |
|
1467
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
82 def destroy(self, infoid): |
|
916
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
83 db = self.opendb('c') |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
84 try: |
|
1467
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
85 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
|
86 del db[infoid] |
|
916
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
87 finally: |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
88 db.close() |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
89 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
90 def opendb(self, mode): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
91 '''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
|
92 eccentricities. |
|
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 # 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
|
95 path = os.path.join(os.getcwd(), self.dir, self.name) |
|
916
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
96 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
|
97 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
98 # 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
|
99 if not db_type: |
|
1075
4af69ca380bd
more 'n' -> 'c' :(
Richard Jones <richard@users.sourceforge.net>
parents:
916
diff
changeset
|
100 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
|
101 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
102 # 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
|
103 dbm = __import__(db_type) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
104 return dbm.open(path, mode) |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
105 |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
106 def commit(self): |
|
76b783c69976
Session storage in the hyperdb was horribly, horribly inefficient.
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
107 pass |
|
1467
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
108 |
|
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
109 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
|
110 name = 'sessions' |
|
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
111 |
|
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
112 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
|
113 name = 'otks' |
|
378081f066cc
registration is now a two-step process with confirmation from the
Richard Jones <richard@users.sourceforge.net>
parents:
1088
diff
changeset
|
114 |
