Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions boot.py
Original file line number Diff line number Diff line change
@@ -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('_'):
Expand All @@ -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()
Expand Down
11 changes: 6 additions & 5 deletions commands/cat.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# WAFCAT :heart:
import typing

help = "Print file contents. Usage: cat <file>"
command_help: str = 'Print file contents. Usage: cat <file>'

def run(args):

def run(args: typing.Any) -> None:
if not args:
print("Usage: cat <file>")
print('Usage: cat <file>')
return
try:
with open(args[0], 'r') as f:
print(f.read())
except Exception as e:
print(f"Error: {e}")
print(f'Error: {e}')
11 changes: 7 additions & 4 deletions commands/cd.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
help = "Change directory. Usage: cd <dir>"
import typing

def run(args):
command_help: str = 'Change directory. Usage: cd <dir>'


def run(args: typing.Any) -> None:
import os
if not args:
print("Usage: cd <directory>")
print('Usage: cd <directory>')
return
try:
os.chdir(args[0])
except Exception as e:
print(f"Error: {e}")
print(f'Error: {e}')
9 changes: 6 additions & 3 deletions commands/clear.py
Original file line number Diff line number Diff line change
@@ -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')
os.system('cls' if os.name == 'nt' else 'clear')
13 changes: 8 additions & 5 deletions commands/del.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
help = "Delete file or directory. Usage: del <file/dir>"
import typing

def run(args):
command_help: str = 'Delete file or directory. Usage: del <file/dir>'


def run(args: typing.Any) -> None:
import os
if not args:
print("Usage: del <file/dir>")
print('Usage: del <file/dir>')
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}")
print(f'Error: {e}')
11 changes: 7 additions & 4 deletions commands/exit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
help = "Exit PyOS."
import typing

def run(args):
print("Goodbye!")
exit(0)
command_help: str = 'Exit PyOS.'


def run(_args: typing.Any) -> None:
print('Goodbye!')
exit(0)
13 changes: 7 additions & 6 deletions commands/help.py
Original file line number Diff line number Diff line change
@@ -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}")
doc = getattr(mod, 'command_help', '')
print(f' {name:<10} {doc}')
9 changes: 6 additions & 3 deletions commands/ls.py
Original file line number Diff line number Diff line change
@@ -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)
print(f)
11 changes: 7 additions & 4 deletions commands/mkdir.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
help = "Create a directory. Usage: mkdir <dir>"
import typing

def run(args):
command_help: str = 'Create a directory. Usage: mkdir <dir>'


def run(args: typing.Any) -> None:
import os
if not args:
print("Usage: mkdir <dir>")
print('Usage: mkdir <dir>')
return
try:
os.mkdir(args[0])
except Exception as e:
print(f"Error: {e}")
print(f'Error: {e}')
10 changes: 6 additions & 4 deletions commands/mkfil.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
help = "Create an empty file. Usage: mkfil <file>"
import typing

command_help: str = 'Create an empty file. Usage: mkfil <file>'

def run(args):

def run(args: typing.Any) -> None:
if not args:
print("Usage: mkfil <file>")
print('Usage: mkfil <file>')
return
try:
open(args[0], 'a').close()
except Exception as e:
print(f"Error: {e}")
print(f'Error: {e}')
9 changes: 6 additions & 3 deletions commands/printdir.py
Original file line number Diff line number Diff line change
@@ -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())
print(os.getcwd())
19 changes: 19 additions & 0 deletions commands/rename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os
import typing

command_help: str = 'Rename a file. Usage: rename <old_file> <new_file>'


def run(args: typing.Any) -> None:
if not args:
print('Usage: rename <old_file> <new_file>')
return

if len(args) != 2:
print('Usage: rename <old_file> <new_file>')
return

try:
os.rename(args[0], args[1])
except Exception as e:
print(f'Error: {e}')
11 changes: 8 additions & 3 deletions commands/whoami.py
Original file line number Diff line number Diff line change
@@ -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())