tiramisu/tiramisu/storage/sqlite3/value.py

144 lines
5.3 KiB
Python
Raw Normal View History

2013-08-19 11:01:21 +02:00
# -*- 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
#
# ____________________________________________________________
2013-09-07 10:31:39 +02:00
from .sqlite3db import Sqlite3DB
2013-08-20 09:47:12 +02:00
from tiramisu.setting import owners
2013-08-19 11:01:21 +02:00
2013-09-07 10:31:39 +02:00
class Values(Sqlite3DB):
__slots__ = ('__weakref__',)
2013-08-19 11:01:21 +02:00
2013-08-20 22:45:11 +02:00
def __init__(self, storage):
2013-08-19 11:01:21 +02:00
"""init plugin means create values storage
"""
# should init cache too
2013-09-07 10:31:39 +02:00
super(Values, self).__init__(storage)
values_table = 'CREATE TABLE IF NOT EXISTS value(path text primary '
values_table += 'key, value text, owner text)'
2013-09-22 20:57:52 +02:00
self._storage.execute(values_table, commit=False)
informations_table = 'CREATE TABLE IF NOT EXISTS information(key text primary '
informations_table += 'key, value text)'
2013-09-22 20:57:52 +02:00
self._storage.execute(informations_table)
for owner in self._storage.select("SELECT DISTINCT owner FROM value", tuple(), False):
try:
getattr(owners, owner[0])
except AttributeError:
2013-08-27 16:12:53 +02:00
owners.addowner(owner[0])
2013-08-19 11:01:21 +02:00
# sqlite
2013-08-20 09:47:12 +02:00
def _sqlite_select(self, path):
2013-09-22 20:57:52 +02:00
return self._storage.select("SELECT value FROM value WHERE path = ?",
2013-08-20 23:00:20 +02:00
(path,))
2013-08-19 11:01:21 +02:00
# value
2013-08-20 09:47:12 +02:00
def setvalue(self, path, value, owner):
2013-08-19 11:01:21 +02:00
"""set value for an option
a specified value must be associated to an owner
"""
2013-08-20 09:47:12 +02:00
self.resetvalue(path)
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2013-09-22 20:57:52 +02:00
self._storage.execute("INSERT INTO value(path, value, owner) VALUES "
2013-08-20 23:00:20 +02:00
"(?, ?, ?)", (path, self._sqlite_encode(value),
str(owner)))
2013-08-19 11:01:21 +02:00
2013-08-20 09:47:12 +02:00
def getvalue(self, path):
2013-08-19 11:01:21 +02:00
"""get value for an option
return: only value, not the owner
"""
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2013-08-20 09:47:12 +02:00
return self._sqlite_decode(self._sqlite_select(path)[0])
2013-08-19 11:01:21 +02:00
2013-08-20 09:47:12 +02:00
def hasvalue(self, path):
2013-08-19 11:01:21 +02:00
"""if opt has a value
return: boolean
"""
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2013-08-20 09:47:12 +02:00
return self._sqlite_select(path) is not None
2013-08-19 11:01:21 +02:00
2013-08-20 09:47:12 +02:00
def resetvalue(self, path):
2013-08-19 11:01:21 +02:00
"""remove value means delete value in storage
"""
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2013-09-22 20:57:52 +02:00
self._storage.execute("DELETE FROM value WHERE path = ?", (path,))
2013-08-19 11:01:21 +02:00
def get_modified_values(self):
2013-08-19 11:01:21 +02:00
"""return all values in a dictionary
example: {option1: (owner, 'value1'), option2: (owner, 'value2')}
"""
ret = {}
2013-09-22 20:57:52 +02:00
for path, value, owner in self._storage.select("SELECT * FROM value",
2013-08-20 23:00:20 +02:00
only_one=False):
2013-08-21 23:21:28 +02:00
path = self._sqlite_decode_path(path)
2013-08-20 09:47:12 +02:00
owner = getattr(owners, owner)
value = self._sqlite_decode(value)
2013-08-21 23:21:28 +02:00
ret[path] = (owner, value)
2013-08-19 11:01:21 +02:00
return ret
# owner
2013-08-20 09:47:12 +02:00
def setowner(self, path, owner):
2013-08-19 11:01:21 +02:00
"""change owner for an option
"""
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2013-09-22 20:57:52 +02:00
self._storage.execute("UPDATE value SET owner = ? WHERE path = ?",
2013-08-20 23:00:20 +02:00
(str(owner), path))
2013-08-19 11:01:21 +02:00
2013-08-20 09:47:12 +02:00
def getowner(self, path, default):
2013-08-19 11:01:21 +02:00
"""get owner for an option
return: owner object
"""
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2013-09-22 20:57:52 +02:00
owner = self._storage.select("SELECT owner FROM value WHERE path = ?",
2013-08-20 23:00:20 +02:00
(path,))
2013-08-19 11:01:21 +02:00
if owner is None:
return default
else:
owner = owner[0]
# autocreate owners
try:
return getattr(owners, owner)
except AttributeError:
2013-08-27 16:12:53 +02:00
owners.addowner(owner)
return getattr(owners, owner)
def set_information(self, 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")
"""
2013-09-22 20:57:52 +02:00
self._storage.execute("DELETE FROM information WHERE key = ?", (key,),
False)
2013-09-22 20:57:52 +02:00
self._storage.execute("INSERT INTO information(key, value) VALUES "
"(?, ?)", (key, self._sqlite_encode(value)))
def get_information(self, key):
"""retrieves one information's item
:param key: the item string (ex: "help")
"""
2013-09-22 20:57:52 +02:00
value = self._storage.select("SELECT value FROM information WHERE key = ?",
(key,))
if value is None:
raise ValueError("not found")
else:
return self._sqlite_decode(value[0])