json schema: flatten dict structure to ease parsing of form entries

This commit is contained in:
David Kilias 2020-03-10 16:00:18 +01:00
parent b1369f900b
commit f4ed62748f
2 changed files with 46 additions and 43 deletions

View file

@ -15,46 +15,49 @@ class FormApp(npyscreen.NPSAppManaged):
f = npyscreen.Form(name='Edit Member data') f = npyscreen.Form(name='Edit Member data')
sd = self.member['stammdaten'] sd = self.member['stammdaten']
sd.fn = f.add(npyscreen.TitleText, name="Name: ", value=sd['fullname']) self.sd2 = {}
sd.nick = f.add(npyscreen.TitleText, name="Nickname: ", self.sd2['fullname'] = f.add(npyscreen.TitleText, name="Name: ", value=sd['fullname'])
self.sd2['nickname'] = f.add(npyscreen.TitleText, name="Nickname: ",
value=sd['nickname']) value=sd['nickname'])
sd.add.street = f.add( self.sd2['address_street'] = f.add(
npyscreen.TitleText, name="Adresse - Straße: ", value=sd['address']['street']) npyscreen.TitleText, name="Adresse - Straße: ", value=sd['address_street'])
sd.add.loc = f.add( self.sd2['address_locality'] = f.add(
npyscreen.TitleText, name="Adresse - Stadt: ", value=sd['address']['locality']) npyscreen.TitleText, name="Adresse - Stadt: ", value=sd['address_locality'])
sd.add.code = f.add(npyscreen.TitleText, self.sd2['address_code'] = f.add(npyscreen.TitleText,
name="Adresse - PLZ: ", value=sd['address']['code']) name="Adresse - PLZ: ", value=sd['address_code'])
sd.add.country = f.add( self.sd2['address_country'] = f.add(
npyscreen.TitleText, name="Adresse - Land: ", value=sd['address']['country']) npyscreen.TitleText, name="Adresse - Land: ", value=sd['address_country'])
sd.birth.date = f.add(npyscreen.TitleText, self.sd2['birth_date'] = f.add(npyscreen.TitleText,
name="Geburtstag: ", value=sd['birth']['date']) name="Geburtstag: ", value=sd['birth_date'])
sd.birth.locality = f.add( self.sd2['birth_location'] = f.add(
npyscreen.TitleText, name="Geburtsort: ", value=sd['birth']['location']) npyscreen.TitleText, name="Geburtsort: ", value=sd['birth_location'])
sd.email = f.add(npyscreen.TitleText, self.sd2['email'] = f.add(npyscreen.TitleText,
name="email: ", value=sd['email']) name="email: ", value=sd['email'])
sd.pgp = f.add(npyscreen.TitleText, name="URI PGP Key: ", value=sd['pgp-key']) self.sd2['pgp-key'] = 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['ssh-key'] = f.add(npyscreen.TitleText, name="URI SSH Key: ", value=sd['ssh-key'])
fin = self.member['finanzdaten'] fin = self.member['finanzdaten']
f.add(npyscreen.TitleText, name="IBAN: ", value=fin['iban']) self.fin2 = {}
f.add(npyscreen.TitleText, name="BIC: ", value=fin['bic']) self.fin2['iban'] = f.add(npyscreen.TitleText, name="IBAN: ", value=fin['iban'])
f.add(npyscreen.TitleText, name="Kontoinhaber: ", value=fin['holder']) self.fin2['bic'] = f.add(npyscreen.TitleText, name="BIC: ", value=fin['bic'])
f.add(npyscreen.TitleText, name="Tag der Erteilung Lastschriftmandat: ", 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']) value=fin['issuance'])
f.add(npyscreen.TitleText, name="Mandatsreferenznummer: ", self.fin2['reference'] = f.add(npyscreen.TitleText, name="Mandatsreferenznummer: ",
value=fin['reference']) 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']) value=fin['scan-sepa-mandate'])
mem = self.member['mitgliederdaten'] mem = self.member['mitgliederdaten']
f.add(npyscreen.TitleText, name="Status: ", value=mem['status']) self.mem2 = {}
f.add(npyscreen.TitleText, name="Von: ", value=mem['von']) self.mem2['status'] = f.add(npyscreen.TitleText, name="Status: ", value=mem['status'])
f.add(npyscreen.TitleText, name="Bis: ", value=mem['bis']) self.mem2['von'] = f.add(npyscreen.TitleText, name="Von: ", value=mem['von'])
f.add(npyscreen.TitleText, name="Mitgliedsbeitrag: ", self.mem2['bis'] = f.add(npyscreen.TitleText, name="Bis: ", value=mem['bis'])
self.mem2['mitgliedsbeitrag'] = f.add(npyscreen.TitleText, name="Mitgliedsbeitrag: ",
value=mem['mitgliedsbeitrag']) value=mem['mitgliedsbeitrag'])
f.add(npyscreen.TitleText, name="Schliessberechtigung: ", self.mem2['schliessberechtigung'] = f.add(npyscreen.TitleText, name="Schliessberechtigung: ",
value=mem['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']) value=mem['scan-antrag'])
f.edit() f.edit()
@ -67,8 +70,12 @@ def main(*_args):
member_struct = json.load(open('example.json', 'r')) member_struct = json.load(open('example.json', 'r'))
app = FormApp(member_struct) app = FormApp(member_struct)
app.run() 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: 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__": if __name__ == "__main__":

View file

@ -3,18 +3,14 @@
"stammdaten": { "stammdaten": {
"fullname": "Max Hackerberg", "fullname": "Max Hackerberg",
"nickname": "maxH", "nickname": "maxH",
"address" : { "address_label": "Max Hackerberg\nLeibnizstr. 32\n39104 Magdeburg",
"label": "Max Hackerberg\nLeibnizstr. 32\n39104 Magdeburg", "address_street": "Leibnizstr. 32",
"street": "Leibnizstr. 32", "address_locality": "Magdeburg",
"locality": "Magdeburg", "address_region": "",
"region": "", "address_code": "39104",
"code": "39104", "address_country": "DE",
"country": "DE" "birth_date": "1972-02-03",
}, "birth_location": "Magdeburg",
"birth": {
"date": "1972-02-03",
"location": "Magdeburg"
},
"email": "max.hackerberg@netz39.de", "email": "max.hackerberg@netz39.de",
"pgp-key": "https://cdn.netz39.de/maxH.asc", "pgp-key": "https://cdn.netz39.de/maxH.asc",
"ssh-key": "https://cdn.netz39.de/maxH.pub" "ssh-key": "https://cdn.netz39.de/maxH.pub"