1 # -*- coding: utf-8 -*-
2 "default plugin for cache: set it in a simple dictionary"
3 # Copyright (C) 2013-2017 Team tiramisu (see AUTHORS for all contributors)
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by the
7 # Free Software Foundation, either version 3 of the License, or (at your
8 # option) any later version.
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # ____________________________________________________________
20 from os.path import basename, splitext, join, isfile
23 from ..util import SerializeObject
26 class Setting(SerializeObject):
27 """:param extension: database file extension (by default: db)
28 :param dir_database: root database directory (by default: /tmp)
39 return join(SETTING.dir_database, '{0}.{1}'.format(SETTING.name, SETTING.extension))
43 conn = sqlite3.connect(_gen_filename())
44 conn.text_factory = str
45 cursor = conn.cursor()
46 names = [row[0] for row in cursor.execute("SELECT DISTINCT session_id FROM value").fetchall()]
51 def delete_session(session_id, session):
52 conn = sqlite3.connect(_gen_filename())
53 conn.text_factory = str
54 cursor = conn.cursor()
55 cursor.execute("DELETE FROM property WHERE session_id = ?",
57 cursor.execute("DELETE FROM permissive WHERE session_id = ?",
59 cursor.execute("DELETE FROM value WHERE session_id = ?",
61 cursor.execute("DELETE FROM information WHERE session_id = ?",
67 class Storage(object):
68 __slots__ = ('_conn', '_cursor', 'persistent', 'session_id', 'serializable')
71 def __init__(self, session_id, persistent, test=False):
72 self.persistent = persistent
73 self.serializable = self.persistent
74 self.session_id = session_id
75 self._conn = sqlite3.connect(_gen_filename())
76 self._conn.text_factory = str
77 self._cursor = self._conn.cursor()
79 def execute(self, sql, params=None, commit=True):
82 self._cursor.execute(sql, params)
86 def select(self, sql, params=None, only_one=True):
87 self.execute(sql, params=params, commit=False)
89 return self._cursor.fetchone()
91 return self._cursor.fetchall()
96 if not self.persistent:
98 if delete_session is not None:
99 delete_session(self.session_id, session)