-
-
Notifications
You must be signed in to change notification settings - Fork 700
/
connector.py
319 lines (268 loc) · 8.33 KB
/
connector.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
# -*- coding: utf-8 -*-
"""
This program 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.
This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
@author: mkaay
"""
SERVER_VERSION = "0.4.2"
from time import sleep
from uuid import uuid4 as uuid
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from xmlrpclib import ServerProxy
import socket
class Connector(QThread):
def __init__(self):
QThread.__init__(self)
self.mutex = QMutex()
self.addr = None
self.errorQueue = []
self.connectionID = None
self.running = True
self.proxy = self.Dummy()
def setAddr(self, addr):
"""
set new address
"""
self.mutex.lock()
self.addr = addr
self.mutex.unlock()
def connectProxy(self):
if isinstance(self.addr, tuple):
while not hasattr(self.addr[1], "server_methods"):
sleep(1)
self.proxy = self.addr[1].server_methods
else:
self.proxy = DispatchRPC(self.mutex, ServerProxy(self.addr, allow_none=True, verbose=False))
self.connect(self.proxy, SIGNAL("proxy_error"), self._proxyError)
self.connect(self.proxy, SIGNAL("connectionLost"), self, SIGNAL("connectionLost"))
try:
server_version = self.proxy.get_server_version()
self.connectionID = uuid().hex
except:
return False
if not server_version:
return False
elif not server_version == SERVER_VERSION:
self.emit(SIGNAL("error_box"), "server is version %s client accepts version %s" % (server_version, SERVER_VERSION))
return False
return True
def canConnect(self):
return self.connectProxy()
def _proxyError(self, func, e):
"""
formats proxy error msg
"""
msg = "proxy error in '%s':\n%s" % (func, e)
self.errorQueue.append(msg)
def getError(self):
self.mutex.lock()
if len(self.errorQueue) > 0:
err = self.errorQueue.pop()
print err
self.emit(SIGNAL("error_box"), err)
self.mutex.unlock()
def stop(self):
"""
stop thread
"""
self.running = False
def run(self):
"""
start thread
(called from thread.start())
"""
self.canConnect()
while self.running:
sleep(1)
self.getError()
class Dummy(object):
def __getattr__(self, attr):
def dummy(*args, **kwargs):
return None
return dummy
def getPackageCollector(self):
"""
grab packages from collector and return the data
"""
return self.proxy.get_collector()
def getLinkInfo(self, id):
"""
grab file info for the given id and return it
"""
w = self.proxy.get_file_info
w.error = False
info = w(id)
if not info: return None
info["downloading"] = None
return info
def getPackageInfo(self, id):
"""
grab package info for the given id and return it
"""
w = self.proxy.get_package_data
w.error = False
return w(id)
def getPackageQueue(self):
"""
grab queue return the data
"""
return self.proxy.get_queue()
def getPackageFiles(self, id):
"""
grab package files and return ids
"""
return self.proxy.get_package_files(id)
def getDownloadQueue(self):
"""
grab files that are currently downloading and return info
"""
return self.proxy.status_downloads()
def getServerStatus(self):
"""
return server status
"""
return self.proxy.status_server()
def addURLs(self, links):
"""
add links to collector
"""
self.proxy.add_urls(links)
def togglePause(self):
"""
toogle pause
"""
return self.proxy.toggle_pause()
def setPause(self, pause):
"""
set pause
"""
if pause:
self.proxy.pause_server()
else:
self.proxy.unpause_server()
def newPackage(self, name):
"""
create a new package and return id
"""
return self.proxy.new_package(name)
def addFileToPackage(self, fileid, packid):
"""
add a file from collector to package
"""
self.proxy.move_file_2_package(fileid, packid)
def pushPackageToQueue(self, packid):
"""
push a package to queue
"""
self.proxy.push_package_2_queue(packid)
def restartPackage(self, packid):
"""
restart a package
"""
self.proxy.restart_package(packid)
def restartFile(self, fileid):
"""
restart a file
"""
self.proxy.restart_file(fileid)
def removePackage(self, packid):
"""
remove a package
"""
self.proxy.del_packages([packid,])
def removeFile(self, fileid):
"""
remove a file
"""
self.proxy.del_links([fileid,])
def uploadContainer(self, filename, type, content):
"""
upload a container
"""
self.proxy.upload_container(filename, type, content)
def getLog(self, offset):
"""
get log
"""
return self.proxy.get_log(offset)
def stopAllDownloads(self):
"""
get log
"""
self.proxy.pause_server()
self.proxy.stop_downloads()
def updateAvailable(self):
"""
update available
"""
return self.proxy.update_available()
def setPackageName(self, pid, name):
"""
set new package name
"""
return self.proxy.set_package_name(pid, name)
def pullOutPackage(self, pid):
"""
pull out package
"""
return self.proxy.pull_out_package(pid)
def captchaWaiting(self):
"""
is the a captcha waiting?
"""
return self.proxy.is_captcha_waiting()
def getCaptcha(self):
"""
get captcha
"""
return self.proxy.get_captcha_task()
def setCaptchaResult(self, cid, result):
"""
get captcha
"""
return self.proxy.set_captcha_result(cid, result)
def getCaptchaStatus(self, cid):
"""
get captcha status
"""
return self.proxy.get_task_status(cid)
def getEvents(self):
"""
get events
"""
return self.proxy.get_events(self.connectionID)
class DispatchRPC(QObject):
def __init__(self, mutex, server):
QObject.__init__(self)
self.mutex = mutex
self.server = server
def __getattr__(self, attr):
self.mutex.lock()
self.fname = attr
f = self.Wrapper(getattr(self.server, attr), self.mutex, self)
return f
class Wrapper(object):
def __init__(self, f, mutex, dispatcher):
self.f = f
self.mutex = mutex
self.dispatcher = dispatcher
self.error = True
def __call__(self, *args, **kwargs):
try:
return self.f(*args, **kwargs)
except socket.error:
self.dispatcher.emit(SIGNAL("connectionLost"))
except Exception, e:
if self.error:
self.dispatcher.emit(SIGNAL("proxy_error"), self.dispatcher.fname, e)
finally:
self.mutex.unlock()