Skip to content

Commit

Permalink
Merge pull request #33 from astrofrog/fix-bytes
Browse files Browse the repository at this point in the history
Finish fixing bytes issues
  • Loading branch information
astrofrog committed Jul 30, 2017
2 parents 1854155 + bc748e7 commit ca5c799
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 40 deletions.
12 changes: 10 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ python:
- 3.5
- 3.6

env:
matrix:
- PIP_DEPENDENCIES='pytest coveralls pytest-cov pillow numpy astropy'
- PIP_DEPENDENCIES='pytest coveralls pytest-cov'

install:
- export PYTHONIOENCODING=UTF8 # just in case
- pip install pip --upgrade
- pip install pytest pillow numpy astropy
- pip install $PIP_DEPENDENCIES

script:
- py.test pyavm -p no:warnings
- py.test pyavm -p no:warnings --cov pyavm

after_success:
- coveralls
23 changes: 11 additions & 12 deletions pyavm/avm.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
basestring = unicode = str

import warnings
try:
from StringIO import StringIO
except ImportError:
from io import BytesIO as StringIO
from io import BytesIO
import xml.etree.ElementTree as et

from .specs import SPECS, REVERSE_SPECS
Expand Down Expand Up @@ -364,7 +361,7 @@ def __getattr__(self, attribute):
return object.__getattr__(self, attribute)

