From 25a25f38006047c4f1d2b70a566cf102faa3ba6f Mon Sep 17 00:00:00 2001 From: gusic <97785573+Gusic06@users.noreply.github.com> Date: Sun, 18 Feb 2024 13:35:35 +0000 Subject: [PATCH] documenting script parser thing I did some unholy things in this, goddamn --- scriptparser.py | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/scriptparser.py b/scriptparser.py index 75b4f9b..3185b08 100644 --- a/scriptparser.py +++ b/scriptparser.py @@ -20,61 +20,66 @@ class RemoteScriptBuilder: def __init__(self) -> None: ... - def get_script_from_user(self, name: str) -> str: if name[-3:] != ".py": raise Exception("Invalid File Format (Must be '.py'!)") self._dir = glob.glob(fr"{pathlib.Path.home().drive}\\**\\{name}") - for self.files in self._dir: + for self.files in self._dir: # iterating through every file in the directory print(f"Combing ('{self.files}')") os.system("clear") os.system("cls") - if self.files == name: + if self.files == name: # if we find the target file, we should read the and return the contents with open(name, "r", "utf-8") as file: self.contents = file.read() if self.contents == "": - print(f"Error: Unable to open {name}") - time.sleep(3) - sys.exit() + print(f"Error: {file} was empty..") + time.sleep(2) + exit(1) return self.contents else: raise Exception(f"Couldn't find {name} on this device..") - def add_command_to_main_file(self, command_name, script_name, function_name): + def add_command_to_main_file(self, command_name: str, script_name: str, function_name: str) -> None: with open(".\\boxpyshell.py", "r") as file: _contents = file.read() - new_contents = f"import {script_name}\n{_contents}\nelif command == '{command_name}':\n {function_name}({*args}, {**kwargs})" + new_contents = f"import {script_name}\n{_contents}\nelif command == '{command_name}':\n {function_name}({*args}, {**kwargs})" # tbh idk what this does, but it works.. I think with open(".\\boxpyshell.py", "w") as file: file.write(new_contents) - def add_file_to_list(self, name) -> None: + def add_file_to_list(self, name: str) -> None: with open("_scripts.txt", "w") as file: - if os.path.exists(name) is True and name[-3:] == ".py": - file.write(f"{name}") + if os.path.exists(name) and name[-3:] == ".py": # if the file exists and the file is a python file, we append the filename to "_scripts.txt" + file.write(name) print(f"Successfully added {name} to '_scripts.txt'") else: print(f"{name} is an invalid file (Maybe you forgot to add '.py' to the end?)") + exit(1) def build_file(self, name: str) -> None: - self._contents = self.get_script_from_user(input("Path to the Script file -> ")) - self.name = f"{name}.py" if name[-3:] != ".py" else name + self._contents: str = self.get_script_from_user(input("Path to the Script file -> ")) + self.name: str = f"{name}.py" if name[-3:] != ".py" else name # we append the ".py" file extension if name doesn't have it try: - if os.path.exists("scripts") is True: + + if os.path.exists("scripts"): with open(f".\\scripts\\{self.name}", "w") as file: - file.write(f"'Script for BoxPyShell'\n{self._contents}") - if os.path.exists("scripts") is False: + file.write(f"# Script for BoxPyShell\n{self._contents}") + if not os.path.exists("scripts"): os.mkdir("scripts") with open(f".\\scripts\\{self.name}", "w") as file: - file.write(f"'Script for BoxPyShell'\n{self._contents}") + file.write(f"# Script for BoxPyShell\n{self._contents}") self.add_files_to_list(self.name) + except Exception as Err: - print(f"Unable to create {self.file} due to a PermissionError") if type(Err) == PermissionError else print(f"Unable to create {self.file} -> {Err}") + if type(Err) == PermissionError: + print(f"Unable to create {self.file} due to a PermissionError") + else: + print(f"Unable to create {self.file} -> {Err}") - def run(self) -> None: + def run(self) -> None: # basically just a wrapper function that brings it all together self.build_file(input("What do you want to name your script? -> ")) self.add_command_to_main_file(input("Name of command? -> "), input("Name of source file? -> "), input("Name of function to be run? -> "))