From 00690e8cfac2648d6aac953605d15aac1105b9aa Mon Sep 17 00:00:00 2001 From: OusmBlueNinja Date: Sat, 26 Aug 2023 23:05:17 -0500 Subject: [PATCH] modified: packages/update.py --- packages - Copy/builtin.py | 133 +++++++++++++++++++++++++++++++++++++ packages - Copy/echo.py | 4 ++ packages - Copy/nano.py | 71 ++++++++++++++++++++ packages - Copy/netget.py | 24 +++++++ packages - Copy/pip.py | 21 ++++++ packages - Copy/update.py | 33 +++++++++ packages/update.py | 11 ++- 7 files changed, 295 insertions(+), 2 deletions(-) create mode 100644 packages - Copy/builtin.py create mode 100644 packages - Copy/echo.py create mode 100644 packages - Copy/nano.py create mode 100644 packages - Copy/netget.py create mode 100644 packages - Copy/pip.py create mode 100644 packages - Copy/update.py diff --git a/packages - Copy/builtin.py b/packages - Copy/builtin.py new file mode 100644 index 0000000..aa96173 --- /dev/null +++ b/packages - Copy/builtin.py @@ -0,0 +1,133 @@ +#["builtin", "packages.builtin", ["echo", "ls", "rm", "clear", "cd", "ll"]] +import os +import stat + +if os.name != 'nt': + import pwd + import grp +import time + + + + +def echo(message: list) -> int: + try: + print(" ".join(message)) + except: + return + +def ls(args): + del args + + + pass + +def rm(files: list): + for file in files: + os.remove(file) + +def clear(args): + del args + os.system('cls' if os.name == 'nt' else 'clear') + + + +def cd(args): + if len(args) == 0 or args[0] == "~": + new_path = os.path.expanduser("~") # Get the home directory path + elif len(args) == 1: + new_path = os.path.abspath(args[0]) # Get the absolute path + else: + print("Usage: cd or cd ~") + return + + try: + os.chdir(new_path) # Change the current working directory + except FileNotFoundError: + print("Directory not found.") + except Exception as e: + print(f"An error occurred: {e}") + + + +def ls(args): + del args # You can remove this line if you're not using args + + blue_color = '\033[34m' + green_color = '\033[32m' + white_color = '\033[37m' + reset_color = '\033[0m' + + dir_path = os.getcwd() + try: + with os.scandir(dir_path) as entries: + for entry in entries: + if not entry.name.startswith('.'): + file_path = os.path.join(dir_path, entry.name) + file_stat = os.stat(file_path) + if stat.S_ISDIR(file_stat.st_mode): + print(blue_color + entry.name + '/' + reset_color, end=' ') + elif file_stat.st_mode & stat.S_IXUSR: + print(green_color + entry.name + '*' + reset_color, end=' ') + else: + print(white_color + entry.name + reset_color, end=' ') + print() # Newline after listing + except Exception as e: + print(f"An error occurred: {e}") + + +def ll(args): + if os.name != 'nt': + print("This command only works for linux") + return + dir_path = os.getcwd() + + green_color = '\033[32m' + blue_color = '\033[34m' + whiteColor = '\033[37m' + orange_color = '\033[33m' + reset_color = '\033[0m' + + try: + with os.scandir(dir_path) as entries: + for entry in entries: + file_stat = entry.stat() + file_name = entry.name + + if os.name != 'nt': + pw = pwd.getpwuid(file_stat.st_uid) + gr = grp.getgrgid(file_stat.st_gid) + + is_hidden = (file_name and file_name[0] == '.') + + time_str = time.ctime(file_stat.st_mtime) + if time_str.endswith('\n'): + time_str = time_str[:-1] # Remove newline character + + permission_str = ( + (green_color + "r" if file_stat.st_mode & stat.S_IRUSR else whiteColor + "-") + + (blue_color + "w" if file_stat.st_mode & stat.S_IWUSR else whiteColor + "-") + + ((orange_color if is_hidden else green_color) + "x" if file_stat.st_mode & stat.S_IXUSR else whiteColor + "-") + + (green_color + "r" if file_stat.st_mode & stat.S_IRGRP else whiteColor + "-") + + (blue_color + "w" if file_stat.st_mode & stat.S_IWGRP else whiteColor + "-") + + ((orange_color if is_hidden else green_color) + "x" if file_stat.st_mode & stat.S_IXGRP else whiteColor + "-") + + (green_color + "r" if file_stat.st_mode & stat.S_IROTH else whiteColor + "-") + + (blue_color + "w" if file_stat.st_mode & stat.S_IWOTH else whiteColor + "-") + + ((orange_color if is_hidden else green_color) + "x" if file_stat.st_mode & stat.S_IXOTH else whiteColor + "-") + ) + + print( + "{:<11}{:>4} {:<8} {:<8} {:>8} {} {}{}".format( + permission_str, + file_stat.st_nlink, + pw.pw_name, + gr.gr_name, + file_stat.st_size, + time_str, + (blue_color if entry.is_dir() else (orange_color if is_hidden else green_color) if file_stat.st_mode & stat.S_IXUSR else whiteColor), + file_name, + reset_color + ) + ) + except Exception as e: + print(f"An error occurred: {e}") diff --git a/packages - Copy/echo.py b/packages - Copy/echo.py new file mode 100644 index 0000000..7d025a5 --- /dev/null +++ b/packages - Copy/echo.py @@ -0,0 +1,4 @@ +#["echo", "packages.echo", ["out"]] + +def out(message): + print(" ".join(message)) \ No newline at end of file diff --git a/packages - Copy/nano.py b/packages - Copy/nano.py new file mode 100644 index 0000000..6fc84aa --- /dev/null +++ b/packages - Copy/nano.py @@ -0,0 +1,71 @@ +#["nano", "packages.nano", ["nano"]] + +import os + +class TextEditor: + def __init__(self): + self.lines = [] + self.file = "" + + def load_file(self, filename): + try: + with open(filename, "r") as file: + self.lines = file.readlines() + except FileNotFoundError: + print(f"File '{filename}' not found.") + + def save_file(self, filename): + if filename == "": + filename = self.file + with open(filename, "w") as file: + file.writelines(self.lines) + + def edit(self): + while True: + os.system('cls' if os.name == 'nt' else 'clear') + self.display() + + action = input("\nCommands: [e]dit, [n]ewline, [s]ave, [q]uit: ").lower() + + if action == "e": + self.edit_lines() + elif action == "n": + self.newline() + elif action == "s": + filename = input("Enter filename to save: ") + self.save_file(filename) + print(f"File '{filename}' saved.") + elif action == "q": + print("Exiting the text editor.") + break + else: + print("Invalid command.") + + def display(self): + print("Editing: "+self.file) + for i, line in enumerate(self.lines): + print(f"{i + 1}: {line}", end="") + + def newline(self): + self.lines.append("\n") + + def edit_lines(self): + line_number = int(input("Enter line number to edit: ")) - 1 + if 0 <= line_number < len(self.lines): + new_line = input("Enter new text: ") + self.lines[line_number] = new_line + "\n" + else: + print("Invalid line number.") + + +def nano(args): + if len(args) != 1: + print("Usage: nano ") + else: + os.system('cls' if os.name == 'nt' else 'clear') + filename = args[0] + + editor = TextEditor() + editor.file = filename + editor.load_file(filename) + editor.edit() \ No newline at end of file diff --git a/packages - Copy/netget.py b/packages - Copy/netget.py new file mode 100644 index 0000000..0a1b32c --- /dev/null +++ b/packages - Copy/netget.py @@ -0,0 +1,24 @@ +#["netget", "packages.netget", ["netget"]] +# Made By OusmeBlueNinja +import os, sys + + +def netget(command: list): + + print(command, len(command)) + if len(command) != 2: + print("comand requires [ url ] [ path ]") + return + + url = command[0] + location = command[1] + try: + import wget + wget.download(url, location) + except ImportError: + os.system(f"wget {url} -O {location}") + + except: + print("cannot download") + + diff --git a/packages - Copy/pip.py b/packages - Copy/pip.py new file mode 100644 index 0000000..c4f77fd --- /dev/null +++ b/packages - Copy/pip.py @@ -0,0 +1,21 @@ +#["pip", "packages.pip", ["pip"]] +# Made By Blurple +import os, sys + +def pip(command: list): + + #print(command, len(command)) + if len(command) != 1: + print("comand requires [ package_name ] [ args (optional) ] ") + return + + package = command[0] + try: + print("Running pip from shell") + os.system(f"python -m pip install {package}") + except Exception as e: + print(e) + + + except: + print("cannot download") \ No newline at end of file diff --git a/packages - Copy/update.py b/packages - Copy/update.py new file mode 100644 index 0000000..e4ff3c9 --- /dev/null +++ b/packages - Copy/update.py @@ -0,0 +1,33 @@ +#["upd", "packages.update", ["upd"]] +# Made By Blurple +import os, sys, requests, threading + +fileList = [ + "https://raw.githubusercontent.com/OusmBlueNinja/TermPY/main/packages/builtin.py", + "https://raw.githubusercontent.com/OusmBlueNinja/TermPY/main/packages/echo.py", + "https://raw.githubusercontent.com/OusmBlueNinja/TermPY/main/packages/nano.py", + "https://raw.githubusercontent.com/OusmBlueNinja/TermPY/main/packages/netget.py", + "https://raw.githubusercontent.com/OusmBlueNinja/TermPY/main/packages/pip.py", + "https://raw.githubusercontent.com/OusmBlueNinja/TermPY/main/packages/upd.py" +] + +def download_and_save(url): + r = requests.get(url) + if 'Requests:' in r.text: + print(r.text) + print(r.headers['Content-Type']) + fileInstallingCreator = os.path.join(os.path.dirname(os.path.realpath(__file__)), os.path.basename(url)) + with open(fileInstallingCreator, "w") as newFile: + newFile.write(r.text) + +def upd(command): + input(f"Settings: {fileList}, {os.path.dirname(os.path.realpath(__file__))}\nPress enter to continue") + + threads = [] + for url in fileList: + thread = threading.Thread(target=download_and_save, args=(url,)) + threads.append(thread) + thread.start() + + for thread in threads: + thread.join() diff --git a/packages/update.py b/packages/update.py index e4ff3c9..74b15ed 100644 --- a/packages/update.py +++ b/packages/update.py @@ -23,11 +23,18 @@ def download_and_save(url): def upd(command): input(f"Settings: {fileList}, {os.path.dirname(os.path.realpath(__file__))}\nPress enter to continue") + print("Downloading and saving files:") + threads = [] for url in fileList: thread = threading.Thread(target=download_and_save, args=(url,)) threads.append(thread) thread.start() - for thread in threads: - thread.join() + # Display loading animation + while any(thread.is_alive() for thread in threads): + for _ in range(len(threads)): + print(".", end="", flush=True) + time.sleep(0.5) + + print("\nAll files downloaded and saved!")