tiramisu/tiramisu/storage/sqlite3/setting.py

95 lines
4.0 KiB
Python
Raw Normal View History

2013-08-19 11:01:21 +02:00
# -*- coding: utf-8 -*-
"default plugin for setting: 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-19 11:01:21 +02:00
2013-09-07 10:31:39 +02:00
class Settings(Sqlite3DB):
2013-08-19 11:01:21 +02:00
__slots__ = tuple()
2013-08-20 22:45:11 +02:00
def __init__(self, storage):
2013-08-20 23:00:20 +02:00
settings_table = 'CREATE TABLE IF NOT EXISTS property(path text '
settings_table += 'primary key, properties text)'
permissives_table = 'CREATE TABLE IF NOT EXISTS permissive(path text '
permissives_table += 'primary key, permissives text)'
2013-08-19 11:01:21 +02:00
# should init cache too
2013-09-07 10:31:39 +02:00
super(Settings, self).__init__(storage)
2013-08-20 22:45:11 +02:00
self.storage.execute(settings_table, commit=False)
self.storage.execute(permissives_table)
2013-08-19 11:01:21 +02:00
# propertives
2013-08-20 09:47:12 +02:00
def setproperties(self, path, properties):
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2013-08-20 23:00:20 +02:00
self.storage.execute("DELETE FROM property WHERE path = ?", (path,),
False)
self.storage.execute("INSERT INTO property(path, properties) VALUES "
"(?, ?)", (path,
self._sqlite_encode(properties)))
2013-08-19 11:01:21 +02:00
2013-08-20 09:47:12 +02:00
def getproperties(self, path, default_properties):
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2013-08-20 23:00:20 +02:00
value = self.storage.select("SELECT properties FROM property WHERE "
"path = ?", (path,))
2013-08-19 11:01:21 +02:00
if value is None:
return set(default_properties)
else:
2013-08-20 09:47:12 +02:00
return set(self._sqlite_decode(value[0]))
2013-08-19 11:01:21 +02:00
2013-08-20 09:47:12 +02:00
def hasproperties(self, path):
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2013-08-20 23:00:20 +02:00
return self.storage.select("SELECT properties FROM property WHERE "
"path = ?", (path,)) is not None
2013-08-19 11:01:21 +02:00
2013-08-20 09:47:12 +02:00
def reset_all_propertives(self):
2013-08-20 22:45:11 +02:00
self.storage.execute("DELETE FROM property")
2013-08-19 11:01:21 +02:00
2013-08-20 09:47:12 +02:00
def reset_properties(self, path):
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2013-08-20 22:45:11 +02:00
self.storage.execute("DELETE FROM property WHERE path = ?", (path,))
2013-08-19 11:01:21 +02:00
2013-08-20 09:47:12 +02:00
def get_properties(self, context):
2013-08-19 11:01:21 +02:00
"""return all properties in a dictionary
"""
ret = {}
2013-08-20 23:00:20 +02:00
for path, properties in self.storage.select("SELECT * FROM property",
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
properties = self._sqlite_decode(properties)
2013-08-21 23:21:28 +02:00
ret[path] = properties
2013-08-19 11:01:21 +02:00
return ret
# permissive
2013-08-20 09:47:12 +02:00
def setpermissive(self, path, permissive):
2013-08-21 23:21:28 +02:00
path = self._sqlite_encode_path(path)
2013-08-20 23:00:20 +02:00
self.storage.execute("DELETE FROM permissive WHERE path = ?", (path,),
False)
self.storage.execute("INSERT INTO permissive(path, permissives) "
"VALUES (?, ?)", (path,
self._sqlite_encode(permissive)
))
2013-08-19 11:01:21 +02:00
2013-08-20 09:47:12 +02:00
def getpermissive(self, path='_none'):
2013-08-20 23:00:20 +02:00
permissives = self.storage.select("SELECT permissives FROM "
"permissive WHERE path = ?",
(path,))
2013-08-19 11:01:21 +02:00
if permissives is None:
return frozenset()
else:
2013-08-20 09:47:12 +02:00
return frozenset(self._sqlite_decode(permissives[0]))