From 9a4d7dff163ef98a0c1ae33d92685388ef0f3ef2 Mon Sep 17 00:00:00 2001
From: "Xia, Ning" <ning.xia@tu-darmstadt.de>
Date: Sat, 4 Nov 2023 18:52:36 +0100
Subject: [PATCH] update check user input

---
 main.py | 38 ++++++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/main.py b/main.py
index e7f1dfa..c5fc989 100644
--- a/main.py
+++ b/main.py
@@ -25,8 +25,7 @@ path_json = "./datasheets"
 path_setup = ""
 # path_setup = ""
 
-# Ask for the type of experiment to be performed
-# until the user gives a valid answer.
+# Ask for the type of experiment to be performed until the user gives a valid answer.
 is_log_heater = None
 while is_log_heater != 0 and is_log_heater != 1:
     if is_log_heater is None:
@@ -46,22 +45,37 @@ while is_log_heater != 0 and is_log_heater != 1:
 
 # Read the metadata necessary to control test rig from the setup file.
 metadata = m_json.get_metadata_from_setup(path_setup)
-# Read the sensor's serial number from the sensor's datasheets
-# and add it to the dictionary that holds the metadata.
+# Read the sensor's serial number from the sensor's datasheets and add it to the dictionary that holds the metadata.
 m_json.add_temperature_sensor_serials(path_json, metadata)
 # Reads sensor measurements and saves the data to a variable.
 data = m_pck.get_meas_data_calorimetry(metadata)
 
-# Read the name of the logging file to be created
-# from stdin (standard input/output).
-name_logging = input("logging file name = ")
-# The folder containing the logging files will be
-# created in the data folder in the root directory
-# of the program.
+# List of valid characters.
+valued_char = "-_.() abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+# Initialize empty filename.
+name_logging = ""
+# Loop until the user enters a valid filename.
+while name_logging == "":
+    # Read the name of the logging file to be created from stdin (standard input/output).
+    name_logging = input("logging file name = ")
+    # Remove spaces at the beginning and end of the filename.
+    name_logging = name_logging.strip()
+    # If the filename entered is empty then re-enter it.
+    if name_logging == "":
+        print("File name can not be empty.")
+        continue
+    # Iterate over all characters of the filename.
+    for c in name_logging:
+        # Requires retyping if invalid characters are present.
+        if c not in valued_char:
+            print("{} is not a valid name. ({} invalid)".format(name_logging, c))
+            name_logging = ""
+            break
+
+# The folder containing the logging files will be created in the data folder in the root directory of the program.
 path_logging = "{}/{}".format("data", name_logging)
 
-# Save the datasets and datasheets used in the experiment
-# to the logging directory.
+# Save the datasets and datasheets used in the experiment to the logging directory.
 m_pck.logging_calorimetry(data, metadata, path_logging, path_json)
 
 if is_log_heater:
-- 
GitLab