@@ -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 ,
@@ -319,7 +317,7 @@ def _request(self, method, url,
319317 json = json
320318 )
321319
322- if (cache is not False ) and self .cache_active :
320+ if (cache is not False ) and conf .cache_active :
323321 cache = True
324322 else :
325323 cache = False
@@ -331,7 +329,7 @@ def _request(self, method, url,
331329 # ":" so replace them with an underscore
332330 local_filename = local_filename .replace (':' , '_' )
333331
334- local_filepath = os .path .join (savedir or self .cache_location or '.' , local_filename )
332+ local_filepath = os .path .join (savedir or self ._cache_location or '.' , local_filename )
335333
336334 response = self ._download_file (url , local_filepath , cache = cache ,
337335 continuation = continuation , method = method ,
@@ -343,23 +341,23 @@ def _request(self, method, url,
343341 return local_filepath
344342 else :
345343 query = AstroQuery (method , url , ** req_kwargs )
346- if ((self .cache_location is None ) or ( not self . cache_active ) or (not cache )):
347- with suspend_cache ( self ):
344+ if ((self ._cache_location is None ) or (not cache )):
345+ with conf . set_temp ( "cache_active" , False ):
348346 response = query .request (self ._session , stream = stream ,
349347 auth = auth , verify = verify ,
350348 allow_redirects = allow_redirects ,
351349 json = json )
352350 else :
353- response = query .from_cache (self .cache_location , self .cache_timeout )
351+ response = query .from_cache (self ._cache_location , conf .cache_timeout )
354352 if not response :
355353 response = query .request (self ._session ,
356- self .cache_location ,
354+ self ._cache_location ,
357355 stream = stream ,
358356 auth = auth ,
359357 allow_redirects = allow_redirects ,
360358 verify = verify ,
361359 json = json )
362- to_cache (response , query .request_file (self .cache_location ))
360+ to_cache (response , query .request_file (self ._cache_location ))
363361
364362 self ._last_query = query
365363 return response
@@ -486,23 +484,6 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
486484 return response
487485
488486
489- class suspend_cache :
490- """
491- A context manager that suspends caching.
492- """
493-
494- def __init__ (self , obj ):
495- self .obj = obj
496- self .original_cache_setting = self .obj .cache_active
497-
498- def __enter__ (self ):
499- self .obj .cache_active = False
500-
501- def __exit__ (self , exc_type , exc_value , traceback ):
502- self .obj .cache_active = self .original_cache_setting
503- return False
504-
505-
506487class QueryWithLogin (BaseQuery ):
507488 """
508489 This is the base class for all the query classes which are required to
@@ -555,7 +536,7 @@ def _login(self, *args, **kwargs):
555536 pass
556537
557538 def login (self , * args , ** kwargs ):
558- with suspend_cache ( self ):
539+ with conf . set_temp ( "cache_active" , False ):
559540 self ._authenticated = self ._login (* args , ** kwargs )
560541 return self ._authenticated
561542
0 commit comments