@@ -17,11 +17,6 @@ access`_ APIs:
17
17
* ``pkg_resources.resource_listdir() ``
18
18
* ``pkg_resources.resource_isdir() ``
19
19
20
- Keep in mind that ``pkg_resources `` defines *resources * to include
21
- directories. ``importlib_resources `` does not treat directories as resources;
22
- since only files are allowed as resources, file names in the
23
- ``importlib_resources `` API may *not * include path separators (e.g. slashes).
24
-
25
20
26
21
pkg_resources.resource_filename()
27
22
=================================
@@ -34,9 +29,11 @@ that ``pkg_resources()`` also *implicitly* cleans up this temporary file,
34
29
without control over its lifetime by the programmer.
35
30
36
31
``importlib_resources `` takes a different approach. Its equivalent API is the
37
- ``path() `` function, which returns a context manager providing a
38
- :py:class: `pathlib.Path ` object. This means users have both the flexibility
39
- and responsibility to manage the lifetime of the temporary file. Note though
32
+ ``files() `` function, which returns a Traversable object implementing a
33
+ subset of the
34
+ :py:class: `pathlib.Path ` interface suitable for reading the contents and
35
+ provides a wrapper for creating a temporary file on the system in a
36
+ context whose lifetime is managed by the user. Note though
40
37
that if the resource is *already * on the file system, ``importlib_resources ``
41
38
still returns a context manager, but nothing needs to get cleaned up.
42
39
@@ -46,7 +43,8 @@ Here's an example from ``pkg_resources()``::
46
43
47
44
The best way to convert this is with the following idiom::
48
45
49
- with importlib_resources.path('my.package', 'resource.dat') as path:
46
+ ref = importlib_resources.files('my.package') / 'resource.dat'
47
+ with importlib_resources.trees.as_file(ref) as path:
50
48
# Do something with path. After the with-statement exits, any
51
49
# temporary file created will be immediately cleaned up.
52
50
@@ -56,8 +54,9 @@ to stick around for a while? One way of doing this is to use an
56
54
57
55
from contextlib import ExitStack
58
56
file_manager = ExitStack()
57
+ ref = importlib_resources.files('my.package') / 'resource.dat'
59
58
path = file_manager.enter_context(
60
- importlib_resources.path('my.package', 'resource.dat' ))
59
+ importlib_resources.trees.as_file(ref ))
61
60
62
61
Now ``path `` will continue to exist until you explicitly call
63
62
``file_manager.close() ``. What if you want the file to exist until the
@@ -67,8 +66,9 @@ process exits, or you can't pass ``file_manager`` around in your code? Use an
67
66
import atexit
68
67
file_manager = ExitStack()
69
68
atexit.register(file_manager.close)
69
+ ref = importlib_resources.files('my.package') / 'resource.dat'
70
70
path = file_manager.enter_context(
71
- importlib_resources.path('my.package', 'resource.dat' ))
71
+ importlib_resources.trees.as_file(ref ))
72
72
73
73
Assuming your Python interpreter exits gracefully, the temporary file will be
74
74
cleaned up when Python exits.
@@ -86,7 +86,8 @@ bytes. E.g.::
86
86
87
87
The equivalent code in ``importlib_resources `` is pretty straightforward::
88
88
89
- with importlib_resources.open_binary('my.package', 'resource.dat') as fp:
89
+ ref = importlib_resources.files('my.package').joinpath('resource.dat')
90
+ with ref.open() as fp:
90
91
my_bytes = fp.read()
91
92
92
93
@@ -103,7 +104,8 @@ following example is often written for clarity as::
103
104
104
105
This can be easily rewritten like so::
105
106
106
- contents = importlib_resources.read_binary('my.package', 'resource.dat')
107
+ ref = importlib_resources.files('my.package').joinpath('resource.dat')
108
+ contents = f.read_bytes()
107
109
108
110
109
111
pkg_resources.resource_listdir()
@@ -117,19 +119,18 @@ but it does not recurse into subdirectories, e.g.::
117
119
118
120
This is easily rewritten using the following idiom::
119
121
120
- for entry in importlib_resources.contents ('my.package.subpackage'):
121
- print(entry)
122
+ for entry in importlib_resources.files ('my.package.subpackage').iterdir( ):
123
+ print(entry.name )
122
124
123
125
Note:
124
126
125
- * ``pkg_resources `` does not require ``subpackage `` to be a Python package,
126
- but ``importlib_resources `` does.
127
- * ``importlib_resources.contents() `` returns an iterator, not a concrete
128
- sequence.
127
+ * ``Traversable.iterdir() `` returns *all * the entries in the
128
+ subpackage, i.e. both resources (files) and non-resources (directories).
129
+ * ``Traversable.iterdir() `` returns additional traversable objects, which if
130
+ directories can also be iterated over (recursively).
131
+ * ``Traversable.iterdir() ``, like ``pathlib.Path `` returns an iterator, not a
132
+ concrete sequence.
129
133
* The order in which the elements are returned is undefined.
130
- * ``importlib_resources.contents() `` returns *all * the entries in the
131
- subpackage, i.e. both resources (files) and non-resources (directories). As
132
- with ``pkg_resources.listdir() `` it does not recurse.
133
134
134
135
135
136
pkg_resources.resource_isdir()
@@ -141,20 +142,10 @@ a package is a directory or not::
141
142
if pkg_resources.resource_isdir('my.package', 'resource'):
142
143
print('A directory')
143
144
144
- Because ``importlib_resources `` explicitly does not define directories as
145
- resources, there's no direct equivalent. However, you can ask whether a
146
- particular resource exists inside a package, and since directories are not
147
- resources you can infer whether the resource is a directory or a file. Here
148
- is a way to do that::
145
+ The ``importlib_resources `` equivalent is straightforward::
149
146
150
- from importlib_resources import contents, is_resource
151
- if 'resource' in contents('my.package') and \
152
- not is_resource('my.package', 'resource'):
153
- print('It must be a directory')
154
-
155
- The reason you have to do it this way and not just call
156
- ``not is_resource('my.package', 'resource') `` is because this conditional will
157
- also return False when ``resource `` is not an entry in ``my.package ``.
147
+ if importlib_resources.files('my.package').joinpath('resource').isdir():
148
+ print('A directory')
158
149
159
150
160
151
.. _`basic resource access` : http://setuptools.readthedocs.io/en/latest/pkg_resources.html#basic-resource-access
0 commit comments