124 lines
4.4 KiB
Python

# -*- coding: utf-8 -*-
"default plugin for cache: set it in a simple dictionary"
# Copyright (C) 2013-2017 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 Lesser General Public License as published by the
# Free Software Foundation, either version 3 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 Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ____________________________________________________________
from os import unlink
from os.path import basename, splitext, join, isfile
import sqlite3
from glob import glob
class Setting(object):
""":param extension: database file extension (by default: db)
:param dir_database: root database directory (by default: /tmp)
"""
extension = 'db'
dir_database = '/tmp'
name = 'tiramisu'
SETTING = Setting()
def _gen_filename():
return join(SETTING.dir_database, '{0}.{1}'.format(SETTING.name, SETTING.extension))
def list_sessions():
cursor = CONN.cursor()
names = [row[0] for row in cursor.execute("SELECT DISTINCT session_id FROM value").fetchall()]
return names
def delete_session(session_id, session):
cursor = CONN.cursor()
cursor.execute("DELETE FROM property WHERE session_id = ?",
(session_id,))
cursor.execute("DELETE FROM permissive WHERE session_id = ?",
(session_id,))
cursor.execute("DELETE FROM value WHERE session_id = ?",
(session_id,))
cursor.execute("DELETE FROM information WHERE session_id = ?",
(session_id,))
CONN.commit()
global CONN
CONN = None
class Storage(object):
__slots__ = ('_conn', '_cursor', 'persistent', 'session_id', 'serializable')
storage = 'sqlite3'
def __init__(self, session_id, persistent, test=False):
self.persistent = persistent
self.serializable = self.persistent
self.session_id = session_id
global CONN
init = False
if CONN is None:
init = True
CONN = sqlite3.connect(_gen_filename())
CONN.text_factory = str
self._conn = CONN
self._cursor = self._conn.cursor()
if init:
settings_table = 'CREATE TABLE IF NOT EXISTS property(path TEXT,'
settings_table += 'properties text, session_id TEXT, PRIMARY KEY(path, session_id))'
permissives_table = 'CREATE TABLE IF NOT EXISTS permissive(path TEXT,'
permissives_table += 'permissives TEXT, session_id TEXT, PRIMARY KEY(path, session_id))'
values_table = 'CREATE TABLE IF NOT EXISTS value(path TEXT, '
values_table += 'value TEXT, owner TEXT, idx INTEGER, session_id TEXT NOT NULL, '\
'PRIMARY KEY (path, idx, session_id))'
informations_table = 'CREATE TABLE IF NOT EXISTS information(key TEXT,'
informations_table += 'value TEXT, session_id TEXT NOT NULL, '
informations_table += 'PRIMARY KEY (key, session_id))'
self.execute(values_table, commit=False)
self.execute(informations_table, commit=False)
self.execute(settings_table, commit=False)
self.execute(permissives_table)
def commit(self):
self._conn.commit()
def execute(self, sql, params=None, commit=True):
#print(sql, params)
if params is None:
params = tuple()
self._cursor.execute(sql, params)
if commit:
self.commit()
def select(self, sql, params=None, only_one=True):
self.execute(sql, params=params, commit=False)
if only_one:
return self._cursor.fetchone()
else:
return self._cursor.fetchall()
def __del__(self):
self._cursor.close()
#FIXME
#self._conn.close()
if not self.persistent:
session = None
if delete_session is not None:
delete_session(self.session_id, session)
def getsession():
pass