Skip to content

python

dev_tool.commands.python

log = logging.getLogger(__name__) module-attribute

PythonCommandGroup

Bases: CommandGroup

A command group for Python operations.

This class provides commands for setting up Python environments, managing dependencies, running scripts, and executing tests and coverage analysis.

The constructor for the PythonCommandGroup class.

This method initializes the command group with required services and registers all command methods.

Parameters:

  • coverage (CoverageService) –

    The coverage service for code coverage analysis.

  • django (DjangoService) –

    The Django service for Django operations.

  • docker (DockerService) –

    The Docker service for container management.

  • portal (PortalService) –

    The Stratus Portal service for connecting to our API.

  • postgres (PostgresService) –

    The PostgreSQL service for database operations.

  • python (PythonService) –

    The Python service for Python operations.

  • scripting (ScriptingService) –

    The scripting service for running Python scripts.

  • unittest (UnitTestService) –

    The unittest service for running tests.

Source code in dev_tool/commands/python.py
def __init__(
    self,
    coverage: CoverageService,
    django: DjangoService,
    docker: DockerService,
    portal: PortalService,
    postgres: PostgresService,
    python: PythonService,
    scripting: ScriptingService,
    unittest: UnitTestService
) -> None:
    """
    The constructor for the PythonCommandGroup class.

    This method initializes the command group with required services
    and registers all command methods.

    :param coverage: The coverage service for code coverage analysis.
    :param django: The Django service for Django operations.
    :param docker: The Docker service for container management.
    :param portal: The Stratus Portal service for connecting to our API.
    :param postgres: The PostgreSQL service for database operations.
    :param python: The Python service for Python operations.
    :param scripting: The scripting service for running Python scripts.
    :param unittest: The unittest service for running tests.
    """

    super().__init__()

    self.coverage = coverage
    self.django = django
    self.docker = docker
    self.portal = portal
    self.postgres = postgres
    self.python = python
    self.scripting = scripting
    self.unittest = unittest

coverage = coverage instance-attribute

django = django instance-attribute

docker = docker instance-attribute

portal = portal instance-attribute

postgres = postgres instance-attribute

python = python instance-attribute

scripting = scripting instance-attribute

unittest = unittest instance-attribute

__str__

The string representation of the command group.

This method returns the category name for the command group.

Returns:

  • str

    The category name as a string.

Source code in dev_tool/commands/python.py
def __str__(self) -> str:
    """
    The string representation of the command group.

    This method returns the category name for the command group.

    :return: The category name as a string.
    """

    return 'Python'

rebuild_project

A command that opens a submenu for project setup options.

This method returns a menu for project-related commands.

Returns:

  • Menu

    A submenu for project setup options.

Source code in dev_tool/commands/python.py
@ordered_submenu(label='Rebuild the project environment')
def rebuild_project(self) -> Menu:
    """
    A command that opens a submenu for project setup options.

    This method returns a menu for project-related commands.

    :return: A submenu for project setup options.
    """

    items: list[Menu | MenuItem] = [
        MenuItem(
            label='(Re)build the development environment',
            action=CommandAction(command=self._rebuild_development_environment)
        ),
        MenuItem(
            label='(Re)build the virtual environment',
            action=CommandAction(command=self._rebuild_virtual_environment)
        ),
        MenuItem(
            label='Download latest environment variables',
            action=CommandAction(command=self._download_environment_variables)
        )
    ]

    return Menu(title='Rebuild the project', items=items)

package_management

A command that opens a submenu for package management options.

This method returns a menu for package-related commands.

Returns:

  • Menu

    A submenu for package management options.

Source code in dev_tool/commands/python.py
@ordered_submenu(label='Package management')
def package_management(self) -> Menu:
    """
    A command that opens a submenu for package management options.

    This method returns a menu for package-related commands.

    :return: A submenu for package management options.
    """

    items: list[Menu | MenuItem] = [
        MenuItem(
            label='Install and upgrade all dependencies',
            action=CommandAction(command=self._upgrade_dependencies)
        ),
        MenuItem(
            label='Install a dependency',
            action=CommandAction(command=self._install_dependency)
        ),
        MenuItem(
            label='Uninstall a dependency',
            action=CommandAction(command=self._uninstall_dependency)
        )
    ]

    return Menu(title='Package management', items=items)

run_python_script

A command that opens a submenu for running Python scripts.

This method returns a menu for selecting and running Python scripts.

Returns:

  • Menu

    A submenu for selecting and running Python scripts.

Source code in dev_tool/commands/python.py
@ordered_submenu(label='Run a Python script')
def run_python_script(self) -> Menu:
    """
    A command that opens a submenu for running Python scripts.

    This method returns a menu for selecting and running Python scripts.

    :return: A submenu for selecting and running Python scripts.
    """

    directories = ScriptingService.discover()

    if not directories:
        return Menu(title='Run a Python script', items=[])

    items = []

    for title, files in directories.items():
        if not files:
            continue

        scripts = []

        for file in files:
            path = Path(file)
            command = partial(ScriptingService.run_python_script, path)
            action = CommandAction(command=command)
            script = MenuItem(label=path.name, action=action)
            scripts.append(script)

        scripts.sort(key=lambda item: item.label.lower())

        submenu = Menu(title=title, items=scripts)
        action = SubmenuAction(submenu=submenu)
        item = MenuItem(label=title, action=action)
        items.append(item)

    return Menu(title='Run a Python script', items=items)

testing

A command that opens a submenu for testing options.

This method returns a menu for test-related commands.

Returns:

  • Menu

    A submenu for testing options.

Source code in dev_tool/commands/python.py
@ordered_submenu(label='Test a Python project')
def testing(self) -> Menu:
    """
    A command that opens a submenu for testing options.

    This method returns a menu for test-related commands.

    :return: A submenu for testing options.
    """

    items: list[Menu | MenuItem] = [
        MenuItem(
            label='Run unit tests',
            action=CommandAction(command=self._python_unittest)
        ),
        MenuItem(
            label='Run coverage',
            action=CommandAction(command=self._python_coverage)
        )
    ]

    return Menu(title='Test a Python project', items=items)