This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.py
125 lines (107 loc) · 4.71 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
"""
This file is part of the reddit Data Extractor.
The reddit Data Extractor is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The reddit Data Extractor is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with The reddit Data Extractor. If not, see <http://www.gnu.org/licenses/>.
"""
import shelve
import sys
import pathlib
import praw
from queue import Queue
from PyQt4.Qt import QApplication, QThread, QObject, pyqtSignal, pyqtSlot
from RedditDataExtractor.redditDataExtractor import RedditDataExtractor
from RedditDataExtractor.GUI.listModel import ListModel
from RedditDataExtractor.GUI.genericListModelObjects import User, Subreddit
from RedditDataExtractor.GUI.redditDataExtractorGUI import RddtDataExtractorGUI
class QueueMessageReceiver(QObject):
queuePutSignal = pyqtSignal(str)
finished = pyqtSignal()
def __init__(self, queue, *args, **kwargs):
"""
A QObject (to be run in a QThread) which sits waiting for data to come through a Queue.Queue().
It blocks until data is available, and once it has got something from the queue, it sends
it to the main GUI thread by emitting the pyqtSignal 'queuePutSignal'
:type queue: Queue.queue
"""
QObject.__init__(self, *args, **kwargs)
self.queue = queue
self.continueOperation = True
@pyqtSlot()
def run(self):
while(self.continueOperation):
text = self.queue.get()
self.queuePutSignal.emit(text)
self.finished.emit()
def stop(self):
"""
Stop the receiver thread from running. Useful for cleaning up threads when the program is exiting.
"""
self.continueOperation = False
self.queue.put("") # wake up the queue (it blocks until it gets something)
def loadState():
"""
Attempt to load the program from a pickled state in the saves directory.
"""
savePath = pathlib.Path("RedditDataExtractor", "saves")
if not savePath.exists():
savePath.mkdir()
shelf = shelve.open(str(savePath / "settings.db"))
rddtDataExtractor = None
try:
rddtDataExtractor = shelf['rddtDataExtractor']
userListSettings = shelf['userLists']
subredditListSettings = shelf['subredditLists']
rddtDataExtractor.userLists = {}
rddtDataExtractor.subredditLists = {}
# Reconstruct the lists because GUI stuff isn't pickleable
for key, val in userListSettings.items():
rddtDataExtractor.userLists[key] = ListModel(val, User)
for key, val in subredditListSettings.items():
rddtDataExtractor.subredditLists[key] = ListModel(val, Subreddit)
except KeyError:
pass
finally:
shelf.close()
return rddtDataExtractor
def main():
app = QApplication(sys.argv)
rddtDataExtractor = loadState()
if rddtDataExtractor is None:
rddtDataExtractor = RedditDataExtractor()
else:
# If something weird happened to cause currentlyDownloading to be saved as True, set it back to False
rddtDataExtractor.currentlyDownloading = False
# reinstantiate the praw instance because it doesn't shelve properly
# praw shelve issue causes http.validate_certs to be uninstantiated
rddtDataExtractor._r = praw.Reddit(user_agent='Data Extractor for reddit v1.1 by /u/VoidXC')
rddtDataExtractor._r.http.validate_certs = 'RedditDataExtractor/cacert.pem'
queue = Queue()
thread = QThread()
recv = QueueMessageReceiver(queue)
mainGUIWindow = RddtDataExtractorGUI(rddtDataExtractor, queue, recv)
recv.queuePutSignal.connect(mainGUIWindow.append_text)
recv.moveToThread(thread)
thread.started.connect(recv.run)
# Add clean up finished signals so the threads end appropriately when the program ends
recv.finished.connect(thread.quit)
recv.finished.connect(recv.deleteLater)
thread.finished.connect(thread.deleteLater)
# start the receiver
thread.start()
# show the GUI
mainGUIWindow.show()
# display Imgur API pop up if not hidden by user and client-id isn't set
if rddtDataExtractor.showImgurAPINotification and rddtDataExtractor.imgurAPIClientID is None:
mainGUIWindow.notifyImgurAPI()
# and wait for the user to exit
sys.exit(app.exec_())
if __name__ == "__main__":
main()