This commit is contained in:
Emmanuel Garette 2013-08-20 23:00:20 +02:00
parent c13063686a
commit cc569efcc7
2 changed files with 39 additions and 20 deletions

View File

@ -24,8 +24,10 @@ class Settings(Cache):
__slots__ = tuple() __slots__ = tuple()
def __init__(self, storage): def __init__(self, storage):
settings_table = 'CREATE TABLE IF NOT EXISTS property(path text primary key, properties text)' settings_table = 'CREATE TABLE IF NOT EXISTS property(path text '
permissives_table = 'CREATE TABLE IF NOT EXISTS permissive(path text primary key, permissives text)' settings_table += 'primary key, properties text)'
permissives_table = 'CREATE TABLE IF NOT EXISTS permissive(path text '
permissives_table += 'primary key, permissives text)'
# should init cache too # should init cache too
super(Settings, self).__init__('property', storage) super(Settings, self).__init__('property', storage)
self.storage.execute(settings_table, commit=False) self.storage.execute(settings_table, commit=False)
@ -33,19 +35,23 @@ class Settings(Cache):
# propertives # propertives
def setproperties(self, path, properties): def setproperties(self, path, properties):
self.storage.execute("DELETE FROM property WHERE path = ?", (path,), False) self.storage.execute("DELETE FROM property WHERE path = ?", (path,),
self.storage.execute("INSERT INTO property(path, properties) VALUES (?, ?)", False)
(path, self._sqlite_encode(properties))) self.storage.execute("INSERT INTO property(path, properties) VALUES "
"(?, ?)", (path,
self._sqlite_encode(properties)))
def getproperties(self, path, default_properties): def getproperties(self, path, default_properties):
value = self.storage.select("SELECT properties FROM property WHERE path = ?", (path,)) value = self.storage.select("SELECT properties FROM property WHERE "
"path = ?", (path,))
if value is None: if value is None:
return set(default_properties) return set(default_properties)
else: else:
return set(self._sqlite_decode(value[0])) return set(self._sqlite_decode(value[0]))
def hasproperties(self, path): def hasproperties(self, path):
return self.storage.select("SELECT properties FROM property WHERE path = ?", (path,)) is not None return self.storage.select("SELECT properties FROM property WHERE "
"path = ?", (path,)) is not None
def reset_all_propertives(self): def reset_all_propertives(self):
self.storage.execute("DELETE FROM property") self.storage.execute("DELETE FROM property")
@ -57,23 +63,30 @@ class Settings(Cache):
"""return all properties in a dictionary """return all properties in a dictionary
""" """
ret = {} ret = {}
for path, properties in self.storage.select("SELECT * FROM property", only_one=False): for path, properties in self.storage.select("SELECT * FROM property",
only_one=False):
if path == '_none': if path == '_none':
opt = None opt = None
else: else:
opt = context.cfgimpl_get_description().impl_get_opt_by_path(path) opt = context.cfgimpl_get_description().impl_get_opt_by_path(
path)
properties = self._sqlite_decode(properties) properties = self._sqlite_decode(properties)
ret[opt] = properties ret[opt] = properties
return ret return ret
# permissive # permissive
def setpermissive(self, path, permissive): def setpermissive(self, path, permissive):
self.storage.execute("DELETE FROM permissive WHERE path = ?", (path,), False) self.storage.execute("DELETE FROM permissive WHERE path = ?", (path,),
self.storage.execute("INSERT INTO permissive(path, permissives) VALUES (?, ?)", False)
(path, self._sqlite_encode(permissive))) self.storage.execute("INSERT INTO permissive(path, permissives) "
"VALUES (?, ?)", (path,
self._sqlite_encode(permissive)
))
def getpermissive(self, path='_none'): def getpermissive(self, path='_none'):
permissives = self.storage.select("SELECT permissives FROM permissive WHERE path = ?", (path,)) permissives = self.storage.select("SELECT permissives FROM "
"permissive WHERE path = ?",
(path,))
if permissives is None: if permissives is None:
return frozenset() return frozenset()
else: else:

View File

@ -28,14 +28,16 @@ class Values(Cache):
def __init__(self, storage): def __init__(self, storage):
"""init plugin means create values storage """init plugin means create values storage
""" """
values_table = 'CREATE TABLE IF NOT EXISTS value(path text primary key, value text, owner text)' values_table = 'CREATE TABLE IF NOT EXISTS value(path text primary '
values_table += 'key, value text, owner text)'
# should init cache too # should init cache too
super(Values, self).__init__('value', storage) super(Values, self).__init__('value', storage)
self.storage.execute(values_table) self.storage.execute(values_table)
# sqlite # sqlite
def _sqlite_select(self, path): def _sqlite_select(self, path):
return self.storage.select("SELECT value FROM value WHERE path = ?", (path,)) return self.storage.select("SELECT value FROM value WHERE path = ?",
(path,))
# value # value
def setvalue(self, path, value, owner): def setvalue(self, path, value, owner):
@ -43,8 +45,9 @@ class Values(Cache):
a specified value must be associated to an owner a specified value must be associated to an owner
""" """
self.resetvalue(path) self.resetvalue(path)
self.storage.execute("INSERT INTO value(path, value, owner) VALUES (?, ?, ?)", self.storage.execute("INSERT INTO value(path, value, owner) VALUES "
(path, self._sqlite_encode(value), str(owner))) "(?, ?, ?)", (path, self._sqlite_encode(value),
str(owner)))
def getvalue(self, path): def getvalue(self, path):
"""get value for an option """get value for an option
@ -68,7 +71,8 @@ class Values(Cache):
example: {option1: (owner, 'value1'), option2: (owner, 'value2')} example: {option1: (owner, 'value1'), option2: (owner, 'value2')}
""" """
ret = {} ret = {}
for path, value, owner in self.storage.select("SELECT value", only_one=False): for path, value, owner in self.storage.select("SELECT value",
only_one=False):
opt = context.cfgimpl_get_description().impl_get_opt_by_path(path) opt = context.cfgimpl_get_description().impl_get_opt_by_path(path)
owner = getattr(owners, owner) owner = getattr(owners, owner)
@ -80,13 +84,15 @@ class Values(Cache):
def setowner(self, path, owner): def setowner(self, path, owner):
"""change owner for an option """change owner for an option
""" """
self.storage.execute("UPDATE value SET owner = ? WHERE path = ?", (str(owner), path)) self.storage.execute("UPDATE value SET owner = ? WHERE path = ?",
(str(owner), path))
def getowner(self, path, default): def getowner(self, path, default):
"""get owner for an option """get owner for an option
return: owner object return: owner object
""" """
owner = self.storage.select("SELECT owner FROM value WHERE path = ?", (path,)) owner = self.storage.select("SELECT owner FROM value WHERE path = ?",
(path,))
if owner is None: if owner is None:
return default return default
else: else: