Skip to content

Commit

Permalink
Make ZipFileDirPath a private class
Browse files Browse the repository at this point in the history
It has an very narrow purpose, and should never be used for anything
else. I see no reason why a user should use it directly.

I made a few minor changes to the implementation itself (f-strings,
and using `super()`) for a lbit leaner code.
  • Loading branch information
mih committed Oct 27, 2023
1 parent bf14adf commit 1b07864
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
12 changes: 6 additions & 6 deletions datalad_next/iter_collections/tests/test_iterzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from ..zipfile import (
FileSystemItemType,
ZipFileDirPath,
_ZipFileDirPath,
ZipfileItem,
iter_zip,
)
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_iter_zip(sample_zip):
root = PurePosixPath('test-archive')
targets = [
ZipfileItem(
name=ZipFileDirPath(root),
name=_ZipFileDirPath(root),
type=FileSystemItemType.directory,
size=0,
),
Expand All @@ -61,7 +61,7 @@ def test_iter_zip(sample_zip):
size=8,
),
ZipfileItem(
name=ZipFileDirPath(root, 'subdir'),
name=_ZipFileDirPath(root, 'subdir'),
type=FileSystemItemType.directory,
size=0,
),
Expand Down Expand Up @@ -92,9 +92,9 @@ def test_iter_zip(sample_zip):


def test_zip_dir_path():
zp1 = ZipFileDirPath("a/b")
zp2 = ZipFileDirPath("a/b")
zp3 = ZipFileDirPath("a/c")
zp1 = _ZipFileDirPath("a/b")
zp2 = _ZipFileDirPath("a/b")
zp3 = _ZipFileDirPath("a/c")
pp = PurePosixPath("a/b")

assert zp1.as_posix()[-1] == '/'
Expand Down
18 changes: 12 additions & 6 deletions datalad_next/iter_collections/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@
)


class ZipFileDirPath(PurePosixPath):
"""Used to create proper names for directories in zip-archives"""
class _ZipFileDirPath(PurePosixPath):
"""PurePosixPath variant that appends a '/' to the str-representation
This is used by class:`ZipfileItem` to represent directory members in
ZIP archives, in order to streamline archive member tests via a
``item.name in zipfile.ZipFile(...)`` pattern. ``ZipFile`` requires
directory members to be identified with a trailing slash.
"""
def __str__(self) -> str:
return PurePosixPath.__str__(self) + '/'
return f'{super().__str__()}/'

def __eq__(self, other):
if not isinstance(other, ZipFileDirPath):
if not isinstance(other, _ZipFileDirPath):
return False
return PurePosixPath.__eq__(self, other)
return super().__eq__(other)


@dataclass
Expand Down Expand Up @@ -83,7 +89,7 @@ def _get_zipfile_item(zip_info: zipfile.ZipInfo) -> ZipfileItem:
return ZipfileItem(
**(
dict(
name=ZipFileDirPath(zip_info.filename),
name=_ZipFileDirPath(zip_info.filename),
type=FileSystemItemType.directory)
if zip_info.is_dir()
else dict(
Expand Down

0 comments on commit 1b07864

Please sign in to comment.