2022-10-12 21:12:56 +00:00
|
|
|
# Copyright 2022 The JAX Authors.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2025-03-03 18:37:45 -05:00
|
|
|
from typing import Protocol
|
2022-10-12 21:12:56 +00:00
|
|
|
import logging
|
2025-03-03 18:37:45 -05:00
|
|
|
import os
|
2022-10-12 21:12:56 +00:00
|
|
|
import pathlib
|
|
|
|
|
2024-08-13 14:48:03 -07:00
|
|
|
__all__ = ["Path"]
|
|
|
|
logger = logging.getLogger(__name__)
|
2025-03-03 18:37:45 -05:00
|
|
|
epath_installed: bool
|
|
|
|
|
|
|
|
class PathProtocol(Protocol):
|
|
|
|
"""A factory that creates a PurePath."""
|
|
|
|
def __call__(self, *pathsegments: str | os.PathLike) -> pathlib.Path:
|
|
|
|
...
|
|
|
|
|
|
|
|
Path: PathProtocol
|
2022-10-12 21:12:56 +00:00
|
|
|
|
|
|
|
# If etils.epath (aka etils[epath] to pip) is present, we prefer it because it
|
|
|
|
# can read and write to, e.g., GCS buckets. Otherwise we use the builtin
|
|
|
|
# pathlib and can only read/write to the local filesystem.
|
2025-03-03 18:37:45 -05:00
|
|
|
try:
|
|
|
|
from etils import epath # type: ignore
|
|
|
|
except ImportError:
|
2024-04-16 20:27:43 -07:00
|
|
|
logger.debug("etils.epath was not found. Using pathlib for file I/O.")
|
|
|
|
Path = pathlib.Path
|
2025-03-03 18:37:45 -05:00
|
|
|
epath_installed = False
|
|
|
|
else:
|
|
|
|
logger.debug("etils.epath found. Using etils.epath for file I/O.")
|
|
|
|
# Ultimately, epath.Path implements pathlib.Path. See:
|
|
|
|
# https://github.com/google/etils/blob/2083f3d932a88d8a135ef57112cd1f9aff5d559e/etils/epath/abstract_path.py#L47
|
|
|
|
Path = epath.Path
|
|
|
|
epath_installed = True
|