From cbd4900c92bdf122c066a2349b78626eba0b283f Mon Sep 17 00:00:00 2001 From: Eero Vaher Date: Mon, 28 Mar 2022 19:15:11 +0200 Subject: [PATCH] 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. --- astroquery/template_module/core.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/astroquery/template_module/core.py b/astroquery/template_module/core.py index d7a7811cad..ac14263df7 100644 --- a/astroquery/template_module/core.py +++ b/astroquery/template_module/core.py @@ -41,10 +41,20 @@ class TemplateClass(BaseQuery): cases, new methods may be added if necessary, follow the guidelines at """ - # use the Configuration Items imported from __init__.py to set the URL, - # TIMEOUT, etc. - URL = conf.server - TIMEOUT = conf.timeout + + # The private properties defined here allow the users to change the + # configuration values at runtime, or to completely override them with + # instance attributes. + URL = '' # A falsy default that cannot be mistaken for a valid value. + TIMEOUT = None # Use `None` if the falsy value could be valid. + + @property + def _url(self): + return self.URL or conf.server + + @property + def _timeout(self): + return conf.timeout if self.TIMEOUT is None else self.TIMEOUT # all query methods are implemented with an "async" method that handles # 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, return request_payload # BaseQuery classes come with a _request method that includes a # built-in caching system - response = self._request('GET', self.URL, params=request_payload, - timeout=self.TIMEOUT, cache=cache) + response = self._request('GET', self._url, params=request_payload, + timeout=self._timeout, cache=cache) return response # For services that can query coordinates, use the query_region method. @@ -169,8 +179,8 @@ def query_region_async(self, coordinates, radius, height, width, width) if get_query_payload: return request_payload - response = self._request('GET', self.URL, params=request_payload, - timeout=self.TIMEOUT, cache=cache) + response = self._request('GET', self._url, params=request_payload, + timeout=self._timeout, cache=cache) return response # 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, request_payload = self._args_to_payload(coordinates, radius) if get_query_payload: return request_payload - response = self._request(method="GET", url=self.URL, + response = self._request(method="GET", url=self._url, data=request_payload, - timeout=self.TIMEOUT, cache=cache) + timeout=self._timeout, cache=cache) return self.extract_image_urls(response.text)