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 ,
@@ -317,10 +319,7 @@ def _request(self, method, url,
317319 json = json
318320 )
319321
320- if (cache is not False ) and conf .cache_active :
321- cache = True
322- else :
323- cache = False
322+ cache &= conf .cache_active
324323
325324 if save :
326325 local_filename = url .split ('/' )[- 1 ]
@@ -329,7 +328,7 @@ def _request(self, method, url,
329328 # ":" so replace them with an underscore
330329 local_filename = local_filename .replace (':' , '_' )
331330
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 )
333332
334333 response = self ._download_file (url , local_filepath , cache = cache ,
335334 continuation = continuation , method = method ,
@@ -341,23 +340,23 @@ def _request(self, method, url,
341340 return local_filepath
342341 else :
343342 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 ):
345344 with conf .set_temp ("cache_active" , False ):
346345 response = query .request (self ._session , stream = stream ,
347346 auth = auth , verify = verify ,
348347 allow_redirects = allow_redirects ,
349348 json = json )
350349 else :
351- response = query .from_cache (self ._cache_location , conf .cache_timeout )
350+ response = query .from_cache (self .cache_location , conf .cache_timeout )
352351 if not response :
353352 response = query .request (self ._session ,
354- self ._cache_location ,
353+ self .cache_location ,
355354 stream = stream ,
356355 auth = auth ,
357356 allow_redirects = allow_redirects ,
358357 verify = verify ,
359358 json = json )
360- to_cache (response , query .request_file (self ._cache_location ))
359+ to_cache (response , query .request_file (self .cache_location ))
361360
362361 self ._last_query = query
363362 return response
@@ -484,6 +483,8 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
484483 return response
485484
486485
486+ @deprecated (since = "v0.4.7" , message = ("The suspend_cache function is deprecated,"
487+ "Use the conf set_temp function instead." ))
487488class suspend_cache :
488489 """
489490 A context manager that suspends caching.
0 commit comments