-
Notifications
You must be signed in to change notification settings - Fork 1
/
scriptparser.py
87 lines (71 loc) · 3.51 KB
/
scriptparser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from boxEngine import *
from configparser import ConfigParser
import pathlib
import os
import sys
import time
import glob
config = ConfigParser()
config.read("config/cmds.ini")
class RemoteScriptBuilder:
"""
Script Parser:
Takes a user script and implements it into boxpyshell (if all goes well)
All params are dealt with in the class so no need to specify them when calling the run function
"""
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:
print(f"Combing ('{self.files}')")
os.system("clear")
os.system("cls")
if self.files == name:
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()
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):
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})"
with open(".\\boxpyshell.py", "w") as file:
file.write(new_contents)
def add_file_to_list(self, name) -> None:
with open("_scripts.txt", "w") as file:
if os.path.exists(name) is True and name[-3:] == ".py":
file.write(f"{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?)")
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
try:
if os.path.exists("scripts") is True:
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:
os.mkdir("scripts")
with open(f".\\scripts\\{self.name}", "w") as file:
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}")
def run(self) -> None:
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? -> "))
if __name__ == "__main__":
main = RemoteScriptBuilder()
main.run()
# This is a parser for scripts that users can implement into boxpyshell
# You can make your own python script and implement it into the "scripts" folder.
# One step closer to release. 2023 might just be the year!