-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdatabase.py
More file actions
executable file
·252 lines (189 loc) · 6.24 KB
/
database.py
File metadata and controls
executable file
·252 lines (189 loc) · 6.24 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#! /usr/bin/python3
# * database.py * #
# * Nicholas DiBari * #
# * --------------------------------------------- * #
# * Provides user interface for Get Quote * #
# * DataBase Management * #
# * --------------------------------------------- * #
import argparse
import sys
from tabulate import tabulate
import settings
from utils import DBClient
def create_arg_parser():
"""
Create parser for command line arguments
- Return an ArgumentParser object that will determine what function to run
"""
description = 'The Easy to use Database Manager'
parser = argparse.ArgumentParser(
prog='./database.py',
usage='%(prog)s [-i --interactive] [-p --print] [-s --search <author>] [-d --delete] [--dump <output file>]',
description=description
)
parser.add_argument(
'-i',
'--interactive',
action='store_true',
default=False,
dest='interactive_mode',
help='Open your database interface'
)
parser.add_argument(
'-p',
'--print',
action='store_true',
default=False,
dest='print_db',
help='Print all quotes from your database'
)
parser.add_argument(
'-s',
'--search',
action='store',
type=str,
default='',
nargs='*',
dest='author',
help='Search your database for quotes matching author'
)
parser.add_argument(
'-d',
'--delete',
action='store_true',
default=False,
dest='delete',
help='Run delete interface to remove quotes from your database'
)
parser.add_argument(
'--dump',
action='store',
type=str,
default='',
dest='output_file',
help='Save contents of your database to a text file.'
)
return parser
def print_quotes(db_client):
"""
Print all quotes from the database
"""
quotes = db_client.get_all_quotes()
if not quotes:
print('Your database is empty!')
return
print(tabulate([quote.to_dict() for quote in quotes], headers='keys'))
def delete_quotes(db_client):
"""
Delete a specific quote from the database
TODO: Add option to send author name to function as kwarg
"""
print_quotes(db_client)
choice = input('Please select the number of the quote to delete: ')
confirm = input('Are you sure you want to delete this quote (y/n): ')
if confirm.lower() == 'y':
db_client.delete_quote_from_database(choice)
print('Deleted quote {}'.format(choice))
def search_quotes(db_client, to_search=None):
"""
Search database for all quotes from an author and write them to the console
:param db_client: (DBClient) Connection to database
:param to_search: Name of author to search database for matching quotes
"""
flag = False
if to_search:
flag = True # Account for search argument from command line
while True:
if not to_search:
to_search = input('Please enter an author to search for: ')
quotes = db_client.get_quotes_for_author(to_search)
if not quotes:
print('Sorry, did not find {} in the database.'.format(to_search))
else:
print('Found the following quotes by {}'.format(to_search))
print(tabulate([quote.to_dict() for quote in quotes], headers='keys'))
if flag:
break
choice = input('Would you like you search again? (y/n): ')
if choice.lower() == 'n':
break
else:
to_search = None
def dump_quotes(db_client, file_name=None):
"""
Write all quotes in the database to a text file
:param db_client: (DBClient) Connection to database
:param file_name: (str) Name of file to write data
"""
if not file_name:
file_name = input('Please enter the filename to save the quotes to: ')
if not file_name.endswith('.txt'):
file_name += '.txt'
quotes = db_client.get_all_quotes()
with open(file_name, 'w') as f:
f.write(tabulate([quote.to_dict() for quote in quotes], headers='keys'))
f.write('\n')
print('Done! Your quotes can be found in {}'.format(file_name))
def interactive_mode(db_client):
"""
Loop to run the functionality in a shell-like mode
"""
flag = True
while flag:
print('Please enter a choice:')
print('1. Print all Quotes')
print('2. Delete a Quote')
print('3. Search for author')
print('4. Dump Database to text file')
print('5. [EXIT]')
choice = input('> ')
try:
choice = int(choice)
# ERROR CHECK
if choice < 1 or choice > 5:
print('Sorry that is not a valid choice. Try again')
# PRINT QUOTES
elif choice == 1:
print_quotes(db_client)
# DELETE QUOTE
elif choice == 2:
delete_quotes(db_client)
# SEARCH QUOTE
elif choice == 3:
search_quotes(db_client)
# DUMP DATABASE
elif choice == 4:
dump_quotes(db_client)
# [EXIT]
elif choice == 5:
flag = False
# ERROR CHECK
else:
print('Sorry that is not a valid input. Try again')
except ValueError:
print('Enter in a number silly!')
def main():
"""
Driver function for script
Determines to run interactive shell or to call specific function using
command line arguments
"""
db_client = DBClient(settings.DB_NAME)
parser = create_arg_parser()
args = parser.parse_args(sys.argv[1:])
if args.interactive_mode:
interactive_mode(db_client)
elif args.print_db:
print_quotes(db_client)
elif args.author:
author = ' '.join(args.author)
search_quotes(db_client, to_search=author)
elif args.delete:
delete_quotes(db_client)
elif args.output_file:
dump_quotes(db_client, file_name=args.output_file)
else:
parser.print_help()
db_client.close_connection()
if __name__ == '__main__':
main()