|
| 1 | +========================= |
| 2 | + importlib_resources API |
| 3 | +========================= |
| 4 | + |
| 5 | +``importlib_resources`` exposes a small number of functions, and it references |
| 6 | +a limited number of types, both as arguments to functions and as return types. |
| 7 | + |
| 8 | + |
| 9 | +Types |
| 10 | +===== |
| 11 | + |
| 12 | +.. py:class:: Package |
| 13 | +
|
| 14 | + ``Package`` types are defined as ``Union[ModuleType, str]``. This means |
| 15 | + that where the function describes accepting a ``Package``, you can pass in |
| 16 | + either a module or a string. Note that in Python 2, the module object |
| 17 | + *must* have a ``__path__`` attribute, while in Python 3, the module object |
| 18 | + must have a resolvable ``__spec__.submodule_search_locations`` that is not |
| 19 | + ``None``. |
| 20 | + |
| 21 | +.. py:class:: FileName |
| 22 | +
|
| 23 | + This type describes the resource names passed into the various functions |
| 24 | + in this package. For Python 3.6 and later, this is defined as |
| 25 | + ``Union[str, os.PathLike]``. For earlier versions (which don't have |
| 26 | + ``os.PathLike``), this is defined as ``str``. |
| 27 | + |
| 28 | + |
| 29 | +Functions |
| 30 | +========= |
| 31 | + |
| 32 | +.. py:function:: importlib_resources.open(package, file_name, encoding=None, errors=None) |
| 33 | +
|
| 34 | + Open for reading the resource named ``file_name`` within the ``package`` |
| 35 | + package. By default, the resource is opened for reading in binary mode. |
| 36 | + With a non-``None`` ``encoding`` argument, the resource is opened in text |
| 37 | + mode, with ``errors`` having the same meaning as for built-in |
| 38 | + :py:func:`open`. |
| 39 | + |
| 40 | + :param package: A package name or module object. See above for the API |
| 41 | + that such module objects must support. |
| 42 | + :type package: ``Package`` |
| 43 | + :param file_name: The name of the resource to open within ``package``. |
| 44 | + ``file_name`` may not contain path separators and it may |
| 45 | + not have sub-resources (i.e. it cannot be a directory). |
| 46 | + :type file_name: ``FileName`` |
| 47 | + :param encoding: When ``None``, the resource is opened in binary mode. |
| 48 | + When an encoding is given, the resource is opened in text |
| 49 | + mode. ``encoding`` has the same meaning as with |
| 50 | + :py:func:`open`. |
| 51 | + :type encoding: str |
| 52 | + :param errors: This parameter is ignored when ``encoding`` is ``None``. |
| 53 | + Otherwise it has the same meaning as with :py:func:`open`. |
| 54 | + :type errors: str |
| 55 | + :returns: an I/O stream open for reading. |
| 56 | + :rtype: ``typing.IO`` |
| 57 | + |
| 58 | + |
| 59 | +.. py:function:: importlib_resources.read(package, file_name, encoding='utf-8', errors='strict') |
| 60 | +
|
| 61 | + Read and return the contents of the resource named ``file_name`` within |
| 62 | + the ``package`` package. By default, the contents are read in UTF-8 and |
| 63 | + returned as a ``str`` (in Python 3 - ``unicode`` in Python 2). With |
| 64 | + ``encoding`` set to ``None``, the resource contents are read in binary |
| 65 | + mode and returned as ``bytes``. |
| 66 | + |
| 67 | + :param package: A package name or module object. See above for the API |
| 68 | + that such module objects must support. |
| 69 | + :type package: ``Package`` |
| 70 | + :param file_name: The name of the resource to read within ``package``. |
| 71 | + ``file_name`` may not contain path separators and it may |
| 72 | + not have sub-resources (i.e. it cannot be a directory). |
| 73 | + :type file_name: ``FileName`` |
| 74 | + :param encoding: When ``None``, the resource is read in binary mode. |
| 75 | + When an encoding is given, the resource is read in text |
| 76 | + mode. ``encoding`` has the same meaning as with |
| 77 | + :py:func:`open`. |
| 78 | + :type encoding: str |
| 79 | + :param errors: This parameter is ignored when ``encoding`` is ``None``. |
| 80 | + Otherwise it has the same meaning as with :py:func:`open`. |
| 81 | + :type errors: str |
| 82 | + :returns: the contents of the resource. |
| 83 | + :rtype: ``bytes`` or ``str`` |
| 84 | + |
| 85 | +.. py:function:: importlib_resources.path(package, file_name) |
| 86 | +
|
| 87 | + Return the path to the resource as an actual file system path. This |
| 88 | + function returns a `context manager`_ for use in a ``with``-statement. |
| 89 | + The context manager provides a :py:class:`pathlib.Path` object. |
| 90 | + |
| 91 | + Exiting the context manager cleans up any temporary file created when the |
| 92 | + resource needs to be extracted from e.g. a zip file. |
| 93 | + |
| 94 | + :param package: A package name or module object. See above for the API |
| 95 | + that such module objects must support. |
| 96 | + :type package: ``Package`` |
| 97 | + :param file_name: The name of the resource to read within ``package``. |
| 98 | + ``file_name`` may not contain path separators and it may |
| 99 | + not have sub-resources (i.e. it cannot be a directory). |
| 100 | + :type file_name: ``FileName`` |
| 101 | + :returns: A context manager for use in a ``with``-statement. Entering |
| 102 | + the context manager provides a :py:class:`pathlib.Path` |
| 103 | + object. |
| 104 | + :rtype: context manager providing a :py:class:`pathlib.Path` object |
| 105 | + |
| 106 | + |
| 107 | +.. _`context manager`: https://docs.python.org/3/library/stdtypes.html#typecontextmanager |
0 commit comments