-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkout.py
492 lines (404 loc) · 17.2 KB
/
checkout.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import goodreads
import inventory
import sys
import os.path as path
import logging
from PyQt4 import QtGui, QtCore
from config import Config
from dialogs import ListDialog
from datetime import datetime
from mainui import MainUi
from logging.handlers import SMTPHandler
import unicodecsv as csv
logger = logging.getLogger()
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
logger.addHandler(sh)
fh = logging.FileHandler(path.expanduser("~/checkout.programlog"))
fh.setLevel(logging.DEBUG)
fh.setFormatter(logging.Formatter('[%(asctime)s] %(levelname)s: %(message)s'))
logger.addHandler(fh)
logger.setLevel(logging.DEBUG)
# Default file paths for configuration
CONFIG_FILE_PATH = path.normpath(path.expanduser("~/checkout.credentials"))
DEFAULT_INVENTORY_FILE_PATH = path.normpath(path.expanduser("~/inventory.csv"))
DEFAULT_LOG_PATH = path.normpath(path.expanduser("~/checkout.csv"))
# Keys for the configuration hash
_LIBRARY_SHELF_KEY = "LIBRARY_SHELF"
_LOG_PATH_KEY = 'LOG_PATH'
_INVENTORY_PATH_KEY = 'INVENTORY_PATH'
_REPORTING_ADDRESS_KEY = 'REPORTING_ADDRESS_KEY'
# UI Constants
USER_LABEL_TEXT = None
LIBRARY_SHELF_LABEL_TEXT = None
LOG_LABEL_TEXT = None
INVENTORY_LABEL_TEXT = None
SYNC_BUTTON_TEXT = None
SHELF_DIALOG_LABEL_TEXT = "Which shelf should be used for the library books?"
DEVELOPER_KEY = "DEVELOPER_KEY"
DEVELOPER_SECRET = "DEVELOPER_SECRET"
SEARCH_TIMEOUT = 100
def openfile(filepath):
import os
if sys.platform.startswith('win'):
os.startfile(filepath)
elif sys.platform.startswith("darwin"):
os.system("open " + filepath)
else:
os.system("xdg-open " + filepath)
# To regenerate the gui from the design:
# pyuic4 checkout.ui -o checkoutgui.py
class Main(QtGui.QMainWindow):
# Signals for a dialog to open and wait for the user to auth us to
# goodreads
startWaitForAuth = QtCore.pyqtSignal()
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.progress = QtGui.QProgressDialog(self)
self.progress.setRange(0, 0)
self.progress.setWindowTitle("Working...")
self.books_in_table = []
self._goodreads = None
self.workers = []
def startup(self):
self.ui = MainUi()
self.ui.setupUi(self)
self.ui.options_button.clicked.connect(
lambda: self.ui.uistack.setCurrentWidget(self.ui.optionspage))
self.ui.back_to_books.clicked.connect(
lambda: self.ui.uistack.setCurrentWidget(self.ui.bookpage))
self.ui.back_to_books.clicked.connect(self.ui.bookpage.setFocus)
self.searchTimer = None
self.config = self.init_config()
self.setup_authentication()
self.setup_inventory()
self.ui.bookpage.setFocus()
def setup_authentication(self):
self._waitDialog = QtGui.QMessageBox(
QtGui.QMessageBox.Information,
"Hold up!",
('I\'m opening a link to goodreads for you. '
'Once the goodreads page loads click "Yes" below to continue.'
'If this is your first time, you will have to give "Checkout" '
'permission to access your goodreads account.'),
QtGui.QMessageBox.Ok, parent=self)
self.startWaitForAuth.connect(self._waitDialog.show)
self.startWaitForAuth.connect(self.progress.hide)
self._waitSemaphore = QtCore.QSemaphore()
self._waitDialog.finished.connect(self.progress.show)
self._waitDialog.finished.connect(
lambda _: self._waitSemaphore.release())
self.ui.sync_button.pressed.connect(
lambda: self.ui.sync_button.setEnabled(False))
def setup_inventory(self):
inventory_path = self.config[_INVENTORY_PATH_KEY]
self.inventory = inventory.Inventory(inventory_path)
self.inventory.bookAdded.connect(
lambda book, index: self.ui.addBook(
book, self.checkin_pressed, self.checkout_pressed, index))
def async_load_inventory():
try:
self.inventory.load_inventory()
except IOError:
logger.warn('Error accessing: %s, is this a first run?',
inventory_path)
(self._inventoryThread, self._inventoryWorker) = self.wire_async(
async_load_inventory)
self._inventoryThread.start()
def wait_for_user(self):
self.startWaitForAuth.emit()
self._waitSemaphore.acquire()
def populate_table(self, books):
self.ui.populate_table(books, self.checkin_pressed,
self.checkout_pressed)
def init_config(self):
config = Config(CONFIG_FILE_PATH)
config.connectKey(
_LOG_PATH_KEY,
lambda x: self.ui.log_label.setText(self.log_file(x)))
config.connectKey(
_INVENTORY_PATH_KEY,
lambda x: self.ui.inventory_label.setText(self.inventory_file(x)))
config.connectKey(
_LIBRARY_SHELF_KEY,
lambda x: self.ui.library_shelf_label.setText(
self.library_shelf(x)))
config.connectKey(
_LIBRARY_SHELF_KEY,
lambda x: self.ui.sync_button.setText(self.sync_button(x)))
def update_report_login_ui(login_info):
username, password = login_info.split(',')
self.ui.report_account_field.setText(username)
self.ui.report_password_field.setText(password)
config.connectKey(_REPORTING_ADDRESS_KEY, update_report_login_ui)
def update_report_login_account(new_username):
new_username = str(new_username).strip()
if _REPORTING_ADDRESS_KEY in config:
old_username, password = (
config[_REPORTING_ADDRESS_KEY].split(','))
else:
old_username, password = "", ""
config[_REPORTING_ADDRESS_KEY] = '%s,%s' % (
str(new_username), str(password))
self.ui.report_account_field.textEdited.connect(
update_report_login_account)
def update_report_login_password(new_password):
new_password = str(new_password).strip()
if _REPORTING_ADDRESS_KEY in config:
username, old_password = (
config[_REPORTING_ADDRESS_KEY].split(','))
else:
username, old_password = "", ""
config[_REPORTING_ADDRESS_KEY] = '%s,%s' % (
str(username), str(new_password))
self.ui.report_password_field.textEdited.connect(
update_report_login_password)
try:
with open(CONFIG_FILE_PATH, "r") as configfile:
config.load_from_file(configfile)
except IOError as e:
logger.warn(("Error loading: %s (%s)."
"(This is normal for a first run)") %
(CONFIG_FILE_PATH, e))
default_configuration = [
(_LOG_PATH_KEY, lambda: DEFAULT_LOG_PATH),
(_INVENTORY_PATH_KEY, lambda: DEFAULT_INVENTORY_FILE_PATH),
(DEVELOPER_KEY, self.request_dev_key),
(DEVELOPER_SECRET, self.request_dev_secret),
]
for key, loader in default_configuration:
if key not in config:
logger.info("Missing a value for your %s property" % key)
config[key] = loader()
if _REPORTING_ADDRESS_KEY in config:
username, password = config[_REPORTING_ADDRESS_KEY].split(',')
eh = SMTPHandler(
('smtp.gmail.com', 587),
username, # From
username, # To
'Crash report', # Subject
(username, password), # Login
()) # Empty tuple needed for TLS (which gmail requires)
eh.setLevel(logging.ERROR)
logger.addHandler(eh)
return config
def shelf(self):
return self.config[_LIBRARY_SHELF_KEY]
def request_dev_key(self, log=logger.info):
key, success = QtGui.QInputDialog.getText(
None,
"Developer Key?",
('A developer key is needed to communicate with goodreads.\n'
'You can find it here: http://www.goodreads.com/api/keys'))
if not success:
exit()
return str(key)
def request_dev_secret(self):
secret, success = QtGui.QInputDialog.getText(
None, "Developer Secret?",
('What is the developer secret for the key that you just gave?\n'
'It\'s also on that page with the key: '
'http://www.goodreads.com/api/keys)'))
if not success:
exit()
return str(secret)
def on_search_query_textEdited(self, text):
def do_search(x):
logger.info("Searching for %s", x)
query = str(x).strip().lower()
if query:
self.ui.setSearchQuery(query)
else:
self.ui.clearSearchQuery()
if self.searchTimer:
self.searchTimer.stop()
self.searchTimer = QtCore.QTimer()
self.searchTimer.setSingleShot(True)
self.searchTimer.setInterval(SEARCH_TIMEOUT)
self.searchTimer.timeout.connect(lambda: do_search(text))
self.searchTimer.start()
def goodreads(self):
if not self._goodreads:
self._goodreads = goodreads.GoodReads(
dev_key=self.config[DEVELOPER_KEY],
dev_secret=self.config[DEVELOPER_SECRET],
wait_function=self.wait_for_user)
self._goodreads.on_progress.connect(self.progress.setLabelText)
return self._goodreads
def on_sync_button_pressed(self):
shelf = self.shelf()
logger.info('Syncing books from shelf: %s', shelf)
def async_sync(shelf):
dirty = False
for id, title, author in self.goodreads().listbooks(shelf):
if not self.inventory.containsTitleAndAuthor(title, author):
dirty = True
self.inventory.addBook(title, author)
if dirty:
self.inventory.persist()
(self._syncThread, self._syncWorker) = self.wire_async(
lambda: async_sync(shelf))
self._syncWorker.finished.connect(
lambda: self.ui.sync_button.setEnabled(True))
self._syncWorker.finished.connect(self.ui.back_to_books.setFocus)
self._syncThread.start()
def on_switch_user_button_pressed(self):
logger.warn("Look at switch user again")
def on_switch_library_button_pressed(self):
dialog = ListDialog(self, SHELF_DIALOG_LABEL_TEXT,
self.goodreads().shelves())
def create_new_shelf():
name, success = QtGui.QInputDialog.getText(
dialog,
'Adding a new shelf',
'What would you like to name the new shelf?')
if success:
self.goodreads().add_shelf(str(name))
dialog.setItems(self.goodreads().shelves())
dialog.button.pressed.connect(create_new_shelf)
dialog.button.setText("Create a new shelf")
def accepted():
shelf = dialog.result()
if shelf:
self.config[_LIBRARY_SHELF_KEY] = shelf
dialog.accepted.connect(accepted)
dialog.exec_()
def on_view_log_button_pressed(self):
openfile(self.config[_LOG_PATH_KEY])
def on_view_inventory_button_pressed(self):
openfile(self.config[_INVENTORY_PATH_KEY])
def on_switch_log_button_pressed(self):
file = QtGui.QFileDialog.getSaveFileName(self,
filter='CSV file (*.csv)')
if file:
self.config[_LOG_PATH_KEY] = str(file)
def on_switch_inventory_button_pressed(self):
file = QtGui.QFileDialog.getSaveFileName(self,
filter='CSV file (*.csv)')
if file:
self.config[_INVENTORY_PATH_KEY] = str(file)
self.inventory.persist()
def checkout_pressed(self, book):
""" Connected to signal in populate_table """
name, success = QtGui.QInputDialog.getText(
self,
'Checking out %s' % book.title, 'What is your name?')
if success:
date = datetime.now().strftime("%m/%d/%Y %I:%M%p")
with open(self.config[_LOG_PATH_KEY], 'ab') as logfile:
writer = csv.writer(logfile)
writer.writerow([date, str(name), "checked out", -1,
book.title])
if book in self.inventory: # This can probably be removed
book.check_out_a_copy()
self.inventory.persist()
else:
logger.critical("checkout.checkout_pressed: Didn't find %s"
% book)
def candidates_for_return(self, book):
possible_people = []
with open(self.config[_LOG_PATH_KEY], 'rb') as logfile:
for row in csv.reader(logfile):
try:
if row[4].strip() == book.title:
name = row[1].strip()
if name not in possible_people:
possible_people.append(name)
except ValueError:
# it's ok if there is a malformed cell/row
logger.warn("Malformed row: " + str(row))
return possible_people
def checkin_pressed(self, book):
""" Connected to signal in populate_table """
dialog = ListDialog(self,
"Who are you?",
self.candidates_for_return(book))
def not_on_list():
name, success = QtGui.QInputDialog.getText(
self,
'Return %s' % book.title, 'What is your name?')
name = str(name).strip()
if success and name:
dialog.forced_result = name
dialog.accept()
dialog.button.setText("I'm not on the list!")
dialog.button.pressed.connect(not_on_list)
if dialog.exec_():
name = dialog.result()
if name:
date = datetime.now().strftime("%m/%d/%Y %I:%M%p")
with open(self.config[_LOG_PATH_KEY], 'ab') as logfile:
writer = csv.writer(logfile)
writer.writerow([date, name, "checked in", -1, book.title])
if book in self.inventory: # Can probably remove this check
book.check_in_a_copy()
self.inventory.persist()
else:
logger.critical('Couldn\'t find book: %s' % book)
def log_file(self, log_file):
"""Returns the string used in the Options GUI for the log file """
global LOG_LABEL_TEXT
if not LOG_LABEL_TEXT:
LOG_LABEL_TEXT = str(self.ui.log_label.text())
return LOG_LABEL_TEXT % log_file
def inventory_file(self, inventory_file):
"""Text used in the Options GUI for the inventory file."""
global INVENTORY_LABEL_TEXT
if not INVENTORY_LABEL_TEXT:
INVENTORY_LABEL_TEXT = str(self.ui.inventory_label.text())
return INVENTORY_LABEL_TEXT % inventory_file
def sync_button(self, shelf):
global SYNC_BUTTON_TEXT
if not SYNC_BUTTON_TEXT:
SYNC_BUTTON_TEXT = str(self.ui.sync_button.text())
return SYNC_BUTTON_TEXT % shelf
def library_shelf(self, shelf):
global LIBRARY_SHELF_LABEL_TEXT
""" Returns the string used in the Options GUI for the shelf """
if not LIBRARY_SHELF_LABEL_TEXT:
LIBRARY_SHELF_LABEL_TEXT = str(self.ui.library_shelf_label.text())
return LIBRARY_SHELF_LABEL_TEXT % shelf
def wire_async(self, executable):
thread = QtCore.QThread()
worker = SyncWorker(executable)
worker.moveToThread(thread)
thread.started.connect(self.progress.show)
thread.started.connect(worker.work)
thread.started.connect(
lambda: QtGui.QApplication.setOverrideCursor(
QtGui.QCursor(QtCore.Qt.WaitCursor)))
worker.finished.connect(thread.quit)
worker.finished.connect(worker.deleteLater)
worker.finished.connect(self.progress.hide)
thread.finished.connect(worker.deleteLater)
thread.finished.connect(thread.deleteLater)
thread.finished.connect(QtGui.QApplication.restoreOverrideCursor)
refs = (thread, worker)
self.workers.append(refs)
return refs
class SyncWorker(QtCore.QObject):
# Emitted whenever done
finished = QtCore.pyqtSignal()
def __init__(self, delegate, parent=None):
QtCore.QObject.__init__(self, parent)
self.delegate = delegate
@QtCore.pyqtSlot()
def work(self):
self.delegate()
self.finished.emit()
def main():
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
window.startup()
returncode = app.exec_()
logger.info("======== All Done (%s) ========" % str(returncode))
sys.exit(returncode)
if __name__ == "__main__":
logger.info("======== Starting Up ========")
def error_handler(type, value, tb):
logger.error("Exiting from uncaught exception",
exc_info=(type, value, tb))
exit(1)
sys.excepthook = error_handler
main()