writes output file to same directory as input file

This commit is contained in:
David Kilias 2020-03-16 10:45:50 +01:00
parent 3cfb69c1b6
commit a46384aa25

View file

@ -5,6 +5,7 @@ import npyscreen
import json import json
import datetime import datetime
from sys import argv from sys import argv
from pathlib import Path
from base_data_model import base_data_model from base_data_model import base_data_model
from financial_data_model import financial_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): class FormApp(npyscreen.NPSAppManaged):
def __init__(self, member_struct): def __init__(self, filename):
super().__init__() super().__init__()
self.member = member_struct
self.base_data = base_data_model(member_struct['stammdaten']) self.dir_path = Path(filename).absolute().parent
self.financial_data = financial_data_model(member_struct['finanzdaten']) self.member = json.load(open(filename, 'r'))
self.membership_data = membership_data_model(member_struct['mitgliederdaten'])
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): def main(self):
f = npyscreen.Form(name='Edit Member data') f = npyscreen.Form(name='Edit Member data')
@ -40,18 +43,20 @@ class FormApp(npyscreen.NPSAppManaged):
def data_has_changed(self): def data_has_changed(self):
return self.base_data.has_changed() | self.financial_data.has_changed() | self.membership_data.has_changed() 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'] name = self.base_data.get_form_values_dict()['fullname']
fn = '{}_{}.json'.format('_'.join(name.split()), datetime.datetime.now().replace(microsecond=0).isoformat()) 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): def main(*_args):
if len(argv) > 1: if len(argv) > 1:
member_struct = json.load(open(argv[1], 'r')) filename = argv[1]
else: else:
member_struct = json.load(open('example.json', 'r')) filename = 'example.json'
app = FormApp(member_struct)
app = FormApp(filename)
try: try:
app.run() app.run()
@ -61,9 +66,9 @@ def main(*_args):
if app.data_has_changed(): if app.data_has_changed():
out_data = app.get_data_from_form() out_data = app.get_data_from_form()
outfile_name = app.get_filename() outfile_path = app.get_filepath()
print(f"Writing changed Member Data to {outfile_name}") print(f"Writing changed Member Data to {outfile_path}")
with open(outfile_name, 'w') as outfile: with open(outfile_path, 'w') as outfile:
json.dump(out_data, outfile, sort_keys=True, indent=4) json.dump(out_data, outfile, sort_keys=True, indent=4)
else: else:
print("Nothing has changed, skipping writing file") print("Nothing has changed, skipping writing file")