everything132 / data /tefig /envs /olmes-eval /lib /python3.10 /site-packages /cloudpathlib /anypath.py
Download data/tefig/envs/olmes-eval/lib/python3.10/site-packages/cloudpathlib/anypath.py from assassinlike/everything132: direct link, hf CLI and curl.
- Browser
- Download file 3.71 kB
-
https://huggingface.co/datasets/assassinlike/everything132/resolve/main/data/tefig/envs/olmes-eval/lib/python3.10/site-packages/cloudpathlib/anypath.py
- Command line
-
hf download hf://datasets/assassinlike/everything132/data/tefig/envs/olmes-eval/lib/python3.10/site-packages/cloudpathlib/anypath.py
-
curl -L -o anypath.py https://huggingface.co/datasets/assassinlike/everything132/resolve/main/data/tefig/envs/olmes-eval/lib/python3.10/site-packages/cloudpathlib/anypath.py
3.71 kB
| import os | |
| from abc import ABC | |
| from pathlib import Path | |
| from typing import Any, Union | |
| from .cloudpath import InvalidPrefixError, CloudPath | |
| from .exceptions import AnyPathTypeError | |
| from .url_utils import path_from_fileurl | |
| class AnyPath(ABC): | |
| """Polymorphic virtual superclass for CloudPath and pathlib.Path. Constructing an instance will | |
| automatically dispatch to CloudPath or Path based on the input. It also supports both | |
| isinstance and issubclass checks. | |
| This class also integrates with Pydantic. When used as a type declaration for a Pydantic | |
| BaseModel, the Pydantic validation process will appropriately run inputs through this class' | |
| constructor and dispatch to CloudPath or Path. | |
| """ | |
| def __new__(cls, *args, **kwargs) -> Union[CloudPath, Path]: # type: ignore | |
| try: | |
| return CloudPath(*args, **kwargs) # type: ignore | |
| except InvalidPrefixError as cloudpath_exception: | |
| try: | |
| if isinstance(args[0], str) and args[0].lower().startswith("file:"): | |
| path = path_from_fileurl(args[0], **kwargs) | |
| for part in args[1:]: | |
| path /= part | |
| return path | |
| return Path(*args, **kwargs) | |
| except TypeError as path_exception: | |
| raise AnyPathTypeError( | |
| "Invalid input for both CloudPath and Path. " | |
| f"CloudPath exception: {repr(cloudpath_exception)} " | |
| f"Path exception: {repr(path_exception)}" | |
| ) | |
| # =========== pydantic integration special methods =============== | |
| def __get_pydantic_core_schema__(cls, _source_type: Any, _handler): | |
| """Pydantic special method. See | |
| https://docs.pydantic.dev/2.0/usage/types/custom/""" | |
| try: | |
| from pydantic_core import core_schema | |
| return core_schema.no_info_after_validator_function( | |
| cls.validate, | |
| core_schema.any_schema(), | |
| serialization=core_schema.plain_serializer_function_ser_schema( | |
| lambda x: str(x), | |
| return_schema=core_schema.str_schema(), | |
| ), | |
| ) | |
| except ImportError: | |
| return None | |
| def validate(cls, v: str) -> Union[CloudPath, Path]: | |
| """Pydantic special method. See | |
| https://docs.pydantic.dev/2.0/usage/types/custom/""" | |
| try: | |
| return cls.__new__(cls, v) | |
| except AnyPathTypeError as e: | |
| # type errors no longer converted to validation errors | |
| # https://docs.pydantic.dev/2.0/migration/#typeerror-is-no-longer-converted-to-validationerror-in-validators | |
| raise ValueError(e) | |
| def __get_validators__(cls): | |
| """Pydantic special method. See | |
| https://pydantic-docs.helpmanual.io/usage/types/#custom-data-types""" | |
| yield cls._validate | |
| def _validate(cls, value) -> Union[CloudPath, Path]: | |
| """Used as a Pydantic validator. See | |
| https://pydantic-docs.helpmanual.io/usage/types/#custom-data-types""" | |
| # Note __new__ is static method and not a class method | |
| return cls.__new__(cls, value) | |
| AnyPath.register(CloudPath) # type: ignore | |
| AnyPath.register(Path) | |
| def to_anypath(s: Union[str, os.PathLike]) -> Union[CloudPath, Path]: | |
| """Convenience method to convert a str or os.PathLike to the | |
| proper Path or CloudPath object using AnyPath. | |
| """ | |
| # shortcut pathlike items that are already valid Path/CloudPath | |
| if isinstance(s, (CloudPath, Path)): | |
| return s | |
| return AnyPath(s) # type: ignore | |