From 2b957d140cc66aba6a2487a7bb7e28cf58a5923d Mon Sep 17 00:00:00 2001 From: Athanasius Date: Fri, 30 Sep 2022 13:35:50 +0100 Subject: [PATCH] EDDNSender: `convert_legacy_file()` belongs with "open the database" --- plugins/eddn.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/plugins/eddn.py b/plugins/eddn.py index f09a4544..505039c4 100644 --- a/plugins/eddn.py +++ b/plugins/eddn.py @@ -136,6 +136,7 @@ class EDDNSender: - Ensure the sqlite3 database for EDDN replays exists and has schema. - Convert any legacy file into the database. + - (Future) Handle any database migrations. :param eddn_endpoint: Where messages should be sent. """ @@ -205,6 +206,25 @@ class EDDNSender: return db_conn + def convert_legacy_file(self): + """Convert a legacy file's contents into the sqlite3 db.""" + filename = config.app_dir_path / 'replay.jsonl' + try: + with open(filename, 'r+', buffering=1) as replay_file: + for line in replay_file: + cmdr, msg = json.loads(line) + self.add_message(cmdr, msg) + + except FileNotFoundError: + pass + + finally: + # Best effort at removing the file/contents + # NB: The legacy code assumed it could write to the file. + replay_file = open(filename, 'w') # Will truncate + replay_file.close() + os.unlink(filename) + def close(self) -> None: """Clean up any resources.""" if self.db: @@ -280,25 +300,6 @@ class EDDNSender: ) self.db_conn.commit() - def convert_legacy_file(self): - """Convert a legacy file's contents into the sqlite3 db.""" - filename = config.app_dir_path / 'replay.jsonl' - try: - with open(filename, 'r+', buffering=1) as replay_file: - for line in replay_file: - cmdr, msg = json.loads(line) - self.add_message(cmdr, msg) - - except FileNotFoundError: - pass - - finally: - # Best effort at removing the file/contents - # NB: The legacy code assumed it could write to the file. - replay_file = open(filename, 'w') # Will truncate - replay_file.close() - os.unlink(filename) - # TODO: a good few of these methods are static or could be classmethods. they should be created as such. class EDDN: