|
23 | 23 | from Chandra.Time import DateTime |
24 | 24 | from ska_helpers.utils import lru_cache_timed |
25 | 25 |
|
| 26 | +from cheta.data_source import DEFAULT_DATA_SOURCE, data_source |
| 27 | + |
26 | 28 | from . import ( |
27 | 29 | __version__, # noqa |
28 | 30 | cache, |
|
138 | 140 | # Cached version (by content type) of first and last available times in archive |
139 | 141 | CONTENT_TIME_RANGES = {} |
140 | 142 |
|
141 | | -# Default source of data. |
142 | | -DEFAULT_DATA_SOURCE = "cxc" |
143 | | - |
144 | | - |
145 | | -class _DataSource(object): |
146 | | - """ |
147 | | - Context manager and quasi-singleton configuration object for managing the |
148 | | - data_source(s) used for fetching telemetry. |
149 | | - """ |
150 | | - |
151 | | - _data_sources = (DEFAULT_DATA_SOURCE,) |
152 | | - _allowed = ("cxc", "maude", "test-drop-half") |
153 | | - |
154 | | - def __init__(self, *data_sources): |
155 | | - self._new_data_sources = data_sources |
156 | | - |
157 | | - def __enter__(self): |
158 | | - self._orig_data_sources = self.__class__._data_sources |
159 | | - self.set(*self._new_data_sources) |
160 | | - |
161 | | - def __exit__(self, type, value, traceback): |
162 | | - self.__class__._data_sources = self._orig_data_sources |
163 | | - |
164 | | - @classmethod |
165 | | - def set(cls, *data_sources): |
166 | | - """ |
167 | | - Set current data sources. |
168 | | -
|
169 | | - :param data_sources: one or more sources (str) |
170 | | - """ |
171 | | - if any( |
172 | | - data_source.split()[0] not in cls._allowed for data_source in data_sources |
173 | | - ): |
174 | | - raise ValueError( |
175 | | - "data_sources {} not in allowed set {}".format( |
176 | | - data_sources, cls._allowed |
177 | | - ) |
178 | | - ) |
179 | | - |
180 | | - if len(data_sources) == 0: |
181 | | - raise ValueError( |
182 | | - "must select at least one data source in {}".format(cls._allowed) |
183 | | - ) |
184 | | - |
185 | | - cls._data_sources = data_sources |
186 | | - |
187 | | - @classmethod |
188 | | - def sources(cls, include_test=True): |
189 | | - """ |
190 | | - Get tuple of current data sources names. |
191 | | -
|
192 | | - :param include_test: include sources that start with 'test' |
193 | | - :returns: tuple of data source names |
194 | | - """ |
195 | | - if include_test: |
196 | | - sources = cls._data_sources |
197 | | - else: |
198 | | - sources = [x for x in cls._data_sources if not x.startswith("test")] |
199 | | - |
200 | | - return tuple(source.split()[0] for source in sources) |
201 | | - |
202 | | - @classmethod |
203 | | - def get_msids(cls, source): |
204 | | - """ |
205 | | - Get the set of MSID names corresponding to ``source`` (e.g. 'cxc' or 'maude') |
206 | | -
|
207 | | - :param source: str |
208 | | - :returns: set of MSIDs |
209 | | - """ |
210 | | - source = source.split()[0] |
211 | | - |
212 | | - if source == "cxc": |
213 | | - out = list(content.keys()) |
214 | | - elif source == "maude": |
215 | | - import maude |
216 | | - |
217 | | - out = list(maude.MSIDS.keys()) |
218 | | - else: |
219 | | - raise ValueError('source must be "cxc" or "msid"') |
220 | | - |
221 | | - return set(out) |
222 | | - |
223 | | - @classmethod |
224 | | - def options(cls): |
225 | | - """ |
226 | | - Get the data sources and corresponding options as a dict. |
227 | | -
|
228 | | - Example:: |
229 | | -
|
230 | | - >>> data_source.set('cxc', 'maude allow_subset=False') |
231 | | - >>> data_source.options() |
232 | | - {'cxc': {}, 'maude': {'allow_subset': False}} |
233 | | -
|
234 | | - :returns: dict of data source options |
235 | | - """ |
236 | | - import ast |
237 | | - |
238 | | - out = {} |
239 | | - for source in cls._data_sources: |
240 | | - vals = source.split() |
241 | | - name, opts = vals[0], vals[1:] |
242 | | - out[name] = {} |
243 | | - for opt in opts: |
244 | | - key, val = opt.split("=") |
245 | | - val = ast.literal_eval(val) |
246 | | - out[name][key] = val |
247 | | - |
248 | | - return out |
249 | | - |
250 | | - |
251 | | -# Public interface is a "data_source" module attribute |
252 | | -data_source = _DataSource |
253 | | - |
254 | 143 |
|
255 | 144 | def local_or_remote_function(remote_print_output): |
256 | 145 | """ |
|
0 commit comments