Skip to content

limelight.capture.camera

SCREENSHOT_NAME_INVALID_PATTERN = re.compile('[^A-Za-z0-9._-]+') module-attribute

Camera

A screenshot recorder for a demo run.

This class numbers each screenshot as it is taken, so the files sort in the order the demo produced them rather than by name.

The constructor for the Camera class.

Parameters:

  • page (Page) –

    The page the screenshots are taken from.

  • directory (Path) –

    The directory the screenshots are written to.

Raises:

  • ValueError –

    If the directory is empty or the current directory.

Source code in limelight/capture/camera.py
def __init__(self, page: Page, directory: Path) -> None:
    """
    The constructor for the Camera class.

    :param page: The page the screenshots are taken from.
    :param directory: The directory the screenshots are written to.
    :raises ValueError: If the directory is empty or the current directory.
    """

    directory_text = str(directory).strip()

    if directory_text in {'', '.'}:
        message = f'directory must be a real path (got "{directory}")'
        raise ValueError(message)

    self._directory = directory
    self._page = page
    self._sequence = 0

screenshot

A method that captures the current page to a numbered PNG file.

Parameters:

  • name (str) –

    The label for the shot, with unusable characters replaced by a dash.

Returns:

  • Path –

    The path the screenshot was written to.

Raises:

  • ValueError –

    If the name carries no characters usable in a file name.

Source code in limelight/capture/camera.py
def screenshot(self, name: str) -> Path:
    """
    A method that captures the current page to a numbered PNG file.

    :param name: The label for the shot, with unusable characters replaced by a dash.
    :return: The path the screenshot was written to.
    :raises ValueError: If the name carries no characters usable in a file name.
    """

    name_clean = SCREENSHOT_NAME_INVALID_PATTERN.sub('-', name).strip('-.')

    if not name_clean:
        message = f'screenshot name carries no filename characters: "{name}"'
        raise ValueError(message)

    self._sequence += 1

    path = self._directory / f'{self._sequence:02d}-{name_clean}.png'

    self._directory.mkdir(parents=True, exist_ok=True)
    self._page.screenshot(path=str(path))

    return path

switch_page

A method that points the camera at a different page.

Parameters:

  • page (Page) –

    The page later screenshots are taken from.

Source code in limelight/capture/camera.py
def switch_page(self, page: Page) -> None:
    """
    A method that points the camera at a different page.

    :param page: The page later screenshots are taken from.
    """

    self._page = page