diff MoinMoin/web/session.py @ 5802:dba48cb280f9

delete some code we had copied from werkzeug 0.6.1 while we bundled 0.5.1 (it is contained in the bundled werkzeug 0.8.1)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 01 Dec 2011 02:05:15 +0100
parents af56baebf4d1
children
line wrap: on
line diff
--- a/MoinMoin/web/session.py	Thu Dec 01 01:34:45 2011 +0100
+++ b/MoinMoin/web/session.py	Thu Dec 01 02:05:15 2011 +0100
@@ -11,169 +11,17 @@
                 2009 MoinMoin:ThomasWaldmann
     @license: GNU GPL, see COPYING for details.
 """
-import sys, os
-from os import path
 import time
-import tempfile
-import re
-try:
-    from cPickle import load, dump, HIGHEST_PROTOCOL
-except ImportError:
-    from pickle import load, dump, HIGHEST_PROTOCOL
 
-from werkzeug.contrib.sessions import SessionStore, ModificationTrackingDict
+from werkzeug.contrib.sessions import Session, FilesystemSessionStore
 
 from MoinMoin import config
 from MoinMoin.util import filesys
-rename = filesys.rename # use MoinMoin's rename until we have it in werkzeug 0.6.1
 
 from MoinMoin import log
 logging = log.getLogger(__name__)
 
 
-# start copy from werkzeug 0.6 - directly import this, if we require >= 0.6:
-
-class Session(ModificationTrackingDict):
-    """Subclass of a dict that keeps track of direct object changes.  Changes
-    in mutable structures are not tracked, for those you have to set
-    `modified` to `True` by hand.
-    """
-    __slots__ = ModificationTrackingDict.__slots__ + ('sid', 'new')
-
-    def __init__(self, data, sid, new=False):
-        ModificationTrackingDict.__init__(self, data)
-        self.sid = sid
-        self.new = new
-
-    @property
-    def should_save(self):
-        """True if the session should be saved.
-
-        .. versionchanged:: 0.6
-           By default the session is now only saved if the session is
-           modified, not if it is new like it was before.
-        """
-        return self.modified
-
-    def __repr__(self):
-        return '<%s %s %s%s>' % (
-            self.__class__.__name__,
-            self.sid,
-            dict.__repr__(self),
-            self.should_save and '*' or ''
-        )
-
-
-#: used for temporary files by the filesystem session store
-_fs_transaction_suffix = '.__wz_sess'
-
-class FilesystemSessionStore(SessionStore):
-    """Simple example session store that saves sessions on the filesystem.
-    This store works best on POSIX systems and Windows Vista / Windows
-    Server 2008 and newer.
-
-    .. versionchanged:: 0.6
-       `renew_missing` was added.  Previously this was considered `True`,
-       now the default changed to `False` and it can be explicitly
-       deactivated.
-
-    :param path: the path to the folder used for storing the sessions.
-                 If not provided the default temporary directory is used.
-    :param filename_template: a string template used to give the session
-                              a filename.  ``%s`` is replaced with the
-                              session id.
-    :param session_class: The session class to use.  Defaults to
-                          :class:`Session`.
-    :param renew_missing: set to `True` if you want the store to
-                          give the user a new sid if the session was
-                          not yet saved.
-    """
-
-    def __init__(self, path=None, filename_template='werkzeug_%s.sess',
-                 session_class=None, renew_missing=False, mode=0644):
-        SessionStore.__init__(self, session_class)
-        if path is None:
-            path = tempfile.gettempdir()
-        self.path = path
-        if isinstance(filename_template, unicode):
-            filename_template = filename_template.encode(
-                sys.getfilesystemencoding() or 'utf-8')
-        assert not filename_template.endswith(_fs_transaction_suffix), \
-            'filename templates may not end with %s' % _fs_transaction_suffix
-        self.filename_template = filename_template
-        self.renew_missing = renew_missing
-        self.mode = mode
-
-    def get_session_filename(self, sid):
-        # out of the box, this should be a strict ASCII subset but
-        # you might reconfigure the session object to have a more
-        # arbitrary string.
-        if isinstance(sid, unicode):
-            sid = sid.encode(sys.getfilesystemencoding() or 'utf-8')
-        return path.join(self.path, self.filename_template % sid)
-
-    def save(self, session):
-        fn = self.get_session_filename(session.sid)
-        fd, tmp = tempfile.mkstemp(suffix=_fs_transaction_suffix,
-                                   dir=self.path)
-        f = os.fdopen(fd, 'wb')
-        try:
-            dump(dict(session), f, HIGHEST_PROTOCOL)
-        finally:
-            f.close()
-        try:
-            rename(tmp, fn)
-            os.chmod(fn, self.mode)
-        except (IOError, OSError):
-            pass
-
-    def delete(self, session):
-        fn = self.get_session_filename(session.sid)
-        try:
-            os.unlink(fn)
-        except OSError:
-            pass
-
-    def get(self, sid):
-        if not self.is_valid_key(sid):
-            return self.new()
-        try:
-            f = open(self.get_session_filename(sid), 'rb')
-        except IOError:
-            if self.renew_missing:
-                return self.new()
-            data = {}
-        else:
-            try:
-                try:
-                    data = load(f)
-                except Exception:
-                    data = {}
-            finally:
-                f.close()
-        return self.session_class(data, sid, False)
-
-    def list(self):
-        """Lists all sessions in the store.
-
-        .. versionadded:: 0.6
-        """
-        before, after = self.filename_template.split('%s', 1)
-        filename_re = re.compile(r'%s(.{5,})%s$' % (re.escape(before),
-                                                    re.escape(after)))
-        result = []
-        for filename in os.listdir(self.path):
-            #: this is a session that is still being saved.
-            if filename.endswith(_fs_transaction_suffix):
-                continue
-            match = filename_re.match(filename)
-            if match is not None:
-                result.append(match.group(1))
-        return result
-
-# end copy of werkzeug 0.6 code
-
-
 class MoinSession(Session):
     """ Compatibility interface to Werkzeug-sessions for old Moin-code.