store session in dictionary storage

This commit is contained in:
Emmanuel Garette 2013-08-27 09:46:52 +02:00
parent fb1eba39ea
commit 36def6533f
7 changed files with 115 additions and 19 deletions

81
test/test_storage.py Normal file
View File

@ -0,0 +1,81 @@
import autopath
#from py.test import raises
from tiramisu.config import Config
from tiramisu.option import BoolOption, OptionDescription
def test_non_persistent():
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
Config(o, session_id='test_non_persistent')
def test_list():
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
c = Config(o, session_id='test_non_persistent')
from tiramisu.setting import list_sessions
assert 'test_non_persistent' in list_sessions()
del(c)
assert 'test_non_persistent' not in list_sessions()
def test_create_persistent():
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
try:
Config(o, session_id='test_persistent', persistent=True)
except ValueError:
# storage is not persistent
pass
def test_list_sessions_persistent():
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
try:
Config(o, session_id='test_persistent', persistent=True)
except ValueError:
# storage is not persistent
pass
else:
from tiramisu.setting import list_sessions
assert 'test_persistent' in list_sessions()
def test_delete_session_persistent():
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
try:
Config(o, session_id='test_persistent', persistent=True)
except ValueError:
# storage is not persistent
pass
else:
from tiramisu.setting import list_sessions, delete_session
assert 'test_persistent' in list_sessions
delete_session('test_persistent')
assert 'test_persistent' not in list_sessions
def test_create_persistent_retrieve():
b = BoolOption('b', '')
o = OptionDescription('od', '', [b])
try:
c = Config(o, session_id='test_persistent', persistent=True)
except ValueError:
# storage is not persistent
pass
else:
assert c.b is False
c.b = True
assert c.b is True
del(c)
c = Config(o, session_id='test_persistent', persistent=True)
assert c.b is True
from tiramisu.setting import list_sessions, delete_session
assert 'test_persistent' in list_sessions
delete_session('test_persistent')
c = Config(o, session_id='test_persistent', persistent=True)
assert c.b is False

View File

@ -506,15 +506,20 @@ class Config(CommonConfig):
"main configuration management entry"
__slots__ = tuple()
def __init__(self, descr, session_id=None, is_persistent=False):
def __init__(self, descr, session_id=None, persistent=False):
""" Configuration option management master class
:param descr: describes the configuration schema
:type descr: an instance of ``option.OptionDescription``
:param context: the current root config
:type context: `Config`
:param session_id: session ID is import with persistent Config to
retrieve good session
:type session_id: `str`
:param persistent: if persistent, don't delete storage when leaving
:type persistent: `boolean`
"""
storage = get_storage(self, session_id, is_persistent)
storage = get_storage(self, session_id, persistent)
self._impl_settings = Settings(self, storage)
self._impl_values = Values(self, storage)
super(Config, self).__init__(descr, self)
@ -534,7 +539,7 @@ class Config(CommonConfig):
#class MetaConfig(CommonConfig):
# __slots__ = ('_impl_children',)
# def __init__(self, children, meta=True, session_id=None, is_persistent=False):
# def __init__(self, children, meta=True, session_id=None, persistent=False):
# if not isinstance(children, list):
# raise ValueError(_("metaconfig's children must be a list"))
# self._impl_descr = None
@ -555,7 +560,7 @@ class Config(CommonConfig):
# child._impl_meta = self
# self._impl_children = children
# storage = get_storage(self, session_id, is_persistent)
# storage = get_storage(self, session_id, persistent)
# self._impl_settings = Settings(self, storage)
# self._impl_values = Values(self, storage)
# self._impl_meta = None

View File

@ -215,14 +215,15 @@ def set_storage(name, **args):
'').format(option, name))
def get_storage(context, session_id, is_persistent):
def get_storage(context, session_id, persistent):
def gen_id(config):
return str(id(config)) + str(time())
if session_id is None:
session_id = gen_id(context)
return __import__(storage_type.get_storage(), globals(), locals(),
['Storage'], -1).Storage(session_id, is_persistent)
a=__import__(storage_type.get_storage(), globals(), locals(),
['Storage'], -1).Storage(session_id, persistent)
return a
def list_sessions():

View File

@ -29,7 +29,7 @@ class Settings(Cache):
self._properties = {}
# permissive properties
self._permissives = {}
super(Settings, self).__init__()
super(Settings, self).__init__(storage)
# propertives
def setproperties(self, path, properties):

View File

@ -28,10 +28,11 @@ class Setting(object):
setting = Setting()
_list_sessions = []
def list_sessions():
return []
return _list_sessions
def delete_session(session_id):
@ -39,20 +40,28 @@ def delete_session(session_id):
class Storage(object):
__slots__ = tuple()
__slots__ = ('session_id',)
storage = 'dictionary'
def __init__(self, session_id, is_persistent):
if is_persistent:
def __init__(self, session_id, persistent):
if session_id in _list_sessions:
raise ValueError(_('session already used'))
if persistent:
raise ValueError(_('a dictionary cannot be persistent'))
self.session_id = session_id
_list_sessions.append(self.session_id)
def __del__(self):
_list_sessions.remove(self.session_id)
class Cache(object):
__slots__ = ('_cache',)
__slots__ = ('_cache', 'storage')
key_is_path = False
def __init__(self):
def __init__(self, storage):
self._cache = {}
self.storage = storage
def setcache(self, cache_type, path, val, time):
self._cache[path] = (val, time)

View File

@ -29,7 +29,7 @@ class Values(Cache):
"""
self._values = {}
# should init cache too
super(Values, self).__init__()
super(Values, self).__init__(storage)
# value
def setvalue(self, path, value, owner):

View File

@ -50,11 +50,11 @@ def delete_session(session_id):
class Storage(object):
__slots__ = ('_conn', '_cursor', 'is_persistent', '_session_id')
__slots__ = ('_conn', '_cursor', 'persistent', '_session_id')
storage = 'sqlite3'
def __init__(self, session_id, is_persistent):
self.is_persistent = is_persistent
def __init__(self, session_id, persistent):
self.persistent = persistent
self._session_id = session_id
self._conn = sqlite3.connect(_gen_filename(self._session_id))
self._conn.text_factory = str
@ -77,7 +77,7 @@ class Storage(object):
def __del__(self):
self._cursor.close()
self._conn.close()
if not self.is_persistent:
if not self.persistent:
delete_session(self._session_id)