-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_utils.py
174 lines (149 loc) · 3.99 KB
/
db_utils.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import sqlite3
import os
import argparse
this_path = os.path.realpath(__file__)
this_dir = os.path.dirname(this_path)
PROJECT_TABLE_NAME = "passwords"
CONFIG_TABLE_NAME = "sekrets"
CONFIG_KEYS = {
"database-name" : "passwords.db",
"auth-key" : "verification"
}
PROJECT_DB_NAME = this_dir + os.path.sep + CONFIG_KEYS["database-name"]
class Connection:
"""
Class to simplify executing SQL queries
"""
def __init__(self, db_name):
"""
Sets up the SQLite3 connection and cursor variable
"""
self.conn = sqlite3.connect(db_name)
self.cursor = self.conn.cursor()
def execute(self, *args):
"""
Executes an SQLite3 command and returns the return value, if any
"""
self.cursor.execute(*args)
self.conn.commit()
return self.cursor.fetchall()
def __del__(self):
"""
Destroys the internal variables of this class
"""
self.conn.close()
del self.cursor
del self.conn
def execute(*args):
"""
Executes a single SQL command
"""
retval = None
try:
retval = Connection(PROJECT_DB_NAME).execute(*args)
except:
retval = None
return retval
def is_project_table_set_up():
"""
Returns True if the project "password keeper" table exists in the database,
False otherwise
"""
return len(execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name=?;",
(PROJECT_TABLE_NAME,)
)) == 1
def is_config_table_set_up():
"""
Returns True if the project configuration table exists in the database,
False otherwise
"""
return len(execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name=?;",
(CONFIG_TABLE_NAME,)
)) == 1
def is_table_set_up():
"""
Returns True if the table exists in the database, False otherwise
"""
return is_project_table_set_up() and is_config_table_set_up()
def create_project_table():
"""
Returns True if table was successfully created, False otherwise
"""
try:
if not is_project_table_set_up():
execute(
"CREATE TABLE %s (key TEXT, username TEXT, hash TEXT);" % (PROJECT_TABLE_NAME,)
)
if not is_config_table_set_up():
execute(
"CREATE TABLE %s (key TEXT, value TEXT);" % (CONFIG_TABLE_NAME,)
)
return True
except:
raise
return False
def delete_project_table():
"""
Returns True if table was successfully deleted, False otherwise
"""
try:
if is_table_set_up():
execute(
"DROP TABLE %s" % (PROJECT_TABLE_NAME,)
)
if is_config_table_set_up():
execute(
"DROP TABLE %s" % (CONFIG_TABLE_NAME,)
)
return True
except:
raise
return False
def reset_project_table():
delete_project_table()
create_project_table()
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--create",
help="Creates the table",
action="store_true"
)
parser.add_argument(
"-d",
"--delete",
help="Deletes the table",
action="store_true"
)
parser.add_argument(
"-r",
"--reset",
help="Resets the table",
action="store_true"
)
parser.add_argument(
"-t",
"--test",
help="Prints True if the table is set up, False otherwise",
action="store_true"
)
args = parser.parse_args()
if args.create:
if create_project_table():
print "Table now exists"
else:
print "Failed to create table"
elif args.delete:
if delete_project_table():
print "Table deleted"
else:
print "Failed to delete table"
elif args.reset:
reset_project_table()
if args.test:
print(is_table_set_up())
if __name__ == "__main__":
main()