|
24 | 24 | import logging |
25 | 25 | import os |
26 | 26 | import sqlite3 |
| 27 | +import warnings |
27 | 28 | from itertools import zip_longest |
28 | 29 | from urllib.request import urlopen |
29 | 30 |
|
@@ -294,36 +295,58 @@ def _get_local_tle_path_from_env(): |
294 | 295 |
|
295 | 296 | def _get_uris_and_open_func(tle_file=None): |
296 | 297 | """Get the uri's and the adequate file open call for the TLE files.""" |
297 | | - def _open(filename): |
298 | | - return io.open(filename, "rb") |
299 | | - |
300 | 298 | local_tle_path = _get_local_tle_path_from_env() |
301 | 299 |
|
302 | 300 | if tle_file: |
303 | | - if isinstance(tle_file, io.StringIO): |
304 | | - uris = (tle_file,) |
305 | | - open_func = _dummy_open_stringio |
306 | | - elif "ADMIN_MESSAGE" in tle_file: |
307 | | - uris = (io.StringIO(read_tle_from_mmam_xml_file(tle_file)),) |
308 | | - open_func = _dummy_open_stringio |
309 | | - else: |
310 | | - uris = (tle_file,) |
311 | | - open_func = _open |
| 301 | + uris, open_func = _get_tle_file_uris_and_open_method(tle_file) |
312 | 302 | elif local_tle_path: |
313 | | - # TODO: get the TLE file closest in time to the actual satellite |
314 | | - # overpass, NOT the latest! |
315 | | - list_of_tle_files = glob.glob(local_tle_path) |
| 303 | + uris, open_func = _get_local_uris_and_open_method(local_tle_path) |
| 304 | + else: |
| 305 | + uris, open_func = _get_internet_uris_and_open_method() |
| 306 | + return uris, open_func |
| 307 | + |
| 308 | + |
| 309 | +def _get_tle_file_uris_and_open_method(tle_file): |
| 310 | + if isinstance(tle_file, io.StringIO): |
| 311 | + uris = (tle_file,) |
| 312 | + open_func = _dummy_open_stringio |
| 313 | + elif "ADMIN_MESSAGE" in tle_file: |
| 314 | + uris = (io.StringIO(read_tle_from_mmam_xml_file(tle_file)),) |
| 315 | + open_func = _dummy_open_stringio |
| 316 | + else: |
| 317 | + uris = (tle_file,) |
| 318 | + open_func = _open |
| 319 | + return uris, open_func |
| 320 | + |
| 321 | + |
| 322 | +def _open(filename): |
| 323 | + return io.open(filename, "rb") |
| 324 | + |
| 325 | + |
| 326 | +def _get_local_uris_and_open_method(local_tle_path): |
| 327 | + # TODO: get the TLE file closest in time to the actual satellite |
| 328 | + # overpass, NOT the latest! |
| 329 | + list_of_tle_files = glob.glob(local_tle_path) |
| 330 | + if list_of_tle_files: |
316 | 331 | uris = (max(list_of_tle_files, key=os.path.getctime), ) |
317 | 332 | LOGGER.debug("Reading TLE from %s", uris[0]) |
318 | 333 | open_func = _open |
319 | 334 | else: |
320 | | - LOGGER.debug("Fetch TLE from the internet.") |
321 | | - uris = TLE_URLS |
322 | | - open_func = urlopen |
| 335 | + LOGGER.warning("TLES environment variable points to no TLE files") |
| 336 | + throttle_warning = "TLEs will be downloaded from Celestrak, which can throttle the connection." |
| 337 | + LOGGER.warning(throttle_warning) |
| 338 | + warnings.warn(throttle_warning) |
| 339 | + |
| 340 | + uris, open_func = _get_internet_uris_and_open_method() |
323 | 341 |
|
324 | 342 | return uris, open_func |
325 | 343 |
|
326 | 344 |
|
| 345 | +def _get_internet_uris_and_open_method(): |
| 346 | + LOGGER.debug("Fetch TLE from the internet.") |
| 347 | + return TLE_URLS, urlopen |
| 348 | + |
| 349 | + |
327 | 350 | def _get_first_tle(uris, open_func, platform=""): |
328 | 351 | return _get_tles_from_uris(uris, open_func, platform=platform, only_first=True) |
329 | 352 |
|
|
0 commit comments