Skip to content

Commit

Permalink
Path.open now mirrors the interface of pathlib.Path.open. Fixes #44.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Feb 17, 2020
1 parent d8d6c64 commit 6e08ece
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
12 changes: 12 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
v1.2.0
======

#44: ``zipp.Path.open()`` now supports a compatible signature
as ``pathlib.Path.open()``, accepting text (default) or binary
modes and soliciting keyword parameters passed through to
``io.TextIOWrapper`` (encoding, newline, etc). The stream is
opened in text-mode by default now. ``open`` no
longer accepts ``pwd`` as a positional argument and does not
accept the ``force_zip64`` parameter at all. This change is
a backward-incompatible change for that single function.

v1.1.1
======

Expand Down
2 changes: 1 addition & 1 deletion test_zipp.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_open(self):
a, b, g = root.iterdir()
with a.open() as strm:
data = strm.read()
assert data == b"content of a"
assert data == "content of a"

def test_read(self):
for alpharep in self.zipfile_alpharep():
Expand Down
23 changes: 17 additions & 6 deletions zipp.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,31 @@ def __init__(self, root, at=""):
self.root = FastLookup.make(root)
self.at = at

@property
def open(self):
return functools.partial(self.root.open, self.at)
def open(self, mode='r', *args, **kwargs):
"""
Open this entry as text or binary following the semantics
of ``pathlib.Path.open()`` by passing arguments through
to io.TextIOWrapper().
"""
pwd = kwargs.pop('pwd', None)
zip_mode = mode[0]
stream = self.root.open(self.at, zip_mode, pwd=pwd)
if 'b' in mode:
if args or kwargs:
raise ValueError("encoding args invalid for binary operation")
return stream
return io.TextIOWrapper(stream, *args, **kwargs)

@property
def name(self):
return posixpath.basename(self.at.rstrip("/"))

def read_text(self, *args, **kwargs):
with self.open() as strm:
return io.TextIOWrapper(strm, *args, **kwargs).read()
with self.open('r', *args, **kwargs) as strm:
return strm.read()

def read_bytes(self):
with self.open() as strm:
with self.open('rb') as strm:
return strm.read()

def _is_child(self, path):
Expand Down

0 comments on commit 6e08ece

Please sign in to comment.