From 3c4fa06c10ab45c0337d97d619fe62ce3bd17407 Mon Sep 17 00:00:00 2001 From: ThomasV Date: Tue, 11 Feb 2014 09:48:02 +0100 Subject: [PATCH] parse_servers --- lib/network.py | 64 +++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/lib/network.py b/lib/network.py index c590d2690..aab98acf0 100644 --- a/lib/network.py +++ b/lib/network.py @@ -22,6 +22,37 @@ DEFAULT_SERVERS = { } +def parse_servers(result): + """ parse servers list into dict format""" + from version import PROTOCOL_VERSION + servers = {} + for item in result: + host = item[1] + out = {} + version = None + pruning_level = '-' + if len(item) > 2: + for v in item[2]: + if re.match("[stgh]\d*", v): + protocol, port = v[0], v[1:] + if port == '': port = DEFAULT_PORTS[protocol] + out[protocol] = port + elif re.match("v(.?)+", v): + version = v[1:] + elif re.match("p\d*", v): + pruning_level = v[1:] + if pruning_level == '': pruning_level = '0' + try: + is_recent = float(version)>=float(PROTOCOL_VERSION) + except Exception: + is_recent = False + + if out and is_recent: + out['pruning'] = pruning_level + servers[host] = out + + return servers + def filter_protocol(servers, p): @@ -353,7 +384,7 @@ class Network(threading.Thread): def on_peers(self, i, r): if not r: return - self.irc_servers = self.parse_servers(r.get('result')) + self.irc_servers = parse_servers(r.get('result')) self.trigger_callback('peers') def on_banner(self, i, r): @@ -379,37 +410,6 @@ class Network(threading.Thread): # return transaction.Transaction(r) - def parse_servers(self, result): - """ parse servers list into dict format""" - from version import PROTOCOL_VERSION - servers = {} - for item in result: - host = item[1] - out = {} - version = None - pruning_level = '-' - if len(item) > 2: - for v in item[2]: - if re.match("[stgh]\d*", v): - protocol, port = v[0], v[1:] - if port == '': port = DEFAULT_PORTS[protocol] - out[protocol] = port - elif re.match("v(.?)+", v): - version = v[1:] - elif re.match("p\d*", v): - pruning_level = v[1:] - if pruning_level == '': pruning_level = '0' - try: - is_recent = float(version)>=float(PROTOCOL_VERSION) - except Exception: - is_recent = False - - if out and is_recent: - out['pruning'] = pruning_level - servers[host] = out - - return servers -