Skip to content

Commit cbd4900

Browse files
committed
Change recommended config item access in template
Previously the template core module read the values of the configuration items at class creation and stored them in class attributes. This meant that changing the configuration items at runtime had no effect. Now the template implements private properties that return the current configuration item value, or the class (or instance) attribute value if the user has changed it. This enables the users to use the full functionality of the `astropy` configuration system, but users not familiar with it can still override it entirely through the attributes.
1 parent 621a13f commit cbd4900

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

astroquery/template_module/core.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,20 @@ class TemplateClass(BaseQuery):
4141
cases, new methods may be added if necessary, follow the guidelines at
4242
<http://astroquery.readthedocs.io/en/latest/api.html>
4343
"""
44-
# use the Configuration Items imported from __init__.py to set the URL,
45-
# TIMEOUT, etc.
46-
URL = conf.server
47-
TIMEOUT = conf.timeout
44+
45+
# The private properties defined here allow the users to change the
46+
# configuration values at runtime, or to completely override them with
47+
# instance attributes.
48+
URL = '' # A falsy default that cannot be mistaken for a valid value.
49+
TIMEOUT = None # Use `None` if the falsy value could be valid.
50+
51+
@property
52+
def _url(self):
53+
return self.URL or conf.server
54+
55+
@property
56+
def _timeout(self):
57+
return conf.timeout if self.TIMEOUT is None else self.TIMEOUT
4858

4959
# all query methods are implemented with an "async" method that handles
5060
# making the actual HTTP request and returns the raw HTTP response, which
@@ -124,8 +134,8 @@ def query_object_async(self, object_name, get_query_payload=False,
124134
return request_payload
125135
# BaseQuery classes come with a _request method that includes a
126136
# built-in caching system
127-
response = self._request('GET', self.URL, params=request_payload,
128-
timeout=self.TIMEOUT, cache=cache)
137+
response = self._request('GET', self._url, params=request_payload,
138+
timeout=self._timeout, cache=cache)
129139
return response
130140

131141
# For services that can query coordinates, use the query_region method.
@@ -169,8 +179,8 @@ def query_region_async(self, coordinates, radius, height, width,
169179
width)
170180
if get_query_payload:
171181
return request_payload
172-
response = self._request('GET', self.URL, params=request_payload,
173-
timeout=self.TIMEOUT, cache=cache)
182+
response = self._request('GET', self._url, params=request_payload,
183+
timeout=self._timeout, cache=cache)
174184
return response
175185

176186
# as we mentioned earlier use various python regular expressions, etc
@@ -294,9 +304,9 @@ def get_image_list(self, coordinates, radius, get_query_payload=False,
294304
request_payload = self._args_to_payload(coordinates, radius)
295305
if get_query_payload:
296306
return request_payload
297-
response = self._request(method="GET", url=self.URL,
307+
response = self._request(method="GET", url=self._url,
298308
data=request_payload,
299-
timeout=self.TIMEOUT, cache=cache)
309+
timeout=self._timeout, cache=cache)
300310

301311
return self.extract_image_urls(response.text)
302312

0 commit comments

Comments
 (0)