Skip to content

Commit

Permalink
Support Python 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsinger committed May 20, 2014
1 parent 8491272 commit 6c8f975
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
28 changes: 20 additions & 8 deletions gcn/voeventclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
import time
# Prefer lxml.etree over xml.etree (it's faster)
try:
from lxml import etree as ElementTree
from lxml import XMLSyntaxError
import lxml.etree
import io
def parse_from_string(text):
return lxml.etree.parse(io.BytesIO(text)).getroot()
from lxml.etree import XMLSyntaxError
except ImportError:
import xml.etree.cElementTree as ElementTree
import xml.etree.cElementTree
parse_from_string = xml.etree.cElementTree.fromstring
try:
from xml.etree.cElementTree import ParseError as XMLSyntaxError
except ImportError: # Python 2.6 raises a different exception
Expand Down Expand Up @@ -84,6 +88,10 @@ def _open_socket(host, port, iamalive_timeout, max_reconnect_timeout, log):

# memoryview was introduced in Python 2.7. If memoryview is not defined,
# fall back to an implementation that concatenates read-only buffers.
try:
buffer
except NameError:
buffer = bytes
try:
memoryview

Expand Down Expand Up @@ -121,7 +129,7 @@ def _recvall(sock, n):
if time.clock() - start > timeout:
raise socket.timeout(
'timed out while trying to read {0} bytes'.format(n))
newdata = buffer(sock.recv(n))
newdata = sock.recv(n)

# According to the POSIX specification
# http://pubs.opengroup.org/onlinepubs/009695399/functions/recv.html
Expand All @@ -132,7 +140,7 @@ def _recvall(sock, n):

n -= len(newdata)
data += newdata
return data
return buffer(data)


def _recv_packet(sock):
Expand All @@ -158,21 +166,25 @@ def _send_packet(sock, payload):
def _form_response(role, origin, response, timestamp):
"""Form a VOEvent Transport Protocol packet suitable for sending an `ack`
or `iamalive` response."""
return '''<?xml version='1.0' encoding='UTF-8'?><trn:Transport role="''' + role + '''" version="1.0" xmlns:trn="http://telescope-networks.org/schema/Transport/v1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://telescope-networks.org/schema/Transport/v1.1 http://telescope-networks.org/schema/Transport-v1.1.xsd"><Origin>''' + origin + '''</Origin><Response>''' + response + '''</Response><TimeStamp>''' + timestamp + '''</TimeStamp></trn:Transport>'''
return ('''<?xml version='1.0' encoding='UTF-8'?><trn:Transport role="'''
+ role + '''" version="1.0" xmlns:trn="http://telescope-networks.org/schema/Transport/v1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://telescope-networks.org/schema/Transport/v1.1 http://telescope-networks.org/schema/Transport-v1.1.xsd"><Origin>'''
+ origin + '''</Origin><Response>''' + response
+ '''</Response><TimeStamp>''' + timestamp
+ '''</TimeStamp></trn:Transport>''').encode('UTF-8')


def _ingest_packet(sock, ivorn, handler, log):
"""Ingest one VOEvent Transport Protocol packet and act on it, first sending
the appropriate response and then calling the handler if the payload is a
VOEvent."""
# Receive payload
payload = str(_recv_packet(sock))
payload = _recv_packet(sock)
log.debug("received packet of %d bytes", len(payload))
log.debug("payload is:\n%s", payload)

# Parse payload and act on it
try:
root = ElementTree.fromstring(payload)
root = parse_from_string(payload)
except XMLSyntaxError:
log.exception("failed to parse XML, base64-encoded payload is:\n%s",
base64.b64encode(payload))
Expand Down
13 changes: 12 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@

from setuptools import setup
import gcn
import sys

install_requires = []
python_version_tuple = sys.version_info[:2]
if python_version_tuple == (2, 6) or python_version_tuple == (3, 1):
install_requires += ['lxml']

setup(
name='pygcn',
Expand All @@ -41,6 +47,10 @@
'Operating System :: POSIX',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.1',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Topic :: Internet',
'Topic :: Scientific/Engineering :: Astronomy'
],
Expand All @@ -49,5 +59,6 @@
packages=['gcn'],
scripts=['bin/pygcn-listen', 'bin/pygcn-serve'],
test_suite='nose.collector',
tests_require=['nose']
tests_require=['nose'],
install_requires=install_requires
)
4 changes: 2 additions & 2 deletions tests/test_kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
<Description>
</Description>
</voe:VOEvent>
''',
'''.encode('UTF-8'),
'''<?xml version = '1.0' encoding = 'UTF-8'?>
<voe:VOEvent
ivorn="ivo://nasa.gsfc.gcn/gcn"
Expand All @@ -138,7 +138,7 @@
<Param name="Packet_Type" value="4" />
</What>
</voe:VOEvent>
''']
'''.encode('UTF-8')]


def serve(payloads, host='127.0.0.1', port=8099, retransmit_timeout=0, log=None):
Expand Down

0 comments on commit 6c8f975

Please sign in to comment.