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 ,
@@ -317,10 +319,7 @@ def _request(self, method, url,
317
319
json = json
318
320
)
319
321
320
- if (cache is not False ) and conf .cache_active :
321
- cache = True
322
- else :
323
- cache = False
322
+ cache &= conf .cache_active
324
323
325
324
if save :
326
325
local_filename = url .split ('/' )[- 1 ]
@@ -329,7 +328,7 @@ def _request(self, method, url,
329
328
# ":" so replace them with an underscore
330
329
local_filename = local_filename .replace (':' , '_' )
331
330
332
- local_filepath = os .path .join (savedir or self ._cache_location or '.' , local_filename )
331
+ local_filepath = os .path .join (savedir or self .cache_location or '.' , local_filename )
333
332
334
333
response = self ._download_file (url , local_filepath , cache = cache ,
335
334
continuation = continuation , method = method ,
@@ -341,23 +340,23 @@ def _request(self, method, url,
341
340
return local_filepath
342
341
else :
343
342
query = AstroQuery (method , url , ** req_kwargs )
344
- if (( self ._cache_location is None ) or (not cache ) ):
343
+ if (self .cache_location is None ) or (not cache ):
345
344
with conf .set_temp ("cache_active" , False ):
346
345
response = query .request (self ._session , stream = stream ,
347
346
auth = auth , verify = verify ,
348
347
allow_redirects = allow_redirects ,
349
348
json = json )
350
349
else :
351
- response = query .from_cache (self ._cache_location , conf .cache_timeout )
350
+ response = query .from_cache (self .cache_location , conf .cache_timeout )
352
351
if not response :
353
352
response = query .request (self ._session ,
354
- self ._cache_location ,
353
+ self .cache_location ,
355
354
stream = stream ,
356
355
auth = auth ,
357
356
allow_redirects = allow_redirects ,
358
357
verify = verify ,
359
358
json = json )
360
- to_cache (response , query .request_file (self ._cache_location ))
359
+ to_cache (response , query .request_file (self .cache_location ))
361
360
362
361
self ._last_query = query
363
362
return response
@@ -484,6 +483,8 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
484
483
return response
485
484
486
485
486
+ @deprecated (since = "v0.4.7" , message = ("The suspend_cache function is deprecated,"
487
+ "Use the conf set_temp function instead." ))
487
488
class suspend_cache :
488
489
"""
489
490
A context manager that suspends caching.
0 commit comments