Path: blob/trunk/py/selenium/webdriver/firefox/service.py
4012 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import logging18import os19import sys20from collections.abc import Mapping, Sequence21from typing import IO, Any2223from selenium.webdriver.common import service, utils242526class Service(service.Service):27"""Service class responsible for starting and stopping of `geckodriver`.2829Args:30executable_path: (Optional) Install path of the executable.31port: (Optional) Port for the service to run on, defaults to 0 where the operating system will decide.32service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.33log_output: (Optional) int representation of STDOUT/DEVNULL, any IO instance or String path to file.34env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.35driver_path_env_key: (Optional) Environment variable to use to get the path to the driver executable.36"""3738def __init__(39self,40executable_path: str | None = None,41port: int = 0,42service_args: Sequence[str] | None = None,43log_output: int | str | IO[Any] | None = None,44env: Mapping[str, str] | None = None,45driver_path_env_key: str | None = None,46**kwargs,47) -> None:48self._service_args = list(service_args or [])49driver_path_env_key = driver_path_env_key or "SE_GECKODRIVER"5051if os.environ.get("SE_DEBUG"):52has_log_arg = "--log" in self._service_args or any(arg.startswith("--log=") for arg in self._service_args)53has_output_conflict = log_output is not None54if has_log_arg or has_output_conflict:55logging.getLogger(__name__).warning(56"Environment Variable `SE_DEBUG` is set; "57"forcing GeckoDriver log level to DEBUG and overriding configured log level/output."58)59if has_log_arg:60if "--log" in self._service_args:61idx = self._service_args.index("--log")62del self._service_args[idx : idx + 2]63else:64self._service_args = [arg for arg in self._service_args if not arg.startswith("--log=")]65self._service_args.append("--log")66self._service_args.append("debug")67log_output = sys.stderr6869super().__init__(70executable_path=executable_path,71port=port,72log_output=log_output,73env=env,74driver_path_env_key=driver_path_env_key,75**kwargs,76)7778# Set a port for CDP79if "--connect-existing" not in self._service_args:80self._service_args.append("--websocket-port")81self._service_args.append(f"{utils.free_port()}")8283def command_line_args(self) -> list[str]:84return ["--port", f"{self.port}"] + self._service_args8586@property87def service_args(self) -> Sequence[str]:88"""Returns the sequence of service arguments."""89return self._service_args9091@service_args.setter92def service_args(self, value: Sequence[str]):93if isinstance(value, str) or not isinstance(value, Sequence):94raise TypeError("service_args must be a sequence")95self._service_args = list(value)969798