@@ -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 ,
@@ -319,7 +317,7 @@ def _request(self, method, url,
319
317
json = json
320
318
)
321
319
322
- if (cache is not False ) and self .cache_active :
320
+ if (cache is not False ) and conf .cache_active :
323
321
cache = True
324
322
else :
325
323
cache = False
@@ -331,7 +329,7 @@ def _request(self, method, url,
331
329
# ":" so replace them with an underscore
332
330
local_filename = local_filename .replace (':' , '_' )
333
331
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 )
335
333
336
334
response = self ._download_file (url , local_filepath , cache = cache ,
337
335
continuation = continuation , method = method ,
@@ -343,23 +341,23 @@ def _request(self, method, url,
343
341
return local_filepath
344
342
else :
345
343
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 ):
348
346
response = query .request (self ._session , stream = stream ,
349
347
auth = auth , verify = verify ,
350
348
allow_redirects = allow_redirects ,
351
349
json = json )
352
350
else :
353
- response = query .from_cache (self .cache_location , self .cache_timeout )
351
+ response = query .from_cache (self ._cache_location , conf .cache_timeout )
354
352
if not response :
355
353
response = query .request (self ._session ,
356
- self .cache_location ,
354
+ self ._cache_location ,
357
355
stream = stream ,
358
356
auth = auth ,
359
357
allow_redirects = allow_redirects ,
360
358
verify = verify ,
361
359
json = json )
362
- to_cache (response , query .request_file (self .cache_location ))
360
+ to_cache (response , query .request_file (self ._cache_location ))
363
361
364
362
self ._last_query = query
365
363
return response
@@ -486,23 +484,6 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
486
484
return response
487
485
488
486
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
-
506
487
class QueryWithLogin (BaseQuery ):
507
488
"""
508
489
This is the base class for all the query classes which are required to
@@ -555,7 +536,7 @@ def _login(self, *args, **kwargs):
555
536
pass
556
537
557
538
def login (self , * args , ** kwargs ):
558
- with suspend_cache ( self ):
539
+ with conf . set_temp ( "cache_active" , False ):
559
540
self ._authenticated = self ._login (* args , ** kwargs )
560
541
return self ._authenticated
561
542
0 commit comments