paulb@239 | 1 | #!/usr/bin/env python |
paulb@239 | 2 | |
paulb@239 | 3 | """ |
paulb@239 | 4 | Session helper functions. |
paulb@239 | 5 | """ |
paulb@239 | 6 | |
paulb@239 | 7 | import shelve |
paulb@239 | 8 | import os |
paulb@239 | 9 | import glob |
paulb@239 | 10 | import time |
paulb@239 | 11 | import random |
paulb@239 | 12 | import sys |
paulb@239 | 13 | |
paulb@239 | 14 | class SessionStore: |
paulb@239 | 15 | |
paulb@239 | 16 | "A class representing a session store." |
paulb@239 | 17 | |
paulb@239 | 18 | def __init__(self, trans, session_directory, session_cookie_name="SID", concurrent=1, delay=1): |
paulb@239 | 19 | |
paulb@239 | 20 | """ |
paulb@239 | 21 | Initialise the session store, specifying the transaction 'trans' within |
paulb@239 | 22 | which all session access will occur, a base 'session_directory', the |
paulb@239 | 23 | optional 'session_cookie_name' where the session identifier is held for |
paulb@239 | 24 | each user, and specifying using the optional 'concurrent' parameter |
paulb@239 | 25 | whether concurrent access within the framework might occur (1) or |
paulb@239 | 26 | whether the framework queues accesses at some other level (0). The |
paulb@239 | 27 | optional 'delay' argument specifies the time in seconds between each |
paulb@239 | 28 | poll of the session file when that file is found to be locked for |
paulb@239 | 29 | editing. |
paulb@239 | 30 | """ |
paulb@239 | 31 | |
paulb@239 | 32 | self.trans = trans |
paulb@239 | 33 | self.session_directory = session_directory |
paulb@239 | 34 | self.session_cookie_name = session_cookie_name |
paulb@239 | 35 | self.concurrent = concurrent |
paulb@239 | 36 | self.delay = delay |
paulb@239 | 37 | |
paulb@239 | 38 | # Internal state. |
paulb@239 | 39 | |
paulb@239 | 40 | self.store = None |
paulb@239 | 41 | self.store_filename, self.edit_filename = None, None |
paulb@239 | 42 | self.to_expire = None |
paulb@239 | 43 | |
paulb@239 | 44 | def close(self): |
paulb@239 | 45 | |
paulb@239 | 46 | "Close the store, tidying up files and filenames." |
paulb@239 | 47 | |
paulb@239 | 48 | if self.store is not None: |
paulb@239 | 49 | self.store.close() |
paulb@239 | 50 | self.store = None |
paulb@239 | 51 | if self.edit_filename is not None: |
paulb@239 | 52 | try: |
paulb@239 | 53 | os.rename(self.edit_filename, self.store_filename) |
paulb@239 | 54 | except OSError: |
paulb@239 | 55 | pass |
paulb@239 | 56 | self.edit_filename, self.store_filename = None, None |
paulb@239 | 57 | |
paulb@239 | 58 | # Handle expiry appropriately. |
paulb@239 | 59 | |
paulb@239 | 60 | if self.to_expire is not None: |
paulb@239 | 61 | self._expire_session(self.to_expire) |
paulb@239 | 62 | self.trans.delete_cookie(self.session_cookie_name) |
paulb@239 | 63 | |
paulb@239 | 64 | def expire_session(self): |
paulb@239 | 65 | |
paulb@239 | 66 | """ |
paulb@239 | 67 | Expire the session in the given transaction. |
paulb@239 | 68 | """ |
paulb@239 | 69 | |
paulb@239 | 70 | # Perform expiry. |
paulb@239 | 71 | |
paulb@239 | 72 | cookie = self.trans.get_cookie(self.session_cookie_name) |
paulb@239 | 73 | if cookie: |
paulb@239 | 74 | self.to_expire = cookie.value |
paulb@239 | 75 | |
paulb@239 | 76 | def _expire_session(self, session_id): |
paulb@239 | 77 | |
paulb@239 | 78 | """ |
paulb@239 | 79 | Expire the session with the given 'session_id'. Note that in concurrent |
paulb@239 | 80 | session stores, this operation will block if another execution context |
paulb@239 | 81 | is editing the session. |
paulb@239 | 82 | """ |
paulb@239 | 83 | |
paulb@239 | 84 | filename = os.path.join(self.session_directory, session_id) |
paulb@239 | 85 | if self.concurrent: |
paulb@239 | 86 | while 1: |
paulb@239 | 87 | try: |
paulb@239 | 88 | os.unlink(filename) |
paulb@239 | 89 | except OSError: |
paulb@239 | 90 | time.sleep(self.delay) |
paulb@239 | 91 | else: |
paulb@239 | 92 | break |
paulb@239 | 93 | else: |
paulb@239 | 94 | try: |
paulb@239 | 95 | os.unlink(filename) |
paulb@239 | 96 | except OSError: |
paulb@239 | 97 | pass |
paulb@239 | 98 | |
paulb@239 | 99 | def get_session(self, create): |
paulb@239 | 100 | |
paulb@239 | 101 | """ |
paulb@239 | 102 | Get the session for the given transaction, creating a new session if |
paulb@239 | 103 | 'create' is set to 1 (rather than 0). Where new sessions are created, an |
paulb@239 | 104 | appropriate session identifier cookie will be created. |
paulb@239 | 105 | Returns a session object or None if no session exists and none is then |
paulb@239 | 106 | created. |
paulb@239 | 107 | """ |
paulb@239 | 108 | |
paulb@239 | 109 | cookie = self.trans.get_cookie(self.session_cookie_name) |
paulb@239 | 110 | if cookie: |
paulb@239 | 111 | return self._get_session(cookie.value, create) |
paulb@239 | 112 | elif create: |
paulb@239 | 113 | session_id = self._get_session_identifier() |
paulb@239 | 114 | self.trans.set_cookie_value(self.session_cookie_name, session_id) |
paulb@239 | 115 | return self._get_session(session_id, create) |
paulb@239 | 116 | else: |
paulb@239 | 117 | return None |
paulb@239 | 118 | |
paulb@239 | 119 | def _get_session(self, session_id, create): |
paulb@239 | 120 | |
paulb@239 | 121 | """ |
paulb@239 | 122 | Get a session with the given 'session_id' and whether new sessions |
paulb@239 | 123 | should be created ('create' set to 1). |
paulb@239 | 124 | Returns a dictionary-like object representing the session. |
paulb@239 | 125 | """ |
paulb@239 | 126 | |
paulb@239 | 127 | filename = os.path.join(self.session_directory, session_id) |
paulb@239 | 128 | |
paulb@239 | 129 | # Enforce locking. |
paulb@239 | 130 | |
paulb@239 | 131 | if self.concurrent: |
paulb@239 | 132 | |
paulb@239 | 133 | # Where the session is present (possibly being edited)... |
paulb@239 | 134 | |
paulb@239 | 135 | if glob.glob(filename + "*"): |
paulb@239 | 136 | while 1: |
paulb@239 | 137 | try: |
paulb@239 | 138 | os.rename(filename, filename + ".edit") |
paulb@239 | 139 | except OSError: |
paulb@239 | 140 | time.sleep(self.delay) |
paulb@239 | 141 | else: |
paulb@239 | 142 | break |
paulb@239 | 143 | |
paulb@239 | 144 | # Where no session is present and none should be created, return. |
paulb@239 | 145 | |
paulb@239 | 146 | elif not create: |
paulb@239 | 147 | return None |
paulb@239 | 148 | |
paulb@239 | 149 | self.store_filename = filename |
paulb@239 | 150 | filename = filename + ".edit" |
paulb@239 | 151 | self.edit_filename = filename |
paulb@239 | 152 | |
paulb@239 | 153 | # For non-concurrent situations, return if no session exists and none |
paulb@239 | 154 | # should be created. |
paulb@239 | 155 | |
paulb@239 | 156 | elif not os.path.exists(filename) and not create: |
paulb@239 | 157 | return None |
paulb@239 | 158 | |
paulb@239 | 159 | self.store = shelve.open(filename) |
paulb@239 | 160 | return Wrapper(self.store) |
paulb@239 | 161 | |
paulb@239 | 162 | def _get_session_identifier(self): |
paulb@239 | 163 | |
paulb@239 | 164 | "Return a session identifier as a string." |
paulb@239 | 165 | |
paulb@239 | 166 | g = random.Random() |
paulb@239 | 167 | return str(g.randint(0, sys.maxint - 1)) |
paulb@239 | 168 | |
paulb@239 | 169 | class Wrapper: |
paulb@239 | 170 | |
paulb@239 | 171 | "A wrapper around shelf objects." |
paulb@239 | 172 | |
paulb@239 | 173 | def __init__(self, store): |
paulb@239 | 174 | self.store = store |
paulb@239 | 175 | |
paulb@239 | 176 | def __getattr__(self, name): |
paulb@239 | 177 | if hasattr(self.store, name): |
paulb@239 | 178 | return getattr(self.store, name) |
paulb@239 | 179 | else: |
paulb@239 | 180 | raise AttributeError, name |
paulb@239 | 181 | |
paulb@263 | 182 | def __getitem__(self, name): |
paulb@263 | 183 | # Convert to UTF-8 to avoid bsddb limitations. |
paulb@263 | 184 | return self.store[name.encode("utf-8")] |
paulb@263 | 185 | |
paulb@263 | 186 | def __delitem__(self, name): |
paulb@263 | 187 | # Convert to UTF-8 to avoid bsddb limitations. |
paulb@263 | 188 | del self.store[name.encode("utf-8")] |
paulb@263 | 189 | |
paulb@263 | 190 | def __setitem__(self, name, value): |
paulb@263 | 191 | # Convert to UTF-8 to avoid bsddb limitations. |
paulb@263 | 192 | self.store[name.encode("utf-8")] = value |
paulb@263 | 193 | |
paulb@263 | 194 | def keys(self): |
paulb@263 | 195 | l = [] |
paulb@263 | 196 | for key in self.store.keys(): |
paulb@263 | 197 | # Convert from UTF-8 to avoid bsddb limitations. |
paulb@263 | 198 | l.append(unicode(key, "utf-8")) |
paulb@263 | 199 | return l |
paulb@263 | 200 | |
paulb@239 | 201 | def items(self): |
paulb@239 | 202 | l = [] |
paulb@263 | 203 | for key in self.keys(): |
paulb@263 | 204 | l.append((key, self[key])) |
paulb@239 | 205 | return l |
paulb@239 | 206 | |
paulb@239 | 207 | # vim: tabstop=4 expandtab shiftwidth=4 |