From 3d07a9e88fd100430a79a5fa18630fa41dfd2172 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 9 Sep 2018 22:38:03 +0200 Subject: [PATCH] set/get/del to options --- test/test_config.py | 22 +++++++++++++++++++++ tiramisu/api.py | 24 +++++++++++++++-------- tiramisu/storage/dictionary/value.py | 15 +++++++------- tiramisu/storage/sqlite3/storage.py | 2 +- tiramisu/storage/sqlite3/value.py | 29 +++++++++++++++------------- tiramisu/value.py | 12 ++++++------ 6 files changed, 69 insertions(+), 35 deletions(-) diff --git a/test/test_config.py b/test/test_config.py index f81582c..ce02e84 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -27,6 +27,7 @@ def make_description(): stroption = StrOption('str', 'Test string option', default="abc", properties=('mandatory', )) boolop = BoolOption('boolop', 'Test boolean option op', default=True, properties=('hidden',)) wantref_option = BoolOption('wantref', 'Test requires', default=False) + wantref_option.impl_set_information('info', 'default value') wantframework_option = BoolOption('wantframework', 'Test requires', default=False) @@ -145,6 +146,27 @@ def test_information_config(): 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): ret = [] for v in val: diff --git a/tiramisu/api.py b/tiramisu/api.py index 1194921..51aa188 100644 --- a/tiramisu/api.py +++ b/tiramisu/api.py @@ -385,19 +385,27 @@ class TiramisuOptionInformation(CommonTiramisuOption): allow_optiondescription = True slave_need_index = False - def get(self, name, default=undefined): + def get(self, key, default=undefined): """get information for a key name""" - option = self.option_bag.option - return option.impl_get_information(name, default) + 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 + return option.impl_get_information(key, default) - def set(self, name, value): + def set(self, key, value): """set information for a key name""" - option = self.option_bag.option - self.option_bag.option.impl_set_information(name, value) + path = self.option_bag.path + 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""" - 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): diff --git a/tiramisu/storage/dictionary/value.py b/tiramisu/storage/dictionary/value.py index 7a80e8c..674b350 100644 --- a/tiramisu/storage/dictionary/value.py +++ b/tiramisu/storage/dictionary/value.py @@ -261,29 +261,30 @@ class Values(Cache): value = list(value) return owner, value - def set_information(self, key, value): + def set_information(self, path, key, value): """updates the information's attribute (which is a dictionary) :param key: information's key (ex: "help", "doc" :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 :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: raise ValueError(_("information's item" " not found: {0}").format(key)) return value - def del_information(self, key, raises): - if key in self._informations: - del(self._informations[key]) + def del_information(self, path, key, raises): + if path in self._informations and key in self._informations[path]: + del self._informations[path][key] else: if raises: raise ValueError(_("information's item not found {0}").format(key)) diff --git a/tiramisu/storage/sqlite3/storage.py b/tiramisu/storage/sqlite3/storage.py index 0375603..0be0262 100644 --- a/tiramisu/storage/sqlite3/storage.py +++ b/tiramisu/storage/sqlite3/storage.py @@ -92,7 +92,7 @@ class Storage(object): 'PRIMARY KEY (path, idx, session_id), ' values_table += 'FOREIGN KEY(session_id) REFERENCES session(session_id))' 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 += 'FOREIGN KEY(session_id) REFERENCES session(session_id))' self.execute(session_table, commit=False) diff --git a/tiramisu/storage/sqlite3/value.py b/tiramisu/storage/sqlite3/value.py index 96cdc9f..de1d70d 100644 --- a/tiramisu/storage/sqlite3/value.py +++ b/tiramisu/storage/sqlite3/value.py @@ -184,7 +184,7 @@ class Values(Sqlite3DB): value = self._sqlite_decode(owner[1]) return nowner, value - def set_information(self, key, value): + def set_information(self, path, key, value): """updates the information's attribute (which is a dictionary) @@ -193,22 +193,24 @@ class Values(Sqlite3DB): """ if DEBUG: print('set_information', key, value) - self._storage.execute("DELETE FROM information WHERE key = ? AND session_id = ?", - (key, self._session_id), + path = self._sqlite_encode_path(path) + self._storage.execute("DELETE FROM information WHERE key = ? AND session_id = ? AND path = ?", + (key, self._session_id, path), False) - self._storage.execute("INSERT INTO information(key, value, session_id) VALUES " - "(?, ?, ?)", (key, self._sqlite_encode(value), self._session_id)) + self._storage.execute("INSERT INTO information(key, value, session_id, path) VALUES " + "(?, ?, ?, ?)", (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 :param key: the item string (ex: "help") """ if DEBUG: print('get_information', key, default) + path = self._sqlite_encode_path(path) value = self._storage.select("SELECT value FROM information WHERE key = ? AND " - "session_id = ?", - (key, self._session_id)) + "session_id = ? AND path = ?", + (key, self._session_id, path)) if value is None: if default is undefined: raise ValueError(_("information's item" @@ -217,15 +219,16 @@ class Values(Sqlite3DB): else: return self._sqlite_decode(value[0]) - def del_information(self, key, raises): + def del_information(self, path, key, raises): if DEBUG: print('del_information', key, raises) + path = self._sqlite_encode_path(path) if raises and self._storage.select("SELECT value FROM information WHERE key = ? " - "AND session_id = ?", - (key, self._session_id)) is None: + "AND session_id = ? AND path = ?", + (key, self._session_id, path)) is None: raise ValueError(_("information's item not found {0}").format(key)) - self._storage.execute("DELETE FROM information WHERE key = ? AND session_id = ?", - (key, self._session_id)) + self._storage.execute("DELETE FROM information WHERE key = ? AND session_id = ? AND path = ?", + (key, self._session_id, path)) def del_informations(self): self._storage.execute("DELETE FROM information WHERE session_id = ?", diff --git a/tiramisu/value.py b/tiramisu/value.py index 505d9df..ad0c8a3 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -511,23 +511,23 @@ class Values(object): #______________________________________________________________________ # information - def set_information(self, key, value): + def set_information(self, key, value, path=None): """updates the information's attribute :param key: information's key (ex: "help", "doc" :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 :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): - self._p_.del_information(key, raises) + def del_information(self, key, raises=True, path=None): + self._p_.del_information(path, key, raises) #______________________________________________________________________ # mandatory warnings