From a46384aa25de54ddf6ba229a2be9dc81012929c4 Mon Sep 17 00:00:00 2001 From: David Kilias Date: Mon, 16 Mar 2020 10:45:50 +0100 Subject: [PATCH 1/3] writes output file to same directory as input file --- edit_data_form.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/edit_data_form.py b/edit_data_form.py index 8c8c299..8167637 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -5,6 +5,7 @@ import npyscreen import json import datetime from sys import argv +from pathlib import Path from base_data_model import base_data_model from financial_data_model import financial_data_model @@ -12,13 +13,15 @@ from membership_data_model import membership_data_model class FormApp(npyscreen.NPSAppManaged): - def __init__(self, member_struct): + def __init__(self, filename): super().__init__() - self.member = member_struct - self.base_data = base_data_model(member_struct['stammdaten']) - self.financial_data = financial_data_model(member_struct['finanzdaten']) - self.membership_data = membership_data_model(member_struct['mitgliederdaten']) + self.dir_path = Path(filename).absolute().parent + self.member = json.load(open(filename, 'r')) + + self.base_data = base_data_model(self.member['stammdaten']) + self.financial_data = financial_data_model(self.member['finanzdaten']) + self.membership_data = membership_data_model(self.member['mitgliederdaten']) def main(self): f = npyscreen.Form(name='Edit Member data') @@ -40,18 +43,20 @@ class FormApp(npyscreen.NPSAppManaged): def data_has_changed(self): return self.base_data.has_changed() | self.financial_data.has_changed() | self.membership_data.has_changed() - def get_filename(self): + 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()) - return fn + file_path = Path(self.dir_path, fn) + return file_path def main(*_args): if len(argv) > 1: - member_struct = json.load(open(argv[1], 'r')) + filename = argv[1] else: - member_struct = json.load(open('example.json', 'r')) - app = FormApp(member_struct) + filename = 'example.json' + + app = FormApp(filename) try: app.run() @@ -61,9 +66,9 @@ def main(*_args): if app.data_has_changed(): out_data = app.get_data_from_form() - outfile_name = app.get_filename() - print(f"Writing changed Member Data to {outfile_name}") - with open(outfile_name, 'w') as outfile: + outfile_path = app.get_filepath() + print(f"Writing changed Member Data to {outfile_path}") + with open(outfile_path, 'w') as outfile: json.dump(out_data, outfile, sort_keys=True, indent=4) else: print("Nothing has changed, skipping writing file") -- 2.45.2 From d3cabbf57321af4d1b945f8a6e24722cc58c26a2 Mon Sep 17 00:00:00 2001 From: David Kilias Date: Mon, 16 Mar 2020 10:50:47 +0100 Subject: [PATCH 2/3] now also finds example.json if called from another location --- edit_data_form.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/edit_data_form.py b/edit_data_form.py index 8167637..b3042a8 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -54,7 +54,7 @@ def main(*_args): if len(argv) > 1: filename = argv[1] else: - filename = 'example.json' + filename = Path(Path(__file__).absolute().parent, 'example.json') app = FormApp(filename) -- 2.45.2 From 1989d432d9dc8663483732557e89f83848c1fd81 Mon Sep 17 00:00:00 2001 From: David Kilias Date: Wed, 18 Mar 2020 19:31:02 +0100 Subject: [PATCH 3/3] use Path with prefix --- edit_data_form.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/edit_data_form.py b/edit_data_form.py index b3042a8..74e6f66 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -5,7 +5,7 @@ import npyscreen import json import datetime from sys import argv -from pathlib import Path +import pathlib from base_data_model import base_data_model from financial_data_model import financial_data_model @@ -16,7 +16,7 @@ class FormApp(npyscreen.NPSAppManaged): def __init__(self, filename): super().__init__() - self.dir_path = Path(filename).absolute().parent + self.dir_path = pathlib.Path(filename).absolute().parent self.member = json.load(open(filename, 'r')) self.base_data = base_data_model(self.member['stammdaten']) @@ -46,7 +46,7 @@ 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()) - file_path = Path(self.dir_path, fn) + file_path = pathlib.Path(self.dir_path, fn) return file_path @@ -54,7 +54,7 @@ def main(*_args): if len(argv) > 1: filename = argv[1] else: - filename = Path(Path(__file__).absolute().parent, 'example.json') + filename = pathlib.Path(pathlib.Path(__file__).absolute().parent, 'example.json') app = FormApp(filename) -- 2.45.2