116 lines
4.2 KiB
Python
116 lines
4.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
"default plugin for value: set it in a simple dictionary"
|
|
# Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors)
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, write to the Free Software
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
#
|
|
# ____________________________________________________________
|
|
|
|
from tiramisu.storage.sqlite3.storage import Cache
|
|
from tiramisu.setting import owners
|
|
|
|
|
|
class Values(Cache):
|
|
__slots__ = tuple()
|
|
|
|
def __init__(self, storage):
|
|
"""init plugin means create values storage
|
|
"""
|
|
# should init cache too
|
|
super(Values, self).__init__('value', storage)
|
|
values_table = 'CREATE TABLE IF NOT EXISTS value(path text primary '
|
|
values_table += 'key, value text, owner text)'
|
|
self.storage.execute(values_table)
|
|
for owner in self.storage.select("SELECT DISTINCT owner FROM value", tuple(), False):
|
|
try:
|
|
getattr(owners, owner[0])
|
|
except AttributeError:
|
|
owners.addowner(owner[0])
|
|
|
|
# sqlite
|
|
def _sqlite_select(self, path):
|
|
return self.storage.select("SELECT value FROM value WHERE path = ?",
|
|
(path,))
|
|
|
|
# value
|
|
def setvalue(self, path, value, owner):
|
|
"""set value for an option
|
|
a specified value must be associated to an owner
|
|
"""
|
|
self.resetvalue(path)
|
|
path = self._sqlite_encode_path(path)
|
|
self.storage.execute("INSERT INTO value(path, value, owner) VALUES "
|
|
"(?, ?, ?)", (path, self._sqlite_encode(value),
|
|
str(owner)))
|
|
|
|
def getvalue(self, path):
|
|
"""get value for an option
|
|
return: only value, not the owner
|
|
"""
|
|
path = self._sqlite_encode_path(path)
|
|
return self._sqlite_decode(self._sqlite_select(path)[0])
|
|
|
|
def hasvalue(self, path):
|
|
"""if opt has a value
|
|
return: boolean
|
|
"""
|
|
path = self._sqlite_encode_path(path)
|
|
return self._sqlite_select(path) is not None
|
|
|
|
def resetvalue(self, path):
|
|
"""remove value means delete value in storage
|
|
"""
|
|
path = self._sqlite_encode_path(path)
|
|
self.storage.execute("DELETE FROM value WHERE path = ?", (path,))
|
|
|
|
def get_modified_values(self):
|
|
"""return all values in a dictionary
|
|
example: {option1: (owner, 'value1'), option2: (owner, 'value2')}
|
|
"""
|
|
ret = {}
|
|
for path, value, owner in self.storage.select("SELECT * FROM value",
|
|
only_one=False):
|
|
path = self._sqlite_decode_path(path)
|
|
owner = getattr(owners, owner)
|
|
|
|
value = self._sqlite_decode(value)
|
|
ret[path] = (owner, value)
|
|
return ret
|
|
|
|
# owner
|
|
def setowner(self, path, owner):
|
|
"""change owner for an option
|
|
"""
|
|
path = self._sqlite_encode_path(path)
|
|
self.storage.execute("UPDATE value SET owner = ? WHERE path = ?",
|
|
(str(owner), path))
|
|
|
|
def getowner(self, path, default):
|
|
"""get owner for an option
|
|
return: owner object
|
|
"""
|
|
path = self._sqlite_encode_path(path)
|
|
owner = self.storage.select("SELECT owner FROM value WHERE path = ?",
|
|
(path,))
|
|
if owner is None:
|
|
return default
|
|
else:
|
|
owner = owner[0]
|
|
# autocreate owners
|
|
try:
|
|
return getattr(owners, owner)
|
|
except AttributeError:
|
|
owners.addowner(owner)
|
|
return getattr(owners, owner)
|