From b285d60fc453ca2f7e7b48f3a34f4ab85c0955d4 Mon Sep 17 00:00:00 2001 From: jpez Date: Fri, 22 Apr 2022 11:11:06 +0100 Subject: [PATCH] added settings file --- OracleSQL.py | 64 +++++++++++++++++++++++++++++++++++++++++++-------- settings.json | 1 + 2 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 settings.json diff --git a/OracleSQL.py b/OracleSQL.py index 8ad9e75..5d398f8 100644 --- a/OracleSQL.py +++ b/OracleSQL.py @@ -1,34 +1,73 @@ import os import subprocess as sp -from time import sleep +import json +import sys -accounts = ["username", "username"] -os.chdir("C://Users/joshu/Documents/Uni/CSY1026/scripts/") +json_file = open("settings.json", "r") +settings = json.load(json_file) +json_file.close() +accounts = settings["accounts"] +script_dir = settings["script_dir"] +password = settings["password"] +json_file = open("settings.json", "w") + +try: + os.chdir(script_dir) +except FileNotFoundError: + print("\nYour script directory path doesn't exist!\n") runFlag = True +def change_settings(): + setting_choice = int(input("\nWhat would you like to do? \nCommands:\n1)Change accounts\n2)Change script dir path\n3)Quit\n>")) + if setting_choice == 1: + normal_account = input("\nWhat is your username for your normal account?\n>") + course_account = input("\nWhat is your username for your coursework account?\n>") + new_password = input("\nWhat is the password for these accounts (This should be your student ID)\n>") + settings["accounts"] = [normal_account, course_account] + settings["password"] = new_password + json.dump(settings, json_file) + elif setting_choice == 2: + new_dir = input("\nWhat is the path to your script directory?\n>") + settings["script_dir"] = new_dir + json.dump(settings, json_file) + else: + if setting_choice != 3: + print("\nIncorrect input!\n") + print("Returning to homepage...") + while runFlag: - account_choice = int(input("\nWhich account would you like to log into?\n1) Normal\n2) Coursework\n>")) + account_choice = int(input("\nWhich account would you like to log into?\n1)Normal\n2)Coursework\n3)Change Settings\n4)Quit\n>")) if (account_choice == 1 or account_choice == 2): runFlag = False + + elif (account_choice == 3): + change_settings() + elif (account_choice == 4): + json.dump(settings, json_file) + json_file.close() + quit() else: print("\nIncorrect Input\n") input("Press Enter to continue...\n") -print("\nHere are your available scripts:\n") files = [f for f in os.listdir(".") if os.path.isfile(f)] runFlag = True while runFlag: - os.chdir("C://Users/joshu/Documents/Uni/CSY1026/scripts/") + os.chdir(script_dir) + print("\n") + print("\nHere are your available scripts:\n") for i in range(0,len(files)): print(files[i][:len(files[i])-4]) - choice = input("\nWhat would you like to do? \nform: 'command file'\n\nCommands:\nedit\nedit folder\nrun\nquit\n> ") + choice = input("\nWhat would you like to do? \nform: 'command file'\n\nCommands:\nedit\nedit folder (open scripts folder)\nrun\nquit\n> ") if (choice == "quit"): runFlag = False + json.dump(settings, json_file) + json_file.close() break try: split_choice = choice.rsplit(" ",1) @@ -47,9 +86,14 @@ while runFlag: elif (command == "run"): os.chdir("C://Oracle12c/") - run_time = sp.Popen(f"sqlplus.exe {accounts[account_choice-1]}@student/password", stdout=sp.PIPE, stdin=sp.PIPE, stderr=sp.PIPE) - print("\n"+run_time.communicate(f"@C://Users/joshu/Documents/Uni/CSY1026/scripts/{current_file}.sql\n".encode())[0].decode()) + run_time = "" + + run_time = sp.Popen(f"sqlplus.exe {accounts[account_choice-1]}@student/{password}", stdout=sp.PIPE, stdin=sp.PIPE, stderr=sp.PIPE) + s = run_time.communicate(f"@{script_dir}{current_file}.sql\n".encode())[0].decode() + s = s[s.find("options")+15:] + s = s[:s.find("SQL>")] + print("\n"+s) input("Press Enter to continue...\n") else: print("\nIncorrect Input\n") - input("Press Enter to continue...\n") + input("Press Enter to continue...\n") \ No newline at end of file diff --git a/settings.json b/settings.json new file mode 100644 index 0000000..e1ed55d --- /dev/null +++ b/settings.json @@ -0,0 +1 @@ +{"accounts": ["CSY1026_109", "CSY1026_309"], "password": "21427656", "script_dir": "C:\\Users\\joshu\\Documents\\Uni\\CSY1026\\scripts\\"} \ No newline at end of file