@@ -33,9 +33,6 @@ def to_cache(response, cache_file):
3333 if hasattr (response , 'request' ):
3434 for key in tuple (response .request .hooks .keys ()):
3535 del response .request .hooks [key ]
36-
37- chache_dir , _ = os .path .split (cache_file )
38- Path (chache_dir ).mkdir (parents = True , exist_ok = True )
3936 with open (cache_file , "wb" ) as f :
4037 pickle .dump (response , f )
4138
@@ -191,7 +188,7 @@ def __init__(self):
191188 olduseragent = S .headers ['User-Agent' ]))
192189
193190 self .name = self .__class__ .__name__ .split ("Class" )[0 ]
194- self .reset_cache_preferences ()
191+ self .cache_location = None
195192
196193 def __call__ (self , * args , ** kwargs ):
197194 """ init a fresh copy of self """
@@ -231,23 +228,24 @@ def _response_hook(self, response, *args, **kwargs):
231228 f"-----------------------------------------" , '\t ' )
232229 log .log (5 , f"HTTP response\n { response_log } " )
233230
234- def clear_cache (self ):
235- """Removes all cache files."""
231+ @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 )
235+ return cl
236236
237- cache_files = [x for x in os .listdir (self .cache_location ) if x .endswith ("pickle" )]
238- for fle in cache_files :
239- os .remove (os .path .join (self .cache_location , fle ))
237+ def get_cache_location (self ):
238+ return self ._cache_location
240239
241- def reset_cache_preferences (self ):
242- """Resets cache preferences to default values"""
240+ def reset_cache_location (self ):
241+ self . cache_location = None
243242
244- self .cache_location = os .path .join (
245- conf .default_cache_location ,
246- self .__class__ .__name__ .split ("Class" )[0 ])
247- os .makedirs (self .cache_location , exist_ok = True )
243+ def clear_cache (self ):
244+ """Removes all cache files."""
248245
249- self .cache_active = conf .default_cache_active
250- self .cache_timeout = conf .default_cache_timeout
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 ))
251249
252250 def _request (self , method , url ,
253251 params = None , data = None , headers = None ,
@@ -311,7 +309,7 @@ def _request(self, method, url,
311309 is True.
312310 """
313311
314- if (cache is not False ) and self .cache_active :
312+ if (cache is not False ) and conf .cache_active :
315313 cache = True
316314 else :
317315 cache = False
@@ -323,7 +321,7 @@ def _request(self, method, url,
323321 # ":" so replace them with an underscore
324322 local_filename = local_filename .replace (':' , '_' )
325323
326- local_filepath = os .path .join (savedir or self .cache_location or '.' , local_filename )
324+ local_filepath = os .path .join (savedir or self ._cache_location or '.' , local_filename )
327325
328326 response = self ._download_file (url , local_filepath , cache = cache , timeout = timeout ,
329327 continuation = continuation , method = method ,
@@ -337,23 +335,23 @@ def _request(self, method, url,
337335 else :
338336 query = AstroQuery (method , url , params = params , data = data , headers = headers ,
339337 files = files , timeout = timeout , json = json )
340- if ((self .cache_location is None ) or ( not self . cache_active ) or (not cache )):
341- with suspend_cache ( self ):
338+ if ((self ._cache_location is None ) or (not cache )):
339+ with conf . set_temp ( "cache_active" , False ):
342340 response = query .request (self ._session , stream = stream ,
343341 auth = auth , verify = verify ,
344342 allow_redirects = allow_redirects ,
345343 json = json )
346344 else :
347- response = query .from_cache (self .cache_location , self .cache_timeout )
345+ response = query .from_cache (self ._cache_location , conf .cache_timeout )
348346 if not response :
349347 response = query .request (self ._session ,
350- self .cache_location ,
348+ self ._cache_location ,
351349 stream = stream ,
352350 auth = auth ,
353351 allow_redirects = allow_redirects ,
354352 verify = verify ,
355353 json = json )
356- to_cache (response , query .request_file (self .cache_location ))
354+ to_cache (response , query .request_file (self ._cache_location ))
357355
358356 self ._last_query = query
359357 return response
@@ -480,23 +478,6 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
480478 return response
481479
482480
483- class suspend_cache :
484- """
485- A context manager that suspends caching.
486- """
487-
488- def __init__ (self , obj ):
489- self .obj = obj
490- self .original_cache_setting = self .obj .cache_active
491-
492- def __enter__ (self ):
493- self .obj .cache_active = False
494-
495- def __exit__ (self , exc_type , exc_value , traceback ):
496- self .obj .cache_active = self .original_cache_setting
497- return False
498-
499-
500481class QueryWithLogin (BaseQuery ):
501482 """
502483 This is the base class for all the query classes which are required to
@@ -549,7 +530,7 @@ def _login(self, *args, **kwargs):
549530 pass
550531
551532 def login (self , * args , ** kwargs ):
552- with suspend_cache ( self ):
533+ with conf . set_temp ( "cache_active" , False ):
553534 self ._authenticated = self ._login (* args , ** kwargs )
554535 return self ._authenticated
555536
0 commit comments