From 852343e3977a83f3907a7889d63d68d8d084d02e Mon Sep 17 00:00:00 2001 From: David Kilias Date: Fri, 20 Mar 2020 13:15:20 +0100 Subject: [PATCH 1/3] ask for a destination if instantiated without a filename --- edit_data_form.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/edit_data_form.py b/edit_data_form.py index 276ccc1..c4ca2d6 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -15,7 +15,7 @@ from membership_data_model import membership_data_model class FormApp(npyscreen.NPSAppManaged): def __init__(self, filename): super().__init__() - + self.is_new_member = self.is_default_file(filename) self.dir_path = pathlib.Path(filename).absolute().parent self.member = json.load(open(filename, 'r')) @@ -45,11 +45,16 @@ class FormApp(npyscreen.NPSAppManaged): def get_filepath(self): name = self.base_data.get_form_values_dict()['fullname'] - date = datetime.datetime.now().replace(microsecond=0).isoformat() - fn = '{}_{}.json'.format('_'.join(name.lower().split()), date.replace(':','')) + fn = '{}_{}.json'.format('_'.join(name.split()), datetime.datetime.now().replace(microsecond=0).isoformat()) + if self.is_new_member: + print('Enter destination directory for new Member data:') + self.dir_path = input() + file_path = pathlib.Path(self.dir_path, fn) return file_path + def is_default_file(self, filename): + return pathlib.Path(filename).name == 'example.json' def main(*_args): if len(argv) > 1: -- 2.45.2 From fe075c7c737b87d73d8527b15ef35c907574097c Mon Sep 17 00:00:00 2001 From: David Kilias Date: Sun, 22 Mar 2020 21:35:36 +0100 Subject: [PATCH 2/3] Check if file exists and abort if t doesn't --- edit_data_form.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/edit_data_form.py b/edit_data_form.py index c4ca2d6..b158b23 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -4,7 +4,7 @@ import npyscreen import json import datetime -from sys import argv +import sys import pathlib from base_data_model import base_data_model @@ -57,18 +57,21 @@ class FormApp(npyscreen.NPSAppManaged): return pathlib.Path(filename).name == 'example.json' def main(*_args): - if len(argv) > 1: - filename = argv[1] + if len(sys.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: - 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: app.run() except Exception as e: print(e) - return 0 + sys.exit(0) if app.data_has_changed(): out_data = app.get_data_from_form() -- 2.45.2 From 13880a3b88b2c3d7a933c491c230672e7171fbbb Mon Sep 17 00:00:00 2001 From: Stefan Haun Date: Sun, 22 Mar 2020 18:33:56 +0100 Subject: [PATCH 3/3] Use lowercase name for file paths --- edit_data_form.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/edit_data_form.py b/edit_data_form.py index b158b23..03ac320 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -45,11 +45,11 @@ class FormApp(npyscreen.NPSAppManaged): def get_filepath(self): name = self.base_data.get_form_values_dict()['fullname'] - fn = '{}_{}.json'.format('_'.join(name.split()), datetime.datetime.now().replace(microsecond=0).isoformat()) + date = datetime.datetime.now().replace(microsecond=0).isoformat() + fn = '{}_{}.json'.format('_'.join(name.lower().split()), date) if self.is_new_member: print('Enter destination directory for new Member data:') self.dir_path = input() - file_path = pathlib.Path(self.dir_path, fn) return file_path -- 2.45.2