@@ -33,9 +33,6 @@ def to_cache(response, cache_file):
33
33
if hasattr (response , 'request' ):
34
34
for key in tuple (response .request .hooks .keys ()):
35
35
del response .request .hooks [key ]
36
-
37
- chache_dir , _ = os .path .split (cache_file )
38
- Path (chache_dir ).mkdir (parents = True , exist_ok = True )
39
36
with open (cache_file , "wb" ) as f :
40
37
pickle .dump (response , f )
41
38
@@ -191,7 +188,7 @@ def __init__(self):
191
188
olduseragent = S .headers ['User-Agent' ]))
192
189
193
190
self .name = self .__class__ .__name__ .split ("Class" )[0 ]
194
- self .reset_cache_preferences ()
191
+ self .cache_location = None
195
192
196
193
def __call__ (self , * args , ** kwargs ):
197
194
""" init a fresh copy of self """
@@ -231,23 +228,24 @@ def _response_hook(self, response, *args, **kwargs):
231
228
f"-----------------------------------------" , '\t ' )
232
229
log .log (5 , f"HTTP response\n { response_log } " )
233
230
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
236
236
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
240
239
241
- def reset_cache_preferences (self ):
242
- """Resets cache preferences to default values"""
240
+ def reset_cache_location (self ):
241
+ self . cache_location = None
243
242
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."""
248
245
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 ))
251
249
252
250
def _request (self , method , url ,
253
251
params = None , data = None , headers = None ,
@@ -311,7 +309,7 @@ def _request(self, method, url,
311
309
is True.
312
310
"""
313
311
314
- if (cache is not False ) and self .cache_active :
312
+ if (cache is not False ) and conf .cache_active :
315
313
cache = True
316
314
else :
317
315
cache = False
@@ -323,7 +321,7 @@ def _request(self, method, url,
323
321
# ":" so replace them with an underscore
324
322
local_filename = local_filename .replace (':' , '_' )
325
323
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 )
327
325
328
326
response = self ._download_file (url , local_filepath , cache = cache , timeout = timeout ,
329
327
continuation = continuation , method = method ,
@@ -337,23 +335,23 @@ def _request(self, method, url,
337
335
else :
338
336
query = AstroQuery (method , url , params = params , data = data , headers = headers ,
339
337
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 ):
342
340
response = query .request (self ._session , stream = stream ,
343
341
auth = auth , verify = verify ,
344
342
allow_redirects = allow_redirects ,
345
343
json = json )
346
344
else :
347
- response = query .from_cache (self .cache_location , self .cache_timeout )
345
+ response = query .from_cache (self ._cache_location , conf .cache_timeout )
348
346
if not response :
349
347
response = query .request (self ._session ,
350
- self .cache_location ,
348
+ self ._cache_location ,
351
349
stream = stream ,
352
350
auth = auth ,
353
351
allow_redirects = allow_redirects ,
354
352
verify = verify ,
355
353
json = json )
356
- to_cache (response , query .request_file (self .cache_location ))
354
+ to_cache (response , query .request_file (self ._cache_location ))
357
355
358
356
self ._last_query = query
359
357
return response
@@ -480,23 +478,6 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
480
478
return response
481
479
482
480
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
-
500
481
class QueryWithLogin (BaseQuery ):
501
482
"""
502
483
This is the base class for all the query classes which are required to
@@ -549,7 +530,7 @@ def _login(self, *args, **kwargs):
549
530
pass
550
531
551
532
def login (self , * args , ** kwargs ):
552
- with suspend_cache ( self ):
533
+ with conf . set_temp ( "cache_active" , False ):
553
534
self ._authenticated = self ._login (* args , ** kwargs )
554
535
return self ._authenticated
555
536
0 commit comments