Merge branch 'etree' into working

This commit is contained in:
Matt Martz 2013-12-02 13:05:43 -06:00
commit 1a0e0b9a9d
1 changed files with 49 additions and 13 deletions

View File

@ -33,7 +33,14 @@ import socket
# Used for bound_interface # Used for bound_interface
socket_socket = socket.socket socket_socket = socket.socket
from xml.dom import minidom as DOM try:
import xml.etree.cElementTree as ET
except ImportError:
try:
import xml.etree.ElementTree as ET
except ImportError:
from xml.dom import minidom as DOM
ET = None
# Begin import game to handle Python 2 and Python 3 # Begin import game to handle Python 2 and Python 3
try: try:
@ -280,6 +287,9 @@ def uploadSpeed(url, sizes, quiet=False):
def getAttributesByTagName(dom, tagName): def getAttributesByTagName(dom, tagName):
"""Retrieve an attribute from an XML document and return it in a """Retrieve an attribute from an XML document and return it in a
consistent format consistent format
Only used with xml.dom.minidom, which is likely only to be used
with python versions older than 2.5
""" """
elem = dom.getElementsByTagName(tagName)[0] elem = dom.getElementsByTagName(tagName)[0]
return dict(list(elem.attributes.items())) return dict(list(elem.attributes.items()))
@ -291,18 +301,30 @@ def getConfig():
""" """
uh = urlopen('http://www.speedtest.net/speedtest-config.php') uh = urlopen('http://www.speedtest.net/speedtest-config.php')
configxml = uh.read() configxml = []
while 1:
configxml.append(uh.read(10240))
if len(configxml[-1]) == 0:
break
if int(uh.code) != 200: if int(uh.code) != 200:
return None return None
uh.close() uh.close()
root = DOM.parseString(configxml) try:
config = { root = ET.fromstring(''.join(configxml))
'client': getAttributesByTagName(root, 'client'), config = {
'times': getAttributesByTagName(root, 'times'), 'client': root.find('client').attrib,
'download': getAttributesByTagName(root, 'download'), 'times': root.find('times').attrib,
'upload': getAttributesByTagName(root, 'upload')} 'download': root.find('download').attrib,
'upload': root.find('upload').attrib}
except AttributeError:
root = DOM.parseString(''.join(configxml))
config = {
'client': getAttributesByTagName(root, 'client'),
'times': getAttributesByTagName(root, 'times'),
'download': getAttributesByTagName(root, 'download'),
'upload': getAttributesByTagName(root, 'upload')}
del root del root
del configxml
return config return config
@ -312,14 +334,26 @@ def closestServers(client, all=False):
""" """
uh = urlopen('http://www.speedtest.net/speedtest-servers.php') uh = urlopen('http://www.speedtest.net/speedtest-servers.php')
serversxml = uh.read() serversxml = []
while 1:
serversxml.append(uh.read(10240))
if len(serversxml[-1]) == 0:
break
if int(uh.code) != 200: if int(uh.code) != 200:
return None return None
uh.close() uh.close()
root = DOM.parseString(serversxml) try:
root = ET.fromstring(''.join(serversxml))
elements = root.getiterator('server')
except AttributeError:
root = DOM.parseString(''.join(serversxml))
elements = root.getElementsByTagName('server')
servers = {} servers = {}
for server in root.getElementsByTagName('server'): for server in elements:
attrib = dict(list(server.attributes.items())) try:
attrib = server.attrib
except AttributeError:
attrib = dict(list(server.attributes.items()))
d = distance([float(client['lat']), float(client['lon'])], d = distance([float(client['lat']), float(client['lon'])],
[float(attrib.get('lat')), float(attrib.get('lon'))]) [float(attrib.get('lat')), float(attrib.get('lon'))])
attrib['d'] = d attrib['d'] = d
@ -328,6 +362,8 @@ def closestServers(client, all=False):
else: else:
servers[d].append(attrib) servers[d].append(attrib)
del root del root
del serversxml
del elements
closest = [] closest = []
for d in sorted(servers.keys()): for d in sorted(servers.keys()):