|
21 | 21 | import astropy.utils.data |
22 | 22 | from astropy.utils import deprecated |
23 | 23 |
|
| 24 | +import pyvo |
| 25 | + |
24 | 26 | from astroquery import version, log, cache_conf |
25 | 27 | from astroquery.utils import system_tools |
26 | 28 |
|
27 | 29 |
|
28 | | -__all__ = ['BaseQuery', 'QueryWithLogin'] |
| 30 | +__all__ = ['BaseVOQuery', 'BaseQuery', 'QueryWithLogin'] |
29 | 31 |
|
30 | 32 |
|
31 | 33 | def to_cache(response, cache_file): |
@@ -175,18 +177,45 @@ def login(*args, **kwargs): |
175 | 177 | return newcls |
176 | 178 |
|
177 | 179 |
|
| 180 | +class BaseVOQuery: |
| 181 | + """ |
| 182 | + Bare minimum base query that sets the Session header to include both astroquery and pyvo. |
| 183 | + Use in modules that rely on PyVO, either on its own or in combination with ``BaseQuery`` (be mindful |
| 184 | + about resolution order of base classes!). |
| 185 | + """ |
| 186 | + def __init__(self): |
| 187 | + super().__init__() |
| 188 | + if not hasattr(self, '_session'): |
| 189 | + # We don't want to override another, e.g. already authenticated session from another baseclass |
| 190 | + self._session = requests.Session() |
| 191 | + |
| 192 | + user_agents = self._session.headers['User-Agent'].split() |
| 193 | + if 'astroquery' in user_agents[0]: |
| 194 | + if 'pyVO' not in user_agents[1]: |
| 195 | + user_agents[0] = f"astroquery/{version.version} pyVO/{pyvo.__version__}" |
| 196 | + elif 'pyVO' in user_agents[0]: |
| 197 | + user_agents[0] = f"astroquery/{version.version} pyVO/{pyvo.__version__}" |
| 198 | + else: |
| 199 | + user_agents = [f"astroquery/{version.version} pyVO/{pyvo.__version__} " |
| 200 | + f"Python/{platform.python_version()} ({platform.system()})"] + user_agents |
| 201 | + |
| 202 | + self._session.headers['User-Agent'] = " ".join(user_agents) |
| 203 | + |
| 204 | + self.name = self.__class__.__name__.split("Class")[0] |
| 205 | + |
| 206 | + |
178 | 207 | class BaseQuery(metaclass=LoginABCMeta): |
179 | 208 | """ |
180 | 209 | This is the base class for all the query classes in astroquery. It |
181 | 210 | is implemented as an abstract class and must not be directly instantiated. |
182 | 211 | """ |
183 | 212 |
|
184 | 213 | def __init__(self): |
185 | | - S = self._session = requests.Session() |
| 214 | + self._session = requests.Session() |
186 | 215 | self._session.hooks['response'].append(self._response_hook) |
187 | | - S.headers['User-Agent'] = ( |
| 216 | + self._session.headers['User-Agent'] = ( |
188 | 217 | f"astroquery/{version.version} Python/{platform.python_version()} ({platform.system()}) " |
189 | | - f"{S.headers['User-Agent']}") |
| 218 | + f"{self._session.headers['User-Agent']}") |
190 | 219 |
|
191 | 220 | self.name = self.__class__.__name__.split("Class")[0] |
192 | 221 | self._cache_location = None |
|
0 commit comments