-
Notifications
You must be signed in to change notification settings - Fork 82
/
ankihub.py
310 lines (278 loc) · 10.6 KB
/
ankihub.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
try:
import httplib
except:
import http.client as httplib
try:
import urllib2
except:
import urllib.request as urllib2
import json
import os
import sys
import zipfile
import traceback
import io
import aqt
from aqt import mw
from aqt.qt import *
from aqt.utils import showInfo
from anki.hooks import addHook
from anki.utils import isMac, isWin
from ..context import APP_ICON
from .AnkiHub.updates import Ui_DialogUpdates
from .AnkiHub.markdown2 import markdown
__all__ = ['update']
# taken from Anki's aqt/profiles.py
def defaultBase():
path = mw.pm.addonFolder()
return os.path.dirname(os.path.abspath(path))
headers = {"User-Agent": "AnkiHub"}
dataPath = os.path.join(defaultBase(),'.fastwq_2.0.x_ankihub.json')
class DialogUpdates(QDialog, Ui_DialogUpdates):
def __init__(self, parent, data, oldData, callback):
parent = parent if parent else mw
QDialog.__init__(self,parent)
self.setModal(True)
self.setWindowFlags(
self.windowFlags() &
~Qt.WindowContextHelpButtonHint
)
self.setWindowIcon(APP_ICON)
self.setupUi(self)
totalSize = sum(map(lambda x:x['size'],data['assets']))
def answer():
self.update.setEnabled(False)
callback(self.appendHtml, self.finish)
self.html = u''
self.appendHtml(markdown(data['body']))
#if not automaticAnswer:
self.update.clicked.connect(lambda:answer())
fromVersion = ''
if 'tag_name' in oldData:
fromVersion = u'from {0} '.format(oldData['tag_name'])
self.labelUpdates.setText(
str(self.labelUpdates.text()).format(
data['name'],
fromVersion,
data['tag_name']))
def appendHtml(self,html='',temp=''):
self.html += html
self.textBrowser.setHtml(u'<html><body>{0}{1}{2}</body></html>'.format(self.html, temp, u'<div id="text_bottom"></div>'))
self.textBrowser.scrollToAnchor('text_bottom')
def finish(self):
self.hide()
self.destroy()
showInfo('Updated. Please restart Anki.')
pass
def installZipFile(data, fname):
base = mw.pm.addonFolder()#os.path.join(defaultBase(),'addons')
if fname.endswith(".py"):
path = os.path.join(base, fname)
with open(path, "wb") as file:
file.write(data)
file.close()
return True
# .zip file
try:
z = zipfile.ZipFile(io.BytesIO(data))
except zipfile.BadZipfile:
return False
for n in z.namelist():
if n.endswith("/"):
# folder; ignore
continue
# write
try:
z.extract(n, base)
except:
print(n)
return True
def asset(a):
return {
'url': a['browser_download_url'],
'size': a['size']
}
def updateSingle(repositories, path, data):
def callback(appendHtml, onReady):
for asset in data['assets']:
code = asset['url']
p, fname = os.path.split(code)
appendHtml(temp='<br />Downloading {1}: {0}%<br/>'.format(0,fname))
try:
urlthread = UrlThread(code)
urlthread.start()
urlthread.join()
response = urlthread.response#urllib2.urlopen(code)
meta = response.info()
file_size = int(meta.getheaders("Content-Length")[0])
except:
appendHtml('Downloading file error!<br/>')
return
d = buffer('')
dl = 0
i = 0
lastPercent = None
while True:
dkb = response.read(1024)
if not dkb:
break
dl += len(dkb)
d += dkb
if dl*100/file_size>i:
lastPercent = int(dl*100/file_size)
i = lastPercent+1
appendHtml(temp='<br />Downloading {1}: {0}%<br/>'.format(lastPercent,fname))
QApplication.instance().processEvents()
appendHtml('<br />Downloading {1}: 100%<br/>'.format(int(dl*100/file_size),fname))
appendHtml('Installing ...<br/>')
if not installZipFile(d, fname):
appendHtml('Corrupt file<br/>')
else:
repositories[path] = data
repositories[path]['update'] = 'ask'
with open(dataPath,'w') as f:
json.dump(repositories,f,indent=2)
f.close()
appendHtml('Done.<br/>')
onReady() # close the AnkiHub update window
return callback
def update(add=[], VERSION='v0.0.0', background=False, parent=None):
parent = parent if parent else mw
# progress win
if not background:
progresswin = QProgressDialog('Update Checking...', '', 0, 0, parent)
progresswin.setWindowModality(Qt.ApplicationModal)
progresswin.setCancelButton(None)
progresswin.setWindowFlags(
progresswin.windowFlags() &
~Qt.WindowContextHelpButtonHint
)
progresswin.setWindowIcon(APP_ICON)
progresswin.setWindowTitle('FastWQ - Updater')
progresswin.resize(280, 60)
progresswin.show()
else:
progresswin = None
#
conn = httplib.HTTPSConnection("api.github.com")
try:
with open(dataPath,'r') as f:
repositories = json.load(f)
f.close()
except:
repositories = {}
for a in add:
if a not in repositories:
repositories[a] = {
'id': 0,
'update': 'ask'
}
for path,repository in repositories.items():
username,repositoryName = path.split('/')
try:
urlthread = UrlThread("https://api.github.com/repos/{0}/releases/latest".format(path))
urlthread.start()
urlthread.join()
release = json.loads(urlthread.response.read())
except Exception as e:
release = {}
if 'id' in release:
if release['id'] != repository['id']:
data = {
'id': release['id'],
'name': repositoryName,
'tag_name': release['tag_name'],
'body': '### {0}\n'.format(release['name']) + release['body'],
'assets': [asset(release['assets'][0])],
'update': 'ask'
}
if 'tag_name' in repository:
oldVersion = map(int,repository['tag_name'][1:].split('.'))
oldVersion = [x for x in oldVersion]
while len(oldVersion)<3:
oldVersion.append(0)
else:
oldVersion = map(int,VERSION[1:].split('.'))#[0,0,0]
oldVersion = [x for x in oldVersion]
newVersion = map(int,data['tag_name'][1:].split('.'))
newVersion = [x for x in newVersion]
isMinor = len(newVersion)>2 and newVersion[2]>0
while len(newVersion)<3:
newVersion.append(0)
i = oldVersion[2]+1
if oldVersion[0]<newVersion[0] or oldVersion[1]<newVersion[1]:
if isMinor:
i = 1
while i<newVersion[2]:
if progresswin and progresswin.wasCanceled():
break
try:
minorTagName = 'v{0}.{1}.{2}'.format(newVersion[0],oldVersion[1],i)
urlthread = UrlThread("https://api.github.com/repos/{0}/releases/tags/{1}".format(path,minorTagName))
urlthread.start()
urlthread.join()
responseData = urlthread.response.read()
minor = json.loads(responseData)
data['body'] += '\n\n### {0}\n'.format(minor['name']) + minor['body']
except:
pass
i += 1
if oldVersion[0]<newVersion[0] or oldVersion[1]<newVersion[1]:
# new major release necessary!
# if the newest version is minor, fetch the additional assets from the major
if isMinor and (background or not progresswin.wasCanceled()):
try:
majorTagName = 'v{0}.{1}'.format(newVersion[0],newVersion[1])
urlthread = UrlThread(
"https://api.github.com/repos/{0}/releases/tags/{1}".format(path,majorTagName),
"https://api.github.com/repos/{0}/releases/tags/{1}.0".format(path,majorTagName)
)
urlthread.start()
urlthread.join()
responseData = urlthread.response.read()
major = json.loads(responseData)
data['body'] += '\n\n### {0}\n'.format(major['name']) + major['body']
except:
pass
if background or not progresswin.wasCanceled():
if progresswin:
progresswin.hide()
progresswin.destroy()
dialog = DialogUpdates(parent, data, repository, updateSingle(repositories, path, data))
dialog.exec_()
dialog.destroy()
else:
if progresswin:
progresswin.hide()
progresswin.destroy()
return 1
else:
if progresswin:
progresswin.hide()
progresswin.destroy()
return 0
if progresswin:
progresswin.hide()
progresswin.destroy()
return -1
class UrlThread(QThread):
def __init__(self, url, backurl=None):
super(UrlThread, self).__init__()
self.response = None
self.url = url
self.backurl = backurl
self.finished = False
def run(self):
try:
self.response = urllib2.urlopen(self.url)
except:
if self.backurl:
try:
self.response = urllib2.urlopen(self.backurl)
except:
pass
self.finished = True
def join(self):
while not self.finished:
QApplication.instance().processEvents()
self.wait(30)