Skip to content

Commit 105d232

Browse files
authored
Merge pull request #2384 from bsipocz/utils_cleanup_docs_downloads
Utils to cleanup docs downloads
2 parents 5ac1834 + 9283715 commit 105d232

File tree

9 files changed

+73
-10
lines changed

9 files changed

+73
-10
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ jplsbdb
2424
- Fix a bug for jplsdbd query when the returned physical quantity contains
2525
a unit with exponential. [#2377]
2626

27+
2728
Infrastructure, Utility and Other Changes and Additions
2829
-------------------------------------------------------
2930

31+
- New function, ``utils.cleanup_downloads.cleanup_saved_downloads``, is
32+
added to help the testcleanup narrative in narrative documentations. [#2384]
3033

3134

3235
0.4.6 (2022-03-22)

astroquery/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
from .commons import *
1212
from .process_asyncs import async_to_sync
1313
from .docstr_chompers import prepend_docstr_nosections
14+
from .cleanup_downloads import cleanup_saved_downloads

astroquery/utils/cleanup_downloads.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2+
"""
3+
Utility to cleanup files created during doctesting.
4+
"""
5+
import glob
6+
import os
7+
import shutil
8+
9+
__all__ = ['cleanup_saved_downloads']
10+
11+
12+
def cleanup_saved_downloads(names):
13+
""" Function to clean up save files.
14+
15+
Parameters
16+
----------
17+
names : str or list of str
18+
Files or directories to clean up. Wildcards are accepted.
19+
"""
20+
21+
if isinstance(names, str):
22+
names = [names]
23+
24+
for path in names:
25+
files = glob.glob(path)
26+
for saved_download in files:
27+
try:
28+
shutil.rmtree(saved_download)
29+
except NotADirectoryError:
30+
os.remove(saved_download)

docs/cadc/cadc.rst

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,6 @@ More details about temporary table upload can be found in the IVOA TAP specifica
490490
tess2021167190903-s0039-1-3-0210-s science
491491
tu1657207 science
492492

493-
494-
.. testcleanup::
495-
496-
>>> import os
497-
>>> if os.path.isfile('my_observations.xml'):
498-
... os.remove('my_observations.xml')
499-
500-
501493
The feature allows a user to save the results of a query to use them later or
502494
correlate them with data in other TAP services.
503495

@@ -665,3 +657,9 @@ Reference/API
665657

666658
.. automodapi:: astroquery.cadc
667659
:no-inheritance-diagram:
660+
661+
662+
.. testcleanup::
663+
664+
>>> from astroquery.utils import cleanup_saved_downloads
665+
>>> cleanup_saved_downloads(['my_observations.xml', 'test_output_noauth.xml'])

docs/esa/hsa/hsa.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ First retrieve the observation IDs based on a position on the sky. To achive thi
203203

204204
>>> from astroquery.esa.hsa import HSA
205205
>>>
206-
>>> HSA.query_hsa_tap("select top 10 observation_id from hsa.v_active_observation where contains(point('ICRS', hsa.v_active_observation.ra, hsa.v_active_observation.dec),circle('ICRS', 100.2417,9.895, 1.1))=1", output_format='csv', output_file='results.cvs')
206+
>>> HSA.query_hsa_tap("select top 10 observation_id from hsa.v_active_observation where "
207+
... "contains(point('ICRS', hsa.v_active_observation.ra, hsa.v_active_observation.dec), "
208+
... "circle('ICRS', 100.2417,9.895, 1.1))=1", output_format='csv', output_file='results.csv')
207209
<Table length=9>
208210
observation_id
209211
int64
@@ -238,3 +240,9 @@ Reference/API
238240

239241
.. automodapi:: astroquery.esa.hsa
240242
:no-inheritance-diagram:
243+
244+
245+
.. testcleanup::
246+
247+
>>> from astroquery.utils import cleanup_saved_downloads
248+
>>> cleanup_saved_downloads(['1342195355*', 'results.csv'])

docs/ipac/irsa/sha/sha.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,8 @@ Reference/API
100100
:no-inheritance-diagram:
101101

102102
.. _API: http://sha.ipac.caltech.edu/applications/Spitzer/SHA/help/doc/api.html
103+
104+
.. testcleanup::
105+
106+
>>> from astroquery.utils import cleanup_saved_downloads
107+
>>> cleanup_saved_downloads(['sha_tmp'])

docs/mast/mast.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ with a `~astropy.table.Table` of data products, or a list (or single) obsid as t
304304

305305
>>> from astroquery.mast import Observations
306306
...
307-
>>> single_obs = Observations.query_criteria(obs_collection="IUE",obs_id="lwp13058")
307+
>>> single_obs = Observations.query_criteria(obs_collection="IUE", obs_id="lwp13058")
308308
>>> data_products = Observations.get_product_list(single_obs)
309309
...
310310
>>> manifest = Observations.download_products(data_products, productType="SCIENCE")
@@ -1259,3 +1259,10 @@ Reference/API
12591259
.. automodapi:: astroquery.mast
12601260
:no-inheritance-diagram:
12611261
:inherited-members:
1262+
1263+
1264+
.. testcleanup::
1265+
1266+
>>> from astroquery.utils import cleanup_saved_downloads
1267+
>>> cleanup_saved_downloads(['mastDownload*', 'tess-*', 'lwp13058*', '3dhst*'])
1268+

docs/testing.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,13 @@ This file only needs the ``get_package_data()`` function, which will tell
101101
paths_test = [os.path.join('data', '*.xml')]
102102
103103
return {'astroquery.module.tests': paths_test}
104+
105+
106+
Doctesting
107+
----------
108+
109+
Narrative documentation should also be tested, the ``doctest-remote-data`` directive provides a way
110+
to mark code snippets that relies on remote data access.
111+
112+
If any of the examples include saving data files locally, use the ``testcleanup`` directive and the
113+
`~astroquery.utils.cleanup_saved_downloads` function at the end of the narrative documentation.

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ filterwarnings =
4141
ignore::pytest.PytestUnraisableExceptionWarning
4242
ignore::numpy.VisibleDeprecationWarning
4343
ignore: unclosed file:ResourceWarning
44+
ignore:unclosed <ssl.SSLSocket:ResourceWarning
4445
ignore::UserWarning
4546
ignore::astroquery.exceptions.InputWarning
4647
ignore::astropy.utils.exceptions.AstropyDeprecationWarning

0 commit comments

Comments
 (0)