Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

documenting script parser thing #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions scriptparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't know then don't comment it. This is unnecessary.

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 -> "))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to set the type then instead of doing ```
var: str = ...

Rather do

var = str(input())

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

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? -> "))

Expand Down