From 033ce007255678d839fca0041d36103713f41389 Mon Sep 17 00:00:00 2001 From: boltgolt Date: Sat, 5 Dec 2020 14:28:57 +0100 Subject: [PATCH] Added --plain argument for machine cli interaction --- src/cli.py | 6 ++++++ src/cli/add.py | 11 +++++++++-- src/cli/list.py | 27 +++++++++++++++++++-------- src/cli/remove.py | 6 ++++-- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/cli.py b/src/cli.py index de9d698..54c4a19 100755 --- a/src/cli.py +++ b/src/cli.py @@ -58,6 +58,12 @@ parser.add_argument( help="Skip all questions.", action="store_true") +# Add the --plain flag +parser.add_argument( + "--plain", + help="Print machine-friendly output.", + action="store_true") + # Overwrite the default help message so we can use a uppercase S parser.add_argument( "-h", "--help", diff --git a/src/cli/add.py b/src/cli/add.py index 112eacd..d1739cf 100644 --- a/src/cli/add.py +++ b/src/cli/add.py @@ -69,7 +69,9 @@ if len(encodings) > 3: print("NOTICE: Each additional model slows down the face recognition engine slightly") print("Press Ctrl+C to cancel\n") -print("Adding face model for the user " + user) +# Make clear what we are doing if not human +if not builtins.howdy_args.plain: + print("Adding face model for the user " + user) # Set the default label label = "Initial model" @@ -83,12 +85,17 @@ if builtins.howdy_args.y: print('Using default label "%s" because of -y flag' % (label, )) else: # Ask the user for a custom label - label_in = input("Enter a label for this new model [" + label + "] (max 24 characters): ") + label_in = input("Enter a label for this new model [" + label + "]: ") # Set the custom label (if any) and limit it to 24 characters if label_in != "": label = label_in[:24] +# Remove illegal characters +if "," in label: + print("NOTICE: Removing illegal character \",\" from model name") + label = label.replace(",", "") + # Prepare the metadata for insertion insert_model = { "time": int(time.time()), diff --git a/src/cli/list.py b/src/cli/list.py index fe05d6e..aad7eb3 100644 --- a/src/cli/list.py +++ b/src/cli/list.py @@ -28,20 +28,31 @@ except FileNotFoundError: print("\n\tsudo howdy -U " + user + " add\n") sys.exit(1) -# Print a header -print("Known face models for " + user + ":") -print("\n\t\033[1;29mID Date Label\033[0m") +# Print a header if we're not in plain mode +if not builtins.howdy_args.plain: + print("Known face models for " + user + ":") + print("\n\033[1;29mID Date Label\033[0m") # Loop through all encodings and print info about them for enc in encodings: - # Start with a tab and print the id - print("\t" + str(enc["id"]), end="") - # Print padding spaces after the id - print((4 - len(str(enc["id"]))) * " ", end="") + # Start with the id + print(str(enc["id"]), end="") + + # Add comma for machine reading + if builtins.howdy_args.plain: + print(",", end="") + # Print padding spaces after the id for a nice layout + else: + print((4 - len(str(enc["id"]))) * " ", end="") + # Format the time as ISO in the local timezone print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(enc["time"])), end="") + + # Seperate with commas again for machines, spaces otherwise + print("," if builtins.howdy_args.plain else " ", end="") + # End with the label - print(" " + enc["label"]) + print(enc["label"]) # Add a closing enter print() diff --git a/src/cli/remove.py b/src/cli/remove.py index 45e2036..b41c9bf 100644 --- a/src/cli/remove.py +++ b/src/cli/remove.py @@ -13,6 +13,8 @@ user = builtins.howdy_user # Check if enough arguments have been passed if builtins.howdy_args.argument is None: print("Please add the ID of the model you want to remove as an argument") + print("For example:") + print("\n\thowdy remove 0\n") print("You can find the IDs by running:") print("\n\thowdy list\n") sys.exit(1) @@ -48,7 +50,7 @@ for enc in encodings: # Abort if the answer isn't yes if (ans.lower() != "y"): - print('\nInerpeting as a "NO"') + print('\nInterpreting as a "NO"') sys.exit() # Add a padding empty line @@ -61,7 +63,7 @@ for enc in encodings: # Abort if no matching id was found if not found: print("No model with ID " + builtins.howdy_args.argument + " exists for " + user) - sys.exit() + sys.exit(1) # Remove the entire file if this encoding is the only one if len(encodings) == 1: