store session in dictionary storage
This commit is contained in:
parent
fb1eba39ea
commit
36def6533f
|
@ -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
|
|
@ -506,15 +506,20 @@ class Config(CommonConfig):
|
||||||
"main configuration management entry"
|
"main configuration management entry"
|
||||||
__slots__ = tuple()
|
__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
|
""" Configuration option management master class
|
||||||
|
|
||||||
:param descr: describes the configuration schema
|
:param descr: describes the configuration schema
|
||||||
:type descr: an instance of ``option.OptionDescription``
|
:type descr: an instance of ``option.OptionDescription``
|
||||||
:param context: the current root config
|
:param context: the current root config
|
||||||
:type context: `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_settings = Settings(self, storage)
|
||||||
self._impl_values = Values(self, storage)
|
self._impl_values = Values(self, storage)
|
||||||
super(Config, self).__init__(descr, self)
|
super(Config, self).__init__(descr, self)
|
||||||
|
@ -534,7 +539,7 @@ class Config(CommonConfig):
|
||||||
#class MetaConfig(CommonConfig):
|
#class MetaConfig(CommonConfig):
|
||||||
# __slots__ = ('_impl_children',)
|
# __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):
|
# if not isinstance(children, list):
|
||||||
# raise ValueError(_("metaconfig's children must be a list"))
|
# raise ValueError(_("metaconfig's children must be a list"))
|
||||||
# self._impl_descr = None
|
# self._impl_descr = None
|
||||||
|
@ -555,7 +560,7 @@ class Config(CommonConfig):
|
||||||
# child._impl_meta = self
|
# child._impl_meta = self
|
||||||
|
|
||||||
# self._impl_children = children
|
# 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_settings = Settings(self, storage)
|
||||||
# self._impl_values = Values(self, storage)
|
# self._impl_values = Values(self, storage)
|
||||||
# self._impl_meta = None
|
# self._impl_meta = None
|
||||||
|
|
|
@ -215,14 +215,15 @@ def set_storage(name, **args):
|
||||||
'').format(option, name))
|
'').format(option, name))
|
||||||
|
|
||||||
|
|
||||||
def get_storage(context, session_id, is_persistent):
|
def get_storage(context, session_id, persistent):
|
||||||
def gen_id(config):
|
def gen_id(config):
|
||||||
return str(id(config)) + str(time())
|
return str(id(config)) + str(time())
|
||||||
|
|
||||||
if session_id is None:
|
if session_id is None:
|
||||||
session_id = gen_id(context)
|
session_id = gen_id(context)
|
||||||
return __import__(storage_type.get_storage(), globals(), locals(),
|
a=__import__(storage_type.get_storage(), globals(), locals(),
|
||||||
['Storage'], -1).Storage(session_id, is_persistent)
|
['Storage'], -1).Storage(session_id, persistent)
|
||||||
|
return a
|
||||||
|
|
||||||
|
|
||||||
def list_sessions():
|
def list_sessions():
|
||||||
|
|
|
@ -29,7 +29,7 @@ class Settings(Cache):
|
||||||
self._properties = {}
|
self._properties = {}
|
||||||
# permissive properties
|
# permissive properties
|
||||||
self._permissives = {}
|
self._permissives = {}
|
||||||
super(Settings, self).__init__()
|
super(Settings, self).__init__(storage)
|
||||||
|
|
||||||
# propertives
|
# propertives
|
||||||
def setproperties(self, path, properties):
|
def setproperties(self, path, properties):
|
||||||
|
|
|
@ -28,10 +28,11 @@ class Setting(object):
|
||||||
|
|
||||||
|
|
||||||
setting = Setting()
|
setting = Setting()
|
||||||
|
_list_sessions = []
|
||||||
|
|
||||||
|
|
||||||
def list_sessions():
|
def list_sessions():
|
||||||
return []
|
return _list_sessions
|
||||||
|
|
||||||
|
|
||||||
def delete_session(session_id):
|
def delete_session(session_id):
|
||||||
|
@ -39,20 +40,28 @@ def delete_session(session_id):
|
||||||
|
|
||||||
|
|
||||||
class Storage(object):
|
class Storage(object):
|
||||||
__slots__ = tuple()
|
__slots__ = ('session_id',)
|
||||||
storage = 'dictionary'
|
storage = 'dictionary'
|
||||||
|
|
||||||
def __init__(self, session_id, is_persistent):
|
def __init__(self, session_id, persistent):
|
||||||
if is_persistent:
|
if session_id in _list_sessions:
|
||||||
|
raise ValueError(_('session already used'))
|
||||||
|
if persistent:
|
||||||
raise ValueError(_('a dictionary cannot be 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):
|
class Cache(object):
|
||||||
__slots__ = ('_cache',)
|
__slots__ = ('_cache', 'storage')
|
||||||
key_is_path = False
|
key_is_path = False
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, storage):
|
||||||
self._cache = {}
|
self._cache = {}
|
||||||
|
self.storage = storage
|
||||||
|
|
||||||
def setcache(self, cache_type, path, val, time):
|
def setcache(self, cache_type, path, val, time):
|
||||||
self._cache[path] = (val, time)
|
self._cache[path] = (val, time)
|
||||||
|
|
|
@ -29,7 +29,7 @@ class Values(Cache):
|
||||||
"""
|
"""
|
||||||
self._values = {}
|
self._values = {}
|
||||||
# should init cache too
|
# should init cache too
|
||||||
super(Values, self).__init__()
|
super(Values, self).__init__(storage)
|
||||||
|
|
||||||
# value
|
# value
|
||||||
def setvalue(self, path, value, owner):
|
def setvalue(self, path, value, owner):
|
||||||
|
|
|
@ -50,11 +50,11 @@ def delete_session(session_id):
|
||||||
|
|
||||||
|
|
||||||
class Storage(object):
|
class Storage(object):
|
||||||
__slots__ = ('_conn', '_cursor', 'is_persistent', '_session_id')
|
__slots__ = ('_conn', '_cursor', 'persistent', '_session_id')
|
||||||
storage = 'sqlite3'
|
storage = 'sqlite3'
|
||||||
|
|
||||||
def __init__(self, session_id, is_persistent):
|
def __init__(self, session_id, persistent):
|
||||||
self.is_persistent = is_persistent
|
self.persistent = persistent
|
||||||
self._session_id = session_id
|
self._session_id = session_id
|
||||||
self._conn = sqlite3.connect(_gen_filename(self._session_id))
|
self._conn = sqlite3.connect(_gen_filename(self._session_id))
|
||||||
self._conn.text_factory = str
|
self._conn.text_factory = str
|
||||||
|
@ -77,7 +77,7 @@ class Storage(object):
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self._cursor.close()
|
self._cursor.close()
|
||||||
self._conn.close()
|
self._conn.close()
|
||||||
if not self.is_persistent:
|
if not self.persistent:
|
||||||
delete_session(self._session_id)
|
delete_session(self._session_id)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue