1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
# -*- coding: utf-8 -*-
"""
Asynchronous shutil module.
"""
from __future__ import annotations
import asyncio
import shutil
from functools import partial, wraps
from typing import (
TYPE_CHECKING,
Any,
Callable,
Coroutine,
Optional,
Sequence,
TypeVar,
Union,
overload,
)
try:
from typing import ParamSpec, TypeAlias # type: ignore
except ImportError:
# Python versions < 3.10
from typing_extensions import ParamSpec, TypeAlias
__all__ = [
"copyfileobj",
"copyfile",
"copymode",
"copystat",
"copy",
"copy2",
"copytree",
"move",
"rmtree",
"Error",
"SpecialFileError",
"ExecError",
"make_archive",
"get_archive_formats",
"register_archive_format",
"unregister_archive_format",
"get_unpack_formats",
"register_unpack_format",
"unregister_unpack_format",
"unpack_archive",
"ignore_patterns",
"chown",
"which",
"get_terminal_size",
"SameFileError",
]
P = ParamSpec("P")
R = TypeVar("R")
if TYPE_CHECKING: # pragma: no cover
# type hints for wrapped functions with overloads (which are incompatible
# with ParamSpec).
import sys
from os import PathLike
StrPath: TypeAlias = Union[str, PathLike[str]]
BytesPath: TypeAlias = Union[bytes, PathLike[bytes]]
StrOrBytesPath: TypeAlias = Union[str, bytes, PathLike[str], PathLike[bytes]]
_PathReturn: TypeAlias = Any
_StrPathT = TypeVar("_StrPathT", bound=StrPath)
@overload
async def copy(
src: StrPath, dst: StrPath, *, follow_symlinks: bool = ...
) -> _PathReturn:
...
@overload
async def copy(
src: BytesPath, dst: BytesPath, *, follow_symlinks: bool = ...
) -> _PathReturn:
...
async def copy(src, dst, *, follow_symlinks=...):
...
@overload
async def copy2(
src: StrPath, dst: StrPath, *, follow_symlinks: bool = ...
) -> _PathReturn:
...
@overload
async def copy2(
src: BytesPath, dst: BytesPath, *, follow_symlinks: bool = ...
) -> _PathReturn:
...
async def copy2(src, dst, *, follow_symlinks=...):
...
@overload
async def register_archive_format(
name: str,
function: Callable[..., object],
extra_args: Sequence[tuple[str, Any] | list[Any]],
description: str = ...,
) -> None:
...
@overload
async def register_archive_format(
name: str,
function: Callable[[str, str], object],
extra_args: None = ...,
description: str = ...,
) -> None:
...
async def register_archive_format(name, function, extra_args=..., description=...):
...
@overload
async def register_unpack_format(
name: str,
extensions: list[str],
function: Callable[..., object],
extra_args: Sequence[tuple[str, Any]],
description: str = ...,
) -> None:
...
@overload
async def register_unpack_format(
name: str,
extensions: list[str],
function: Callable[[str, str], object],
extra_args: None = ...,
description: str = ...,
) -> None:
...
async def register_unpack_format(
name, extensions, function, extra_args=..., description=...
):
...
@overload
async def chown(
path: StrOrBytesPath, user: Union[str, int], group: None = ...
) -> None:
...
@overload
async def chown(
path: StrOrBytesPath, user: None = ..., *, group: Union[str, int]
) -> None:
...
@overload
async def chown(path: StrOrBytesPath, user: None, group: Union[str, int]) -> None:
...
@overload
async def chown(
path: StrOrBytesPath, user: Union[str, int], group: Union[str, int]
) -> None:
...
async def chown(path, user=..., group=...):
...
if sys.version_info >= (3, 8):
@overload
async def which(
cmd: _StrPathT, mode: int = ..., path: Optional[StrPath] = ...
) -> Union[str, _StrPathT, None]:
...
@overload
async def which(
cmd: bytes, mode: int = ..., path: Optional[StrPath] = ...
) -> Optional[bytes]:
...
async def which(
cmd, mode=..., path=...
) -> Union[bytes, str, StrPath, PathLike[str], None]:
...
else:
async def which(
cmd: _StrPathT, mode: int = ..., path: StrPath | None = ...
) -> str | _StrPathT | None:
...
def sync_to_async(func: Callable[P, R]) -> Callable[P, Coroutine[Any, Any, R]]:
@wraps(func)
async def run_in_executor(*args: P.args, **kwargs: P.kwargs) -> R:
loop = asyncio.get_event_loop()
pfunc = partial(func, *args, **kwargs)
return await loop.run_in_executor(None, pfunc)
return run_in_executor
rmtree = sync_to_async(shutil.rmtree)
copyfile = sync_to_async(shutil.copyfile)
copyfileobj = sync_to_async(shutil.copyfileobj)
copymode = sync_to_async(shutil.copymode)
copystat = sync_to_async(shutil.copystat)
copy = sync_to_async(shutil.copy) # type: ignore # noqa: F811
copy2 = sync_to_async(shutil.copy2) # type: ignore # noqa: F811
copytree = sync_to_async(shutil.copytree)
move = sync_to_async(shutil.move)
Error = shutil.Error
SpecialFileError = shutil.SpecialFileError
ExecError = shutil.ExecError
make_archive = sync_to_async(shutil.make_archive)
get_archive_formats = sync_to_async(shutil.get_archive_formats)
register_archive_format = sync_to_async(shutil.register_archive_format) # type: ignore # noqa: F811
unregister_archive_format = sync_to_async(shutil.unregister_archive_format)
get_unpack_formats = sync_to_async(shutil.get_unpack_formats)
register_unpack_format = sync_to_async(shutil.register_unpack_format) # type: ignore # noqa: F811
unregister_unpack_format = sync_to_async(shutil.unregister_unpack_format)
unpack_archive = sync_to_async(shutil.unpack_archive)
ignore_patterns = sync_to_async(shutil.ignore_patterns)
chown = sync_to_async(shutil.chown) # type: ignore # noqa: F811
which = sync_to_async(shutil.which) # type: ignore # noqa: F811
get_terminal_size = sync_to_async(shutil.get_terminal_size)
SameFileError = shutil.SameFileError
if "disk_usage" in shutil.__all__: # pragma: no cover
__all__.append("disk_usage")
disk_usage = sync_to_async(shutil.disk_usage)
|