-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
463 lines (346 loc) · 16.1 KB
/
client.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import cmd2
from cmd2 import with_argparser, with_category
import getpass
import argparse
from pathlib import Path
from lazyme.string import color_print, color_str
import matplotlib.pyplot as plt
from util.batch_transfer import BatchTransfer
from util.communicator import Communicator
from util.input import prompt_for_integers, prompt_for_selection_and_quantity, prompt_for_binary_choice
from util.input import PositiveIntegerAction, PositiveFloatAction
from util.shopping import ShoppingCart, Product
from util.enum.command import Command
from util.enum.signal import Signal
from util.enum.tags import Tags
class ClientPrompt(cmd2.Cmd):
"""Interface for user to interact with image repository.
Allows a user to manage the image repository. Use `help` to list
available commands.
Attributes:
prompt (str): Command prompt displayed to the user.
intro (str): Welcome message displayed when the application is launched.
user (str): Initially an empty string until a user logs in, upon which
it stores the username of the logged in user.
Used as a proxy for identifying whether a user is logged in.
"""
prompt = color_str("image-repo> ", color='green')
intro = color_str("Welcome to Image Repository. Type ? to list commands", color='blue')
goodbye = color_str("Thank you for using Image Repository. Goodbye.", color='blue')
CMD_CAT_USER_MANAGEMENT = "User Management"
CMD_CAT_IMAGE_REPOSITORY = "Image Repository"
CMD_CAT_SHOPPING_CART = "Shopping Cart"
def __init__(self):
self.user = None
self.shopping_cart = ShoppingCart()
self.communicator = Communicator()
self.batch_transfer = BatchTransfer(self.communicator)
super().__init__()
del cmd2.Cmd.do_quit
self.hidden_commands.append('py')
self.hidden_commands.append('alias')
self.hidden_commands.append('edit')
self.hidden_commands.append('history')
self.hidden_commands.append('macro')
self.hidden_commands.append('run_pyscript')
self.hidden_commands.append('run_script')
self.hidden_commands.append('quit')
self.hidden_commands.append('set')
self.hidden_commands.append('shell')
self.hidden_commands.append('shortcuts')
self.disable_category(self.CMD_CAT_IMAGE_REPOSITORY, "You must be logged in to manipulate the repository")
self.disable_category(self.CMD_CAT_SHOPPING_CART, "You must be logged in to manage your shopping cart")
def check_if_logged_in(self):
"""Checks if the user has successfully logged in.
When a user logs in, their username is stored and used for quick and simple checks
that the client has been authenticated before performing operations.
Returns:
bool: True if the user has logged already, False otherwise.
"""
is_not_logged_in = (self.user is None)
if is_not_logged_in:
color_print("User must be logged in for this command to function", color='magenta')
return not is_not_logged_in
@with_category(CMD_CAT_USER_MANAGEMENT)
def do_create_user(self, username):
"""Creates a user with the given username.
Prompts the user for the password to be associated with the username.
The username and password are encrypted and sent to the server where
they are stored in the database. Note: the password is not stored as
plaintext in that database, a salted hashed version is stored instead.
Args:
username (str): Username of the user to be created.
"""
if username == "":
color_print("Error: Must provide a username", color='red')
return
color_print("Attempting to create user %s" % username, 'blue')
password = getpass.getpass()
if not password:
color_print("Error: Must enter a password", color='red')
return
self.send_command(Command.CREATE_USER)
self.communicator.send_string(username)
self.communicator.send_string(password)
result = self.communicator.receive_enum(Signal)
if result == Signal.SUCCESS:
color_print("Successfully created user %s" % username, color='blue')
else:
color_print("Error: User already exists", color='red')
@with_category(CMD_CAT_USER_MANAGEMENT)
def do_login(self, username):
"""Logs user in as given username.
Prompts user for the password associated with username. If the
password is incorrect the command fails. Most commands for
the repository don't function unless the user is logged in.
Arguments:
username (str): Username to log in with.
"""
if username == "":
color_print("Error: Must provide a username", color='red')
return
color_print("Attempting to log in as %s" % username, color='blue')
password = getpass.getpass()
if not password:
color_print("Error: Must enter a password", color='red')
return
self.send_command(Command.LOGIN)
if self.verify_password(username, password) == Signal.SUCCESS:
self.user = username
color_print("Successfully logged in as %s" % username, color='blue')
self.enable_category(self.CMD_CAT_IMAGE_REPOSITORY)
self.enable_category(self.CMD_CAT_SHOPPING_CART)
else:
# Note: failure could be owed to two reasons: (1) incorrect password,
# (2) user doesn't exist. It is intentional that the status of a user
# not be revealed - as is standard when usernames can be global
# identifiers such as emails, etc. It would be trivial to enhance
# the Signal codes to specifically specify INCORRECT_PASSWORD and
# USER_DOES_NOT_EXIST should confidentiality of existing users not
# be required.
color_print("Error: Failed to log in as %s" % username, color='red')
complete_add_image = cmd2.Cmd.path_complete
argparser_add_image = argparse.ArgumentParser()
argparser_add_image.add_argument('path', type=str, nargs='+', help='path to an image file (backslash escape is not supported)')
argparser_add_image.add_argument('price', type=float, help='price of the image (product)', action=PositiveFloatAction)
argparser_add_image.add_argument('quantity', type=int, help='number of image (product) to stock', action=PositiveIntegerAction)
@with_category(CMD_CAT_IMAGE_REPOSITORY)
@with_argparser(argparser_add_image)
def do_add_image(self, opts):
"""Adds an image (product) to Image Repository.
Uploads the image specified by path to the server along with the price. If the image does
not exist or is of an invalid format an error will be reported. Prompts the user
to select tags from a displayed list. Pre-defined tags are used as opposed to freeform
text in order to avoid the issue of typos leading to missed matches.
Note: Paths can be entered using tab for autocompletion.
"""
if not self.check_if_logged_in():
return
image_path = Path(" ".join(opts.path)).expanduser().resolve()
if not self.communicator.check_image(image_path):
return
Tags.display_tags_for_selection()
tags = prompt_for_integers(list(map(int, Tags)))
self.send_command(Command.ADD_IMAGE)
self.communicator.send_image(image_path)
self.communicator.send_string(image_path.name)
self.communicator.send_float(opts.price)
self.communicator.send_int(opts.quantity)
self.communicator.send_list(tags)
if self.communicator.receive_enum(Signal) == Signal.FAILURE:
color_print("Error: Image %s could not be added" % image_path.name, color='red')
else:
image_id = self.communicator.receive_int()
color_print("Added image [%d] %s ($%.2f, %d)" % (image_id, image_path.name, opts.price, opts.quantity), color='blue')
argparser_update_image = argparse.ArgumentParser()
argparser_update_image.add_argument('image_id', type=int, help='the identifier of the image to be updated')
argparser_update_image.add_argument('price', type=float, help='price of the image (product)', action=PositiveFloatAction)
argparser_update_image.add_argument('quantity', type=int, help='number of image (product) to stock', action=PositiveIntegerAction)
@with_category(CMD_CAT_IMAGE_REPOSITORY)
@with_argparser(argparser_update_image)
def do_update_image(self, opts):
"""Updates an image (product) in the Image Repository.
Updates the image specified by id with the new cost and quantity. If the image does not
exist in the repository, this operation fails and reports an error.
"""
if not self.check_if_logged_in():
return
self.send_command(Command.UPDATE_IMAGE)
self.communicator.send_int(opts.image_id)
self.communicator.send_float(opts.price)
self.communicator.send_int(opts.quantity)
if self.communicator.receive_enum(Signal) == Signal.FAILURE:
color_print("Error: Image [%d] could not be updated" % opts.image_id, color='red')
else:
color_print("Updated image [%d] with ($%.2f, %d)" % (opts.image_id, opts.price, opts.quantity), color='blue')
argparser_delete_image = argparse.ArgumentParser()
argparser_delete_image.add_argument('image_id', type=int, help='the identifier of the image to be deleted')
@with_category(CMD_CAT_IMAGE_REPOSITORY)
@with_argparser(argparser_delete_image)
def do_delete_image(self, opts):
"""Deletes an image (product) in the Image Repository.
Sends a request to the server to delete the image if it exists. If the image does not exist
in the repository, this operation fails and reports an error.
"""
if not self.check_if_logged_in():
return
if self.shopping_cart.contains(opts.image_id):
if prompt_for_binary_choice("Warning: Product is in shopping cart, do you want to continue and remove it? (y/n)"):
self.shopping_cart.remove_from_cart(opts.image_id)
else:
return
self.send_command(Command.DELETE_IMAGE)
self.communicator.send_int(opts.image_id)
if self.communicator.receive_enum(Signal) == Signal.FAILURE:
color_print("Error: Image [%d] could not be deleted" % opts.image_id, color='red')
else:
color_print("Deleted image [%d]" % opts.image_id, color='blue')
complete_search_by_image = cmd2.Cmd.path_complete
argparser_search_by_image = argparse.ArgumentParser()
argparser_search_by_image.add_argument('path', type=str, nargs='+', help='path to an image file (backslash escape is not supported)')
@with_category(CMD_CAT_IMAGE_REPOSITORY)
@with_argparser(argparser_search_by_image)
def do_search_by_image(self, opts):
"""Find images (products) similar to the provided image.
Uploads the given image to the server and performs a similarity computation
on the other images in the database using nearest neighbours.
"""
if not self.check_if_logged_in():
return
image_path = Path(" ".join(opts.path)).expanduser().resolve()
if not self.communicator.check_image(image_path):
return
self.send_command(Command.SEARCH_BY_IMAGE)
self.communicator.send_image(image_path)
self.communicator.send_string(image_path.name)
signal = self.communicator.receive_enum(Signal)
if signal == Signal.NO_RESULTS:
color_print("No images similar to the provided image were found", color='magenta')
return
self.batch_transfer.receive_batches_of_images(self.add_to_cart_prompt)
@with_category(CMD_CAT_IMAGE_REPOSITORY)
def do_browse_by_tag(self, args):
"""Browses for images (products) matching the given tag(s).
Prompts user to select tags and retrieves images (products) matching those tags
from the database. If the user enters no tags, all images can be shown. If the user
enters multiple tags, then images matching the _intersection_ of those tags will be
retrieved and displayed.
"""
if not self.check_if_logged_in():
return
Tags.display_tags_for_selection()
color_print("Note: if no tags are selected, browse will return all images in the repository", color='blue')
tags = prompt_for_integers(list(map(int, Tags)))
self.send_command(Command.BROWSE_BY_TAG)
self.communicator.send_list(tags)
signal = self.communicator.receive_enum(Signal)
if signal == Signal.NO_RESULTS:
color_print("No matches found for the given tags", color='magenta')
return
self.batch_transfer.receive_batches_of_images(self.add_to_cart_prompt)
@with_category(CMD_CAT_IMAGE_REPOSITORY)
def do_browse_images(self, args):
"""Browses the images (products) in the repository.
Retrieves batches of images from the repository and displays them to the user from most recently added
to least recently added.
"""
if not self.check_if_logged_in():
return
self.send_command(Command.BROWSE_IMAGES)
signal = self.communicator.receive_enum(Signal)
if signal == Signal.NO_RESULTS:
color_print("No images found in the repository", color='magenta')
return
self.batch_transfer.receive_batches_of_images(self.add_to_cart_prompt)
def add_to_cart_prompt(self, image_batch):
"""Prompts the user to enter desired products and quantities.
Prompts the user to enter an item id and quantity or nothing at all if they do not wish
to add any items to their cart. The quantity must not exceed stock, if the user enters
such a quantity, they will have to repeat the request.
Args:
image_batch (list(tuple)): A batch of images where each tuple is an image.
"""
prompt = True
while prompt:
response = prompt_for_selection_and_quantity([image[0] for image in image_batch])
if not response:
break
(product_id, quantity) = response
image_product = Product([image for image in image_batch if image[0] == product_id].pop())
if quantity <= image_product.stock:
image_product.quantity = quantity
self.shopping_cart.add_to_cart(image_product)
else:
color_print("Error: Quantity entered for [%d] exceeds existing stock of %d" % (image_product.id, image_product.stock), color='red')
@with_category(CMD_CAT_SHOPPING_CART)
def do_view_cart(self, args):
"""Display contents of cart.
Lists each item in the shopping cart along with quantity and price.
Total is also listed.
"""
self.check_if_logged_in()
self.shopping_cart.display_cart()
argparser_remove_from_cart = argparse.ArgumentParser()
argparser_remove_from_cart.add_argument('product_id', type=int, help='id of an image (product)')
@with_category(CMD_CAT_SHOPPING_CART)
@with_argparser(argparser_remove_from_cart)
def do_remove_from_cart(self, opts):
"""Removes a product from the cart.
If the product exists in the cart it is removed. If the product was not in the cart a warning
is displayed to the user.
"""
self.check_if_logged_in()
self.shopping_cart.remove_from_cart(opts.product_id)
argparser_update_cart = argparse.ArgumentParser()
argparser_update_cart.add_argument('product_id', type=int, help='id of an image (product)')
argparser_update_cart.add_argument('quantity', type=int, help='new quantity of the image (product)')
@with_category(CMD_CAT_SHOPPING_CART)
@with_argparser(argparser_update_cart)
def do_update_in_cart(self, opts):
"""Updates the quantity of a product in the cart.
If the product exists in the cart it is updated with the new quantity. If the product was not in
the cart a warning is displayed to the user.
"""
self.check_if_logged_in()
self.shopping_cart.update_in_cart(opts.product_id, opts.quantity)
def do_exit(self, args):
"""Exits the image repository.
Closes the connection to the server and exits the client.
"""
print(self.goodbye)
try:
self.send_command(Command.EXIT)
except (BrokenPipeError, ConnectionError):
pass
self.communicator.shutdown()
raise SystemExit
def do_eof(self, args):
"""Exits the image repository.
Closes the connection to the server and exits the client.
"""
self.do_exit(args)
def send_command(self, command):
"""Sends a command to the server.
Encodes the command as a string then encrypts it and sends it to
the server.
Args:
command (Command): The command representing the operation to be performed.
"""
self.communicator.send_enum(command)
def verify_password(self, username, password):
"""Verifies the username and password with the server.
Sends encrypted versions of the username and password to the server and
receives a result in return stating whether the combination was valid.
Args:
username: Username of the user attempting to log in.
password: Password provided by the user attempting to log in.
Returns:
Signal: Either SUCCESS or FAILURE depending on whether the username
password combination was valid.
"""
self.communicator.send_string(username)
self.communicator.send_string(password)
return self.communicator.receive_enum(Signal)
if __name__ == '__main__':
prompt = ClientPrompt()
prompt.cmdloop()