comparison src/memory/memory.py @ 756:efa0e0f57950

core (memory): new Sessions management class (similar to dict)
author Goffi <goffi@goffi.org>
date Tue, 24 Dec 2013 15:19:08 +0100
parents 1def5b7edf9f
children bbe55c7bee43
comparison
equal deleted inserted replaced
755:e3ad48a2aab2 756:efa0e0f57950
21 21
22 import os.path 22 import os.path
23 import csv 23 import csv
24 from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError 24 from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError
25 from xml.dom import minidom 25 from xml.dom import minidom
26 from uuid import uuid4
26 from logging import debug, info, warning, error 27 from logging import debug, info, warning, error
27 from twisted.internet import defer 28 from twisted.internet import defer, reactor
28 from twisted.words.protocols.jabber import jid 29 from twisted.words.protocols.jabber import jid
29 from twisted.python.failure import Failure 30 from twisted.python.failure import Failure
30 from sat.tools.xml_tools import paramsXml2xmlUI 31 from sat.tools.xml_tools import paramsXml2xmlUI
31 from sat.core.default_config import default_config 32 from sat.core.default_config import default_config
32 from sat.memory.sqlite import SqliteStorage 33 from sat.memory.sqlite import SqliteStorage
35 36
36 SAVEFILE_DATABASE = "/sat.db" 37 SAVEFILE_DATABASE = "/sat.db"
37 NO_SECURITY_LIMIT = -1 38 NO_SECURITY_LIMIT = -1
38 INDIVIDUAL = "individual" 39 INDIVIDUAL = "individual"
39 GENERAL = "general" 40 GENERAL = "general"
41
42
43 class Sessions(object):
44 DEFAULT_TIMEOUT = 600
45
46 def __init__(self, timeout = None):
47 """
48 @param timeout: nb of seconds before session destruction
49 """
50 self._sessions = dict()
51 self.timeout = timeout or Sessions.DEFAULT_TIMEOUT
52
53 def newSession(self, session_data=None):
54 """ Create a new session
55 @param session_data: mutable data to use, default to a dict
56 @return: session_id, session_data
57 """
58 session_id = str(uuid4())
59 timer = reactor.callLater(self.timeout, self._purgeSession, session_id)
60 if session_data is None:
61 session_data = {}
62 self._sessions[session_id] = (timer, session_data)
63 return session_id, session_data
64
65 def _purgeSession(self, session_id):
66 del self._sessions[session_id]
67 debug("Session [%s] purged" % session_id)
68
69 def __len__(self):
70 return len(self._sessions)
71
72 def __contains__(self, session_id):
73 return session_id in self._sessions
74
75 def __getitem__(self, session_id):
76 timer, session_data = self._sessions[session_id]
77 timer.reset(self.timeout)
78 return session_data
79
80 def __setitem__(self, key, value):
81 raise NotImplementedError("You need do use newSession to create a session")
82
83 def __delitem__(self, session_id):
84 """ Cancel the timer, then actually delete the session data """
85 try:
86 timer = self._sessions[session_id][0]
87 timer.cancel()
88 self._purgeSession(session_id)
89 except KeyError:
90 debug ("Session [%s] doesn't exists, timeout expired ?" % session_id)
91
92 def keys(self):
93 return self._sessions.keys()
94
95 def iterkeys(self):
96 return self._sessions.iterkeys()
40 97
41 98
42 class Params(object): 99 class Params(object):
43 """This class manage parameters with xml""" 100 """This class manage parameters with xml"""
44 ### TODO: add desciption in params 101 ### TODO: add desciption in params