109 lines
3.7 KiB
Python
Executable file
109 lines
3.7 KiB
Python
Executable file
#!/usr/bin/env python
|
|
# encoding: utf-8
|
|
|
|
import os
|
|
import npyscreen
|
|
import json
|
|
import datetime
|
|
import sys
|
|
import pathlib
|
|
|
|
from base_data_model import base_data_model
|
|
from financial_data_model import financial_data_model
|
|
from membership_data_model import membership_data_model
|
|
from id import new_id
|
|
|
|
|
|
class FormApp(npyscreen.NPSAppManaged):
|
|
def __init__(self, filename):
|
|
super().__init__()
|
|
self.is_new_member = self.is_default_file(filename)
|
|
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')
|
|
|
|
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()
|
|
try:
|
|
data_dict['id'] = self.member['id']
|
|
except KeyError:
|
|
data_dict['id'] = new_id()
|
|
return data_dict
|
|
|
|
def data_has_changed(self):
|
|
return self.base_data.has_changed() | self.financial_data.has_changed() | self.membership_data.has_changed()
|
|
|
|
def get_filepath(self):
|
|
name = self.base_data.get_form_values_dict()['fullname']
|
|
date = datetime.datetime.now().replace(microsecond=0).isoformat()
|
|
fn = '{}_{}.json'.format('_'.join(name.lower().split()), date.replace(':',''))
|
|
if self.is_new_member:
|
|
print('Enter destination directory for new Member data:')
|
|
self.dir_path = input()
|
|
file_path = pathlib.Path(self.dir_path, fn)
|
|
return file_path
|
|
|
|
def is_default_file(self, filename):
|
|
return pathlib.Path(filename).name == 'example.json'
|
|
|
|
|
|
def main(*_args):
|
|
if len(sys.argv) > 1:
|
|
file_path = pathlib.Path(sys.argv[1])
|
|
|
|
if(sys.argv[1].endswith(".json")):
|
|
# json file as argument
|
|
|
|
if not file_path.exists():
|
|
print("Trying to open nonexistent file. Aborting.")
|
|
sys.exit(0)
|
|
|
|
elif file_path.is_dir():
|
|
# we want the latest json file in given folder
|
|
filesList = [f for f in os.listdir(file_path) if os.path.isfile(os.path.join(file_path, f)) and f.endswith(".json")]
|
|
filesList.sort()
|
|
file_path = os.path.join(file_path, filesList[-1])
|
|
|
|
else:
|
|
print("Please enter a valid json file or the directory containing the json file!")
|
|
sys.exit(0)
|
|
|
|
else:
|
|
file_path = pathlib.Path(pathlib.Path(__file__).absolute().parent, 'example.json')
|
|
|
|
app = FormApp(file_path)
|
|
|
|
try:
|
|
app.run()
|
|
except Exception as e:
|
|
print(e)
|
|
sys.exit(0)
|
|
|
|
if app.data_has_changed():
|
|
out_data = app.get_data_from_form()
|
|
outfile_path = app.get_filepath()
|
|
print(f"Writing changed Member Data to {outfile_path}")
|
|
with open(outfile_path, 'w', encoding='utf8') as outfile:
|
|
json.dump(out_data, outfile, sort_keys=True, indent=4, ensure_ascii=False)
|
|
else:
|
|
print("Nothing has changed, skipping writing file")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
npyscreen.wrapper_basic(main)
|