Skip to content

service

dev_tool.services.scripting.service

log = logging.getLogger(__name__) module-attribute

ScriptingService

Bases: BaseService

A service class for Python script discovery and execution.

This class provides methods for discovering and executing Python scripts.

The constructor for the ScriptingService class.

It initializes the scripting service by calling the parent constructor.

Source code in dev_tool/services/scripting/service.py
def __init__(self) -> None:
    """
    The constructor for the ScriptingService class.

    It initializes the scripting service by calling the parent constructor.
    """

    super().__init__()

discover staticmethod

A method that discovers Python scripts in configured directories.

Returns:

  • dict[str, list[Path]]

    A dictionary mapping directory names to lists of Python script files.

Source code in dev_tool/services/scripting/service.py
@staticmethod
def discover() -> dict[str, list[Path]]:
    """
    A method that discovers Python scripts in configured directories.

    :return: A dictionary mapping directory names to lists of Python script files.
    """

    config = CONTEXT.configuration.get_scripting_config()

    if not config:
        message = '[tool.scripting] is missing from pyproject.toml'
        CONTEXT.notification.warning_text(message)

        log.debug(message)
        return {}

    try:
        directories = [
            BASE / directory
            for directory in config.get('include', [])
            if (BASE / directory).is_dir()
        ]

        exclude = ['__init__.py']

        return {
            directory.stem: [
                file for file in directory.rglob('*.py')
                if not any(
                    file.match(pattern)
                    for pattern in exclude
                )
            ]
            for directory in directories
        }
    except Exception:
        message = 'Failed to discover Python scripts'
        log.exception(message)

        raise ScriptDiscoveryError(message) from None

run_python_script staticmethod

A method that runs a Python script in the virtual environment.

Parameters:

  • script (Path) –

    The path to the Python script to run.

Source code in dev_tool/services/scripting/service.py
@staticmethod
@is_virtual_environment
def run_python_script(script: Path) -> None:
    """
    A method that runs a Python script in the virtual environment.

    :param script: The path to the Python script to run.
    """

    message = f'Running Python script: {script}'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    if not script.exists():
        message = f'Script not found: {script}'
        log.exception(message)

        raise ScriptNotFoundError(message)

    command = [
        VENV_PYTHON,
        str(script)
    ]

    message = f'Running {command}...'
    CONTEXT.notification.normal_text(message)

    log.debug(message)

    try:
        subprocess.run(command, check=True)
    except subprocess.CalledProcessError:
        message = f'Failed to execute script: {script}'
        log.exception(message)

        raise ScriptExecutionError(message) from None
    except Exception:
        message = f'Unexpected error executing script: {script}'
        log.exception(message)

        raise ScriptExecutionError(message) from None