Skip to content

Commit

Permalink
try fix rtd build - try 4 different web browsers before giving up
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Dec 20, 2023
1 parent 1f42680 commit 76c034d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 37 deletions.
24 changes: 1 addition & 23 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
from pathlib import Path
from contextlib import contextmanager
import platform
from sphinxcontrib import typer as sphinxcontrib_typer

# Configuration file for the Sphinx documentation builder.
Expand Down Expand Up @@ -63,26 +64,3 @@

latex_engine = 'xelatex'


@contextmanager
def typer_get_web_driver(directive):
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType

# Set up headless browser options
options = Options()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")

# Initialize WebDriver
driver = webdriver.Chrome(
service=ChromiumService(
ChromeDriverManager(chrome_type=ChromeType.CHROMIUM)
)
)
yield driver
driver.quit()
84 changes: 70 additions & 14 deletions sphinxcontrib/typer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,29 +742,85 @@ def typer_get_web_driver(directive: TyperDirective) -> t.Any:
:param directive: The TyperDirective instance
"""
import platform
try:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options as ChromeOptions
except ImportError:
raise directive.error(
f'This feature requires selenium and webdriver-manager to be '
'installed.'
)

# Set up headless browser options
options = Options()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")

# Initialize WebDriver
driver = webdriver.Chrome(
service=Service(ChromeDriverManager().install()), options=options
)
yield driver
driver.quit()
def opts(options=ChromeOptions()):
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920x1080")
return options

def chrome():
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

return webdriver.Chrome(
service=Service(ChromeDriverManager().install()),
options=opts()
)

def chromium():
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType
return webdriver.Chrome(
service=ChromiumService(
ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()
),
options=opts()
)

def firefox():
from selenium.webdriver.firefox.options import Options
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
return webdriver.Firefox(
service=FirefoxService(GeckoDriverManager().install()),
options=opts(Options())
)

def edge():
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service as EdgeService
from webdriver_manager.microsoft import EdgeChromiumDriverManager
options = Options()
options.use_chromium = True
return webdriver.Edge(
service=EdgeService(EdgeChromiumDriverManager().install()),
options=opts(options)
)

services = [
chrome,
edge if platform.system().lower() == 'windows' else chromium,
firefox
]

driver = None
for service in services:
try:
driver = service()
break # use the first one that works!
except Exception as err:
directive.debug(f'Unable to initialize webdriver {service.__name__}: {err}')

if driver:
yield driver
driver.quit()
else:
raise directive.error(f'Unable to initialize any webdriver.')


def typer_convert_png(
Expand Down

0 comments on commit 76c034d

Please sign in to comment.