Path: blob/trunk/py/selenium/webdriver/remote/file_detector.py
1864 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 Path20from typing import Optional2122from selenium.types import AnyKey23from selenium.webdriver.common.utils import keys_to_typing242526class FileDetector(metaclass=ABCMeta):27"""Used for identifying whether a sequence of chars represents the path to28a file."""2930@abstractmethod31def is_local_file(self, *keys: AnyKey) -> Optional[str]:32raise NotImplementedError333435class UselessFileDetector(FileDetector):36"""A file detector that never finds anything."""3738def is_local_file(self, *keys: AnyKey) -> Optional[str]:39return None404142class LocalFileDetector(FileDetector):43"""Detects files on the local disk."""4445def is_local_file(self, *keys: AnyKey) -> Optional[str]:46file_path = "".join(keys_to_typing(keys))4748with suppress(OSError):49if Path(file_path).is_file():50return file_path51return None525354