@classmethod
def from_image(cls, filename, xmp_packet_index=0):
def from_image(cls, filename, xmp_packet_index=None):
"""
Instantiate an AVM object from an existing image.
Expand All @@ -381,10 +378,12 @@ def from_image(cls, filename, xmp_packet_index=0):
# Get XMP data from file
xmp = extract_xmp(filename, xmp_packet_index=xmp_packet_index)

print(xmp)

# Extract XML
start = xmp.index("<?xpacket begin=")
start = xmp.index("?>", start) + 2
end = xmp.index("</x:xmpmeta>") + 12
start = xmp.index(b"<?xpacket begin=")
start = xmp.index(b"?>", start) + 2
end = xmp.index(b"</x:xmpmeta>") + 12

# Extract XML
xml = xmp[start:end]
Expand All @@ -406,7 +405,7 @@ def from_xml(cls, xml):
self = cls()

# Parse XML
tree = et.parse(StringIO(xml))
tree = et.parse(BytesIO(xml))
root = tree.getroot()
avm_content = parse_avm_content(root)

Expand Down Expand Up @@ -461,7 +460,7 @@ def to_wcs(self, use_full_header=False, target_image=None):

if use_full_header and self.Spatial.FITSheader is not None:
print("Using full FITS header from Spatial.FITSheader")
header = fits.Header(txtfile=StringIO(self.Spatial.FITSheader))
header = fits.Header(txtfile=BytesIO(self.Spatial.FITSheader))
return WCS(header)

# Initializing WCS object
Expand Down Expand Up @@ -706,8 +705,8 @@ def to_xml(self):
# Create XML Tree
tree = et.ElementTree(root)

# Need to create a StringIO object to write to
s = StringIO()
# Need to create a BytesIO object to write to
s = BytesIO()
tree.write(s, encoding='utf-8')

# Rewind and read the contents
Expand Down
2 changes: 1 addition & 1 deletion pyavm/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def extract_xmp(image, xmp_packet_index=None):

# Loop through chunks and search for XMP packet
for chunk in png_file.chunks:
if chunk.type == 'iTXt':
if chunk.type == b'iTXt':
if chunk.data.startswith(b'XML:com.adobe.xmp'):
xmp_chunks.append(chunk.data[22:])

Expand Down
Binary file added pyavm/tests/data/eso_eso1723a_320.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pyavm/tests/data/eso_eso1723a_320.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions pyavm/tests/test_avm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
from ..avm import AVM

ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')


def test_from_image_jpg():
avm = AVM.from_image(os.path.join(ROOT, 'eso_eso1723a_320.jpg'))
assert avm.ID == 'eso1723a'


def test_from_image_png():
avm = AVM.from_image(os.path.join(ROOT, 'eso_eso1723a_320.png'))
assert avm.ID == 'eso1723a'


def test_from_image_other(tmpdir):
# Here we test the brute-force search
with open(os.path.join(ROOT, '3c321.avm.xml'), 'rb') as f:
content = f.read()
filename = tmpdir.join('testfile').strpath
with open(filename, 'wb') as f:
f.write(b'<?xpacket begin="<\ufeff>" id="W5M0MpCehiHzreSzNTczkc9d"?>')
f.write(content)
f.write(b'</x:xmpmeta>')
avm = AVM.from_image(filename)
assert avm.Publisher == 'Chandra X-ray Observatory'
10 changes: 5 additions & 5 deletions pyavm/tests/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
import os
import pytest

pytest.importorskip('astropy')

from astropy.io import fits
from astropy.wcs import WCS

from ..avm import AVM, NoSpatialInformation

ROOT = os.path.dirname(os.path.abspath(__file__))


def test_from_header():
from astropy.io import fits
header = fits.Header.fromtextfile(os.path.join(ROOT, 'data', 'example_header.hdr'))
a = AVM.from_header(header)
assert isinstance(a.Spatial.FITSheader, basestring)
Expand All @@ -33,7 +37,6 @@ def test_from_header():


def test_from_header_cd():
from astropy.io import fits
header = fits.Header.fromtextfile(os.path.join(ROOT, 'data', 'example_header.hdr'))
header['CD1_1'] = header.pop('CDELT1')
header['CD2_2'] = header.pop('CDELT2')
Expand All @@ -54,7 +57,6 @@ def test_from_header_cd():


def test_wcs_1():
from astropy.io import fits
header = fits.Header.fromtextfile(os.path.join(ROOT, 'data', 'example_header.hdr'))
a = AVM.from_header(header)
b = AVM.from_wcs(a.to_wcs(), shape=(header['NAXIS2'], header['NAXIS1']))
Expand All @@ -72,8 +74,6 @@ def test_wcs_1():


def test_wcs_2():
from astropy.io import fits
from astropy.wcs import WCS
header = fits.Header.fromtextfile(os.path.join(ROOT, 'data', 'example_header.hdr'))
a = WCS(header)
b = AVM.from_wcs(a).to_wcs()
Expand Down
19 changes: 6 additions & 13 deletions pyavm/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,17 @@

import pytest

try:
from PIL import Image
except ImportError:
try:
import Image
except ImportError:
pytest.skip()
pytest.importorskip('PIL')
pytest.importorskip('numpy')

try:
import numpy as np
except ImportError:
pytest.skip()
from PIL import Image
import numpy as np

from .. import AVM

ROOT = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')

XML_FILES = glob.glob(os.path.join(ROOT, 'data', '*.xml'))
XML_FILES = glob.glob(os.path.join(ROOT, '*.xml'))


@pytest.mark.parametrize('xml_file', XML_FILES)
Expand Down
4 changes: 4 additions & 0 deletions pyavm/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ def test_to_xmp(filename):

@pytest.mark.parametrize('filename', XML_FILES_WCS)
def test_to_wcs(filename):
pytest.importorskip('astropy')
a = AVM.from_xml_file(filename)
a.to_wcs()


@pytest.mark.parametrize('filename', XML_FILES_WCS)
def test_to_wcs_target_image(filename, tmpdir):
pytest.importorskip('PIL')
pytest.importorskip('astropy')
from PIL import Image
image = Image.frombytes(data=b"1111", size=(2, 2), mode="L")
image_file = tmpdir.join('test.png').strpath
Expand All @@ -57,6 +60,7 @@ def test_to_wcs_target_image(filename, tmpdir):

@pytest.mark.parametrize('filename', NO_WCS)
def test_to_wcs_nowcs(filename):
pytest.importorskip('astropy')
a = AVM.from_xml_file(filename)
with pytest.raises(NoSpatialInformation):
a.to_wcs()
8 changes: 1 addition & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#!/usr/bin/env python

from distutils.core import setup

try: # Python 3.x
from distutils.command.build_py import build_py_2to3 as build_py
except ImportError: # Python 2.x
from distutils.command.build_py import build_py
from setuptools import setup

version = '0.9.3.dev'

Expand All @@ -19,7 +14,6 @@
packages=['pyavm', 'pyavm.tests'],
package_data={'pyavm.tests':['data/*.xml', 'data/*.hdr']},
provides=['pyavm'],
cmdclass={'build_py': build_py},
keywords=['Scientific/Engineering'],
long_description="PyAVM is a module to represent, read, and write metadata following the `Astronomy Visualization Metadata <http://www.virtualastronomy.org/avm_metadata.php>`_ (AVM) standard.",
classifiers=[
Expand Down

0 comments on commit ca5c799

Please sign in to comment.