Skip to content

Commit

Permalink
Add support for files containing a TLE set
Browse files Browse the repository at this point in the history
  • Loading branch information
jamlamberti committed Oct 16, 2019
1 parent 2c42e5a commit b78c38b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
15 changes: 8 additions & 7 deletions orbit_predictor/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit b78c38b

Please sign in to comment.