Merge pull request 'ask for a destination if instantiated without a filename' (#15) from ask-for-destination into master

This commit is contained in:
Stefan Haun 2020-07-28 09:14:48 +00:00
commit b88c9af7bd

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
@ -15,7 +15,7 @@ from membership_data_model import membership_data_model
class FormApp(npyscreen.NPSAppManaged): class FormApp(npyscreen.NPSAppManaged):
def __init__(self, filename): def __init__(self, filename):
super().__init__() super().__init__()
self.is_new_member = self.is_default_file(filename)
self.dir_path = pathlib.Path(filename).absolute().parent self.dir_path = pathlib.Path(filename).absolute().parent
self.member = json.load(open(filename, 'r')) self.member = json.load(open(filename, 'r'))
@ -47,23 +47,32 @@ class FormApp(npyscreen.NPSAppManaged):
name = self.base_data.get_form_values_dict()['fullname'] name = self.base_data.get_form_values_dict()['fullname']
date = datetime.datetime.now().replace(microsecond=0).isoformat() date = datetime.datetime.now().replace(microsecond=0).isoformat()
fn = '{}_{}.json'.format('_'.join(name.lower().split()), date.replace(':','')) fn = '{}_{}.json'.format('_'.join(name.lower().split()), date.replace(':',''))
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) file_path = pathlib.Path(self.dir_path, fn)
return file_path return file_path
def is_default_file(self, filename):
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()