Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:AI Northstar Tech Vector io Dynamic Module Discovery

From Leeroopedia
Knowledge Sources
Domains Software_Architecture, Plugin_System, CLI
Last Updated 2026-10-15 00:00 GMT

Overview

A software architecture pattern that enables automatic registration of plugin modules at runtime by scanning a package directory, importing each module, and collecting classes that subclass a known base class.

Description

Dynamic Module Discovery eliminates the need for manual plugin registration. Instead of maintaining a hardcoded list of supported databases, the system scans a package directory using pkgutil.walk_packages, imports every non-package module via importlib.import_module, and checks each class in the module's namespace against a target base class. Classes that match are registered by a key (e.g. a DB_NAME_SLUG class variable) into a dispatch table.

This pattern solves the problem of tightly-coupled plugin systems where adding a new component requires modifying a central registry. With dynamic discovery, a new vector database export or import module is automatically registered simply by placing a file in the package directory and defining the required class variable (DB_NAME_SLUG).

The __init_subclass__ hook on the base class enforces the contract: any subclass that lacks DB_NAME_SLUG raises a TypeError at import time, providing early validation.

Usage

Use this principle when designing a plugin-based system where new components should be auto-registered without modifying central configuration files. It is particularly suited for:

  • CLI tools with multiple backends (database connectors, format converters)
  • Systems where new integrations are added frequently
  • Open-source projects where contributors can add support for new systems by simply adding a module file

Theoretical Basis

The discovery algorithm operates as follows:

Pseudo-code Logic:

# Abstract algorithm description (NOT real implementation)
def discover_plugins(base_package_dir, base_class, registry_key_attr):
    package = importlib.import_module(base_package_dir)
    registry = {}

    for module_name in pkgutil.walk_packages(package.__path__):
        module = importlib.import_module(f"{base_package_dir}.{module_name}")

        for name, cls in module.__dict__.items():
            if isinstance(cls, type) and issubclass(cls, base_class) and cls is not base_class:
                key = getattr(cls, registry_key_attr)  # e.g. DB_NAME_SLUG
                registry[key] = cls

    return registry

The __init_subclass__ enforcement:

# Contract enforcement (abstract)
class BasePlugin(ABC):
    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        if not hasattr(cls, "REGISTRY_KEY"):
            raise TypeError(f"{cls.__name__} lacks required 'REGISTRY_KEY'")

Practical Guide

To add a new plugin (e.g. a new vector database export):

  1. Create a new module file in the package directory (e.g. mydb_export.py)
  2. Define a class that subclasses the base class (e.g. ExportVDB)
  3. Set the required class variable (e.g. DB_NAME_SLUG = "mydb")
  4. Implement all abstract methods
  5. The module is automatically discovered and registered on next CLI invocation

No changes to any central registry or configuration file are needed.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment