Skip to content

Commit 255334a

Browse files
authored
Merge pull request #223 from pnuu/bugfix-downloader-skip-empty-lines
Bugfix `Downloader` handling of trailing empty lines
2 parents 26b8c5b + 682f9aa commit 255334a

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

pyorbital/tests/test_tlefile.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,10 @@ def test_from_mmam_xml(self):
499499
}
500500

501501

502-
class TestDownloader(unittest.TestCase):
502+
class TestDownloader:
503503
"""Test TLE downloader."""
504504

505-
def setUp(self):
505+
def setup_method(self):
506506
"""Create a downloader instance."""
507507
from pyorbital.tlefile import Downloader
508508
self.config = {}
@@ -648,11 +648,20 @@ def test_fetch_spacetrack_success(self, requests):
648648
assert res[0].line1 == LINE1
649649
assert res[0].line2 == LINE2
650650

651-
def test_read_tle_files(self):
651+
@pytest.mark.parametrize(
652+
"tle_lines", [(LINE1, LINE2),
653+
(LINE1, LINE2, ""),
654+
(LINE1, LINE2, "", ""),
655+
(LINE0, LINE1, LINE2),
656+
(LINE0, LINE1, LINE2, ""),
657+
(LINE0, LINE1, LINE2, "", ""),
658+
]
659+
)
660+
def test_read_tle_files(self, tle_lines):
652661
"""Test reading TLE files from a file system."""
653662
from tempfile import TemporaryDirectory
654663

655-
tle_text = "\n".join((LINE0, LINE1, LINE2))
664+
tle_text = "\n".join(tle_lines)
656665

657666
save_dir = TemporaryDirectory()
658667
with save_dir:

pyorbital/tlefile.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -415,23 +415,34 @@ def _decode_lines(fid, l_0, platform, only_first, open_is_dummy=False):
415415
designator = "1 " + SATELLITES.get(platform, "")
416416
tle = ""
417417
l_0 = _decode(l_0)
418-
if l_0.strip() == platform:
419-
l_1 = _decode(next(fid))
420-
l_2 = _decode(next(fid))
421-
tle = _merge_tle_from_two_lines(l_1, l_2)
418+
if l_0.strip() == platform and platform != "":
419+
tle = _decode_lines_with_platform_header(fid)
422420
elif l_0.strip().startswith(designator):
423-
if (platform in SATELLITES or not only_first) or open_is_dummy:
424-
l_1 = l_0
425-
l_2 = _decode(next(fid))
426-
tle = _merge_tle_from_two_lines(l_1, l_2)
427-
if platform:
428-
LOGGER.debug("Found platform %s, ID: %s", platform, SATELLITES[platform])
421+
tle = _decode_lines_without_platform_header(fid, l_0, platform, only_first, open_is_dummy)
429422
elif l_0.startswith(platform) and platform not in SATELLITES:
430423
LOGGER.debug("Found a possible match: %s?", str(l_0.strip()))
431424

432425
return tle
433426

434427

428+
def _decode_lines_with_platform_header(fid):
429+
l_1 = _decode(next(fid))
430+
l_2 = _decode(next(fid))
431+
tle = _merge_tle_from_two_lines(l_1, l_2)
432+
return tle
433+
434+
435+
def _decode_lines_without_platform_header(fid, l_0, platform, only_first, open_is_dummy):
436+
tle = ""
437+
if (platform in SATELLITES or not only_first) or open_is_dummy:
438+
l_1 = l_0
439+
l_2 = _decode(next(fid))
440+
tle = _merge_tle_from_two_lines(l_1, l_2)
441+
if platform:
442+
LOGGER.debug("Found platform %s, ID: %s", platform, SATELLITES[platform])
443+
return tle
444+
445+
435446
def _merge_tle_from_two_lines(l_1, l_2):
436447
"""Merge line1 and line2 to fulle TLE string."""
437448
return l_1.strip() + "\n" + l_2.strip()

0 commit comments

Comments
 (0)