injector_plugin¶

Plugin Scaffolder.

Generates valid Python source files for new hooks in the plugins directory. Used by the CLI to provide a starting point for complex logic implementations. Feature 083: Supports compiling declarative rules including complex operators into switch statements. Feature 084: Supports Preservative Updates (Body Extraction).

Attributes¶

HELPER_LOGIC

TEMPLATE_HEADER

TEMPLATE_FUNC_DEF

Classes¶

BodyExtractor

Extracts the body of a specific function definition.

PluginGenerator

Writes Python plugin files to disk based on scaffold definitions.

Module Contents¶

injector_plugin.HELPER_LOGIC = Multiline-String¶
Show Value
"""
def _get_kwarg_value(node: cst.Call, arg_name: str):
    for arg in node.args:
        if arg.keyword and arg.keyword.value == arg_name:
             return _node_to_literal(arg.value)
    return None

def _node_to_literal(node):
    if isinstance(node, cst.Integer): return int(node.value)
    if isinstance(node, cst.Float): return float(node.value)
    if isinstance(node, cst.SimpleString): return node.value.strip("'").strip('"')
    if isinstance(node, cst.Name):
         if node.value == "True": return True
         if node.value == "False": return False
         if node.value == "None": return None
    return None

def _create_dotted_name(name_str: str) -> cst.BaseExpression:
    parts = name_str.split(".")
    node = cst.Name(parts[0])
    for part in parts[1:]:
        node = cst.Attribute(value=node, attr=cst.Name(part))
    return node
"""
injector_plugin.TEMPLATE_HEADER = Multiline-String¶
Show Value
""""""
{doc}
"""
import libcst as cst
from ml_switcheroo.core.hooks import register_hook, HookContext
"""
injector_plugin.TEMPLATE_FUNC_DEF = Multiline-String¶
Show Value
"""
@register_hook("{name}")
def {name}(node: {node_type}, ctx: HookContext) -> cst.CSTNode:
    """
    Plugin Hook: {doc}
    """
"""
class injector_plugin.BodyExtractor(func_name: str)¶

Bases: libcst.CSTVisitor

Extracts the body of a specific function definition. Used to preserve user implementation logic during scaffolding updates.

func_name¶
body_node: libcst.BaseSuite | None = None¶
found = False¶
visit_FunctionDef(node: libcst.FunctionDef) → bool | None¶

Visits function definitions to find the target hook. If found, captures the body and stops recursion.

class injector_plugin.PluginGenerator(plugins_dir: pathlib.Path)¶

Writes Python plugin files to disk based on scaffold definitions.

plugins_dir¶

The directory where new files will be written.

Type:

Path

plugins_dir¶
generate(scaffold: ml_switcheroo.core.dsl.PluginScaffoldDef) → bool¶

Creates or updates a plugin file.

If the file exists, it attempts to preserve the existing function body logic while updating the wrapper (docstrings/decorators/imports).

Parameters:

scaffold – Definition model containing name, type, docs, and rules.

Returns:

True if file was written/updated.

Return type:

bool