Check if file exists and abort if t doesn't

This commit is contained in:
David Kilias 2020-03-22 21:35:36 +01:00
parent 852343e397
commit fe075c7c73

View file

@ -4,7 +4,7 @@
import npyscreen import npyscreen
import json import json
import datetime import datetime
from sys import argv import sys
import pathlib import pathlib
from base_data_model import base_data_model from base_data_model import base_data_model
@ -57,18 +57,21 @@ class FormApp(npyscreen.NPSAppManaged):
return pathlib.Path(filename).name == 'example.json' return pathlib.Path(filename).name == 'example.json'
def main(*_args): def main(*_args):
if len(argv) > 1: if len(sys.argv) > 1:
filename = argv[1] file_path = pathlib.Path(sys.argv[1])
if not file_path.exists():
print("Trying to open nonexistent file. Aborting.")
sys.exit(0)
else: else:
filename = pathlib.Path(pathlib.Path(__file__).absolute().parent, 'example.json') file_path = pathlib.Path(pathlib.Path(__file__).absolute().parent, 'example.json')
app = FormApp(filename) app = FormApp(file_path)
try: try:
app.run() app.run()
except Exception as e: except Exception as e:
print(e) print(e)
return 0 sys.exit(0)
if app.data_has_changed(): if app.data_has_changed():
out_data = app.get_data_from_form() out_data = app.get_data_from_form()