set/get/del to options

This commit is contained in:
Emmanuel Garette 2018-09-09 22:38:03 +02:00
parent 657b401124
commit 3d07a9e88f
6 changed files with 69 additions and 35 deletions

View File

@ -27,6 +27,7 @@ def make_description():
stroption = StrOption('str', 'Test string option', default="abc", properties=('mandatory', )) stroption = StrOption('str', 'Test string option', default="abc", properties=('mandatory', ))
boolop = BoolOption('boolop', 'Test boolean option op', default=True, properties=('hidden',)) boolop = BoolOption('boolop', 'Test boolean option op', default=True, properties=('hidden',))
wantref_option = BoolOption('wantref', 'Test requires', default=False) wantref_option = BoolOption('wantref', 'Test requires', default=False)
wantref_option.impl_set_information('info', 'default value')
wantframework_option = BoolOption('wantframework', 'Test requires', wantframework_option = BoolOption('wantframework', 'Test requires',
default=False) default=False)
@ -145,6 +146,27 @@ def test_information_config():
raises(ValueError, "config.information.reset('noinfo')") raises(ValueError, "config.information.reset('noinfo')")
def test_information_option():
descr = make_description()
config = Config(descr)
string = 'some informations'
#
config.option('gc.name').information.set('info', string)
assert config.option('gc.name').information.get('info') == string
#
raises(ValueError, "config.option('gc.name').information.get('noinfo')")
assert config.option('gc.name').information.get('noinfo', 'default') == 'default'
config.option('gc.name').information.reset('info')
raises(ValueError, "config.option('gc.name').information.get('info')")
raises(ValueError, "config.option('gc.name').information.reset('noinfo')")
#
assert config.option('wantref').information.get('info') == 'default value'
config.option('wantref').information.set('info', 'default value')
assert config.option('wantref').information.get('info') == 'default value'
config.option('wantref').information.reset('info')
assert config.option('wantref').information.get('info') == 'default value'
def to_tuple(val): def to_tuple(val):
ret = [] ret = []
for v in val: for v in val:

View File

@ -385,19 +385,27 @@ class TiramisuOptionInformation(CommonTiramisuOption):
allow_optiondescription = True allow_optiondescription = True
slave_need_index = False slave_need_index = False
def get(self, name, default=undefined): def get(self, key, default=undefined):
"""get information for a key name""" """get information for a key name"""
path = self.option_bag.path
values = self.option_bag.config_bag.context.cfgimpl_get_values()
try:
return values.get_information(key, default, path=path)
except ValueError:
option = self.option_bag.option option = self.option_bag.option
return option.impl_get_information(name, default) return option.impl_get_information(key, default)
def set(self, name, value): def set(self, key, value):
"""set information for a key name""" """set information for a key name"""
option = self.option_bag.option path = self.option_bag.path
self.option_bag.option.impl_set_information(name, value) values = self.option_bag.config_bag.context.cfgimpl_get_values()
values.set_information(key, value, path=path)
def reset(self, name): def reset(self, key):
"""remove information for a key name""" """remove information for a key name"""
self.option_bag.option.impl_del_information(name) path = self.option_bag.path
values = self.option_bag.config_bag.context.cfgimpl_get_values()
values.del_information(key, path=path)
class TiramisuOptionValue(CommonTiramisuOption): class TiramisuOptionValue(CommonTiramisuOption):

View File

