-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: update display mode management
- Loading branch information
Showing
6 changed files
with
176 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
"""ffpuppet display module""" | ||
|
||
from __future__ import annotations | ||
|
||
from contextlib import suppress | ||
from enum import Enum, auto, unique | ||
from logging import getLogger | ||
from platform import system | ||
from types import MappingProxyType | ||
|
||
with suppress(ImportError): | ||
from xvfbwrapper import Xvfb | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Iterable, Mapping | ||
|
||
|
||
LOG = getLogger(__name__) | ||
|
||
|
||
@unique | ||
class DisplayMode(Enum): | ||
"""Supported display modes.""" | ||
|
||
DEFAULT = auto() | ||
HEADLESS = auto() | ||
if system() == "Linux": | ||
XVFB = auto() | ||
|
||
|
||
class Display: | ||
"""Default display mode. | ||
Attributes: | ||
args: Extra Firefox command line arguments to use. | ||
env: Extra environment variables to use. | ||
mode: DisplayMode enum name. | ||
""" | ||
|
||
__slots__ = ("args", "env", "mode") | ||
|
||
def __init__(self) -> None: | ||
self.args: Iterable[str] = () | ||
self.env: Mapping[str, str] = MappingProxyType({}) | ||
self.mode: str = DisplayMode.DEFAULT.name | ||
|
||
def close(self) -> None: | ||
"""Perform any required operations to shutdown and cleanup. | ||
Args: | ||
None | ||
Returns: | ||
None | ||
""" | ||
|
||
|
||
class HeadlessDisplay(Display): | ||
"""Headless display mode.""" | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
self.args = ("-headless",) | ||
self.mode = DisplayMode.HEADLESS.name | ||
|
||
|
||
class XvfbDisplay(Display): | ||
"""Xvfb display mode.""" | ||
|
||
__slots__ = ("_xvfb",) | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
self.env = MappingProxyType({"MOZ_ENABLE_WAYLAND": "0"}) | ||
self.mode = DisplayMode.XVFB.name | ||
try: | ||
self._xvfb: Xvfb | None = Xvfb(width=1280, height=1024) | ||
except NameError: | ||
LOG.error("Missing xvfbwrapper") | ||
raise | ||
self._xvfb.start() | ||
|
||
def close(self) -> None: | ||
if self._xvfb is not None: | ||
self._xvfb.stop() | ||
self._xvfb = None | ||
|
||
|
||
_displays: dict[DisplayMode, type[Display]] = { | ||
DisplayMode.DEFAULT: Display, | ||
DisplayMode.HEADLESS: HeadlessDisplay, | ||
} | ||
if system() == "Linux": | ||
_displays[DisplayMode.XVFB] = XvfbDisplay | ||
|
||
DISPLAYS = MappingProxyType(_displays) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
# You can obtain one at http://mozilla.org/MPL/2.0/. | ||
"""display.py tests""" | ||
from platform import system | ||
|
||
from pytest import mark, raises | ||
|
||
from .display import DISPLAYS, DisplayMode, XvfbDisplay | ||
|
||
|
||
@mark.parametrize("mode", tuple(x for x in DisplayMode)) | ||
def test_displays(mocker, mode): | ||
"""test Displays()""" | ||
if system() == "Linux": | ||
mocker.patch("ffpuppet.display.Xvfb", autospec=True) | ||
display = DISPLAYS[mode]() | ||
assert display | ||
assert display.mode == mode.name | ||
display.close() | ||
|
||
|
||
@mark.skipif(system() != "Linux", reason="Only supported on Linux") | ||
def test_xvfb_missing_deps(mocker): | ||
"""test XvfbDisplay() missing deps""" | ||
mocker.patch("ffpuppet.display.Xvfb", side_effect=NameError("test")) | ||
with raises(NameError): | ||
XvfbDisplay() |
Oops, something went wrong.