Skip to content

environment

dev_tool.services.portal.environment

__all__ = ['EnvironmentService'] module-attribute

EnvironmentService

Bases: BaseService

A service class for managing portal environment operations.

This class provides methods for downloading environment variables, parsing environment data, and saving environment files.

The constructor for the EnvironmentService class.

Parameters:

Source code in dev_tool/services/portal/environment/service.py
def __init__(self, config: PortalServiceConfig, client: APIClientService) -> None:
    """
    The constructor for the EnvironmentService class.

    :param config: The portal service configuration.
    :param client: The API client service for making requests.
    """

    super().__init__()

    self.config = config
    self.client = client
    self.task_manager = TaskManager()

config = config instance-attribute

client = client instance-attribute

task_manager = TaskManager() instance-attribute

download_environment_variables

A method that downloads environment variables synchronously.

Parameters:

  • name (str) –

    The name of the environment to download.

Returns:

  • bool

    True if the download was successful.

Source code in dev_tool/services/portal/environment/service.py
def download_environment_variables(self, name: str) -> bool:
    """
    A method that downloads environment variables synchronously.

    :param name: The name of the environment to download.
    :return: True if the download was successful.
    """

    return self._download_environment_variables(name)

get_environment_variables

A method that downloads environment variables as a background task.

Parameters:

  • name (str) –

    The name of the environment to download.

Returns:

  • str

    The task ID for tracking the download progress.

Source code in dev_tool/services/portal/environment/service.py
def get_environment_variables(self, name: str) -> str:
    """
    A method that downloads environment variables as a background task.

    :param name: The name of the environment to download.
    :return: The task ID for tracking the download progress.
    """

    def process() -> str:
        try:
            success = self._download_environment_variables(name)

            if success:
                return f'Environment variables for {name} downloaded successfully'
        except (PortalAuthenticationError, EnvironmentDownloadError, EnvironmentParseError, EnvironmentSaveError):
            raise
        except Exception:
            message = f'Failed to download environment variables for {name}'
            log.exception(message)

            raise EnvironmentDownloadError(message) from None
        else:
            return f'Failed to download environment variables for {name}'

    title = f'Downloading environment variables for: {name}'

    return self.task_manager.submit(
        TaskType.ENVIRONMENT_DOWNLOAD,
        title,
        process,
        environment=name
    )

parse_environment_values

A method that parses environment variable values from a string.

Parameters:

  • values (str) –

    The environment values string with newline-separated key=value pairs.

Returns:

  • dict[str, str]

    A dictionary of environment variables.

Source code in dev_tool/services/portal/environment/service.py
def parse_environment_values(self, values: str) -> dict[str, str]:
    """
    A method that parses environment variable values from a string.

    :param values: The environment values string with newline-separated key=value pairs.
    :return: A dictionary of environment variables.
    """

    return EnvironmentDiff.parse_string(values)

set_environment_variables

A method that saves environment variables to a file.

Parameters:

  • values (str) –

    The environment values string to save.

Source code in dev_tool/services/portal/environment/service.py
def set_environment_variables(self, values: str) -> None:
    """
    A method that saves environment variables to a file.

    :param values: The environment values string to save.
    """

    filename = 'development.env'
    path = BASE / filename

    if path.exists() and not get_user_confirmation(f'Do you wish to overwrite {filename}?'):
        message = 'Operation cancelled by user.'
        self.notification.normal_text(message)
        return

    self._set_environment_variables(values)