bpo-36832: add zipfile.Path (#13153)

* bpo-36832: add zipfile.Path

* bpo-36832: add documentation for zipfile.Path

* 📜🤖 Added by blurb_it.

* Remove module reference from blurb.

* Sort the imports

* Update docstrings and docs per recommendations.

* Rely on test.support.temp_dir

* Signal that 'root' is the parameter.

* Correct spelling of 'mod'

* Convert docstring to comment for brevity.

* Fix more errors in the docs
This commit is contained in:
Jason R. Coombs 2019-05-08 09:45:06 -04:00 committed by Barry Warsaw
parent 70b80541bb
commit b2758ff955
4 changed files with 320 additions and 8 deletions

View file

@ -52,6 +52,15 @@ The module defines the following items:
:ref:`zipfile-objects` for constructor details.
.. class:: Path
:noindex:
A pathlib-compatible wrapper for zip files. See section
:ref:`path-objects` for details.
.. versionadded:: 3.8
.. class:: PyZipFile
:noindex:
@ -456,6 +465,64 @@ The following data attributes are also available:
truncated.
.. _path-objects:
Path Objects
------------
.. class:: Path(root, at='')
Construct a Path object from a ``root`` zipfile (which may be a
:class:`ZipFile` instance or ``file`` suitable for passing to
the :class:`ZipFile` constructor).
``at`` specifies the location of this Path within the zipfile,
e.g. 'dir/file.txt', 'dir/', or ''. Defaults to the empty string,
indicating the root.
Path objects expose the following features of :mod:`pathlib.Path`
objects:
Path objects are traversable using the ``/`` operator.
.. attribute:: Path.name
The final path component.
.. method:: Path.open(*, **)
Invoke :meth:`ZipFile.open` on the current path. Accepts
the same arguments as :meth:`ZipFile.open`.
.. method:: Path.listdir()
Enumerate the children of the current directory.
.. method:: Path.is_dir()
Return ``True`` if the current context references a directory.
.. method:: Path.is_file()
Return ``True`` if the current context references a file.
.. method:: Path.exists()
Return ``True`` if the current context references a file or
directory in the zip file.
.. method:: Path.read_text(*, **)
Read the current file as unicode text. Positional and
keyword arguments are passed through to
:class:`io.TextIOWrapper` (except ``buffer``, which is
implied by the context).
.. method:: Path.read_bytes()
Read the current file as bytes.
.. _pyzipfile-objects:
PyZipFile Objects