@ -261,29 +261,30 @@ class Values(Cache):
value = list(value) value = list(value)
return owner, value return owner, value
def set_information(self, key, value): def set_information(self, path, key, value):
"""updates the information's attribute """updates the information's attribute
(which is a dictionary) (which is a dictionary)
:param key: information's key (ex: "help", "doc" :param key: information's key (ex: "help", "doc"
:param value: information's value (ex: "the help string") :param value: information's value (ex: "the help string")
""" """
self._informations[key] = value self._informations.setdefault(path, {})
self._informations[path][key] = value
def get_information(self, key, default): def get_information(self, path, key, default):
"""retrieves one information's item """retrieves one information's item
:param key: the item string (ex: "help") :param key: the item string (ex: "help")
""" """
value = self._informations.get(key, default) value = self._informations.get(path, {}).get(key, default)
if value is undefined: if value is undefined:
raise ValueError(_("information's item" raise ValueError(_("information's item"
" not found: {0}").format(key)) " not found: {0}").format(key))
return value return value
def del_information(self, key, raises): def del_information(self, path, key, raises):
if key in self._informations: if path in self._informations and key in self._informations[path]:
del(self._informations[key]) del self._informations[path][key]
else: else:
if raises: if raises:
raise ValueError(_("information's item not found {0}").format(key)) raise ValueError(_("information's item not found {0}").format(key))

View File

@ -92,7 +92,7 @@ class Storage(object):
'PRIMARY KEY (path, idx, session_id), ' 'PRIMARY KEY (path, idx, session_id), '
values_table += 'FOREIGN KEY(session_id) REFERENCES session(session_id))' values_table += 'FOREIGN KEY(session_id) REFERENCES session(session_id))'
informations_table = 'CREATE TABLE IF NOT EXISTS information(key TEXT,' informations_table = 'CREATE TABLE IF NOT EXISTS information(key TEXT,'
informations_table += 'value TEXT, session_id INTEGER, ' informations_table += 'value TEXT, session_id INTEGER, path TEXT, '
informations_table += 'PRIMARY KEY (key, session_id), ' informations_table += 'PRIMARY KEY (key, session_id), '
informations_table += 'FOREIGN KEY(session_id) REFERENCES session(session_id))' informations_table += 'FOREIGN KEY(session_id) REFERENCES session(session_id))'
self.execute(session_table, commit=False) self.execute(session_table, commit=False)

View File

@ -184,7 +184,7 @@ class Values(Sqlite3DB):
value = self._sqlite_decode(owner[1]) value = self._sqlite_decode(owner[1])
return nowner, value return nowner, value
def set_information(self, key, value): def set_information(self, path, key, value):
"""updates the information's attribute """updates the information's attribute
(which is a dictionary) (which is a dictionary)
@ -193,22 +193,24 @@ class Values(Sqlite3DB):
""" """
if DEBUG: if DEBUG:
print('set_information', key, value) print('set_information', key, value)
self._storage.execute("DELETE FROM information WHERE key = ? AND session_id = ?", path = self._sqlite_encode_path(path)
(key, self._session_id), self._storage.execute("DELETE FROM information WHERE key = ? AND session_id = ? AND path = ?",
(key, self._session_id, path),
False) False)
self._storage.execute("INSERT INTO information(key, value, session_id) VALUES " self._storage.execute("INSERT INTO information(key, value, session_id, path) VALUES "
"(?, ?, ?)", (key, self._sqlite_encode(value), self._session_id)) "(?, ?, ?, ?)", (key, self._sqlite_encode(value), self._session_id, path))
def get_information(self, key, default): def get_information(self, path, key, default):
"""retrieves one information's item """retrieves one information's item
:param key: the item string (ex: "help") :param key: the item string (ex: "help")
""" """
if DEBUG: if DEBUG:
print('get_information', key, default) print('get_information', key, default)
path = self._sqlite_encode_path(path)
value = self._storage.select("SELECT value FROM information WHERE key = ? AND " value = self._storage.select("SELECT value FROM information WHERE key = ? AND "
"session_id = ?", "session_id = ? AND path = ?",
(key, self._session_id)) (key, self._session_id, path))
if value is None: if value is None:
if default is undefined: if default is undefined:
raise ValueError(_("information's item" raise ValueError(_("information's item"
@ -217,15 +219,16 @@ class Values(Sqlite3DB):
else: else:
return self._sqlite_decode(value[0]) return self._sqlite_decode(value[0])
def del_information(self, key, raises): def del_information(self, path, key, raises):
if DEBUG: if DEBUG:
print('del_information', key, raises) print('del_information', key, raises)
path = self._sqlite_encode_path(path)
if raises and self._storage.select("SELECT value FROM information WHERE key = ? " if raises and self._storage.select("SELECT value FROM information WHERE key = ? "
"AND session_id = ?", "AND session_id = ? AND path = ?",
(key, self._session_id)) is None: (key, self._session_id, path)) is None:
raise ValueError(_("information's item not found {0}").format(key)) raise ValueError(_("information's item not found {0}").format(key))
self._storage.execute("DELETE FROM information WHERE key = ? AND session_id = ?", self._storage.execute("DELETE FROM information WHERE key = ? AND session_id = ? AND path = ?",
(key, self._session_id)) (key, self._session_id, path))
def del_informations(self): def del_informations(self):
self._storage.execute("DELETE FROM information WHERE session_id = ?", self._storage.execute("DELETE FROM information WHERE session_id = ?",

View File

@ -511,23 +511,23 @@ class Values(object):
#______________________________________________________________________ #______________________________________________________________________
# information # information
def set_information(self, key, value): def set_information(self, key, value, path=None):
"""updates the information's attribute """updates the information's attribute
:param key: information's key (ex: "help", "doc" :param key: information's key (ex: "help", "doc"
:param value: information's value (ex: "the help string") :param value: information's value (ex: "the help string")
""" """
self._p_.set_information(key, value) self._p_.set_information(path, key, value)
def get_information(self, key, default=undefined): def get_information(self, key, default=undefined, path=None):
"""retrieves one information's item """retrieves one information's item
:param key: the item string (ex: "help") :param key: the item string (ex: "help")
""" """
return self._p_.get_information(key, default) return self._p_.get_information(path, key, default)
def del_information(self, key, raises=True): def del_information(self, key, raises=True, path=None):
self._p_.del_information(key, raises) self._p_.del_information(path, key, raises)
#______________________________________________________________________ #______________________________________________________________________
# mandatory warnings # mandatory warnings