From a8a56801a2d8a634a3e6c7a8e2c7694cab944c43 Mon Sep 17 00:00:00 2001 From: MG-5 Date: Mon, 24 Oct 2022 23:58:39 +0200 Subject: [PATCH 1/4] Automatically get the latest json file if only given a folder as path --- edit_data_form.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/edit_data_form.py b/edit_data_form.py index 68ab61a..7fda31b 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # encoding: utf-8 +import os import npyscreen import json import datetime @@ -65,9 +66,20 @@ class FormApp(npyscreen.NPSAppManaged): def main(*_args): if len(sys.argv) > 1: file_path = pathlib.Path(sys.argv[1]) - if not file_path.exists(): - print("Trying to open nonexistent file. Aborting.") - sys.exit(0) + + 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[len(filesList) - 1]) + else: file_path = pathlib.Path(pathlib.Path(__file__).absolute().parent, 'example.json') -- 2.45.2 From a8f408941a37f406dc009357904f76a8e804d481 Mon Sep 17 00:00:00 2001 From: MG-5 Date: Tue, 25 Oct 2022 00:02:53 +0200 Subject: [PATCH 2/4] Add catching for invalid file input --- edit_data_form.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/edit_data_form.py b/edit_data_form.py index 7fda31b..605f18c 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -80,6 +80,10 @@ def main(*_args): filesList.sort() file_path = os.path.join(file_path, filesList[len(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') -- 2.45.2 From 914881a104f41df1205462de2ca30340ce8dc44a Mon Sep 17 00:00:00 2001 From: MG-5 Date: Tue, 25 Oct 2022 00:10:09 +0200 Subject: [PATCH 3/4] Update Readme --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b2b9fad..c5cd171 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,15 @@ python3 edit_data_form.py ## Daten eines bestehenden Mitglieds ändern +Eine Nennung des Ordners des Members genügt. +Dann wird stets die aktuelle json-Datei verwendet. + ```bash -MEMBER_FILE= -python3 edit_data_form.py $MEMBER_FILE +python3 edit_data_form.py +``` + +Alternativ kann die json-Datei explizit genannt werden: + +```bash +python3 edit_data_form.py ``` \ No newline at end of file -- 2.45.2 From 2c0fdb13c795118653e930e5ec2329bddaa41926 Mon Sep 17 00:00:00 2001 From: MG-5 Date: Tue, 25 Oct 2022 00:11:40 +0200 Subject: [PATCH 4/4] Use better variant to get the last element --- edit_data_form.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/edit_data_form.py b/edit_data_form.py index 605f18c..7c41f17 100755 --- a/edit_data_form.py +++ b/edit_data_form.py @@ -78,7 +78,7 @@ def main(*_args): # 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[len(filesList) - 1]) + file_path = os.path.join(file_path, filesList[-1]) else: print("Please enter a valid json file or the directory containing the json file!") -- 2.45.2