Add set command, mostly so UI can set config options
This commit is contained in:
parent
033ce00725
commit
2342e219a2
5 changed files with 71 additions and 18 deletions
12
src/cli.py
12
src/cli.py
|
|
@ -36,15 +36,15 @@ parser = argparse.ArgumentParser(
|
||||||
# Add an argument for the command
|
# Add an argument for the command
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"command",
|
"command",
|
||||||
help="The command option to execute, can be one of the following: add, clear, config, disable, list, remove, snapshot, test or version.",
|
help="The command option to execute, can be one of the following: add, clear, config, disable, list, remove, snapshot, set, test or version.",
|
||||||
metavar="command",
|
metavar="command",
|
||||||
choices=["add", "clear", "config", "disable", "list", "remove", "snapshot", "test", "version"])
|
choices=["add", "clear", "config", "disable", "list", "remove", "set", "snapshot", "test", "version"])
|
||||||
|
|
||||||
# Add an argument for the extra arguments of diable and remove
|
# Add an argument for the extra arguments of diable and remove
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"argument",
|
"arguments",
|
||||||
help="Either 0 (enable) or 1 (disable) for the disable command, or the model ID for the remove command.",
|
help="Optional arguments for the add, disable, remove and set commands.",
|
||||||
nargs="?")
|
nargs="*")
|
||||||
|
|
||||||
# Add the user flag
|
# Add the user flag
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
|
@ -109,6 +109,8 @@ elif args.command == "list":
|
||||||
import cli.list
|
import cli.list
|
||||||
elif args.command == "remove":
|
elif args.command == "remove":
|
||||||
import cli.remove
|
import cli.remove
|
||||||
|
elif args.command == "set":
|
||||||
|
import cli.set
|
||||||
elif args.command == "snapshot":
|
elif args.command == "snapshot":
|
||||||
import cli.snap
|
import cli.snap
|
||||||
elif args.command == "test":
|
elif args.command == "test":
|
||||||
|
|
|
||||||
|
|
@ -76,8 +76,12 @@ if not builtins.howdy_args.plain:
|
||||||
# Set the default label
|
# Set the default label
|
||||||
label = "Initial model"
|
label = "Initial model"
|
||||||
|
|
||||||
|
# Get the label from the cli arguments if provided
|
||||||
|
if builtins.howdy_args.arguments:
|
||||||
|
label = builtins.howdy_args.arguments[0]
|
||||||
|
|
||||||
# If models already exist, set that default label
|
# If models already exist, set that default label
|
||||||
if encodings:
|
elif encodings:
|
||||||
label = "Model #" + str(len(encodings) + 1)
|
label = "Model #" + str(len(encodings) + 1)
|
||||||
|
|
||||||
# Keep de default name if we can't ask questions
|
# Keep de default name if we can't ask questions
|
||||||
|
|
|
||||||
|
|
@ -15,18 +15,21 @@ config = configparser.ConfigParser()
|
||||||
config.read(config_path)
|
config.read(config_path)
|
||||||
|
|
||||||
# Check if enough arguments have been passed
|
# Check if enough arguments have been passed
|
||||||
if builtins.howdy_args.argument is None:
|
if not builtins.howdy_args.arguments:
|
||||||
print("Please add a 0 (enable) or a 1 (disable) as an argument")
|
print("Please add a 0 (enable) or a 1 (disable) as an argument")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Get the cli argument
|
||||||
|
argument = builtins.howdy_args.arguments[0]
|
||||||
|
|
||||||
# Translate the argument to the right string
|
# Translate the argument to the right string
|
||||||
if builtins.howdy_args.argument == "1" or builtins.howdy_args.argument.lower() == "true":
|
if argument == "1" or argument.lower() == "true":
|
||||||
out_value = "true"
|
out_value = "true"
|
||||||
elif builtins.howdy_args.argument == "0" or builtins.howdy_args.argument.lower() == "false":
|
elif argument == "0" or argument.lower() == "false":
|
||||||
out_value = "false"
|
out_value = "false"
|
||||||
else:
|
else:
|
||||||
# Of it's not a 0 or a 1, it's invalid
|
# Of it's not a 0 or a 1, it's invalid
|
||||||
print("Please only use false (enable) or true (disable) as an argument")
|
print("Please only use 0 (enable) or 1 (disable) as an argument")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Don't do anything when the state is already the requested one
|
# Don't do anything when the state is already the requested one
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ path = os.path.dirname(os.path.realpath(__file__)) + "/.."
|
||||||
user = builtins.howdy_user
|
user = builtins.howdy_user
|
||||||
|
|
||||||
# Check if enough arguments have been passed
|
# Check if enough arguments have been passed
|
||||||
if builtins.howdy_args.argument is None:
|
if not builtins.howdy_args.arguments:
|
||||||
print("Please add the ID of the model you want to remove as an argument")
|
print("Please add the ID of the model you want to remove as an argument")
|
||||||
print("For example:")
|
print("For example:")
|
||||||
print("\n\thowdy remove 0\n")
|
print("\n\thowdy remove 0\n")
|
||||||
|
|
@ -39,9 +39,12 @@ except FileNotFoundError:
|
||||||
# Tracks if a encoding with that id has been found
|
# Tracks if a encoding with that id has been found
|
||||||
found = False
|
found = False
|
||||||
|
|
||||||
|
# Get the ID from the cli arguments
|
||||||
|
id = builtins.howdy_args.arguments[0]
|
||||||
|
|
||||||
# Loop though all encodings and check if they match the argument
|
# Loop though all encodings and check if they match the argument
|
||||||
for enc in encodings:
|
for enc in encodings:
|
||||||
if str(enc["id"]) == builtins.howdy_args.argument:
|
if str(enc["id"]) == id:
|
||||||
# Only ask the user if there's no -y flag
|
# Only ask the user if there's no -y flag
|
||||||
if not builtins.howdy_args.y:
|
if not builtins.howdy_args.y:
|
||||||
# Double check with the user
|
# Double check with the user
|
||||||
|
|
@ -50,8 +53,8 @@ for enc in encodings:
|
||||||
|
|
||||||
# Abort if the answer isn't yes
|
# Abort if the answer isn't yes
|
||||||
if (ans.lower() != "y"):
|
if (ans.lower() != "y"):
|
||||||
print('\nInterpreting as a "NO"')
|
print('\nInterpreting as a "NO", aborting')
|
||||||
sys.exit()
|
sys.exit(1)
|
||||||
|
|
||||||
# Add a padding empty line
|
# Add a padding empty line
|
||||||
print()
|
print()
|
||||||
|
|
@ -62,7 +65,7 @@ for enc in encodings:
|
||||||
|
|
||||||
# Abort if no matching id was found
|
# Abort if no matching id was found
|
||||||
if not found:
|
if not found:
|
||||||
print("No model with ID " + builtins.howdy_args.argument + " exists for " + user)
|
print("No model with ID " + id + " exists for " + user)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Remove the entire file if this encoding is the only one
|
# Remove the entire file if this encoding is the only one
|
||||||
|
|
@ -73,13 +76,13 @@ else:
|
||||||
# A place holder to contain the encodings that will remain
|
# A place holder to contain the encodings that will remain
|
||||||
new_encodings = []
|
new_encodings = []
|
||||||
|
|
||||||
# Loop though all encodin and only add thos that don't need to be removed
|
# Loop though all encodings and only add those that don't need to be removed
|
||||||
for enc in encodings:
|
for enc in encodings:
|
||||||
if str(enc["id"]) != builtins.howdy_args.argument:
|
if str(enc["id"]) != id:
|
||||||
new_encodings.append(enc)
|
new_encodings.append(enc)
|
||||||
|
|
||||||
# Save this new set to disk
|
# Save this new set to disk
|
||||||
with open(enc_file, "w") as datafile:
|
with open(enc_file, "w") as datafile:
|
||||||
json.dump(new_encodings, datafile)
|
json.dump(new_encodings, datafile)
|
||||||
|
|
||||||
print("Removed model " + builtins.howdy_args.argument)
|
print("Removed model " + id)
|
||||||
|
|
|
||||||
41
src/cli/set.py
Normal file
41
src/cli/set.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
# Set a config value
|
||||||
|
|
||||||
|
# Import required modules
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import builtins
|
||||||
|
import fileinput
|
||||||
|
|
||||||
|
# Get the absolute filepath
|
||||||
|
config_path = os.path.dirname(os.path.abspath(__file__)) + "/../config.ini"
|
||||||
|
|
||||||
|
# Check if enough arguments have been passed
|
||||||
|
if len(builtins.howdy_args.arguments) < 2:
|
||||||
|
print("Please add a setting you would like to change and the value to set it to")
|
||||||
|
print("For example:")
|
||||||
|
print("\n\thowdy set certainty 3\n")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Get the name and value from the cli
|
||||||
|
set_name = builtins.howdy_args.arguments[0]
|
||||||
|
set_value = builtins.howdy_args.arguments[1]
|
||||||
|
|
||||||
|
# Will be filled with the correctly config line to update
|
||||||
|
found_line = ""
|
||||||
|
|
||||||
|
# Loop through all lines in the config file
|
||||||
|
for line in fileinput.input([config_path]):
|
||||||
|
# Save the line if it starts with the requested config option
|
||||||
|
if line.startswith(set_name + " "):
|
||||||
|
found_line = line
|
||||||
|
|
||||||
|
# If we don't have the line it is not in the config file
|
||||||
|
if not found_line:
|
||||||
|
print('Could not find a "' + set_name + '" config option to set')
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Go through the file again and update the correct line
|
||||||
|
for line in fileinput.input([config_path], inplace=1):
|
||||||
|
print(line.replace(found_line, set_name + " = " + set_value + "\n"), end="")
|
||||||
|
|
||||||
|
print("Config option updated")
|
||||||
Loading…
Reference in a new issue