-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathsmb.py
64 lines (54 loc) · 1.65 KB
/
smb.py
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
from __future__ import annotations
import os
import sys
import warnings
from typing import Any
if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self
import smbprotocol.exceptions
from upath import UPath
_unset: Any = object()
class SMBPath(UPath):
__slots__ = ()
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
# smbclient does not support setting mode externally
if parents and not exist_ok and self.exists():
raise FileExistsError(str(self))
try:
self.fs.mkdir(
self.path,
create_parents=parents,
)
except smbprotocol.exceptions.SMBOSError:
if not exist_ok:
raise FileExistsError(str(self))
if not self.is_dir():
raise FileExistsError(str(self))
def iterdir(self):
if not self.is_dir():
raise NotADirectoryError(str(self))
else:
return super().iterdir()
def rename(
self,
target: str | os.PathLike[str] | UPath,
*,
recursive: bool = _unset,
maxdepth: int | None = _unset,
**kwargs: Any,
) -> Self:
if recursive is not _unset:
warnings.warn(
"SMBPath.rename(): recursive is currently ignored.",
UserWarning,
stacklevel=2,
)
if maxdepth is not _unset:
warnings.warn(
"SMBPath.rename(): maxdepth is currently ignored.",
UserWarning,
stacklevel=2,
)
return super().rename(target, **kwargs)