1
0

py3 in qtgui

This commit is contained in:
Dmitry Sorokin
2017-01-30 12:36:56 +03:00
committed by ThomasV
parent 5be78950ca
commit d304ccdf17
28 changed files with 246 additions and 323 deletions

View File

@@ -103,7 +103,7 @@ SERVER_RETRY_INTERVAL = 10
def parse_servers(result):
""" parse servers list into dict format"""
from version import PROTOCOL_VERSION
from .version import PROTOCOL_VERSION
servers = {}
for item in result:
host = item[1]
@@ -123,7 +123,8 @@ def parse_servers(result):
if pruning_level == '': pruning_level = '0'
try:
is_recent = cmp(util.normalize_version(version), util.normalize_version(PROTOCOL_VERSION)) >= 0
except Exception:
except Exception as e:
print_error(e)
is_recent = False
if out and is_recent:
@@ -152,13 +153,15 @@ from .simple_config import SimpleConfig
proxy_modes = ['socks4', 'socks5', 'http']
def serialize_proxy(p):
if type(p) != dict:
if not isinstance(p, dict):
return None
return ':'.join([p.get('mode'),p.get('host'), p.get('port'), p.get('user'), p.get('password')])
def deserialize_proxy(s):
if type(s) not in [str, unicode]:
if not isinstance(s, str):
return None
if s.lower() == 'none':
return None
@@ -183,15 +186,18 @@ def deserialize_proxy(s):
proxy["password"] = args[n]
return proxy
def deserialize_server(server_str):
host, port, protocol = str(server_str).split(':')
assert protocol in 'st'
int(port) # Throw if cannot be converted to int
return host, port, protocol
def serialize_server(host, port, protocol):
return str(':'.join([host, port, protocol]))
class Network(util.DaemonThread):
"""The Network class manages a set of connections to remote electrum
servers, each connected socket is handled by an Interface() object.
@@ -209,7 +215,7 @@ class Network(util.DaemonThread):
if config is None:
config = {} # Do not use mutables as default values!
util.DaemonThread.__init__(self)
self.config = SimpleConfig(config) if type(config) == type({}) else config
self.config = SimpleConfig(config) if isinstance(config, dict) else config
self.num_server = 10 if not self.config.get('oneserver') else 0
self.blockchains = blockchain.read_blockchains(self.config)
self.print_error("blockchains", self.blockchains.keys())
@@ -390,7 +396,7 @@ class Network(util.DaemonThread):
def get_interfaces(self):
'''The interfaces that are in connected state'''
return self.interfaces.keys()
return list(self.interfaces.keys())
def get_servers(self):
if self.irc_servers:
@@ -456,7 +462,7 @@ class Network(util.DaemonThread):
def stop_network(self):
self.print_error("stopping network")
for interface in self.interfaces.values():
for interface in list(self.interfaces.values()):
self.close_interface(interface)
if self.interface:
self.close_interface(self.interface)
@@ -596,7 +602,7 @@ class Network(util.DaemonThread):
def get_index(self, method, params):
""" hashable index for subscriptions and cache"""
return str(method) + (':' + str(params[0]) if params else '')
return str(method) + (':' + str(params[0]) if params else '')
def process_responses(self, interface):
responses = interface.get_responses()
@@ -647,6 +653,7 @@ class Network(util.DaemonThread):
def send(self, messages, callback):
'''Messages is a list of (method, params) tuples'''
messages = list(messages)
with self.lock:
self.pending_sends.append((messages, callback))
@@ -730,7 +737,8 @@ class Network(util.DaemonThread):
self.connection_down(server)
# Send pings and shut down stale interfaces
for interface in self.interfaces.values():
# must use copy of values
for interface in list(self.interfaces.values()):
if interface.has_timed_out():
self.connection_down(interface.server)
elif interface.ping_required():
@@ -1059,15 +1067,14 @@ class Network(util.DaemonThread):
host, port, protocol = server.split(':')
self.set_parameters(host, port, protocol, proxy, auto_connect)
def get_local_height(self):
return self.blockchain().height()
def synchronous_get(self, request, timeout=30):
queue = queue.Queue()
self.send([request], queue.put)
q = queue.Queue()
self.send([request], q.put)
try:
r = queue.get(True, timeout)
r = q.get(True, timeout)
except queue.Empty:
raise BaseException('Server did not answer')
if r.get('error'):