out-of-dir-files #13

Merged
tux merged 3 commits from out-of-dir-files into master 2020-03-18 21:37:00 +01:00

View file

@ -5,6 +5,7 @@ import npyscreen
import json
import datetime
from sys import argv
import pathlib
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 = pathlib.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 = pathlib.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 = pathlib.Path(pathlib.Path(__file__).absolute().parent, '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")