-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
196 lines (156 loc) · 6.26 KB
/
Copy pathmain.py
File metadata and controls
196 lines (156 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Quartermaster
# A supply planning program by sudo-nano
import sys
import argparse
import shlex
import mechanics
import help
import units
import os
# Initialize base argument parser
parser_base = argparse.ArgumentParser(prog="", exit_on_error=False)
parser_base.set_defaults(exit_on_error=False)
subparsers = parser_base.add_subparsers(dest="subcommand", help="subcommand help")
# Scale subcommand takes parameters recipe and amount
parser_scale = subparsers.add_parser(
"scale", aliases=["sc"], help="scale help", exit_on_error=False
)
parser_scale.add_argument("recipe")
parser_scale.add_argument("amount")
# Exit subcommand closes the program
parser_exit = subparsers.add_parser("exit", aliases=["quit", "q"], exit_on_error=False)
# Load subcommand loads a file
parser_load = subparsers.add_parser(
"load", aliases=["lo"], help="load help", exit_on_error=False
)
parser_load.add_argument("type")
parser_load.add_argument("file")
# List subcommand lists all items of the specified type
parser_list = subparsers.add_parser(
"list", aliases=["ls"], help="list help", exit_on_error=False
)
parser_list.add_argument("type")
# Inspect subcommand allows you to inspect any item in the current data set
parser_inspect = subparsers.add_parser(
"inspect", aliases=["i"], help="inspect help", exit_on_error=False
)
parser_inspect.add_argument("type")
parser_inspect.add_argument("item")
# Help subcommand shows help page
parser_help = subparsers.add_parser("help", aliases=["h"], exit_on_error=False)
# Convert command converts between units
# When converting between mass and volume, user must either explicitly specify a density
# or specify an ingredient whose density will be used.
parser_convert = subparsers.add_parser("convert", aliases=["c"], exit_on_error=False)
parser_convert.add_argument("initial")
parser_convert.add_argument("target")
parser_convert.add_argument("-d", "--density")
parser_convert.add_argument("-i", "--ingredient")
# Run the interactive prompt
def prompt(session: mechanics.DataSet):
command = input("quartermaster > ")
try:
args = parser_base.parse_args(shlex.split(command))
execute_command(session, args)
except argparse.ArgumentError as error:
print(error)
def execute_command(session: mechanics.DataSet, args: argparse.Namespace):
# Create string listing valid data types. This will be presented to the user if
# they send an invalid type.
valid_types = str(mechanics.DataType._member_names_)
valid_types_cleaned = valid_types[1 : len(valid_types) - 1].replace("'", "")
match args.subcommand:
case "scale" | "sc":
try:
amount = float(args.amount)
mechanics.calc_and_output(current_session, args.recipe, amount)
except ValueError:
print("Please provide an integer or decimal for amount.")
case "exit" | "quit" | "q":
exit()
case "load" | "lo":
try:
session.load_file(args.file, args.type)
except TypeError:
print(f"Invalid file type. Please choose from {valid_types_cleaned}")
# List ingredients, recipes, or people
case "list" | "ls":
try:
session.list(args.type)
except TypeError:
print(f"Invalid data type. Please choose from {valid_types_cleaned}")
# Inspect an ingredient, recipe, or person
case "inspect" | "i":
# session.item_exists() ensures that an item with the specified name and type exists
# in the current DataSet
if session.debug:
item_type = mechanics.DataType.from_str(args.type, True)
print(f"[DEBUG] inspect command: item_type is {item_type}")
else:
item_type = mechanics.DataType.from_str(args.type)
if session.item_exists(item_type, args.item):
session.inspect(item_type, args.item)
else:
print(
"No item with name "
+ args.item
+ " of type "
+ args.type
+ " exists."
)
# Convert between two unit values.
# When converting between mass and volume, the user must specify either density or
# an ingredient whose density value will be used.
# Not yet implemented. Needs str_to_MassUnit implemented first.
case "convert" | "c":
# Detect units of initial and target values
pass
# List which dataset is active (not yet implemented)
case "active_dataset":
print("Not yet implemented.")
print()
pass
# Export all of specified type (ingredients, recipes, people, entire session) to file
# TODO: Merge all save commands into this one, make it take type as argument
case "save":
print("Not yet implemented.")
print()
case "help":
help.print_help_db()
print()
case _:
print("Invalid command. See 'help' for commands.")
print()
# Initialize session
current_session = mechanics.DataSet()
# Load stock ingredients and recipes
ingredient_path = r"./Stock Datasets/ingredients/"
for name in os.listdir(ingredient_path):
extension = name.split(".")[-1]
if extension in ["toml", "drf"]:
current_session.load_file(ingredient_path + name, "ingredient")
else:
print(f"[INFO] Skipping import of ingredient file {ingredient_path + name} because its extension is invalid")
recipe_path = r"./Stock Datasets/recipes/"
for name in os.listdir(recipe_path):
extension = name.split(".")[-1]
if extension in ["toml", "drf"]:
current_session.load_file(recipe_path + name, "recipe")
else:
print(f"[INFO] Skipping import of recipe file {recipe_path + name} because its extension is invalid")
# Main program loop
match len(sys.argv):
case 1:
pass
case 2:
if "--debug" in sys.argv:
current_session.debug = True
case other:
print(
"Error: Runtime arguments are not supported at this time. They will be implemented in the future."
)
exit()
running = True
while running:
prompt(current_session)
print()