def from_terminus(inpt, info): """Function the terminus core program uses to interface with set-up.""" commands = {"user": new_user, "table": new_table, "column": new_column} for el in inpt: session.clear_screen() commands[el](connection=info[0])
def landing(info): """Loop which queries user for commands to run on the DB.""" not_done = True while not_done: session.clear_screen() command = input(info[2] + "@" + info[1] + ": ") not_done = run_command(command, info)
def get_user(): """Gathers user info about the new user.""" not_match = True while not_match: session.clear_screen() user = input(theme.NEW_USER) password = getpass(theme.NEW_PW) confirm = getpass(theme.CONFIRM_PW) match = False if password == confirm else True return user, password
def main(): """Landing point for set-up when entered directly from command line.""" session.title() session.clear_screen() print(theme.TITLE) print(theme.SETUP_MSG) input(theme.PAUSE) session.clear_screen() print(theme.SQLITE3) check = input(theme.HAS_SQLITE3) if check.lower() == "n": exit() get_command()
def get_command(): """Loop functions for users entering program directly from command line.""" cont = True commands = { "user": new_user, "table": new_table, "column": new_column, "help": help, "exit": close_out } while cont: session.clear_screen() print(theme.VERSION + theme.SETUP_OPTIONS) cmd = input(theme.CHEVRON) print() cmd = "help" if cmd not in commands.keys() else cmd commands[cmd]()
def upload(inpt, info): """Uploads a CSV or TSV into the current DB host table.""" # loops through the file counting rows file_len = 0 for line in open(inpt[0], "r"): file_len += 1 # sets the row processing function depending on file format funct = (lambda r: "', '".join(prepare_row(r))) if inpt[0][-3:] == "csv" else (lambda r: r.replace("\t", "', '")) with open(inpt[0], "r") as infile: row = infile.readline().strip("\n") curr = 1 while row: # adjusts statics.PROGRESS if there is a change in terminal size while running session.terminal_size() # processes progress ratios ratio = curr / file_len progress = ceil(ratio * theme.PROGRESS) ratio = ceil(ratio * 100) row = row.replace("'", "''") row = funct(row) # prints the progress bar session.clear_screen() print(info[2] + "@" + info[1] + ": upload " + " ".join(inpt)) print("\n\n |" + ("#" * progress) + (" " * (theme.PROGRESS - progress)) + "| " + str(ratio) + "%") print("\n '" + row + "'") sql_query = "INSERT INTO " + info[1] + "(" + ", ".join(theme.HOST_SET[info[1]]) + ")" \ " VALUES('" + row + "')" query.execute_sql(info[0], sql_query) curr += 1 row = infile.readline().strip("\n") info[0].commit()
def table(operation, connection): """Gathers user input for altering or creating a table.""" not_finished = True while not_finished: session.clear_screen() print(theme.TABLE) table = input(theme.NEW_TABLE) confirm = input(theme.CONFIRM_TABLE) not_finished = False if table == confirm else True not_finished = True while not_finished: session.clear_screen() print(theme.COLUMN_TEXT) column_space = [] redo = False while not_finished: col = [] for text in theme.COLUMN_GET: col.append(input(text)) if col[-1] == theme.END: not_finished = False break elif col[-1] == theme.REDO: redo = True break if redo: break if operation == "alter": not_finished = False column_space.append(col) print() session.clear_screen() if operation == "alter": alter_table(table, column_space, connection) print(theme.adjust(table, operation) + theme.EXPLAIN2) else: create_table(table, column_space[:-1], connection) print(theme.adjust(table, operation) + theme.EXPLAIN1) input(theme.PAUSE)
def help(): """Prints set-up help text.""" session.clear_screen() print(theme.SETUP_HELP) input(theme.PAUSE)
def close_out(): """Safely exits set-up.""" session.clear_screen() exit()