From f4ed62748f97cc239df31c646ff2090a3865a6e2 Mon Sep 17 00:00:00 2001 From: David Kilias Date: Tue, 10 Mar 2020 16:00:18 +0100 Subject: [PATCH] json schema: flatten dict structure to ease parsing of form entries --- edit_data_form.py | 67 ++++++++++++++++++++++++++--------------------- example.json | 22 +++++++--------- 2 files changed, 46 insertions(+), 43 deletions(-) diff --git a/edit_data_form.py b/edit_data_form.py index 43aeaee..c23955b 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -15,46 +15,49 @@ class FormApp(npyscreen.NPSAppManaged): f = npyscreen.Form(name='Edit Member data') sd = self.member['stammdaten'] - sd.fn = f.add(npyscreen.TitleText, name="Name: ", value=sd['fullname']) - sd.nick = f.add(npyscreen.TitleText, name="Nickname: ", + self.sd2 = {} + self.sd2['fullname'] = f.add(npyscreen.TitleText, name="Name: ", value=sd['fullname']) + self.sd2['nickname'] = f.add(npyscreen.TitleText, name="Nickname: ", value=sd['nickname']) - sd.add.street = f.add( - npyscreen.TitleText, name="Adresse - Straße: ", value=sd['address']['street']) - sd.add.loc = f.add( - npyscreen.TitleText, name="Adresse - Stadt: ", value=sd['address']['locality']) - sd.add.code = f.add(npyscreen.TitleText, - name="Adresse - PLZ: ", value=sd['address']['code']) - sd.add.country = f.add( - npyscreen.TitleText, name="Adresse - Land: ", value=sd['address']['country']) - sd.birth.date = f.add(npyscreen.TitleText, - name="Geburtstag: ", value=sd['birth']['date']) - sd.birth.locality = f.add( - npyscreen.TitleText, name="Geburtsort: ", value=sd['birth']['location']) - sd.email = f.add(npyscreen.TitleText, + self.sd2['address_street'] = f.add( + npyscreen.TitleText, name="Adresse - Straße: ", value=sd['address_street']) + self.sd2['address_locality'] = f.add( + npyscreen.TitleText, name="Adresse - Stadt: ", value=sd['address_locality']) + self.sd2['address_code'] = f.add(npyscreen.TitleText, + name="Adresse - PLZ: ", value=sd['address_code']) + self.sd2['address_country'] = f.add( + npyscreen.TitleText, name="Adresse - Land: ", value=sd['address_country']) + self.sd2['birth_date'] = f.add(npyscreen.TitleText, + name="Geburtstag: ", value=sd['birth_date']) + self.sd2['birth_location'] = f.add( + npyscreen.TitleText, name="Geburtsort: ", value=sd['birth_location']) + self.sd2['email'] = f.add(npyscreen.TitleText, name="email: ", value=sd['email']) - sd.pgp = f.add(npyscreen.TitleText, name="URI PGP Key: ", value=sd['pgp-key']) - sd.ssh = f.add(npyscreen.TitleText, name="URI SSH Key: ", value=sd['ssh-key']) + self.sd2['pgp-key'] = f.add(npyscreen.TitleText, name="URI PGP Key: ", value=sd['pgp-key']) + self.sd2['ssh-key'] = f.add(npyscreen.TitleText, name="URI SSH Key: ", value=sd['ssh-key']) fin = self.member['finanzdaten'] - f.add(npyscreen.TitleText, name="IBAN: ", value=fin['iban']) - f.add(npyscreen.TitleText, name="BIC: ", value=fin['bic']) - f.add(npyscreen.TitleText, name="Kontoinhaber: ", value=fin['holder']) - f.add(npyscreen.TitleText, name="Tag der Erteilung Lastschriftmandat: ", + self.fin2 = {} + self.fin2['iban'] = f.add(npyscreen.TitleText, name="IBAN: ", value=fin['iban']) + self.fin2['bic'] = f.add(npyscreen.TitleText, name="BIC: ", value=fin['bic']) + self.fin2['holder'] = f.add(npyscreen.TitleText, name="Kontoinhaber: ", value=fin['holder']) + self.fin2['issuance'] = f.add(npyscreen.TitleText, name="Tag der Erteilung Lastschriftmandat: ", value=fin['issuance']) - f.add(npyscreen.TitleText, name="Mandatsreferenznummer: ", + self.fin2['reference'] = f.add(npyscreen.TitleText, name="Mandatsreferenznummer: ", value=fin['reference']) - f.add(npyscreen.TitleText, name="URI Scan SEPA-Mandat: ", + self.fin2['scan-sepa-mandate'] = f.add(npyscreen.TitleText, name="URI Scan SEPA-Mandat: ", value=fin['scan-sepa-mandate']) mem = self.member['mitgliederdaten'] - f.add(npyscreen.TitleText, name="Status: ", value=mem['status']) - f.add(npyscreen.TitleText, name="Von: ", value=mem['von']) - f.add(npyscreen.TitleText, name="Bis: ", value=mem['bis']) - f.add(npyscreen.TitleText, name="Mitgliedsbeitrag: ", + self.mem2 = {} + self.mem2['status'] = f.add(npyscreen.TitleText, name="Status: ", value=mem['status']) + self.mem2['von'] = f.add(npyscreen.TitleText, name="Von: ", value=mem['von']) + self.mem2['bis'] = f.add(npyscreen.TitleText, name="Bis: ", value=mem['bis']) + self.mem2['mitgliedsbeitrag'] = f.add(npyscreen.TitleText, name="Mitgliedsbeitrag: ", value=mem['mitgliedsbeitrag']) - f.add(npyscreen.TitleText, name="Schliessberechtigung: ", + self.mem2['schliessberechtigung'] = f.add(npyscreen.TitleText, name="Schliessberechtigung: ", value=mem['schliessberechtigung']) - f.add(npyscreen.TitleText, name="URI Scan Antrag: ", + self.mem2['scan-antrag'] = f.add(npyscreen.TitleText, name="URI Scan Antrag: ", value=mem['scan-antrag']) f.edit() @@ -67,8 +70,12 @@ def main(*_args): member_struct = json.load(open('example.json', 'r')) app = FormApp(member_struct) app.run() + new_data = {} + new_data['stammdaten'] = dict(zip(app.sd2.keys(), map(lambda f: f.value, app.sd2.values()))) + new_data['finanzdaten'] = dict(zip(app.fin2.keys(), map(lambda f: f.value, app.fin2.values()))) + new_data['mitgliederdaten'] = dict(zip(app.mem2.keys(), map(lambda f: f.value, app.mem2.values()))) with open('foo.json', 'w') as outfile: - json.dump(app.member, outfile, sort_keys=True, indent=4) + json.dump(new_data, outfile, sort_keys=True, indent=4) if __name__ == "__main__": diff --git a/example.json b/example.json index e8225c9..5e5008c 100644 --- a/example.json +++ b/example.json @@ -3,18 +3,14 @@ "stammdaten": { "fullname": "Max Hackerberg", "nickname": "maxH", - "address" : { - "label": "Max Hackerberg\nLeibnizstr. 32\n39104 Magdeburg", - "street": "Leibnizstr. 32", - "locality": "Magdeburg", - "region": "", - "code": "39104", - "country": "DE" - }, - "birth": { - "date": "1972-02-03", - "location": "Magdeburg" - }, + "address_label": "Max Hackerberg\nLeibnizstr. 32\n39104 Magdeburg", + "address_street": "Leibnizstr. 32", + "address_locality": "Magdeburg", + "address_region": "", + "address_code": "39104", + "address_country": "DE", + "birth_date": "1972-02-03", + "birth_location": "Magdeburg", "email": "max.hackerberg@netz39.de", "pgp-key": "https://cdn.netz39.de/maxH.asc", "ssh-key": "https://cdn.netz39.de/maxH.pub" @@ -36,4 +32,4 @@ "schliessberechtigung": "", "scan-antrag": "" } -} +} \ No newline at end of file