Path: blob/trunk/py/selenium/webdriver/remote/file_detector.py
3991 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.1617from abc import ABCMeta, abstractmethod18from contextlib import suppress19from pathlib import Path2021from selenium.webdriver.common.utils import keys_to_typing222324class FileDetector(metaclass=ABCMeta):25"""Identify whether a sequence of characters represents a file path."""2627@abstractmethod28def is_local_file(self, *keys: str | int | float) -> str | None:29raise NotImplementedError303132class UselessFileDetector(FileDetector):33"""A file detector that never finds anything."""3435def is_local_file(self, *keys: str | int | float) -> str | None:36return None373839class LocalFileDetector(FileDetector):40"""Detects files on the local disk."""4142def is_local_file(self, *keys: str | int | float) -> str | None:43file_path = "".join(keys_to_typing(keys))4445with suppress(OSError):46if Path(file_path).is_file():47return file_path48return None495051