2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-06-13 12:44:36 +03:00
|
|
|
import os
|
2023-12-27 02:17:57 +03:00
|
|
|
from typing import Any, NoReturn
|
2023-12-27 16:57:20 +03:00
|
|
|
|
2024-01-22 22:37:37 +03:00
|
|
|
from ._typing import StrOrBytesPath, TypeGuard
|
2018-04-20 02:19:13 +03:00
|
|
|
|
2023-12-27 02:17:57 +03:00
|
|
|
|
2024-01-22 22:37:37 +03:00
|
|
|
def is_path(f: Any) -> TypeGuard[StrOrBytesPath]:
|
2024-01-26 20:11:18 +03:00
|
|
|
return isinstance(f, (bytes, str, os.PathLike))
|
2013-06-30 22:50:38 +04:00
|
|
|
|
2014-07-06 02:50:24 +04:00
|
|
|
|
2024-01-31 22:37:53 +03:00
|
|
|
def is_directory(f: Any) -> TypeGuard[StrOrBytesPath]:
|
2022-04-10 21:20:48 +03:00
|
|
|
"""Checks if an object is a string, and that it points to a directory."""
|
|
|
|
return is_path(f) and os.path.isdir(f)
|
2014-04-03 07:09:04 +04:00
|
|
|
|
2014-07-06 02:50:24 +04:00
|
|
|
|
2022-04-10 21:21:50 +03:00
|
|
|
class DeferredError:
|
2023-12-27 02:17:57 +03:00
|
|
|
def __init__(self, ex: BaseException):
|
2014-04-04 03:05:02 +04:00
|
|
|
self.ex = ex
|
2014-07-06 02:50:24 +04:00
|
|
|
|
2023-12-27 02:17:57 +03:00
|
|
|
def __getattr__(self, elt: str) -> NoReturn:
|
2014-04-04 03:05:02 +04:00
|
|
|
raise self.ex
|
2023-12-27 02:17:57 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def new(ex: BaseException) -> Any:
|
|
|
|
"""
|
|
|
|
Creates an object that raises the wrapped exception ``ex`` when used,
|
|
|
|
and casts it to :py:obj:`~typing.Any` type.
|
|
|
|
"""
|
|
|
|
return DeferredError(ex)
|