This repository has been archived on 2023-10-10. You can view files and clone it, but cannot push or open issues/pull-requests.
hackfridge/mainframe/console.py

66 lines
2.1 KiB
Python

import logic
import sys
import shlex
actions = {
"initialize": (logic.initialize, [], True),
"financing-add": (logic.add_financing, [("title", "string"), ("value", "int")], False),
"info": (logic.print_info, [], False),
"topup-add": (logic.add_topup, [("user_id", "string"), ("value", "string")], False),
"purchase-add": (logic.add_purchase, [("user_id", "string"), ("product_id", "strings")], False),
"product-add": (logic.add_product, [("name", "string"), ("code", "string"), ("value", "int"), ("cost", "int")], False),
"restock-add": (logic.add_restock, [("product_id", "string"), ("title", "string"), ("count", "integer")], False),
}
def start():
db = logic.sql_connect()
print "[i] Available actions: quit, %s" % (", ".join(actions))
while True:
sys.stdout.write("> ")
sys.stdout.flush()
line = raw_input().strip()
command = shlex.split(line)[0]
if command == "quit":
break
if command not in actions:
print "[e] No such action."
continue
action = actions[command]
method = action[0]
arguments = action[1]
unsafe = action[2]
if unsafe:
print "[w] Warning! This command is UNSAFE. It will probably BREAK SHIT. Type an uppercase 'yes' to continue."
sys.stdout.write("> ")
sys.stdout.flush()
result = raw_input()
if result != "YES":
print "[e] Make up your mind."
continue
given_arguments = shlex.split(line)[1:]
if len(given_arguments) != len(arguments):
print "[e] Syntax: %s %s" % (command, " ".join([argument[0] for argument in arguments]))
continue
parsed_arguments = []
for i in range(len(arguments)):
given = given_arguments[i]
argument = arguments[i]
t = argument[1]
if t == "int":
given = int(given)
parsed_arguments.append(given)
method(db, *parsed_arguments)
if __name__ == "__main__":
start()