Skip to content

Commit 960a124

Browse files
authored
Merge pull request #205 from pnuu/fallback_empty_tles_env
Add fallback when `TLES` environment variable not listing any files
2 parents 67edf1c + c9e2909 commit 960a124

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

pyorbital/tests/test_tlefile.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,25 @@ def test_get_uris_and_open_func_using_tles_env(caplog, fake_local_tles_dir, monk
384384
assert log_message in caplog.text
385385

386386

387+
def test_get_uris_and_open_func_using_empty_tles_env(caplog, fake_local_tles_dir, monkeypatch):
388+
"""Test getting the uris and associated open-function for reading tles.
389+
390+
Test providing no tle file but using TLES env that returns no files.
391+
"""
392+
from pyorbital.tlefile import _get_uris_and_open_func
393+
394+
monkeypatch.setenv("TLES", "/no/files/here/tle*txt")
395+
396+
with caplog.at_level(logging.DEBUG):
397+
with pytest.warns():
398+
uris, _ = _get_uris_and_open_func()
399+
400+
warning_text = "TLES environment variable points to no TLE files"
401+
debug_text = "Fetch TLE from the internet."
402+
assert warning_text in caplog.text
403+
assert debug_text in caplog.text
404+
405+
387406
class TLETest(unittest.TestCase):
388407
"""Test TLE reading.
389408

pyorbital/tlefile.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import logging
2525
import os
2626
import sqlite3
27+
import warnings
2728
from itertools import zip_longest
2829
from urllib.request import urlopen
2930

@@ -294,36 +295,58 @@ def _get_local_tle_path_from_env():
294295

295296
def _get_uris_and_open_func(tle_file=None):
296297
"""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-
300298
local_tle_path = _get_local_tle_path_from_env()
301299

302300
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)
312302
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:
316331
uris = (max(list_of_tle_files, key=os.path.getctime), )
317332
LOGGER.debug("Reading TLE from %s", uris[0])
318333
open_func = _open
319334
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()
323341

324342
return uris, open_func
325343

326344

345+
def _get_internet_uris_and_open_method():
346+
LOGGER.debug("Fetch TLE from the internet.")
347+
return TLE_URLS, urlopen
348+
349+
327350
def _get_first_tle(uris, open_func, platform=""):
328351
return _get_tles_from_uris(uris, open_func, platform=platform, only_first=True)
329352

0 commit comments

Comments
 (0)