Service class responsible for starting and stopping of `geckodriver`. Args: executable_path: (Optional) Install path of the executable. port: (Optional) Port for the service to run on, defaults to 0 where the operating system will decide. service_args: (Optional) Sequenc
| 25 | |
| 26 | |
| 27 | class Service(service.Service): |
| 28 | """Service class responsible for starting and stopping of `geckodriver`. |
| 29 | |
| 30 | Args: |
| 31 | executable_path: (Optional) Install path of the executable. |
| 32 | port: (Optional) Port for the service to run on, defaults to 0 where the operating system will decide. |
| 33 | service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable. |
| 34 | log_output: (Optional) int representation of STDOUT/DEVNULL, any IO instance or String path to file. |
| 35 | env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`. |
| 36 | driver_path_env_key: (Optional) Environment variable to use to get the path to the driver executable. |
| 37 | """ |
| 38 | |
| 39 | def __init__( |
| 40 | self, |
| 41 | executable_path: str | None = None, |
| 42 | port: int = 0, |
| 43 | service_args: Sequence[str] | None = None, |
| 44 | log_output: int | str | IO[Any] | None = None, |
| 45 | env: Mapping[str, str] | None = None, |
| 46 | driver_path_env_key: str | None = None, |
| 47 | **kwargs, |
| 48 | ) -> None: |
| 49 | self._service_args = list(service_args or []) |
| 50 | driver_path_env_key = driver_path_env_key or "SE_GECKODRIVER" |
| 51 | |
| 52 | if os.environ.get("SE_DEBUG"): |
| 53 | has_log_arg = "--log" in self._service_args or any(arg.startswith("--log=") for arg in self._service_args) |
| 54 | has_output_conflict = log_output is not None |
| 55 | if has_log_arg or has_output_conflict: |
| 56 | logging.getLogger(__name__).warning( |
| 57 | "Environment Variable `SE_DEBUG` is set; " |
| 58 | "forcing GeckoDriver log level to DEBUG and overriding configured log level/output." |
| 59 | ) |
| 60 | if has_log_arg: |
| 61 | if "--log" in self._service_args: |
| 62 | idx = self._service_args.index("--log") |
| 63 | del self._service_args[idx : idx + 2] |
| 64 | else: |
| 65 | self._service_args = [arg for arg in self._service_args if not arg.startswith("--log=")] |
| 66 | self._service_args.append("--log") |
| 67 | self._service_args.append("debug") |
| 68 | log_output = sys.stderr |
| 69 | |
| 70 | super().__init__( |
| 71 | executable_path=executable_path, |
| 72 | port=port, |
| 73 | log_output=log_output, |
| 74 | env=env, |
| 75 | driver_path_env_key=driver_path_env_key, |
| 76 | **kwargs, |
| 77 | ) |
| 78 | |
| 79 | # Set a port for CDP |
| 80 | if "--connect-existing" not in self._service_args: |
| 81 | self._service_args.append("--websocket-port") |
| 82 | self._service_args.append(f"{utils.free_port()}") |
| 83 | |
| 84 | def command_line_args(self) -> list[str]: |