<commit_before>## $Id$
## CDSware Web Session utilities, implementing persistence.

## This file is part of the CERN Document Server Software (CDSware).
## Copyright (C) 2002, 2003, 2004, 2005 CERN.
##
## The CDSware is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## The CDSware is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.  
##
## You should have received a copy of the GNU General Public License
## along with CDSware; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

"""
Classes necessary for using in CDSware, as a complement of session, which adds persistence to sessions
by using a MySQL table. Consists of the following classes:

	- SessionNotInDb: Exception to be raised when a session doesn't exit
	- pSession(Session): Specialisation of the class Session which adds persistence to session
	- pSessionMapping: Implements only the necessary methods to make it work with the session manager 
"""
import cPickle
import time
from dbquery import run_sql
import session
from session import Session
from UserDict import UserDict

class SessionNotInDb(Exception):
    """Exception to be raised when a requested session doesn't exist in the DB
    """
    pass


class pSession(Session):
    """Specialisation of the class Session which adds persistence to sessions 
        by using a MySQL table (it pickles itself into the corresponding row of 
        the table). The class provides methods to save and retrieve an instance 
        to/from the DB and to access the main session attributes (uid). The 
        table in the DB must have the following structure:
            session_key - text - unique
            uid         - int
            session_object - blob
       Attributes:
            __tableName -- (string) name of the table in the DB where the 
                sessions are going to be stored
            __uid -- (int) user identifier who initiated the session
            __dirty -- (bool) flag indicating whether the session has been 
                modified (and therefore needs to be saved back to the DB) or not
    """
    
    __tableName = "session" 
    __ExpireTime = 1050043127
    
    def __init__( self, request, id, uid=-1 ):
        Session.__init__( self, request, id )
        self.__uid = uid
        self.__dirty = 0

    def is_dirty( self ):
        return self.__dirty

    def getUid( self ):
        return self.__uid

    def setUid( self, newUid ):
        self.__uid = int(newUid)
        self.__dirty = 1    

    def retrieve( cls, sessionId ):
        """method for retrieving a session from the DB for the given id. If the
             id has no corresponding session an exception is raised
        """
        sql = "select session_object from %s where session_key='%s'"%(cls.__tableName, sessionId)
        res = run_sql(sql)
        if len(res)==0:
            raise SessionNotInDb("Session %s doesn't exist"%sessionId)
        s = cPickle.loads( res[0][0] )
        return s
    retrieve = classmethod( retrieve )

    def __getRepr( self ):
        return cPickle.dumps( self )

    def save( self ):
        """method that tries to insert the session as NEW in the DB. If this 
            fails (giving an integrity error) it means the session already
            exists there and it must be updated, so it performs the 
            corresponding SQL update
        """
      <commit_msg>Compatibility with recent MySQLdb.<commit_after>## $Id$
## CDSware Web Session utilities, implementing persistence.

## This file is part of the CERN Document Server Software (CDSware).
## Copyright (C) 2002, 2003, 2004, 2005 CERN.
##
## The CDSware is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## The CDSware is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.  
##
## You should have received a copy of the GNU General Public License
## along with CDSware; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

"""
Classes necessary for using in CDSware, as a complement of session, which adds persistence to sessions
by using a MySQL table. Consists of the following classes:

	- SessionNotInDb: Exception to be raised when a session doesn't exit
	- pSession(Session): Specialisation of the class Session which adds persistence to session
	- pSessionMapping: Implements only the necessary methods to make it work with the session manager 
"""
import cPickle
import time
from dbquery import run_sql, blob_to_string
import session
from session import Session
from UserDict import UserDict

class SessionNotInDb(Exception):
    """Exception to be raised when a requested session doesn't exist in the DB
    """
    pass


class pSession(Session):
    """Specialisation of the class Session which adds persistence to sessions 
        by using a MySQL table (it pickles itself into the corresponding row of 
        the table). The class provides methods to save and retrieve an instance 
        to/from the DB and to access the main session attributes (uid). The 
        table in the DB must have the following structure:
            session_key - text - unique
            uid         - int
            session_object - blob
       Attributes:
            __tableName -- (string) name of the table in the DB where the 
                sessions are going to be stored
            __uid -- (int) user identifier who initiated the session
            __dirty -- (bool) flag indicating whether the session has been 
                modified (and therefore needs to be saved back to the DB) or not
    """
    
    __tableName = "session" 
    __ExpireTime = 1050043127
    
    def __init__( self, request, id, uid=-1 ):
        Session.__init__( self, request, id )
        self.__uid = uid
        self.__dirty = 0

    def is_dirty( self ):
        return self.__dirty

    def getUid( self ):
        return self.__uid

    def setUid( self, newUid ):
        self.__uid = int(newUid)
        self.__dirty = 1    

    def retrieve( cls, sessionId ):
        """method for retrieving a session from the DB for the given id. If the
             id has no corresponding session an exception is raised
        """
        sql = "select session_object from %s where session_key='%s'"%(cls.__tableName, sessionId)
        res = run_sql(sql)
        if len(res)==0:
            raise SessionNotInDb("Session %s doesn't exist"%sessionId)
        s = cPickle.loads(blob_to_string(res[0][0]))
        return s
    retrieve = classmethod( retrieve )

    def __getRepr( self ):
        return cPickle.dumps( self )

    def save( self ):
        """method that tries to insert the session as NEW in the DB. If this 
            fails (giving an integrity error) it means the session already
            exists there and it must be updated, so it performs the 
            corresponding SQL update
        """
      
{'fraction_non_alphanumeric': 0.06529434902320702, 'fraction_numerical': 0.012324636161006949, 'mean_word_length': 2.6921587608906097, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2177339', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1894, 'n_words': 965}