From 689dcc45850603373e5f7d4a790360394287d7e8 Mon Sep 17 00:00:00 2001 From: IceWizard Date: Sat, 22 Nov 2025 17:36:29 +0100 Subject: [PATCH 1/4] Added rename command --- commands/rename.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/commands/rename.py b/commands/rename.py index e69de29..3feb74b 100644 --- a/commands/rename.py +++ b/commands/rename.py @@ -0,0 +1,29 @@ +import os +import typing + +command_help: str = 'Rename a file. Usage: rename ' + + +def run(args: typing.Any) -> None: + if not args: + print('Usage: rename ') + return + + target = args[0] + if os.path.isdir(target): + print('Usage: rename ') + return + else: + try: + with open(target, 'r') as file: + content = file.read() + os.remove(target) + except Exception as e: + print(f'Error: {e}') + return + + try: + with open(args[1], 'w') as file: + file.write(content) + except Exception as e: + print(f'Error: {e}') From 6110e5e1a8c1451032e238e46fe4233c58e86b16 Mon Sep 17 00:00:00 2001 From: IceWizard Date: Sat, 22 Nov 2025 17:37:32 +0100 Subject: [PATCH 2/4] Updated code style, type annotations, no overiding built-ins --- boot.py | 7 ++++--- commands/cat.py | 11 ++++++----- commands/cd.py | 11 +++++++---- commands/clear.py | 9 ++++++--- commands/del.py | 13 ++++++++----- commands/exit.py | 11 +++++++---- commands/help.py | 13 +++++++------ commands/ls.py | 9 ++++++--- commands/mkdir.py | 11 +++++++---- commands/mkfil.py | 10 ++++++---- commands/printdir.py | 9 ++++++--- commands/whoami.py | 11 ++++++++--- 12 files changed, 78 insertions(+), 47 deletions(-) diff --git a/boot.py b/boot.py index 65ff06d..8f3adbc 100644 --- a/boot.py +++ b/boot.py @@ -1,12 +1,13 @@ import os import importlib import pkgutil +import types COMMANDS_PACKAGE = 'commands' -def load_commands(): - commands = {} +def load_commands() -> dict[str, types.ModuleType]: + commands: dict[str, types.ModuleType] = {} package = importlib.import_module(COMMANDS_PACKAGE) for loader, name, is_pkg in pkgutil.iter_modules(package.__path__): if name.startswith('_'): @@ -17,7 +18,7 @@ def load_commands(): return commands -def main(): +def main() -> None: os.system('cls' if os.name == 'nt' else 'clear') print('Booting PyOS...') commands = load_commands() diff --git a/commands/cat.py b/commands/cat.py index a210f17..d57f0ee 100644 --- a/commands/cat.py +++ b/commands/cat.py @@ -1,13 +1,14 @@ -# WAFCAT :heart: +import typing -help = "Print file contents. Usage: cat " +command_help: str = 'Print file contents. Usage: cat ' -def run(args): + +def run(args: typing.Any) -> None: if not args: - print("Usage: cat ") + print('Usage: cat ') return try: with open(args[0], 'r') as f: print(f.read()) except Exception as e: - print(f"Error: {e}") \ No newline at end of file + print(f'Error: {e}') diff --git a/commands/cd.py b/commands/cd.py index edeac91..bcee7c7 100644 --- a/commands/cd.py +++ b/commands/cd.py @@ -1,11 +1,14 @@ -help = "Change directory. Usage: cd " +import typing -def run(args): +command_help: str = 'Change directory. Usage: cd ' + + +def run(args: typing.Any) -> None: import os if not args: - print("Usage: cd ") + print('Usage: cd ') return try: os.chdir(args[0]) except Exception as e: - print(f"Error: {e}") \ No newline at end of file + print(f'Error: {e}') diff --git a/commands/clear.py b/commands/clear.py index 81a82cb..976c908 100644 --- a/commands/clear.py +++ b/commands/clear.py @@ -1,5 +1,8 @@ -help = "Clear the screen." +import typing -def run(args): +command_help: str = 'Clear the screen.' + + +def run(_args: typing.Any) -> None: import os - os.system('cls' if os.name == 'nt' else 'clear') \ No newline at end of file + os.system('cls' if os.name == 'nt' else 'clear') diff --git a/commands/del.py b/commands/del.py index 6dcd741..a02e5bc 100644 --- a/commands/del.py +++ b/commands/del.py @@ -1,18 +1,21 @@ -help = "Delete file or directory. Usage: del " +import typing -def run(args): +command_help: str = 'Delete file or directory. Usage: del ' + + +def run(args: typing.Any) -> None: import os if not args: - print("Usage: del ") + print('Usage: del ') return target = args[0] if os.path.isdir(target): try: os.rmdir(target) except Exception as e: - print(f"Error: {e}") + print(f'Error: {e}') else: try: os.remove(target) except Exception as e: - print(f"Error: {e}") \ No newline at end of file + print(f'Error: {e}') diff --git a/commands/exit.py b/commands/exit.py index b7543ca..e09a0dd 100644 --- a/commands/exit.py +++ b/commands/exit.py @@ -1,5 +1,8 @@ -help = "Exit PyOS." +import typing -def run(args): - print("Goodbye!") - exit(0) \ No newline at end of file +command_help: str = 'Exit PyOS.' + + +def run(_args: typing.Any) -> None: + print('Goodbye!') + exit(0) diff --git a/commands/help.py b/commands/help.py index b35a63b..5ce2a5d 100644 --- a/commands/help.py +++ b/commands/help.py @@ -1,12 +1,13 @@ -import os +import typing -help = """Show help for all commands.""" +command_help: str = 'Show help for all commands.' -def run(args): + +def run(_args: typing.Any) -> None: from boot import load_commands commands = load_commands() - print("Available commands:") + print('Available commands:') for name in sorted(commands): mod = commands[name] - doc = getattr(mod, "help", "") - print(f" {name:<10} {doc}") \ No newline at end of file + doc = getattr(mod, 'command_help', '') + print(f' {name:<10} {doc}') diff --git a/commands/ls.py b/commands/ls.py index 5362f56..6a8044d 100644 --- a/commands/ls.py +++ b/commands/ls.py @@ -1,6 +1,9 @@ -help = "List files in current directory." +import typing -def run(args): +command_help: str = 'List files in current directory.' + + +def run(_args: typing.Any) -> None: import os for f in os.listdir(): - print(f) \ No newline at end of file + print(f) diff --git a/commands/mkdir.py b/commands/mkdir.py index 3292439..2656e8a 100644 --- a/commands/mkdir.py +++ b/commands/mkdir.py @@ -1,11 +1,14 @@ -help = "Create a directory. Usage: mkdir " +import typing -def run(args): +command_help: str = 'Create a directory. Usage: mkdir ' + + +def run(args: typing.Any) -> None: import os if not args: - print("Usage: mkdir ") + print('Usage: mkdir ') return try: os.mkdir(args[0]) except Exception as e: - print(f"Error: {e}") \ No newline at end of file + print(f'Error: {e}') diff --git a/commands/mkfil.py b/commands/mkfil.py index b9518b5..07ba5ea 100644 --- a/commands/mkfil.py +++ b/commands/mkfil.py @@ -1,11 +1,13 @@ -help = "Create an empty file. Usage: mkfil " +import typing +command_help: str = 'Create an empty file. Usage: mkfil ' -def run(args): + +def run(args: typing.Any) -> None: if not args: - print("Usage: mkfil ") + print('Usage: mkfil ') return try: open(args[0], 'a').close() except Exception as e: - print(f"Error: {e}") + print(f'Error: {e}') diff --git a/commands/printdir.py b/commands/printdir.py index 0f7f281..593e255 100644 --- a/commands/printdir.py +++ b/commands/printdir.py @@ -1,5 +1,8 @@ -help = "Print working directory." +import typing -def run(args): +command_help: str = 'Print working directory.' + + +def run(_args: typing.Any) -> None: import os - print(os.getcwd()) \ No newline at end of file + print(os.getcwd()) diff --git a/commands/whoami.py b/commands/whoami.py index 045050a..8c7a72c 100644 --- a/commands/whoami.py +++ b/commands/whoami.py @@ -1,3 +1,8 @@ -def run(args): - import getpass - print(getpass.getuser()) +import typing + +command_help: str = 'Get current user' + + +def run(_args: typing.Any) -> None: + import getpass + print(getpass.getuser()) From d1dbfc3e585e24256533a0394197ac4dec7e0829 Mon Sep 17 00:00:00 2001 From: IceWizard Date: Sun, 23 Nov 2025 19:05:04 +0100 Subject: [PATCH 3/4] Improved rename.py logic --- commands/rename.py | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/commands/rename.py b/commands/rename.py index 3feb74b..c1d5dd8 100644 --- a/commands/rename.py +++ b/commands/rename.py @@ -9,21 +9,8 @@ def run(args: typing.Any) -> None: print('Usage: rename ') return - target = args[0] - if os.path.isdir(target): + if len(args) != 2: print('Usage: rename ') return - else: - try: - with open(target, 'r') as file: - content = file.read() - os.remove(target) - except Exception as e: - print(f'Error: {e}') - return - try: - with open(args[1], 'w') as file: - file.write(content) - except Exception as e: - print(f'Error: {e}') + os.rename(args[0], args[1]) From f79215c2b2ddfc634dc7a64c966d55aae043bcd1 Mon Sep 17 00:00:00 2001 From: IceWizard Date: Sun, 23 Nov 2025 19:05:55 +0100 Subject: [PATCH 4/4] Improved rename.py logic --- commands/rename.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/commands/rename.py b/commands/rename.py index c1d5dd8..f198c5a 100644 --- a/commands/rename.py +++ b/commands/rename.py @@ -13,4 +13,7 @@ def run(args: typing.Any) -> None: print('Usage: rename ') return - os.rename(args[0], args[1]) + try: + os.rename(args[0], args[1]) + except Exception as e: + print(f'Error: {e}')