Skip to content

Commit

Permalink
Save critical files not directly, but via temp file
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Jun 1, 2024
1 parent 98d648e commit 46e86e4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/mmdiary/utils/jsoncache.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ def __load(self):
def __save(self):
if self.__filename is None or not self.__changed:
return
with open(self.__filename, "wb") as f:
tmpfile = self.__filename + ".tmp"
with open(tmpfile, "wb") as f:
pickle.dump(self.__data, f)
os.replace(tmpfile, self.__filename)

def __load_json(self, filename):
with open(filename, "r", encoding="utf-8") as f:
Expand Down
4 changes: 3 additions & 1 deletion src/mmdiary/video/uploader/dailymotion.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ def __read(self):
self.__current = current

def __save(self):
with open(self.__accounts_file, "w", encoding="utf-8") as f:
tmpfile = self.__accounts_file + ".tmp"
with open(tmpfile, "w", encoding="utf-8") as f:
data = {"accounts": self.__accounts, "current": self.__current}
json.dump(data, f, ensure_ascii=False, indent=2)
os.replace(tmpfile, self.__accounts_file)

def get_all(self):
return self.__accounts
Expand Down

0 comments on commit 46e86e4

Please sign in to comment.