18
18
import astropy .units as u
19
19
from astropy .utils .console import ProgressBarOrSpinner
20
20
import astropy .utils .data
21
+ from astropy .utils import deprecated
22
+ from astropy .utils .exceptions import AstropyDeprecationWarning
21
23
22
24
from astroquery import version , log , conf
23
25
from astroquery .utils import system_tools
@@ -106,7 +108,7 @@ def hash(self):
106
108
return self ._hash
107
109
108
110
def request_file (self , cache_location ):
109
- fn = os . path . join ( cache_location , self .hash () + ".pickle" )
111
+ fn = cache_location . joinpath ( self .hash () + ".pickle" )
110
112
return fn
111
113
112
114
def from_cache (self , cache_location , cache_timeout ):
@@ -116,7 +118,7 @@ def from_cache(self, cache_location, cache_timeout):
116
118
expired = False
117
119
else :
118
120
current_time = datetime .utcnow ()
119
- cache_time = datetime .utcfromtimestamp (os . path . getmtime ( request_file ) )
121
+ cache_time = datetime .utcfromtimestamp (request_file . stat (). st_mtime )
120
122
expired = ((current_time - cache_time ) > timedelta (seconds = cache_timeout ))
121
123
if not expired :
122
124
with open (request_file , "rb" ) as f :
@@ -139,8 +141,8 @@ def remove_cache_file(self, cache_location):
139
141
"""
140
142
request_file = self .request_file (cache_location )
141
143
142
- if os . path . exists ( request_file ) :
143
- os . remove ( request_file )
144
+ if request_file . exists :
145
+ request_file . unlink ( )
144
146
else :
145
147
raise FileNotFoundError (f"Tried to remove cache file { request_file } but "
146
148
"it does not exist" )
@@ -188,7 +190,7 @@ def __init__(self):
188
190
olduseragent = S .headers ['User-Agent' ]))
189
191
190
192
self .name = self .__class__ .__name__ .split ("Class" )[0 ]
191
- self .cache_location = None
193
+ self ._cache_location = None
192
194
193
195
def __call__ (self , * args , ** kwargs ):
194
196
""" init a fresh copy of self """
@@ -229,23 +231,23 @@ def _response_hook(self, response, *args, **kwargs):
229
231
log .log (5 , f"HTTP response\n { response_log } " )
230
232
231
233
@property
232
- def _cache_location (self ):
233
- cl = self .cache_location or os . path . join (paths .get_cache_dir (), 'astroquery' , self .name )
234
- Path ( cl ) .mkdir (parents = True , exist_ok = True )
234
+ def cache_location (self ):
235
+ cl = self ._cache_location or Path (paths .get_cache_dir (), 'astroquery' , self .name )
236
+ cl .mkdir (parents = True , exist_ok = True )
235
237
return cl
236
238
237
- def get_cache_location (self ):
238
- return self ._cache_location
239
+ @cache_location .setter
240
+ def cache_location (self , loc ):
241
+ self ._cache_location = Path (loc )
239
242
240
243
def reset_cache_location (self ):
241
- self .cache_location = None
244
+ """Resets the cache location to the default astropy cache"""
245
+ self ._cache_location = None
242
246
243
247
def clear_cache (self ):
244
248
"""Removes all cache files."""
245
-
246
- cache_files = [x for x in os .listdir (self ._cache_location ) if x .endswith ("pickle" )]
247
- for fle in cache_files :
248
- os .remove (os .path .join (self ._cache_location , fle ))
249
+ for fle in self .cache_location .glob ("*.pickle" ):
250
+ fle .unlink ()
249
251
250
252
def _request (self , method , url ,
251
253
params = None , data = None , headers = None ,
@@ -309,10 +311,7 @@ def _request(self, method, url,
309
311
is True.
310
312
"""
311
313
312
- if (cache is not False ) and conf .cache_active :
313
- cache = True
314
- else :
315
- cache = False
314
+ cache &= conf .cache_active
316
315
317
316
if save :
318
317
local_filename = url .split ('/' )[- 1 ]
@@ -321,7 +320,7 @@ def _request(self, method, url,
321
320
# ":" so replace them with an underscore
322
321
local_filename = local_filename .replace (':' , '_' )
323
322
324
- local_filepath = os .path .join (savedir or self ._cache_location or '.' , local_filename )
323
+ local_filepath = os .path .join (savedir or self .cache_location or '.' , local_filename )
325
324
326
325
response = self ._download_file (url , local_filepath , cache = cache , timeout = timeout ,
327
326
continuation = continuation , method = method ,
@@ -335,23 +334,23 @@ def _request(self, method, url,
335
334
else :
336
335
query = AstroQuery (method , url , params = params , data = data , headers = headers ,
337
336
files = files , timeout = timeout , json = json )
338
- if (( self ._cache_location is None ) or (not cache ) ):
337
+ if (self .cache_location is None ) or (not cache ):
339
338
with conf .set_temp ("cache_active" , False ):
340
339
response = query .request (self ._session , stream = stream ,
341
340
auth = auth , verify = verify ,
342
341
allow_redirects = allow_redirects ,
343
342
json = json )
344
343
else :
345
- response = query .from_cache (self ._cache_location , conf .cache_timeout )
344
+ response = query .from_cache (self .cache_location , conf .cache_timeout )
346
345
if not response :
347
346
response = query .request (self ._session ,
348
- self ._cache_location ,
347
+ self .cache_location ,
349
348
stream = stream ,
350
349
auth = auth ,
351
350
allow_redirects = allow_redirects ,
352
351
verify = verify ,
353
352
json = json )
354
- to_cache (response , query .request_file (self ._cache_location ))
353
+ to_cache (response , query .request_file (self .cache_location ))
355
354
356
355
self ._last_query = query
357
356
return response
@@ -478,6 +477,8 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
478
477
return response
479
478
480
479
480
+ @deprecated (since = "v0.4.7" , message = ("The suspend_cache function is deprecated,"
481
+ "Use the conf set_temp function instead." ))
481
482
class suspend_cache :
482
483
"""
483
484
A context manager that suspends caching.
0 commit comments