-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpersistent.py
More file actions
115 lines (95 loc) · 4.63 KB
/
Copy pathpersistent.py
File metadata and controls
115 lines (95 loc) · 4.63 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python3
# First choice pack and unpack into sqlite
# Paul H Alfille 2021
# Manages persistent data (across invocations) in an SQL database
# called "persistent.db"
try:
import sqlite3
except:
print("Please install the sqlite3 module")
print("\tit should be part of the standard python3 distribution")
raise
try:
import json
except:
print("Please install the json module")
print("\tit should be part of the standard python3 distribution")
raise
import common
class SQL_persistent:
def __init__( self, user, filename ):
# open or create database
self.user = user
self.filename = filename
self.connection = type(self)._Connect()
@classmethod
def _Connect( cls ):
# class method because filelist and userlist are class methods
connection = sqlite3.connect('persistent.db')
if common.args.sql:
print('CREATE TABLE IF NOT EXISTS persistent (user TEXT NOT NULL, filename TEXT NOT NULL, ptype TEXT NOT NULL, NAME TEXT NOT NULL, jsondata TEXT)' )
cursor = connection.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS persistent (user TEXT NOT NULL, filename TEXT NOT NULL, ptype TEXT NOT NULL, NAME TEXT NOT NULL, jsondata TEXT)' )
if common.args.sql:
print('CREATE UNIQUE INDEX IF NOT EXISTS ipersistent ON persistent (user, filename , ptype , NAME )' )
cursor = connection.cursor()
cursor.execute('CREATE UNIQUE INDEX IF NOT EXISTS ipersistent ON persistent (user, filename , ptype , NAME )' )
connection.commit()
return connection
@classmethod
def Userlist( cls ):
connection = cls._Connect()
if common.args.sql:
print('SELECT DISTINCT user FROM persistent ORDER BY user')
cursor = connection.cursor()
users = cursor.execute('SELECT DISTINCT user FROM persistent ORDER BY user' ).fetchall()
return [u[0] for u in users]
@classmethod
def Filelist( cls ):
connection = cls._Connect()
if common.args.sql:
print('SELECT DISTINCT filename FROM persistent ORDER BY filename')
cursor = connection.cursor()
files = cursor.execute('SELECT DISTINCT filename FROM persistent ORDER BY filename' ).fetchall()
return [f[0] for f in files]
def _NameList( self, ptype ):
if common.args.sql:
print('SELECT name FROM persistent WHERE user=? AND filename=? AND ptype=?',(self.user,self.filename,ptype) )
cursor = self.connection.cursor()
nm = cursor.execute('SELECT name FROM persistent WHERE user=? AND filename=? AND ptype=?',(self.user,self.filename,ptype) ).fetchall()
return [n[0] for n in nm]
def _SetField( self, name, ptype, data ):
if data is None:
#Do a DELETE !
if common.args.sql:
print('DELETE FROM persistent WHERE user=? AND filename=? AND ptype=? AND name=?',(self.user,self.filename,ptype,name) )
cursor = self.connection.cursor()
cursor.execute('DELETE FROM persistent WHERE user=? AND filename=? AND ptype=? AND name=?',(self.user,self.filename,ptype,name) )
self.connection.commit()
else:
j = json.dumps(data)
if common.args.sql:
print('INSERT OR REPLACE INTO persistent (user,filename,ptype,name,jsondata) VALUES (?,?,?,?,?)',(self.user,self.filename,ptype,name,j) )
cursor = self.connection.cursor()
cursor.execute('INSERT OR REPLACE INTO persistent (user,filename,ptype,name,jsondata) VALUES (?,?,?,?,?)',(self.user,self.filename,ptype,name,j) )
self.connection.commit()
def _GetField( self, name, ptype ):
if common.args.sql:
print('SELECT jsondata FROM persistent WHERE user=? AND filename=? AND ptype=? AND name=?',(self.user,self.filename,ptype,name) )
cursor = self.connection.cursor()
j = cursor.execute('SELECT jsondata FROM persistent WHERE user=? AND filename=? AND ptype=? AND name=?',(self.user,self.filename,ptype,name) ).fetchone()
if j is not None:
return json.loads(j[0])
return None
def SearchNames( self ):
return self._NameList("search")
def SetSearch( self, name, searchdict ):
self._SetField( name, "search", searchdict )
def GetSearch( self, name ):
return self._GetField( name, "search" )
def TableNames( self ):
return self._NameList("table")
def SetTable( self, name, table ):
self._SetField( name, "table", table )
def GetTable( self, name ):
return self._GetField( name, "table" )