Skip to content

Commit 30d0fdf

Browse files
committed
Fix changing simbad config at runtime
Previously changing `simbad` config items at runtime had no effect. Now the config items are read when they are needed, unless the user has specified the corresponding class or instance variable, in which case the latter takes precedence.
1 parent ad9a5d3 commit 30d0fdf

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

astroquery/simbad/core.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ class SimbadClass(SimbadBaseQuery):
271271
(http://simbad.u-strasbg.fr/simbad/sim-help?Page=sim-url)
272272
273273
"""
274-
SIMBAD_URL = 'http://' + conf.server + '/simbad/sim-script'
275-
TIMEOUT = conf.timeout
274+
SIMBAD_URL = ''
275+
TIMEOUT = None
276276
WILDCARDS = {
277277
'*': 'Any string of characters (including an empty one)',
278278
'?': 'Any character (exactly one character)',
@@ -293,7 +293,7 @@ class SimbadClass(SimbadBaseQuery):
293293
'query_bibobj_async': 'query bibobj'
294294
}
295295

296-
ROW_LIMIT = conf.row_limit
296+
ROW_LIMIT = None
297297

298298
# also find a way to fetch the votable fields table from
299299
# <http://simbad.u-strasbg.fr/simbad/sim-help?Page=sim-fscript#VotableFields>
@@ -305,6 +305,18 @@ def __init__(self):
305305
super().__init__()
306306
self._VOTABLE_FIELDS = copy.copy(self._VOTABLE_FIELDS)
307307

308+
@property
309+
def _row_limit(self):
310+
return conf.row_limit if self.ROW_LIMIT is None else self.ROW_LIMIT
311+
312+
@property
313+
def _simbad_url(self):
314+
return self.SIMBAD_URL or f'https://{conf.server}/simbad/sim-script'
315+
316+
@property
317+
def _timeout(self):
318+
return conf.timeout if self.TIMEOUT is None else self.TIMEOUT
319+
308320
def list_wildcards(self):
309321
"""
310322
Displays the available wildcards that may be used in Simbad queries and
@@ -513,8 +525,8 @@ def query_criteria_async(self, *args, **kwargs):
513525

514526
request_payload = self._args_to_payload(caller='query_criteria_async',
515527
*args, **kwargs)
516-
response = self._request("POST", self.SIMBAD_URL, data=request_payload,
517-
timeout=self.TIMEOUT, cache=cache)
528+
response = self._request("POST", self._simbad_url, data=request_payload,
529+
timeout=self._timeout, cache=cache)
518530
return response
519531

520532
def query_object(self, object_name, wildcard=False, verbose=False,
@@ -577,8 +589,8 @@ def query_object_async(self, object_name, wildcard=False, cache=True,
577589
if get_query_payload:
578590
return request_payload
579591

580-
response = self._request("POST", self.SIMBAD_URL, data=request_payload,
581-
timeout=self.TIMEOUT, cache=cache)
592+
response = self._request("POST", self._simbad_url, data=request_payload,
593+
timeout=self._timeout, cache=cache)
582594
return response
583595

584596
def query_objects(self, object_names, wildcard=False, verbose=False,
@@ -715,8 +727,8 @@ def query_region_async(self, coordinates, radius=2*u.arcmin,
715727
if get_query_payload:
716728
return request_payload
717729

718-
response = self._request("POST", self.SIMBAD_URL, data=request_payload,
719-
timeout=self.TIMEOUT, cache=cache)
730+
response = self._request("POST", self._simbad_url, data=request_payload,
731+
timeout=self._timeout, cache=cache)
720732
return response
721733

722734
def query_catalog(self, catalog, verbose=False, cache=True,
@@ -772,8 +784,8 @@ def query_catalog_async(self, catalog, cache=True, get_query_payload=False):
772784
if get_query_payload:
773785
return request_payload
774786

775-
response = self._request("POST", self.SIMBAD_URL, data=request_payload,
776-
timeout=self.TIMEOUT, cache=cache)
787+
response = self._request("POST", self._simbad_url, data=request_payload,
788+
timeout=self._timeout, cache=cache)
777789
return response
778790

779791
def query_bibobj(self, bibcode, verbose=False, get_query_payload=False):
@@ -826,8 +838,8 @@ def query_bibobj_async(self, bibcode, cache=True, get_query_payload=False):
826838
if get_query_payload:
827839
return request_payload
828840

829-
response = self._request("POST", self.SIMBAD_URL, data=request_payload,
830-
timeout=self.TIMEOUT, cache=cache)
841+
response = self._request("POST", self._simbad_url, data=request_payload,
842+
timeout=self._timeout, cache=cache)
831843
return response
832844

833845
def query_bibcode(self, bibcode, wildcard=False, verbose=False,
@@ -894,8 +906,8 @@ def query_bibcode_async(self, bibcode, wildcard=False, cache=True,
894906
if get_query_payload:
895907
return request_payload
896908

897-
response = self._request("POST", self.SIMBAD_URL, cache=cache,
898-
data=request_payload, timeout=self.TIMEOUT)
909+
response = self._request("POST", self._simbad_url, data=request_payload,
910+
timeout=self._timeout, cache=cache)
899911

900912
return response
901913

@@ -950,8 +962,8 @@ def query_objectids_async(self, object_name, cache=True,
950962
if get_query_payload:
951963
return request_payload
952964

953-
response = self._request("POST", self.SIMBAD_URL, data=request_payload,
954-
timeout=self.TIMEOUT, cache=cache)
965+
response = self._request("POST", self._simbad_url, data=request_payload,
966+
timeout=self._timeout, cache=cache)
955967

956968
return response
957969

@@ -988,8 +1000,8 @@ def _args_to_payload(self, *args, **kwargs):
9881000
votable_header = self._get_query_header(get_raw)
9891001
votable_footer = self._get_query_footer(get_raw)
9901002

991-
if self.ROW_LIMIT > 0:
992-
script = "set limit " + str(self.ROW_LIMIT)
1003+
if self._row_limit > 0:
1004+
script = "set limit " + str(self._row_limit)
9931005
script = "\n".join([script, votable_header, command])
9941006
using_wildcard = False
9951007
if kwargs.get('wildcard'):

astroquery/simbad/tests/test_simbad.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,28 @@ def test_query_criteria2(patch_post):
383383
assert 'otype=SNR' in S._last_query.data['script']
384384

385385

386+
def test_config_runtime_change():
387+
sb = simbad.Simbad()
388+
389+
assert sb._row_limit == 0
390+
with simbad.conf.set_temp('row_limit', 7):
391+
assert sb._row_limit == 7
392+
sb.ROW_LIMIT = 3
393+
assert sb._row_limit == 3
394+
395+
assert sb._simbad_url == 'https://simbad.u-strasbg.fr/simbad/sim-script'
396+
with simbad.conf.set_temp('server', 'simbad.harvard.edu'):
397+
assert sb._simbad_url == 'https://simbad.harvard.edu/simbad/sim-script'
398+
sb.SIMBAD_URL = 'asdfg'
399+
assert sb._simbad_url == 'asdfg'
400+
401+
assert sb._timeout == 60
402+
with simbad.conf.set_temp('timeout', 5):
403+
assert sb._timeout == 5
404+
sb.TIMEOUT = 3
405+
assert sb._timeout == 3
406+
407+
386408
def test_simbad_settings1():
387409
sb = simbad.core.Simbad()
388410
assert sb.get_votable_fields() == ['main_id', 'coordinates']

0 commit comments

Comments
 (0)