Skip to content

Commit ebb0822

Browse files
committed
regularizing conf usage ande fixing suspend cache function
1 parent f1d0b31 commit ebb0822

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

astroquery/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ class Conf(_config.ConfigNamespace):
5858
604800, # 1 week
5959
'Astroquery-wide default cache timeout (seconds).'
6060
)
61-
cache_location = _config.ConfigItem(
61+
default_cache_location = _config.ConfigItem(
6262
os.path.join(_config.paths.get_cache_dir(), 'astroquery'),
6363
'Astroquery default cache location (within astropy cache).'
6464
)
65-
use_cache = _config.ConfigItem(
65+
default_cache_active = _config.ConfigItem(
6666
True,
6767
"Astroquery global cache usage, False turns off all caching."
6868
)

astroquery/query.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,10 @@ def __init__(self):
190190
.format(vers=version.version,
191191
olduseragent=S.headers['User-Agent']))
192192

193-
self.cache_location = os.path.join(
194-
conf.cache_location,
195-
self.__class__.__name__.split("Class")[0])
196-
os.makedirs(self.cache_location, exist_ok=True)
197-
198193
self.name = self.__class__.__name__.split("Class")[0]
199-
self._cache_active = conf.use_cache
200-
self.cache_timeout = conf.default_cache_timeout
194+
195+
196+
self.reset_cache_preferences()
201197

202198
def __call__(self, *args, **kwargs):
203199
""" init a fresh copy of self """
@@ -248,11 +244,11 @@ def reset_cache_preferences(self):
248244
"""Resets cache preferences to default values"""
249245

250246
self.cache_location = os.path.join(
251-
conf.cache_location,
247+
conf.default_cache_location,
252248
self.__class__.__name__.split("Class")[0])
253249
os.makedirs(self.cache_location, exist_ok=True)
254250

255-
self._cache_active = conf.use_cache
251+
self.cache_active = conf.default_cache_active
256252
self.cache_timeout = conf.default_cache_timeout
257253

258254
def _request(self, method, url,
@@ -325,7 +321,7 @@ def _request(self, method, url,
325321
json=json
326322
)
327323

328-
if (cache is not False) and self._cache_active:
324+
if (cache is not False) and self.cache_active:
329325
cache = True
330326
else:
331327
cache = False
@@ -349,7 +345,7 @@ def _request(self, method, url,
349345
return local_filepath
350346
else:
351347
query = AstroQuery(method, url, **req_kwargs)
352-
if ((self.cache_location is None) or (not self._cache_active) or (not cache)):
348+
if ((self.cache_location is None) or (not self.cache_active) or (not cache)):
353349
with suspend_cache(self):
354350
response = query.request(self._session, stream=stream,
355351
auth=auth, verify=verify,
@@ -499,12 +495,13 @@ class suspend_cache:
499495

500496
def __init__(self, obj):
501497
self.obj = obj
498+
self.original_cache_setting = self.obj.cache_active
502499

503500
def __enter__(self):
504-
self.obj._cache_active = False
501+
self.obj.cache_active = False
505502

506503
def __exit__(self, exc_type, exc_value, traceback):
507-
self.obj._cache_active = True
504+
self.obj.cache_active = self.original_cache_setting
508505
return False
509506

510507

astroquery/tests/test_cache.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def _login(self, username):
4848

4949
def test_cache_reset():
5050
mytest = TestClass()
51-
assert mytest._cache_active
51+
assert mytest.cache_active
5252

5353
default_timeout = mytest.cache_timeout
5454
default_loc = mytest.cache_location
@@ -65,7 +65,7 @@ def test_cache_reset():
6565
def test_basic_caching():
6666

6767
mytest = TestClass()
68-
assert mytest._cache_active
68+
assert mytest.cache_active
6969

7070
mytest.clear_cache()
7171
assert len(os.listdir(mytest.cache_location)) == 0
@@ -96,7 +96,7 @@ def test_basic_caching():
9696
def test_login():
9797

9898
mytest = TestClass()
99-
assert mytest._cache_active
99+
assert mytest.cache_active
100100

101101
mytest.clear_cache()
102102
assert len(os.listdir(mytest.cache_location)) == 0
@@ -116,7 +116,7 @@ def test_login():
116116
def test_timeout():
117117

118118
mytest = TestClass()
119-
assert mytest._cache_active
119+
assert mytest.cache_active
120120

121121
mytest.clear_cache()
122122
assert len(os.listdir(mytest.cache_location)) == 0
@@ -136,3 +136,24 @@ def test_timeout():
136136
sleep(2) # run out cache time
137137
resp = mytest.test_func(URL1)
138138
assert resp.content == TEXT2 # no see the new response
139+
140+
141+
def test_deactivate():
142+
143+
mytest = TestClass()
144+
mytest.cache_active = False
145+
146+
mytest.clear_cache()
147+
assert len(os.listdir(mytest.cache_location)) == 0
148+
149+
set_response(TEXT1)
150+
151+
resp = mytest.test_func(URL1)
152+
assert resp.content == TEXT1
153+
assert len(os.listdir(mytest.cache_location)) == 0
154+
155+
set_response(TEXT2)
156+
157+
resp = mytest.test_func(URL1)
158+
assert resp.content == TEXT2
159+
assert len(os.listdir(mytest.cache_location)) == 0

0 commit comments

Comments
 (0)