1818import astropy .units as u
1919from astropy .utils .console import ProgressBarOrSpinner
2020import astropy .utils .data
21+ from astropy .utils import deprecated
22+ from astropy .utils .exceptions import AstropyDeprecationWarning
2123
2224from astroquery import version , log , conf
2325from astroquery .utils import system_tools
@@ -106,7 +108,7 @@ def hash(self):
106108 return self ._hash
107109
108110 def request_file (self , cache_location ):
109- fn = os . path . join ( cache_location , self .hash () + ".pickle" )
111+ fn = cache_location . joinpath ( self .hash () + ".pickle" )
110112 return fn
111113
112114 def from_cache (self , cache_location , cache_timeout ):
@@ -116,7 +118,7 @@ def from_cache(self, cache_location, cache_timeout):
116118 expired = False
117119 else :
118120 current_time = datetime .utcnow ()
119- cache_time = datetime .utcfromtimestamp (os . path . getmtime ( request_file ) )
121+ cache_time = datetime .utcfromtimestamp (request_file . stat (). st_mtime )
120122 expired = ((current_time - cache_time ) > timedelta (seconds = cache_timeout ))
121123 if not expired :
122124 with open (request_file , "rb" ) as f :
@@ -139,8 +141,8 @@ def remove_cache_file(self, cache_location):
139141 """
140142 request_file = self .request_file (cache_location )
141143
142- if os . path . exists ( request_file ) :
143- os . remove ( request_file )
144+ if request_file . exists :
145+ request_file . unlink ( )
144146 else :
145147 raise FileNotFoundError (f"Tried to remove cache file { request_file } but "
146148 "it does not exist" )
@@ -188,7 +190,7 @@ def __init__(self):
188190 olduseragent = S .headers ['User-Agent' ]))
189191
190192 self .name = self .__class__ .__name__ .split ("Class" )[0 ]
191- self .cache_location = None
193+ self ._cache_location = None
192194
193195 def __call__ (self , * args , ** kwargs ):
194196 """ init a fresh copy of self """
@@ -229,23 +231,23 @@ def _response_hook(self, response, *args, **kwargs):
229231 log .log (5 , f"HTTP response\n { response_log } " )
230232
231233 @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 )
235237 return cl
236238
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 )
239242
240243 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
242246
243247 def clear_cache (self ):
244248 """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 ()
249251
250252 def _request (self , method , url ,
251253 params = None , data = None , headers = None ,
@@ -309,10 +311,7 @@ def _request(self, method, url,
309311 is True.
310312 """
311313
312- if (cache is not False ) and conf .cache_active :
313- cache = True
314- else :
315- cache = False
314+ cache &= conf .cache_active
316315
317316 if save :
318317 local_filename = url .split ('/' )[- 1 ]
@@ -321,7 +320,7 @@ def _request(self, method, url,
321320 # ":" so replace them with an underscore
322321 local_filename = local_filename .replace (':' , '_' )
323322
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 )
325324
326325 response = self ._download_file (url , local_filepath , cache = cache , timeout = timeout ,
327326 continuation = continuation , method = method ,
@@ -335,23 +334,23 @@ def _request(self, method, url,
335334 else :
336335 query = AstroQuery (method , url , params = params , data = data , headers = headers ,
337336 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 ):
339338 with conf .set_temp ("cache_active" , False ):
340339 response = query .request (self ._session , stream = stream ,
341340 auth = auth , verify = verify ,
342341 allow_redirects = allow_redirects ,
343342 json = json )
344343 else :
345- response = query .from_cache (self ._cache_location , conf .cache_timeout )
344+ response = query .from_cache (self .cache_location , conf .cache_timeout )
346345 if not response :
347346 response = query .request (self ._session ,
348- self ._cache_location ,
347+ self .cache_location ,
349348 stream = stream ,
350349 auth = auth ,
351350 allow_redirects = allow_redirects ,
352351 verify = verify ,
353352 json = json )
354- to_cache (response , query .request_file (self ._cache_location ))
353+ to_cache (response , query .request_file (self .cache_location ))
355354
356355 self ._last_query = query
357356 return response
@@ -478,6 +477,8 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
478477 return response
479478
480479
480+ @deprecated (since = "v0.4.7" , message = ("The suspend_cache function is deprecated,"
481+ "Use the conf set_temp function instead." ))
481482class suspend_cache :
482483 """
483484 A context manager that suspends caching.
0 commit comments