paulb@239 | 1 | #!/usr/bin/env python |
paulb@239 | 2 | |
paulb@239 | 3 | """ |
paulb@239 | 4 | Session helper functions. |
paulb@403 | 5 | |
paulb@403 | 6 | Copyright (C) 2004, 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@489 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@239 | 21 | """ |
paulb@239 | 22 | |
paulb@239 | 23 | import shelve |
paulb@239 | 24 | import random |
paulb@239 | 25 | import sys |
paulb@637 | 26 | import os |
paulb@641 | 27 | from WebStack.Repositories.Directory import SessionDirectoryRepository |
paulb@239 | 28 | |
paulb@239 | 29 | class SessionStore: |
paulb@239 | 30 | |
paulb@239 | 31 | "A class representing a session store." |
paulb@239 | 32 | |
paulb@519 | 33 | def __init__(self, trans, session_directory, session_cookie_name="SID", delay=1): |
paulb@239 | 34 | |
paulb@239 | 35 | """ |
paulb@239 | 36 | Initialise the session store, specifying the transaction 'trans' within |
paulb@519 | 37 | which all session access will occur, a base 'session_directory', and the |
paulb@239 | 38 | optional 'session_cookie_name' where the session identifier is held for |
paulb@519 | 39 | each user. |
paulb@519 | 40 | |
paulb@519 | 41 | The optional 'delay' argument specifies the time in seconds between each |
paulb@239 | 42 | poll of the session file when that file is found to be locked for |
paulb@519 | 43 | editing. (This is part of the underlying repository's behaviour.) |
paulb@239 | 44 | """ |
paulb@239 | 45 | |
paulb@239 | 46 | self.trans = trans |
paulb@239 | 47 | self.session_cookie_name = session_cookie_name |
paulb@641 | 48 | self.repository = SessionDirectoryRepository(session_directory, delay=delay) |
paulb@239 | 49 | |
paulb@239 | 50 | # Internal state. |
paulb@239 | 51 | |
paulb@239 | 52 | self.store = None |
paulb@519 | 53 | self.current_session_id = None |
paulb@239 | 54 | self.to_expire = None |
paulb@239 | 55 | |
paulb@239 | 56 | def close(self): |
paulb@239 | 57 | |
paulb@239 | 58 | "Close the store, tidying up files and filenames." |
paulb@239 | 59 | |
paulb@239 | 60 | if self.store is not None: |
paulb@239 | 61 | self.store.close() |
paulb@239 | 62 | self.store = None |
paulb@519 | 63 | if self.current_session_id is not None: |
paulb@519 | 64 | self.repository.unlock(self.current_session_id) |
paulb@519 | 65 | self.current_session_id = None |
paulb@239 | 66 | |
paulb@239 | 67 | # Handle expiry appropriately. |
paulb@239 | 68 | |
paulb@239 | 69 | if self.to_expire is not None: |
paulb@239 | 70 | self._expire_session(self.to_expire) |
paulb@239 | 71 | self.trans.delete_cookie(self.session_cookie_name) |
paulb@239 | 72 | |
paulb@239 | 73 | def expire_session(self): |
paulb@239 | 74 | |
paulb@239 | 75 | """ |
paulb@239 | 76 | Expire the session in the given transaction. |
paulb@239 | 77 | """ |
paulb@239 | 78 | |
paulb@239 | 79 | # Perform expiry. |
paulb@239 | 80 | |
paulb@239 | 81 | cookie = self.trans.get_cookie(self.session_cookie_name) |
paulb@239 | 82 | if cookie: |
paulb@239 | 83 | self.to_expire = cookie.value |
paulb@239 | 84 | |
paulb@239 | 85 | def _expire_session(self, session_id): |
paulb@239 | 86 | |
paulb@519 | 87 | "Expire the session with the given 'session_id'." |
paulb@239 | 88 | |
paulb@637 | 89 | self.repository.lock(session_id) |
paulb@519 | 90 | del self.repository[session_id] |
paulb@239 | 91 | |
paulb@239 | 92 | def get_session(self, create): |
paulb@239 | 93 | |
paulb@239 | 94 | """ |
paulb@239 | 95 | Get the session for the given transaction, creating a new session if |
paulb@239 | 96 | 'create' is set to 1 (rather than 0). Where new sessions are created, an |
paulb@239 | 97 | appropriate session identifier cookie will be created. |
paulb@239 | 98 | Returns a session object or None if no session exists and none is then |
paulb@239 | 99 | created. |
paulb@239 | 100 | """ |
paulb@239 | 101 | |
paulb@419 | 102 | if self.store is not None: |
paulb@419 | 103 | return self.store |
paulb@419 | 104 | |
paulb@419 | 105 | # No existing store - get the token and the actual session. |
paulb@419 | 106 | |
paulb@239 | 107 | cookie = self.trans.get_cookie(self.session_cookie_name) |
paulb@239 | 108 | if cookie: |
paulb@239 | 109 | return self._get_session(cookie.value, create) |
paulb@239 | 110 | elif create: |
paulb@239 | 111 | session_id = self._get_session_identifier() |
paulb@239 | 112 | self.trans.set_cookie_value(self.session_cookie_name, session_id) |
paulb@239 | 113 | return self._get_session(session_id, create) |
paulb@239 | 114 | else: |
paulb@239 | 115 | return None |
paulb@239 | 116 | |
paulb@239 | 117 | def _get_session(self, session_id, create): |
paulb@239 | 118 | |
paulb@239 | 119 | """ |
paulb@239 | 120 | Get a session with the given 'session_id' and whether new sessions |
paulb@239 | 121 | should be created ('create' set to 1). |
paulb@239 | 122 | Returns a dictionary-like object representing the session. |
paulb@239 | 123 | """ |
paulb@239 | 124 | |
paulb@519 | 125 | try: |
paulb@519 | 126 | store_filename = self.repository.lock(session_id, create=create, opener=shelve.open) |
paulb@519 | 127 | self.current_session_id = session_id |
paulb@519 | 128 | except KeyError: |
paulb@239 | 129 | return None |
paulb@239 | 130 | |
paulb@637 | 131 | # NOTE: Using inside knowledge about the repository structure. |
paulb@637 | 132 | |
paulb@637 | 133 | self.store = shelve.open(os.path.join(store_filename, "data")) |
paulb@239 | 134 | return Wrapper(self.store) |
paulb@239 | 135 | |
paulb@239 | 136 | def _get_session_identifier(self): |
paulb@239 | 137 | |
paulb@239 | 138 | "Return a session identifier as a string." |
paulb@239 | 139 | |
paulb@239 | 140 | g = random.Random() |
paulb@239 | 141 | return str(g.randint(0, sys.maxint - 1)) |
paulb@239 | 142 | |
paulb@239 | 143 | class Wrapper: |
paulb@239 | 144 | |
paulb@239 | 145 | "A wrapper around shelf objects." |
paulb@239 | 146 | |
paulb@239 | 147 | def __init__(self, store): |
paulb@239 | 148 | self.store = store |
paulb@239 | 149 | |
paulb@239 | 150 | def __getattr__(self, name): |
paulb@239 | 151 | if hasattr(self.store, name): |
paulb@239 | 152 | return getattr(self.store, name) |
paulb@239 | 153 | else: |
paulb@239 | 154 | raise AttributeError, name |
paulb@239 | 155 | |
paulb@263 | 156 | def __getitem__(self, name): |
paulb@263 | 157 | # Convert to UTF-8 to avoid bsddb limitations. |
paulb@263 | 158 | return self.store[name.encode("utf-8")] |
paulb@263 | 159 | |
paulb@263 | 160 | def __delitem__(self, name): |
paulb@263 | 161 | # Convert to UTF-8 to avoid bsddb limitations. |
paulb@263 | 162 | del self.store[name.encode("utf-8")] |
paulb@263 | 163 | |
paulb@263 | 164 | def __setitem__(self, name, value): |
paulb@263 | 165 | # Convert to UTF-8 to avoid bsddb limitations. |
paulb@263 | 166 | self.store[name.encode("utf-8")] = value |
paulb@263 | 167 | |
paulb@263 | 168 | def keys(self): |
paulb@263 | 169 | l = [] |
paulb@263 | 170 | for key in self.store.keys(): |
paulb@263 | 171 | # Convert from UTF-8 to avoid bsddb limitations. |
paulb@263 | 172 | l.append(unicode(key, "utf-8")) |
paulb@263 | 173 | return l |
paulb@263 | 174 | |
paulb@239 | 175 | def items(self): |
paulb@239 | 176 | l = [] |
paulb@263 | 177 | for key in self.keys(): |
paulb@263 | 178 | l.append((key, self[key])) |
paulb@239 | 179 | return l |
paulb@239 | 180 | |
paulb@497 | 181 | def values(self): |
paulb@497 | 182 | l = [] |
paulb@497 | 183 | for key in self.keys(): |
paulb@497 | 184 | l.append(self[key]) |
paulb@497 | 185 | return l |
paulb@497 | 186 | |
paulb@239 | 187 | # vim: tabstop=4 expandtab shiftwidth=4 |