This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.py
69 lines (49 loc) · 1.47 KB
/
storage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
"""
Module for accessing persistent sqlite3 storage.
Classes:
Storage --- Sqlite3 database storage.
"""
__author__ = "Petr Morávek ([email protected])"
__copyright__ = ["Copyright (C) 2008 Kevin Smith",
"Copyright (C) 2009-2011 Petr Morávek"]
__license__ = "GPL 3.0"
__version__ = "0.5.0"
import sqlite3
import threading
class Storage:
"""
Sqlite3 database storage.
Methods:
get_db --- Get database connection instance.
query --- Return ANSI code for changing terminal title.
"""
lock = threading.RLock()
def __init__(self, filename):
"""
Arguments:
filename --- Path to sqlite3 file.
"""
self._filename = filename
def get_db(self, timeout=30):
"""
Get database connection instance.
Keyworded arguments:
timeout --- Number of seconds to wait for database to release lock.
"""
con = sqlite3.connect(self._filename, timeout)
con.row_factory = sqlite3.Row
return con
def query(self, query, values=()):
"""
Perform query in current database.
Arguments:
query --- SQL query.
Keyworded arguments:
values --- Values to substitute in the query.
"""
db = self.get_db()
result = db.cursor().execute(query, values).fetchall()
db.commit()
db.close()
return result