Add path support in config.mash

This commit is contained in:
Maxwell Jeffress 2024-07-15 10:03:44 +10:00
parent 0703cfc3dc
commit 745b3ae02f

211
main.py
View File

@ -13,16 +13,14 @@ from os.path import expanduser
# Some initial variables # Some initial variables
version = "0.1.0" version = "0.2.0"
user = os.getlogin() user = os.getlogin()
initDirectory = expanduser("~") initDirectory = expanduser("~")
currentDirectory = initDirectory
argList = ["", ""] argList = ["", ""]
# Start work on checking whether a config file exists # Start work on checking whether a config file exists
if os.path.isfile(expanduser("~") + "/.config/mash/mash.conf") == False: if os.path.isfile(expanduser("~") + "/.config/mash/mash.conf") == False:
print("Config file does not exist. Copying default file to ~/.config/mash/mash.conf") print("Config file does not exist. Make a file at .config/mash/mash.conf to configure things")
shutil.copyfile("/etc/mash/mash.conf", (expanduser("~") + "/.config/mash/mash.conf"))
# Parse config file, adding variables # Parse config file, adding variables
configFileFile = open((expanduser("~") + "/.config/mash/mash.conf")) configFileFile = open((expanduser("~") + "/.config/mash/mash.conf"))
@ -50,7 +48,19 @@ for word in confArgList:
for word in confArgList: for word in confArgList:
if word == "motd:": if word == "motd:":
motd = confArgList[confArgList.index(word) + 1] motd = confArgList[confArgList.index(word) + 1]
if word == ",mashPath:":
mashPathStr = (confArgList[confArgList.index(word) + 1])
pathStrIndex = 0
mashPath = [""]
for letter in mashPathStr:
if letter == ";":
pathStrIndex = pathStrIndex + 1
mashPath.append("")
else:
try:
mashPath[pathStrIndex] = mashPath[pathStrIndex] + letter
except Exception:
mashPath.append(letter)
# Write the time to the history file # Write the time to the history file
historyFile = open((expanduser("~") + "/.history.mash"), "a") historyFile = open((expanduser("~") + "/.history.mash"), "a")
historyFile.write("\n") historyFile.write("\n")
@ -59,103 +69,112 @@ historyFile.write("\n")
# Welcome message. Configurable with "motd: (your motd here)," # Welcome message. Configurable with "motd: (your motd here),"
print(motd) print(motd)
os.chdir(initDirectory)
currentDirectory = initDirectory
# Main loop. # Main loop.
try: def mainLoop(startDir):
while True: try:
# Set directory and prompt for input. currentDirectory = startDir
os.chdir(currentDirectory) while True:
command = input(Fore.GREEN + user + Fore.WHITE + "@" + socket.gethostname() + Fore.BLUE + " " + currentDirectory + Fore.WHITE + " > ") # Set directory and prompt for input.
os.chdir(currentDirectory)
command = input(Fore.GREEN + user + Fore.WHITE + "@" + socket.gethostname() + Fore.BLUE + " " + currentDirectory + Fore.WHITE + " > ")
# Clean some things up for the argument parser. # Clean some things up for the argument parser.
argNumber = 0 argNumber = 0
argList.clear() argList.clear()
# Argument parser # Argument parser
for letter in command: for letter in command:
if letter == " ": if letter == " ":
argNumber = argNumber + 1 argNumber = argNumber + 1
else: else:
try:
argList[argNumber] = argList[argNumber] + letter
except Exception:
argList.append(letter)
# Start work on pipes.
for word in argList:
if word == "&&":
print("This hasnt been implemented yet hahahahah")
elif word == "&":
print("This hasnt been implemented yet heheheheh")
elif word == "|":
print("What are you? A pipe master? We dont like those people here")
# Built in commands section
if argList[0] == "help":
print("This is Mash, Max's Shell. Mash has a few built in commands, which you can use to run commands, view files and interact with your system.")
print("Some commands you can run:")
print("")
print("cd Change your directory.")
print("history See previously ran commands.")
print("mat View contents of a file. If you're on a Unix-like OS, use 'cat' instead.")
print("mls View all files in a directory. If you're on a Unix-like OS, use 'ls' instead.")
print("version Print current version of Mash.")
print("exit Exit Mash.")
print("")
print("You can also run commands installed by your system. See your OS's documentation for more information.")
elif argList[0] == "exit":
exit()
elif argList[0] == "version":
print(version)
elif argList[0] == "mat":
matFile = open(argList[1])
print(matFile.read())
elif argList[0] == "mls":
try: try:
argList[argNumber] = argList[argNumber] + letter if argList[1] == "":
except Exception: print(os.listdir(currentDirectory))
argList.append(letter)
# Start work on pipes.
for word in argList:
if word == "&&":
print("This hasnt been implemented yet hahahahah")
elif word == "&":
print("This hasnt been implemented yet heheheheh")
elif word == "|":
print("What are you? A pipe master? We dont like those people here")
# Built in commands section
if argList[0] == "help":
print("This is Mash, Max's Shell. Mash has a few built in commands, which you can use to run commands, view files and interact with your system.")
print("Some commands you can run:")
print("")
print("cd Change your directory.")
print("history See previously ran commands.")
print("mat View contents of a file. If you're on a Unix-like OS, use 'cat' instead.")
print("mls View all files in a directory. If you're on a Unix-like OS, use 'ls' instead.")
print("version Print current version of Mash.")
print("exit Exit Mash.")
print("")
print("You can also run commands installed by your system. See your OS's documentation for more information.")
elif argList[0] == "exit":
exit()
elif argList[0] == "version":
print(version)
elif argList[0] == "mat":
matFile = open(argList[1])
print(matFile.read())
elif argList[0] == "mls":
try:
if argList[1] == "":
print(os.listdir(currentDirectory))
else:
argList[1] = mlsDirectory
if mlsDirectory[0] == "/":
print(os.listdir(mlsDirectory))
else: else:
print(os.listdir(currentDirectory + "/" + mlsDirectory)) argList[1] = mlsDirectory
except Exception: if mlsDirectory[0] == "/":
print(os.listdir(currentDirectory)) print(os.listdir(mlsDirectory))
elif argList[0] == "cd": else:
try: print(os.listdir(currentDirectory + "/" + mlsDirectory))
directoryForCD = argList[1] except Exception:
if directoryForCD[0] == "~": print(os.listdir(currentDirectory))
currentDirectory = expanduser("~") + directoryForCD.replace("~", "") elif argList[0] == "cd":
if directoryForCD[0] == "/": try:
currentDirectory = directoryForCD directoryForCD = argList[1]
else: if directoryForCD[0] == "~":
currentDirectory = (currentDirectory + "/" + directoryForCD) currentDirectory = expanduser("~") + directoryForCD.replace("~", "")
except Exception: if directoryForCD[0] == "/":
print("Either a directory was not provided or an error occured.") currentDirectory = directoryForCD
elif argList[0] == "history": else:
historyReadFile = open((expanduser("~") + "/.history.mash")) currentDirectory = (currentDirectory + "/" + directoryForCD)
print(historyReadFile.read()) except Exception:
elif argList[0] == "mtime": print("Either a directory was not provided or an error occured.")
print(datetime.now()) elif argList[0] == "history":
elif argList[0] == "printconf": historyReadFile = open((expanduser("~") + "/.history.mash"))
print(configFile) print(historyReadFile.read())
# If a built in command isn't what you wanted, run a new subprocess. elif argList[0] == "mtime":
else: print(datetime.now())
try: elif argList[0] == "printconf":
subprocess.run(argList) print(configFile)
except Exception: # If a built in command isn't what you wanted, run a new subprocess.
print("Command", command, "either doesn't exist, isn't in the path or isn't in this directory.") else:
for word in mashPath:
if os.path.isfile(word + "/" + argList[0]):
argList[0] = (word + "/" + argList[0])
try:
subprocess.run(argList)
except Exception:
print("Command", command, "either doesn't exist, isn't in the path or isn't in this directory.")
# Write to the history file # Write to the history file
historyFile = open((expanduser("~") + "/.history.mash"), "a") historyFile = open((expanduser("~") + "/.history.mash"), "a")
historyFile.write(command) historyFile.write(command)
historyFile.write("\n") historyFile.write("\n")
historyFile.close() historyFile.close()
# Handle errors in the main loop # Handle errors in the main loop
except Exception: #except Exception:
print("Looks like there was an error. Exiting...") #print("Looks like there was an error. Exiting...")
# Handle ^C # Handle ^C
except KeyboardInterrupt: except KeyboardInterrupt:
print(" detected! Exiting...") print("\n")
mainLoop(currentDirectory)
mainLoop(initDirectory)