source: TI04-geosplat/trunk/pygss/SessionObject.py @ 798

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI04-geosplat/trunk/pygss/SessionObject.py@6809
Revision 798, 3.3 KB checked in by astephen, 14 years ago (diff)

Latest working version with install method.
Can accept more than one file but doesn't combine variables yet.

Line 
1#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
2#   This software may be distributed under the terms of the
3#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
4
5"""
6SessionObject.py
7=================
8
9Session control module for the package.
10
11This module holds the SessionObject class that is used
12to store the contents of user sessions between calls.
13
14"""
15
16# Import required modules
17import os
18import shelve
19import time
20import random
21
22# import package modules including global variables
23from serverConfig import *
24from common import *
25
26class SessionObject:
27    """
28    An instance of this holds a user session that is stored in a python shelve object.
29    It also acts as a dictionary with getitem and setitem funtionality.
30    """
31
32    def __init__(self, sessionID=None):
33        """
34        Generates a unique id for a session if the session object does not already exist.
35        """
36        if sessionID==None:
37            timestamp= time.strftime("%Y%m%d%H%M%S", time.gmtime(time.time()))
38            randomInt=random.randint(0,999)
39            sessionID="session_%s%s" % (timestamp, randomInt) 
40        self.id=sessionID
41        self.dict={"sessionID":sessionID}
42
43    def getSessionID(self):
44        """
45        Returns the session ID.
46        """
47        return self.id
48
49    def readSessionObject(self):
50        """
51        Reads the current session object from the python shelve object.
52        """
53        self.dict={}
54        shelve_obj=shelve.open(os.path.join(SESSION_OBJECT_DIR, self.id))
55        for key in shelve_obj.keys():
56             self.dict[key]=shelve_obj[key]
57        shelve_obj.close()
58        return self.dict
59
60    def writeSessionObject(self, sessionObj):
61        """
62        Takes session dictionary the current session to a python shelve object.
63        """
64        shelve_obj=shelve.open(os.path.join(SESSION_OBJECT_DIR, self.id))
65        # Sort through session items and write them to the object
66        # If any are not included then delete them     
67        for key in shelve_obj.keys():
68            del shelve_obj[key]   
69
70        for key in sessionObj.keys():
71            newkey=deUnicodeObject(key)
72            value=deUnicodeObject(sessionObj[key])
73            shelve_obj[newkey]=value
74            """if type(key)==type(u'1'):
75                newkey=str(key)
76            else:
77                newkey=key
78               
79            if type(sessionObj[key])==type(u'1'):
80                shelve_obj[newkey]=str(sessionObj[key])
81            elif type(sessionObj[key])==type([]):
82                newList=[]
83                for item in sessionObj[key]:
84                    if type(item)==type(u'1'):
85                        item=str(item)
86                    newList.append(item)
87                shelve_obj[newkey]=newList
88            else:
89                shelve_obj[newkey]=sessionObj[key]"""       
90        return
91
92    def clearSessionObject(self, session):
93        """
94        Clears all the user-selected content of a session dictionary.
95        """
96        shelve_obj=shelve.open(os.path.join(SESSION_OBJECT_DIR, self.id))
97        for key in shelve_obj.keys():
98            if key not in ("sessionID", "username"):
99                del shelve_obj[key]
100            print shelve_obj.keys(), "ARE THE KEYS"
101        return
102
103    def loadSessionObject(self):
104        """
105        [not yet implemented]
106        """
107        pass
108
109    def saveSessionObject(self):
110        """
111        [not yet implemented]
112        """
113        pass
114
115
Note: See TracBrowser for help on using the repository browser.