From e46915b58da29c4fadc9bf6fe69627f0aeddb87e Mon Sep 17 00:00:00 2001 From: James Lu Date: Sat, 9 Mar 2019 11:15:34 -0800 Subject: [PATCH] accountsdb: write DB to a temporary file first --- LastFM/local/accountsdb.py | 4 +++- NuWeather/local/accountsdb.py | 4 +++- accountsdb.py | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/LastFM/local/accountsdb.py b/LastFM/local/accountsdb.py index 65296ca..53355fb 100644 --- a/LastFM/local/accountsdb.py +++ b/LastFM/local/accountsdb.py @@ -86,9 +86,11 @@ class AccountsDB(): def flush(self): """Exports the database to a file.""" + tmp_filename = self.filename + '.tmp' try: - with open(self.filename, 'wb') as f: + with open(tmp_filename, 'wb') as f: pickle.dump(self.db, f, 2) + os.rename(tmp_filename, self.filename) except Exception as e: log.warning('%s: Unable to write database: %s', self._plugin_name, e) diff --git a/NuWeather/local/accountsdb.py b/NuWeather/local/accountsdb.py index 65296ca..53355fb 100644 --- a/NuWeather/local/accountsdb.py +++ b/NuWeather/local/accountsdb.py @@ -86,9 +86,11 @@ class AccountsDB(): def flush(self): """Exports the database to a file.""" + tmp_filename = self.filename + '.tmp' try: - with open(self.filename, 'wb') as f: + with open(tmp_filename, 'wb') as f: pickle.dump(self.db, f, 2) + os.rename(tmp_filename, self.filename) except Exception as e: log.warning('%s: Unable to write database: %s', self._plugin_name, e) diff --git a/accountsdb.py b/accountsdb.py index 99442d6..53c198e 100644 --- a/accountsdb.py +++ b/accountsdb.py @@ -85,9 +85,11 @@ class AccountsDB(): def flush(self): """Exports the database to a file.""" + tmp_filename = self.filename + '.tmp' try: - with open(self.filename, 'wb') as f: + with open(tmp_filename, 'wb') as f: pickle.dump(self.db, f, 2) + os.rename(tmp_filename, self.filename) except Exception as e: log.warning('%s: Unable to write database: %s', self._plugin_name, e)