Skip to content

service

dev_tool.services.sass.service

log = logging.getLogger(__name__) module-attribute

SassService

Bases: BaseService

A service class for Sass/SCSS operations.

This class provides methods for compiling SCSS stylesheets to CSS and for watching source stylesheets during development.

The constructor for the SassService class.

Parameters:

  • runner (CommandRunnerProtocol) –

    The command runner for executing commands.

  • bun (BunService) –

    The Bun service, used to provision sass on the host.

  • config (dict[str, Any] | None, default: None ) –

    The configuration dictionary for Sass settings.

Source code in dev_tool/services/sass/service.py
def __init__(self, runner: CommandRunnerProtocol, bun: BunService, config: dict[str, Any] | None = None) -> None:
    """
    The constructor for the SassService class.

    :param runner: The command runner for executing commands.
    :param bun: The Bun service, used to provision sass on the host.
    :param config: The configuration dictionary for Sass settings.
    """

    super().__init__()

    self.runner = runner
    self.bun = bun
    self.config = config or {}
    self.source_directories = self.config.get('source_directories', ['static/scss'])
    self.output_directory = self.config.get('output_directory', 'static/css')
    self.style = self.config.get('style', 'expanded')
    self.source_map = self.config.get('source_map', True)

runner = runner instance-attribute

bun = bun instance-attribute

config = config or {} instance-attribute

source_directories = self.config.get('source_directories', ['static/scss']) instance-attribute

output_directory = self.config.get('output_directory', 'static/css') instance-attribute

style = self.config.get('style', 'expanded') instance-attribute

source_map = self.config.get('source_map', True) instance-attribute

compile_styles

A method that compiles SCSS stylesheets to CSS.

Parameters:

  • production (bool, default: False ) –

    Whether to compile with production settings.

Raises:

  • SassCompileError

    If compilation fails.

Source code in dev_tool/services/sass/service.py
def compile_styles(self, production: bool = False) -> None:
    """
    A method that compiles SCSS stylesheets to CSS.

    :param production: Whether to compile with production settings.
    :raises SassCompileError: If compilation fails.
    """

    targets = self._existing_targets()

    if not targets:
        message = 'No SCSS source directories found'
        self.notification.warning_text(message)

        log.debug(message)
        return

    message = 'Compiling SCSS to CSS...'
    self.notification.normal_text(message)

    log.debug(message)

    command = [*targets, *self._build_options(production)]

    result = self._run_sass(command, check=False)

    if result.returncode != 0:
        message = 'SCSS compilation failed'
        log.exception(message)

        raise SassCompileError(message)

    message = 'SCSS compiled successfully'
    self.notification.normal_text(message)

    log.debug(message)

create_runner

A method that creates a Sass watcher runner.

Returns:

  • SassWatcherRunner | None

    A watcher runner if source directories exist, None otherwise.

Source code in dev_tool/services/sass/service.py
def create_runner(self) -> SassWatcherRunner | None:
    """
    A method that creates a Sass watcher runner.

    :return: A watcher runner if source directories exist, None otherwise.
    """

    targets = self._existing_targets()

    if not targets:
        return None

    self.ensure_available()

    from dev_tool.context import CONTEXT  # noqa: PLC0415
    from dev_tool.services.sass.runner import SassWatcherRunner  # noqa: PLC0415

    project_name = CONTEXT.configuration.get_project_name()
    return SassWatcherRunner(
        project_name=project_name,
        targets=targets,
        style=self.style,
        source_map=self.source_map
    )

ensure_available

A method that ensures sass is available, installing it on the host if needed.

In containerized mode sass is provided by the app container, so no host installation is performed.

Returns:

  • bool

    True if sass is available (or running in a container), False otherwise.

Source code in dev_tool/services/sass/service.py
def ensure_available(self) -> bool:
    """
    A method that ensures sass is available, installing it on the host if needed.

    In containerized mode sass is provided by the app container, so no host
    installation is performed.

    :return: True if sass is available (or running in a container), False otherwise.
    """

    if self._is_containerized():
        return True

    if self.is_available():
        return True

    return self._install_sass()

is_available

A method that checks if Sass is available.

Returns:

  • bool

    True if Sass is available, False otherwise.

Source code in dev_tool/services/sass/service.py
def is_available(self) -> bool:
    """
    A method that checks if Sass is available.

    :return: True if Sass is available, False otherwise.
    """

    return shutil.which('sass') is not None