-
Notifications
You must be signed in to change notification settings - Fork 10
/
updater.py
82 lines (63 loc) · 2.43 KB
/
updater.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
# *-* coding: utf-8 *-*
import logHandler
import config
ADDON_NAME = "translate"
UPDATE_CHECK_INTERVAL = 1200
import threading
import time
import queue
import urllib
import json
import os
class TranslateUpdater(threading.Thread):
quit = False
quitLock = threading.RLock()
queue = queue.Queue()
def run(self):
self.lastCheck = 0
while self.quit is False:
time.sleep(1)
if time.time() - self.lastCheck < UPDATE_CHECK_INTERVAL:
continue
logHandler.log.info("translate: Checking for update...")
try:
res = urllib.request.urlopen("http://www.mtyp.fr/nvda")
data = res.read()
packet = json.loads(data)
mod = packet.get(ADDON_NAME, None)
if mod is not None:
new_version = self.getLatestVersion(mod)
if new_version is not None:
logHandler.log.info("Translate update available: %s" %(new_version["version"]))
self.queue.put({"update": new_version})
self.download(new_version)
except Exception as ex:
logHandler.log.exception("Failed to retrieve update list: %s" %(ex))
self.lastCheck = time.time()
logHandler.log.ingo("Translate: exiting update thred...")
def getLatestVersion(self, mod):
import addonHandler
actual = None
for addon in addonHandler.getAvailableAddons():
if addon.name == ADDON_NAME:
actual = addon
if actual is None:
return None
target = None
for version in mod["versions"]:
if version["version"] > actual.version:
target = version
return target
def download(self, mod):
tmp = os.path.join(config.getUserDefaultConfigPath(), ADDON_NAME + ".nvda-addon")
try:
f = open(tmp, "wb")
res = urllib.request.urlopen(mod["url"])
f.write(res.read())
f.close()
except Exception as ex:
logHandler.log.error("Translate: failed to download %s: %s" %(mod["url"], ex))
return False
self.queue.put({"download": tmp,
"version": mod["version"]})
return True