Skip to content

Commit b78c38b

Browse files
committed
Add support for files containing a TLE set
1 parent 2c42e5a commit b78c38b

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

orbit_predictor/sources.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,20 @@ class EtcTLESource(TLESource):
9191
def __init__(self, filename="/etc/latest_tle"):
9292
self.filename = filename
9393

94-
def add_tle(self, sate_id, tle, epoch):
95-
with open(self.filename, "w") as fd:
94+
def add_tle(self, sate_id, tle, epoch, append=False):
95+
with open(self.filename, "a+" if append else 'w') as fd:
9696
fd.write(sate_id + "\n")
9797
for l in tle:
9898
fd.write(l + "\n")
9999

100100
def _get_tle(self, sate_id, date):
101101
with open(self.filename) as fd:
102-
data = fd.read()
103-
lines = data.split("\n")
104-
if not lines[0] == sate_id:
105-
raise LookupError("Stored satellite id not found")
106-
return tuple(lines[1:3])
102+
data = fd.read().split("\n")
103+
tle_lines = [data[i:i+3] for i in range(0, len(data), 3)]
104+
for tle in tle_lines:
105+
if tle[0] == sate_id:
106+
return tuple(tle[1:])
107+
raise LookupError("Stored satellite id not found")
107108

108109

109110
class WSTLESource(TLESource):

tests/test_sources.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ def tearDown(self):
117117
shutil.rmtree(self.dirname)
118118

119119

120+
class TestEtcTLESourceMultiple(TestEtcTLESource):
121+
def test_add_tle(self):
122+
db = sources.EtcTLESource(self.filename)
123+
sate_id_2 = SATE_ID + 'I'
124+
db.add_tle(sate_id_2, SAMPLE_TLE2, dt.datetime.utcnow(), append=True)
125+
126+
tle_sat_1 = db._get_tle(SATE_ID, dt.datetime.utcnow())
127+
self.assertEqual(tle_sat_1, SAMPLE_TLE)
128+
129+
tle_sat_2 = db._get_tle(sate_id_2, dt.datetime.utcnow())
130+
self.assertEqual(tle_sat_2, SAMPLE_TLE2)
131+
132+
120133
class TestWSTLESource(unittest.TestCase):
121134
def setUp(self):
122135
self.mock_json = {

0 commit comments

Comments
 (0)