Move saved-game deletion to separate file

This commit is contained in:
Samuel Sloniker 2022-03-08 13:29:12 -08:00
parent 2715d1992f
commit b91481f685
2 changed files with 32 additions and 24 deletions

View File

@ -12,6 +12,7 @@ import json
import info
import stty
import shutil
import delete_save
from blessings import Terminal
@ -223,30 +224,7 @@ group.add_argument(
args = parser.parse_args()
if args.delete_save:
try:
shutil.move(gamesave_path, gamesave_path + "-old")
print("Saved game deleted.")
print(f"A backup was created at `{gamesave_path}-old`. To restore it, run:")
print(f"mv {gamesave_path}-old {gamesave_path}")
except FileNotFoundError:
print("No saved game found.")
except Exception as e:
print("An error occurred.")
print(str(type(e)).split("'")[1] + ": " + str(e))
cont = input(
"Do you want to try to non-recoverably delete the saved game? [Y/n] "
)
if cont[0] in "Yy":
try:
os.unlink(gamesave_path)
print("Saved game deleted.")
except Exception as e:
print("An error occurred.")
print(str(type(e)).split("'")[1] + ": " + str(e))
sys.exit(1)
else:
sys.exit(1)
sys.exit(0)
delete_save.delete_save(gamesave_path)
elif args.save_game:
height = 24
width = 80

30
wormpy/delete_save.py Normal file
View File

@ -0,0 +1,30 @@
import os
import sys
import shutil
def delete_save(gamesave_path):
try:
shutil.move(gamesave_path, gamesave_path + "-old")
print("Saved game deleted.")
print(f"A backup was created at `{gamesave_path}-old`. To restore it, run:")
print(f"mv {gamesave_path}-old {gamesave_path}")
except FileNotFoundError:
print("No saved game found.")
except Exception as e:
print("An error occurred.")
print(str(type(e)).split("'")[1] + ": " + str(e))
cont = input(
"Do you want to try to non-recoverably delete the saved game? [Y/n] "
)
if cont[0] in "Yy":
try:
os.unlink(gamesave_path)
print("Saved game deleted.")
except Exception as e:
print("An error occurred.")
print(str(type(e)).split("'")[1] + ": " + str(e))
sys.exit(1)
else:
sys.exit(1)
sys.exit(0)