Browse Source

labels: pull in separate thread. also fix error messages

283
ThomasV 11 years ago
parent
commit
3948ef64fa
  1. 78
      plugins/labels.py

78
plugins/labels.py

@ -2,6 +2,7 @@ from electrum.util import print_error
import httplib, urllib import httplib, urllib
import socket import socket
import threading
import hashlib import hashlib
import json import json
from urlparse import urlparse, parse_qs from urlparse import urlparse, parse_qs
@ -74,7 +75,7 @@ class Plugin(BasePlugin):
if self.auth_token(): if self.auth_token():
# If there is an auth token we can try to actually start syncing # If there is an auth token we can try to actually start syncing
self.full_pull() threading.Thread(target=self.do_full_pull).start()
def auth_token(self): def auth_token(self):
return self.config.get("plugin_label_api_key") return self.config.get("plugin_label_api_key")
@ -141,7 +142,7 @@ class Plugin(BasePlugin):
layout.addWidget(self.upload, 2,1) layout.addWidget(self.upload, 2,1)
self.download = QPushButton("Force download") self.download = QPushButton("Force download")
self.download.clicked.connect(lambda: self.full_pull(True)) self.download.clicked.connect(self.full_pull)
layout.addWidget(self.download, 2,2) layout.addWidget(self.download, 2,2)
c = QPushButton(_("Cancel")) c = QPushButton(_("Cancel"))
@ -165,13 +166,17 @@ class Plugin(BasePlugin):
if self.do_full_push(): if self.do_full_push():
QMessageBox.information(None, _("Labels uploaded"), _("Your labels have been uploaded.")) QMessageBox.information(None, _("Labels uploaded"), _("Your labels have been uploaded."))
def full_pull(self, force = False): def full_pull(self):
if self.do_full_pull(force) and force: try:
QMessageBox.information(None, _("Labels synchronized"), _("Your labels have been synchronized.")) self.do_full_pull(True)
self.window.update_history_tab() except BaseException as e:
self.window.update_completions() QMessageBox.information(None, _("Error"), str(e))
self.window.update_receive_tab() return
self.window.update_contacts_tab() QMessageBox.information(None, _("Labels synchronized"), _("Your labels have been synchronized."))
self.window.update_history_tab()
self.window.update_completions()
self.window.update_receive_tab()
self.window.update_contacts_tab()
def do_full_push(self): def do_full_push(self):
try: try:
@ -180,12 +185,12 @@ class Plugin(BasePlugin):
try: try:
encoded_key = self.encode(key) encoded_key = self.encode(key)
except: except:
print_error('cannot encode', encoded_key) print_error('cannot encode', key)
continue continue
try: try:
encoded_value = self.encode(value) encoded_value = self.encode(value)
except: except:
print_error('cannot encode', encoded_value) print_error('cannot encode', value)
continue continue
bundle["labels"][encoded_key] = encoded_value bundle["labels"][encoded_key] = encoded_value
@ -211,34 +216,25 @@ class Plugin(BasePlugin):
return False return False
def do_full_pull(self, force = False): def do_full_pull(self, force = False):
try: connection = httplib.HTTPConnection(self.target_host)
connection = httplib.HTTPConnection(self.target_host) connection.request("GET", ("/api/wallets/%s/labels.json?auth_token=%s" % (self.wallet_id, self.auth_token())),"", {'Content-Type': 'application/json'})
connection.request("GET", ("/api/wallets/%s/labels.json?auth_token=%s" % (self.wallet_id, self.auth_token())),"", {'Content-Type': 'application/json'}) response = connection.getresponse()
response = connection.getresponse() if response.reason == httplib.responses[httplib.NOT_FOUND]:
if response.reason == httplib.responses[httplib.NOT_FOUND]: return
return response = json.loads(response.read())
try: if "error" in response:
response = json.loads(response.read()) raise BaseException(_("Could not sync labels: %s" % response["error"]))
except ValueError as e:
return False
if "error" in response:
QMessageBox.warning(None, _("Error"),_("Could not sync labels: %s" % response["error"]))
return False
for label in response: for label in response:
decoded_key = self.decode(label["external_id"]) decoded_key = self.decode(label["external_id"])
decoded_label = self.decode(label["text"]) decoded_label = self.decode(label["text"])
try: try:
json.dumps(decoded_key) json.dumps(decoded_key)
json.dumps(decoded_label) json.dumps(decoded_label)
except: except:
print_error('json error: cannot save label', decoded_key) print_error('json error: cannot save label', decoded_key)
continue continue
if force or not self.wallet.labels.get(decoded_key): if force or not self.wallet.labels.get(decoded_key):
self.wallet.labels[decoded_key] = decoded_label self.wallet.labels[decoded_key] = decoded_label
self.wallet.storage.put('labels', self.wallet.labels) self.wallet.storage.put('labels', self.wallet.labels)
return True print_error("received labels")
except socket.gaierror as e:
print_error('Error connecting to service: %s ' % e)
return False

Loading…
Cancel
Save