forked from hubertgrzeskowiak/PSSOptimisation2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
243 lines (211 loc) · 9.14 KB
/
main.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import codecs
import urllib
import re
import keyring
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import SIGNAL, SLOT
from login_dialog import LoginDialog
from donation_dialog import DonationDialog
from openfile_dialog import OpenFileDialog
from grades import GradesModel, GradesModelProxy
from checkbox_delegate import CheckBoxDelegate
from web_scraper import ParsingError, LoginError, ConnectionError
from web_scraper import ServiceUnavailableError
from main_window import MainWindow
VERSION = 0.9
class PSSOptimisation(object):
def __init__(self):
QtCore.QCoreApplication.setApplicationName("PSSOptimisation")
QtCore.QCoreApplication.setApplicationVersion(str(VERSION))
QtCore.QCoreApplication.setOrganizationName("Hubert Grzeskowiak")
self.settings = QtCore.QSettings()
self.main_window = MainWindow(True)
self.grades_model = GradesModel(self.main_window)
self.proxy_model = GradesModelProxy()
self.proxy_model.setSourceModel(self.grades_model)
self.initUI()
# tray = QtGui.QSystemTrayIcon(self.main_window)
# tray.setIcon(QtGui.QIcon("icons/Accessories-calculator.svg"))
# tray.connect(tray,
# SIGNAL("activated(QSystemTrayIcon::ActivationReason)"),
# self.trayClicked)
# tray.show()
# self.tray = tray
# def trayClicked(self, reason):
# print reason
# if reason == QtGui.QSystemTrayIcon.DoubleClick:
# self.main_window.setVisible(not self.main_window.isVisible())
def initUI(self):
self.connectUI()
self.main_window.show()
def __columnsChanged(self):
"""This should be called whenever the columns filter is changed.
It's a workaround for a bug in the headerview not updating the columns
properly on filter change.
"""
header = self.main_window.grades_table.horizontalHeader()
header.resizeSections(QtGui.QHeaderView.ResizeToContents)
header.resizeSection(0, header.sectionSize(0)-1)
header.resizeSection(0, header.sectionSize(0)+1)
def connectUI(self):
self.main_window.grades_table.setModel(self.proxy_model)
delegate = CheckBoxDelegate()
self.main_window.grades_table.setItemDelegate(delegate)
self.main_window.connect(self.main_window.action_download,
SIGNAL("triggered()"), self.openLoginDialog)
self.main_window.connect(self.main_window.action_donate,
SIGNAL("triggered()"), self.openDonationDialog)
self.main_window.connect(self.main_window.action_open,
SIGNAL("triggered()"), self.openFileDialog)
self.main_window.connect(self.grades_model,
SIGNAL("dataChanged()"), self.updateStats)
self.main_window.connect(self.grades_model,
SIGNAL("dataChanged()"),
self.main_window.grades_table.resizeColumnsToContents)
self.main_window.connect(self.grades_model,
SIGNAL("modelReset()"), self.updateStats)
self.main_window.connect(self.grades_model,
SIGNAL("modelReset()"),
self.main_window.grades_table.resizeColumnsToContents)
# workaround for buggy headerview
self.main_window.connect(self.proxy_model,
SIGNAL("columnsVisibilityChanged()"),
self.__columnsChanged)
# create actions for toggling table columns
header = self.main_window.grades_table.horizontalHeader()
self.header_actions = QtGui.QActionGroup(header)
self.header_actions.setExclusive(False)
for nr, (name, visible) in enumerate(zip(
self.grades_model.header_data,
self.proxy_model.col_visibility)):
action = self.header_actions.addAction(name)
action.setCheckable(True)
action.setChecked(visible)
action.connect(action, SIGNAL("triggered()"),
lambda nr=nr: self.proxy_model.toggleColumn(nr))
# add that menu as context menu for the header
header.addActions(self.header_actions.actions())
header.setContextMenuPolicy(
QtCore.Qt.ActionsContextMenu)
# and put it into the main window's menus
self.main_window.menu_table_columns.clear()
for action in self.header_actions.actions():
self.main_window.menu_table_columns.addAction(action)
# automatically download new grades (depends on settings)
if self.settings.value("updateOnStart", False).toBool():
QtCore.QTimer.singleShot(200, self.tryAutoDownloadFromPSSO)
def openLoginDialog(self):
self.login_dialog = LoginDialog(self.main_window)
self.login_dialog.exec_()
if self.login_dialog.result():
username = str(self.login_dialog.username_line.text())
password = str(self.login_dialog.password_line.text())
if username and password:
remember = self.login_dialog.remember_checkbox.isChecked()
self.handleLoginData(username, password, remember)
def handleLoginData(self, username, password, remember=False):
"""Try to load the grades by using the provided login credentials.
On failure, an error popup is displayed. On success a loading progress
bar and after that a table is shown.
If "remember" is True, then the login data is saved.
"""
self.main_window.setDisabled(True)
QtGui.QApplication.processEvents()
QtGui.QApplication.processEvents()
progress = 0
try:
iterator = self.grades_model.getFromPSSOIterator(username, password)
for step in iterator:
self.main_window.showProgress(progress, step)
# getFromPSSOIterator defines 8 steps, but 1st is at 0
progress += 100.0/7
QtGui.QApplication.processEvents()
except (ConnectionError, ServiceUnavailableError, ParsingError) as e:
QtGui.QMessageBox.critical(self.main_window,
e.title, e.message)
return
except LoginError as e:
self.clearLoginData(username)
QtGui.QMessageBox.critical(self.main_window,
e.title, e.message)
return
finally:
self.main_window.setEnabled(True)
self.main_window.showProgress(-1)
self.main_window.showTable()
self.main_window.setEnabled(True)
if remember:
self.saveLoginData(username, password)
def saveLoginData(self, username, password):
assert username and password
self.settings.setValue("username", username)
keyring.set_password("PSSO", username, password)
def getLoginData(self):
"""Try to retrieve previously saved username and password. Returns
a tuple of two empty strings on failure.
"""
username = str(self.settings.value("username").toString() or "")
try:
password = keyring.get_password("PSSO", username) or ""
except IOError:
return "", ""
return username, password
def clearLoginData(self, username=None):
"""Remove username and password settings. If a username is given,
its password will be removed. If there is a saved username,
it will also get removed alongside with the corresponding password.
"""
username2 = str(self.settings.value("username").toString() or "")
try:
keyring.delete_password("PSSO", username)
except:
pass
try:
keyring.delete_password("PSSO", username2)
except:
pass
self.settings.remove("username")
def tryAutoDownloadFromPSSO(self):
u, p = self.getLoginData()
if u and p:
self.handleLoginData(u, p)
def openFileDialog(self):
ofd = OpenFileDialog(self.main_window)
accepted = ofd.exec_()
if accepted and ofd.html_file:
try:
html = codecs.open(ofd.html_file, encoding='utf-8')
except IOError:
QtGui.QMessageBox.warning(self.main_window, "File not found",
"Sorry, but there seems to be no file called {}.".format(
ofd.html_file))
return
self.grades_model.getFromHTML(html)
self.main_window.showTable()
def openDonationDialog(self):
dp = DonationDialog(self.main_window)
dp.exec_()
def updateStats(self):
self.main_window.num_of_grades.setText(str(
self.grades_model.getNumOfGrades()))
self.main_window.num_of_credits.setText(str(
self.grades_model.getNumOfCredits()))
self.main_window.average_grade.setText(str(
self.grades_model.getAverageGrade()))
def main():
app = QtGui.QApplication(sys.argv)
#translator = QtCore.QTranslator()
#translator.load("de.qm", ".")
# enable
#app.installTranslator(translator)
# disable
#app.removeTranslator(translator)
ex = PSSOptimisation()
sys.exit(app.exec_())
if __name__ == '__main__':
main()