Skip to content

Commit 0258503

Browse files
authored
Merge pull request #2541 from bsipocz/BUG_cadc_Path_output
ENH: CADC: fix the ability to pass on Path objects as `output_file`
2 parents b0d9f5f + 5ce572b commit 0258503

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ cadc
5656

5757
- Deprecated keywords and ``run_query`` method have been removed. [#2389]
5858

59+
- Added the ability to pass longer that filename Path objects as
60+
``output_file``. [#2541]
61+
5962
casda
6063
^^^^^
6164

astroquery/cadc/core.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import warnings
1212
import requests
1313
from numpy import ma
14+
from pathlib import Path
1415
from urllib.parse import urlencode
1516
from urllib.error import HTTPError
1617

@@ -587,7 +588,7 @@ def exec_sync(self, query, maxrec=None, uploads=None, output_file=None,
587588
the maximum records to return. defaults to the service default
588589
uploads :
589590
Temporary tables to upload and run with the queries
590-
output_file : str or file handler
591+
output_file : str, Path, or file handler
591592
File to save the results to
592593
output_format :
593594
Format of the output (default is basic). Must be one
@@ -608,10 +609,13 @@ def exec_sync(self, query, maxrec=None, uploads=None, output_file=None,
608609
if output_file:
609610
if isinstance(output_file, str):
610611
fname = output_file
612+
elif isinstance(output_file, Path):
613+
# Merge this case into the str once astropy is >=5.1
614+
fname = str(output_file)
611615
elif hasattr(output_file, 'name'):
612616
fname = output_file.name
613617
else:
614-
raise AttributeError('Not a valid file name or file handler')
618+
raise AttributeError('Not a valid file name, Path, or file handler')
615619
result.write(fname, format=output_format, overwrite=True)
616620
return result
617621

astroquery/cadc/tests/test_cadctap.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
from astropy.io.fits.hdu.hdulist import HDUList
1616
from astropy.io.votable.tree import VOTableFile, Resource, Table, Field
1717
from astropy.io.votable import parse
18+
from astropy.utils.diff import report_diff_values
1819
from astroquery.utils.commons import parse_coordinates, FileContainer
1920
from astropy import units as u
2021
import pytest
21-
import tempfile
2222
import requests
2323

2424
from pyvo.auth import securitymethods
@@ -348,7 +348,7 @@ def __init__(self, **param_dict):
348348

349349
@patch('astroquery.cadc.core.get_access_url',
350350
Mock(side_effect=lambda x, y=None: 'https://some.url'))
351-
def test_exec_sync():
351+
def test_exec_sync(tmp_path):
352352
# save results in a file
353353
# create the VOTable result
354354
# example from http://docs.astropy.org/en/stable/io/votable/
@@ -369,20 +369,29 @@ def test_exec_sync():
369369
response = Mock()
370370
response.to_table.return_value = table.to_table()
371371
cadc.cadctap.search = Mock(return_value=response)
372-
output_file = '{}/test_vooutput.xml'.format(tempfile.tempdir)
373-
cadc.exec_sync('some query', output_file=output_file)
374-
375-
actual = parse(output_file)
376-
assert len(votable.resources) == len(actual.resources) == 1
377-
assert len(votable.resources[0].tables) ==\
378-
len(actual.resources[0].tables) == 1
379-
actual_table = actual.resources[0].tables[0]
380-
try:
381-
# TODO remove when astropy LTS upgraded
382-
from astropy.utils.diff import report_diff_values
372+
373+
output_files = [os.path.join(tmp_path, 'test_vooutput.xml'),
374+
Path(tmp_path, 'test_path_vooutput.xml')]
375+
376+
for output_file in output_files:
377+
cadc.exec_sync('some query', output_file=output_file)
378+
379+
actual = parse(output_file)
380+
assert len(votable.resources) == len(actual.resources) == 1
381+
assert len(votable.resources[0].tables) ==\
382+
len(actual.resources[0].tables) == 1
383+
actual_table = actual.resources[0].tables[0]
384+
385+
assert report_diff_values(table, actual_table, fileobj=sys.stdout)
386+
387+
# check file handlers, but skip on windows as it has issues with
388+
# context managers and open files
389+
if not sys.platform.startswith('win'):
390+
with open(os.path.join(tmp_path, 'test_open_file_handler.xml'), 'w+b') as open_file:
391+
cadc.exec_sync('some query', output_file=open_file)
392+
393+
actual = parse(os.path.join(tmp_path, 'test_open_file_handler.xml'))
383394
assert report_diff_values(table, actual_table, fileobj=sys.stdout)
384-
except ImportError:
385-
pass
386395

387396

388397
@patch('astroquery.cadc.core.CadcClass.exec_sync', Mock())

0 commit comments

Comments
 (0)