tiramisu/tiramisu/plugins/sqlite3/value.py

105 lines
3.6 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-08-20 09:47:12 +02:00
from tiramisu.plugins.sqlite3.cache import Cache
from tiramisu.setting import owners
2013-08-19 11:01:21 +02:00
2013-08-20 09:47:12 +02:00
class Values(Cache):
2013-08-19 11:01:21 +02:00
__slots__ = tuple()
def __init__(self, config_id):
"""init plugin means create values storage
"""
values_table = 'CREATE TABLE IF NOT EXISTS value(path text primary key, value text, owner text)'
# should init cache too
2013-08-20 09:47:12 +02:00
super(Values, self).__init__(config_id, 'value')
2013-08-19 11:01:21 +02:00
self._cursor.execute(values_table)
self._conn.commit()
# sqlite
2013-08-20 09:47:12 +02:00
def _sqlite_select(self, path):
2013-08-19 11:01:21 +02:00
self._cursor.execute("SELECT value FROM value WHERE path = ?", (path,))
return self._cursor.fetchone()
# 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-19 11:01:21 +02:00
self._cursor.execute("INSERT INTO value(path, value, owner) VALUES (?, ?, ?)",
2013-08-20 09:47:12 +02:00
(path, self._sqlite_encode(value), str(owner)))
2013-08-19 11:01:21 +02:00
self._conn.commit()
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-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-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
"""
self._cursor.execute("DELETE FROM value WHERE path = ?", (path,))
self._conn.commit()
2013-08-20 09:47:12 +02:00
def get_modified_values(self, context):
2013-08-19 11:01:21 +02:00
"""return all values in a dictionary
example: {option1: (owner, 'value1'), option2: (owner, 'value2')}
"""
self._cursor.execute("SELECT value")
ret = {}
for path, value, owner in self._cursor.fetchall():
2013-08-20 09:47:12 +02:00
opt = context.cfgimpl_get_description().impl_get_opt_by_path(path)
owner = getattr(owners, owner)
value = self._sqlite_decode(value)
2013-08-19 11:01:21 +02:00
ret[opt] = (owner, value)
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
"""
self._cursor.execute("UPDATE value SET owner = ? WHERE path = ?", (str(owner), path))
self._conn.commit()
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
"""
self._cursor.execute("SELECT owner FROM value WHERE path = ?", (path,))
owner = self._cursor.fetchone()
if owner is None:
return default
else:
2013-08-20 09:47:12 +02:00
return getattr(owners, owner[0])
2013-08-19 11:01:21 +02:00
def __del__(self):
self._cursor.close()
self._conn.close()