#!/usr/bin/env python # encoding: utf-8 import npyscreen import json import datetime from sys import argv from base_data_model import base_data_model from financial_data_model import financial_data_model from membership_data_model import membership_data_model class FormApp(npyscreen.NPSAppManaged): def __init__(self, member_struct): 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']) def main(self): f = npyscreen.Form(name='Edit Member data') self.base_data.add_edit_fields(f) self.financial_data.add_edit_fields(f) self.membership_data.add_edit_fields(f) f.edit() def get_data_from_form(self): data_dict = {} data_dict['stammdaten'] = self.base_data.get_form_values_dict() data_dict['finanzdaten'] = self.financial_data.get_form_values_dict() data_dict['mitgliederdaten'] = self.membership_data.get_form_values_dict() data_dict['timestamp'] = datetime.datetime.now().replace(microsecond=0).isoformat() return data_dict def data_has_changed(self): print(f"{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): name = self.base_data.get_form_values_dict()['fullname'] fn = '{}_{}.json'.format('_'.join(name.split()), datetime.datetime.now().replace(microsecond=0).isoformat()) return fn def main(*_args): if len(argv) > 1: member_struct = json.load(open(argv[1], 'r')) else: member_struct = json.load(open('example.json', 'r')) app = FormApp(member_struct) app.run() if app.data_has_changed(): out_data = app.get_data_from_form() outfile_name = app.get_filename() with open(outfile_name, 'w') as outfile: json.dump(out_data, outfile, sort_keys=True, indent=4) else: print("Nothing has changed") if __name__ == "__main__": npyscreen.wrapper_basic(main)