mini_arcade.utils.module_loader

Module for discovering and loading packages from a specified directory and namespace. This module defines the OneLevelPackageLoader class, which can be used to discover and load packages from a specified directory and namespace. It also defines the DiscoveredPackage dataclass, which represents a discovered package, and the load_command_packages function, which is a convenience function for loading command packages.

Exceptions

ModuleDiscoveryError

Raised when there is an error discovering modules.

ModuleImportError

Raised when there is an error importing modules.

Classes

DiscoveredPackage

Represents a discovered package.

OneLevelPackageLoader

Loads packages from a specified directory and namespace.

Functions

load_command_packages(→ list[DiscoveredPackage])

Load command packages from the specified directory and namespace.

Module Contents

class mini_arcade.utils.module_loader.DiscoveredPackage[source]

Represents a discovered package.

Variables:
  • import_name – The import name of the package (e.g. “mini_arcade.modules.game_runner”).”

  • path – The filesystem path to the package.

import_name: str
path: pathlib.Path
exception mini_arcade.utils.module_loader.ModuleDiscoveryError[source]

Bases: RuntimeError

Raised when there is an error discovering modules.

exception mini_arcade.utils.module_loader.ModuleImportError[source]

Bases: RuntimeError

Raised when there is an error importing modules.

class mini_arcade.utils.module_loader.OneLevelPackageLoader(*, base_namespace: str, base_dir: str | pathlib.Path, require_init: bool = True, strict: bool = True, import_commands_fallback: bool = False)[source]

Loads packages from a specified directory and namespace.

Parameters:
  • base_namespace (str) – The base namespace for the packages (e.g. “mini_arcade.modules”).

  • base_dir (str | Path) – The base directory to search for packages.

  • require_init (bool) – Whether to require an __init__.py file in each package (default: True).

  • strict (bool) – Whether to raise an error if a package fails to import (default: True).

  • import_commands_fallback (bool) – Whether to attempt importing <package>.commands as a fallback (default: False).

base_namespace
base_dir
require_init = True
strict = True
import_commands_fallback = False
discover() list[DiscoveredPackage][source]

Discover packages in the base directory.

Returns:

A list of DiscoveredPackage instances.

Return type:

list[DiscoveredPackage]

load_all() list[DiscoveredPackage][source]

Load all discovered packages.

Returns:

A list of successfully loaded DiscoveredPackage instances.

Return type:

list[DiscoveredPackage]

Raises:

ModuleImportError – If strict is True and a package fails to import.

mini_arcade.utils.module_loader.load_command_packages(*, base_namespace: str, base_dir: str | pathlib.Path, strict: bool = True) list[DiscoveredPackage][source]

Load command packages from the specified directory and namespace.

Parameters:
  • base_namespace (str) – The base namespace for the command packages (e.g. “mini_arcade.modules”).

  • base_dir (str | Path) – The base directory to search for command packages.

  • strict (bool) – Whether to raise an error if a package fails to import (default: True).

Returns:

A list of successfully loaded DiscoveredPackage instances.

Return type:

list[DiscoveredPackage]