Skip to content

Commit 043248d

Browse files
authored
upath.implementations.zip: disable write mode in universal-pathlib (#520)
1 parent f9d9445 commit 043248d

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

upath/implementations/zip.py

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import sys
44
from typing import TYPE_CHECKING
5-
from zipfile import ZipInfo
65

6+
from upath.core import UnsupportedOperation
77
from upath.core import UPath
88
from upath.types import JoinablePathLike
99

@@ -35,41 +35,36 @@ def __init__(
3535
**storage_options: Unpack[ZipStorageOptions],
3636
) -> None: ...
3737

38-
if sys.version_info >= (3, 11):
38+
@classmethod
39+
def _transform_init_args(cls, args, protocol, storage_options):
40+
if storage_options.get("mode") in {"a", "x", "w"}:
41+
raise UnsupportedOperation(
42+
"ZipPath write mode disabled in universal-pathlib"
43+
)
44+
return super()._transform_init_args(args, protocol, storage_options)
3945

40-
def mkdir(
41-
self,
42-
mode: int = 0o777,
43-
parents: bool = False,
44-
exist_ok: bool = False,
45-
) -> None:
46-
is_dir = self.is_dir()
47-
if is_dir and not exist_ok:
48-
raise FileExistsError(f"File exists: {self.path!r}")
49-
elif not is_dir:
50-
zipfile = self.fs.zip
51-
zipfile.mkdir(self.path, mode)
46+
def touch(self, mode: int = 0o666, exist_ok: bool = True) -> None:
47+
raise UnsupportedOperation
5248

53-
else:
49+
def mkdir(
50+
self,
51+
mode: int = 0o777,
52+
parents: bool = False,
53+
exist_ok: bool = False,
54+
) -> None:
55+
raise UnsupportedOperation
5456

55-
def mkdir(
56-
self,
57-
mode: int = 0o777,
58-
parents: bool = False,
59-
exist_ok: bool = False,
60-
) -> None:
61-
is_dir = self.is_dir()
62-
if is_dir and not exist_ok:
63-
raise FileExistsError(f"File exists: {self.path!r}")
64-
elif not is_dir:
65-
dirname = self.path
66-
if dirname and not dirname.endswith("/"):
67-
dirname += "/"
68-
zipfile = self.fs.zip
69-
zinfo = ZipInfo(dirname)
70-
zinfo.compress_size = 0
71-
zinfo.CRC = 0
72-
zinfo.external_attr = ((0o40000 | mode) & 0xFFFF) << 16
73-
zinfo.file_size = 0
74-
zinfo.external_attr |= 0x10
75-
zipfile.writestr(zinfo, b"")
57+
def unlink(self, missing_ok: bool = False) -> None:
58+
raise UnsupportedOperation
59+
60+
def write_bytes(self, data: bytes) -> int:
61+
raise UnsupportedOperation("DataPath does not support writing")
62+
63+
def write_text(
64+
self,
65+
data: str,
66+
encoding: str | None = None,
67+
errors: str | None = None,
68+
newline: str | None = None,
69+
) -> int:
70+
raise UnsupportedOperation("DataPath does not support writing")

0 commit comments

Comments
 (0)