From b78c38b1fdaf714df4d651996a0160c25b100bfe Mon Sep 17 00:00:00 2001 From: Jack Lamberti Date: Tue, 15 Oct 2019 02:13:15 -0400 Subject: [PATCH] Add support for files containing a TLE set --- orbit_predictor/sources.py | 15 ++++++++------- tests/test_sources.py | 13 +++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/orbit_predictor/sources.py b/orbit_predictor/sources.py index e171978..90e7909 100644 --- a/orbit_predictor/sources.py +++ b/orbit_predictor/sources.py @@ -91,19 +91,20 @@ class EtcTLESource(TLESource): def __init__(self, filename="/etc/latest_tle"): self.filename = filename - def add_tle(self, sate_id, tle, epoch): - with open(self.filename, "w") as fd: + def add_tle(self, sate_id, tle, epoch, append=False): + with open(self.filename, "a+" if append else 'w') as fd: fd.write(sate_id + "\n") for l in tle: fd.write(l + "\n") def _get_tle(self, sate_id, date): with open(self.filename) as fd: - data = fd.read() - lines = data.split("\n") - if not lines[0] == sate_id: - raise LookupError("Stored satellite id not found") - return tuple(lines[1:3]) + data = fd.read().split("\n") + tle_lines = [data[i:i+3] for i in range(0, len(data), 3)] + for tle in tle_lines: + if tle[0] == sate_id: + return tuple(tle[1:]) + raise LookupError("Stored satellite id not found") class WSTLESource(TLESource): diff --git a/tests/test_sources.py b/tests/test_sources.py index d68f1c9..819c2d0 100644 --- a/tests/test_sources.py +++ b/tests/test_sources.py @@ -117,6 +117,19 @@ def tearDown(self): shutil.rmtree(self.dirname) +class TestEtcTLESourceMultiple(TestEtcTLESource): + def test_add_tle(self): + db = sources.EtcTLESource(self.filename) + sate_id_2 = SATE_ID + 'I' + db.add_tle(sate_id_2, SAMPLE_TLE2, dt.datetime.utcnow(), append=True) + + tle_sat_1 = db._get_tle(SATE_ID, dt.datetime.utcnow()) + self.assertEqual(tle_sat_1, SAMPLE_TLE) + + tle_sat_2 = db._get_tle(sate_id_2, dt.datetime.utcnow()) + self.assertEqual(tle_sat_2, SAMPLE_TLE2) + + class TestWSTLESource(unittest.TestCase): def setUp(self): self.mock_json = {