-
Notifications
You must be signed in to change notification settings - Fork 1
/
yajsig.py
422 lines (289 loc) · 12.7 KB
/
yajsig.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
#!/usr/bin/python
import operator, copy, os, sys, threading, cStringIO, traceback
import gc
from PySide.QtCore import *
from PySide.QtGui import *
import preferences
pref = preferences.pref
prefDir = preferences.prefDir
from log import *
from tools import unicode_cleanup
import jsitwindow
import TorrentTable
import jsit_manager
import jsit
VERSION="0.5.2" # Adjusted by make_release
qApp = None
# From http://pydev.blogspot.com.br/2014/03/should-python-garbage-collector-be.html
class GarbageCollector(QObject):
'''
Disable automatic garbage collection and instead collect manually
every INTERVAL milliseconds.
This is done to ensure that garbage collection only happens in the GUI
thread, as otherwise Qt can crash.
'''
INTERVAL = 3000
def __init__(self, parent, debug=False):
QObject.__init__(self, parent)
self.debug = debug
self.timer = QTimer(self)
self.timer.timeout.connect(self.check)
self.threshold = gc.get_threshold()
if self.debug:
print ('gc thresholds:', self.threshold)
gc.disable()
self.timer.start(self.INTERVAL)
def check(self):
#return self.debug_cycles() # uncomment to just debug cycles
l0, l1, l2 = gc.get_count()
if self.debug:
print 'gc_check called:', l0, l1, l2
if l0 > self.threshold[0]:
num = gc.collect(0)
if self.debug:
print 'collecting gen 0, found:', num, 'unreachable'
if l1 > self.threshold[1]:
#gc.set_debug(gc.DEBUG_LEAK)
num = gc.collect(1)
if self.debug:
print 'collecting gen 1, found:', num, 'unreachable'
if l2 > self.threshold[2]:
num = gc.collect(2)
if self.debug:
print 'collecting gen 2, found:', num, 'unreachable'
def debug_cycles(self):
gc.set_debug(gc.DEBUG_SAVEALL)
gc.collect()
for obj in gc.garbage:
print (obj, repr(obj), type(obj))
class JSITWindow(QMainWindow):
def __init__(self, mgr, *args):
QMainWindow.__init__(self, *args)
self.mgr = mgr
self.ui = jsitwindow.Ui_JSIT()
self.ui.setupUi(self)
self._visible = True
self.model = TorrentTable.TorrentTableModel(self, mgr)
self.ui.tableView.setDataModel(self.model)
self._clip = QApplication.clipboard()
# Set up routes to my methods
self.ui.addFiles.clicked.connect(self.addTorrentFiles)
self.ui.addURL.clicked.connect(self.addTorrentURL)
self.ui.startB.clicked.connect(self.startAll)
self.ui.stopB.clicked.connect(self.stopAll)
self.ui.downloadB.clicked.connect(self.downloadAll)
self.ui.watchClipboard.stateChanged.connect(self.watchClipboard)
self.ui.watchDirectory.stateChanged.connect(self.watchDirectory)
self.ui.reloadB.clicked.connect(self.reloadList)
self.ui.actionSave_Preferences.activated.connect(self.savePreferences)
self.ui.actionEdit_Preferences.activated.connect(self.NIY)
self.ui.actionAbout.activated.connect(self.about)
# Set up values from preferences
self.ui.watchClipboard.setChecked(bool(pref("jsit_manager", "watchClipboard", False)))
self.ui.watchDirectory.setChecked(bool(pref("jsit_manager", "watchDirectory", False)))
# Set up log catching for errors
## Not threadsafe...
##addLogCallback(self.logError)
def __repr__(self):
return "JSITWindow(0x%x)" % id(self)
def closeStartBox(self):
self._startBox.close()
self._startBox = None
def update(self):
log(DEBUG)
try:
self.model.update(clip = [self._clip.text(QClipboard.Clipboard), self._clip.text(QClipboard.Selection)])
finally:
QTimer.singleShot(pref("yajsig", "updateRate", 1000), self.update)
def addTorrentFiles(self):
log(INFO)
fns,ftype = QFileDialog.getOpenFileNames(self, "Open Torrent File", "", "Torrent Files (*.torrent)")
for fn in fns:
tor = self.mgr.addTorrentFile(fn, basedir=pref("downloads", "basedir", "downloads"),
unquoteNames=pref("downloads", "unquoteNames", True), interpretDirectories=pref("downloads", "interpretDirectories", True))
def addTorrentURL(self):
log(INFO)
dlg = QInputDialog(self)
dlg.setInputMode(QInputDialog.TextInput)
dlg.setLabelText("Enter http/magnet link:")
dlg.setWindowTitle("Add Torrent from URL")
dlg.resize(500,100)
ok = dlg.exec_()
url = dlg.textValue()
if ok:
tor = self.mgr.addTorrentURL(url, basedir=pref("downloads", "basedir", "downloads"),
unquoteNames=pref("downloads", "unquoteNames", True), interpretDirectories=pref("downloads", "interpretDirectories", True))
def startAll(self):
log(INFO)
self.mgr.startAll()
def stopAll(self):
log(INFO)
self.mgr.stopAll()
def downloadAll(self):
log(INFO)
self.mgr.downloadAll()
def reloadList(self):
log(INFO)
self.mgr.reloadList()
def watchClipboard(self, value):
log(INFO)
self.mgr.watchClipboard(bool(value))
preferences.setValue("jsit_manager", "watchClipboard", bool(value))
def watchDirectory(self, value):
log(INFO)
self.mgr.watchDirectory(bool(value))
preferences.setValue("jsit_manager", "watchDirectory", bool(value))
def showEvent(self, event):
log(DEBUG)
self._visible = True
QTimer.singleShot(0, self.update)
def hideEvent(self, event):
log(DEBUG)
self._visible = False
def quit(self):
log(WARNING)
cpref = preferences.changed()
# Somehow the MBox doesn't close. Figure out later.
if False and cpref:
b = QMessageBox(flags = Qt.Dialog);
b.setText("Unsaved preferences")
b.setInformativeText("The preferences %s have changed."% cpref)
b.setStandardButtons(QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel);
b.setDefaultButton(QMessageBox.Save);
ret = b.exec_();
log(DEBUG, "ret= %s"% ret)
if ret == QMessageBox.Save:
event.handled()
self.savePreferences()
elif ret == QMessageBox.Cancel:
event.ignore()
elif ret == QMessageBox.Disacrd:
event.handled()
else:
log(WARNING, "Messagebox returned %s, not sure what to do." % ret)
else:
self.savePreferences()
log(WARNING, "Quitting.")
self.mgr.release()
qApp.quit()
def savePreferences(self):
log(INFO)
preferences.save(os.path.join(basedir, "preferences.json"))
def NIY(self):
log(INFO)
b = QMessageBox();
b.setText("Sorry!")
b.setInformativeText("Not implemented yet...")
b.setStandardButtons(QMessageBox.Ok);
b.setDefaultButton(QMessageBox.Ok);
ret = b.exec_();
def about(self):
log(INFO)
b = QMessageBox();
b.setText("About YAJSIG")
b.setInformativeText("Yet Another Justseed.it GUI\nVersion %s" % VERSION)
b.setStandardButtons(QMessageBox.Ok);
b.setDefaultButton(QMessageBox.Ok);
ret = b.exec_();
def logError(self, fullmsg, threadName, ltime, level, levelName, caller, msg):
if level > ERROR:
return
log(INFO)
ret = QMessageBox.question(None, "Error Caught!", str(msg), QMessageBox.Ok | QMessageBox.Abort)
if ret == QMessageBox.Abort:
os._exit(1) # Brute force exit, avoid thread problems...
# Exception catchall helper
# Based on http://www.riverbankcomputing.com/pipermail/pyqt/2009-May/022961.html (originally from Eric IDE)
exception_active = False
def excepthook(excType, excValue, tracebackobj):
"""
Global function to catch unhandled exceptions.
@param excType exception type
@param excValue exception value
@param tracebackobj traceback object
"""
# Not reentrant, ignore everything after the first
global exception_active
if exception_active:
return
separator = '-' * 80
logFile = "simple.log"
notice = \
"""An unhandled exception occurred. Please report the problem\n"""\
"""at https://github.com/TouchTone/pyjsit/issues .\n"""\
"""Please include the 'yajsig.log' and 'aria.log' log files in your report.\n\nError information:\n"""
timeString = time.strftime("%Y-%m-%d, %H:%M:%S")
tbinfofile = cStringIO.StringIO()
traceback.print_tb(tracebackobj, None, tbinfofile)
tbinfofile.seek(0)
tbinfo = tbinfofile.read()
errmsg = '%s: \n%s' % (str(excType), str(excValue))
sections = [separator, timeString, separator, errmsg, separator, tbinfo]
msg = '\n'.join(sections)
log(ERROR, "Caught unhandled exception %s!" % errmsg)
log(ERROR, tbinfo)
exception_active = True
ret = QMessageBox.question(None, "Unhandled Exception Caught!", str(notice)+str(msg)+str(VERSION), QMessageBox.Ok | QMessageBox.Abort)
if ret == QMessageBox.Abort:
os._exit(1) # Brute force exit, avoid thread problems...
exception_active = False
# Not threadsafe...
##sys.excepthook = excepthook
if __name__ == "__main__":
global basedir
if len(sys.argv) < 1:
print "Call as %s" % sys.argv[0]
sys.exit(1)
if getattr(sys, 'frozen', None):
basedir = sys._MEIPASS
else:
basedir = os.path.dirname(__file__)
preferences.setBaseDir(basedir)
if os.path.isfile(os.path.join(basedir, "preferences.json")):
preferences.load(os.path.join(basedir, "preferences.json"))
else:
preferences.load(os.path.join(basedir, "defaults.json"))
setLogLevel(pref("yajsig", "logLevel", INFO))
setFileLog(os.path.join(basedir, "yajsig.log"), pref("yajsig", "fileLogLevel", DEBUG))
global qapp
qapp = QApplication([])
# Make sure GC only runs in this thread to prevent crashes
gcol = GarbageCollector(qapp, debug=False)
if len(sys.argv) == 3:
username = sys.argv[1]
password = sys.argv[2]
log(DEBUG, "Got %s:%s from command line." % (username, password))
else:
username = pref("jsit", "username", None)
password = pref("jsit", "password", None)
log(DEBUG, "Got %s:%s from preferences." % (username, password))
while True:
try:
if username == None or password == None:
log(DEBUG, "Need username and password, trigger input.")
raise jsit.APIError("No user/password")
mgr = jsit_manager.Manager(username = username, password = password, torrentdir = prefDir("jsit", "torrentDirectory", "intorrents"))
break
except jsit.APIError, e:
log(WARNING, "JSIT login failed (%s)!" % e)
username, ok = QInputDialog.getText(None, "JS.it Username", "Enter JS.it username:", QLineEdit.Normal, username)
if not ok:
log(ERROR, "Username aborted!")
sys.exit(1)
password, ok = QInputDialog.getText(None, "JS.it Password", "Enter JS.it password:", QLineEdit.Normal, password)
if not ok:
log(ERROR, "Password aborted!")
sys.exit(1)
log(DEBUG, "jsit_manager started...")
preferences.setValue("jsit", "username", username)
preferences.setValue("jsit", "password", password)
##addIgnoreModule("jsit")
##addIgnoreModule("jsit_manager")
##addOnlyModule("TorrentTable")
win = JSITWindow(mgr)
QObject.connect(qapp, SIGNAL("lastWindowClosed()"), win, SLOT("quit()"))
win.show()
if False:
import stacktracer
stacktracer.trace_start("trace.html",interval=5,auto=True) # Set auto flag to always update file!
qapp.exec_()