Skip to content

Commit f68da1c

Browse files
committed
use files() API in contents()
Signed-off-by: Filipe Laíns <[email protected]>
1 parent 193ede9 commit f68da1c

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

importlib_resources/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
from ._common import (
44
as_file,
55
files,
6+
contents,
67
)
78

89
from importlib_resources._py3 import (
910
Package,
1011
Resource,
11-
contents,
1212
is_resource,
1313
open_binary,
1414
open_text,

importlib_resources/_common.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import types
77
import importlib
88

9-
from typing import Union, Any, Optional
9+
from typing import Union, Any, Optional, Iterable
1010
from .abc import ResourceReader
1111

1212
from ._compat import wrap_spec
@@ -112,3 +112,16 @@ def _(path):
112112
Degenerate behavior for pathlib.Path objects.
113113
"""
114114
yield path
115+
116+
117+
# legacy API
118+
119+
120+
def contents(package: Package) -> Iterable[str]:
121+
"""Return an iterable of entries in `package`.
122+
123+
Note that not all entries are resources. Specifically, directories are
124+
not considered resources. Use `is_resource()` on each entry returned here
125+
to check if it is a resource or not.
126+
"""
127+
return [path.name for path in files(package).iterdir()]

importlib_resources/_py3.py

+2-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from io import BytesIO, TextIOWrapper
99
from pathlib import Path
1010
from types import ModuleType
11-
from typing import ContextManager, Iterable, Union
11+
from typing import ContextManager, Union
1212
from typing import cast
1313
from typing.io import BinaryIO, TextIO
1414
from collections.abc import Sequence
@@ -135,29 +135,12 @@ def is_resource(package: Package, name: str) -> bool:
135135
reader = _common.get_resource_reader(package)
136136
if reader is not None:
137137
return reader.is_resource(name)
138-
package_contents = set(contents(package))
138+
package_contents = set(_common.contents(package))
139139
if name not in package_contents:
140140
return False
141141
return (_common.from_package(package) / name).is_file()
142142

143143

144-
def contents(package: Package) -> Iterable[str]:
145-
"""Return an iterable of entries in `package`.
146-
147-
Note that not all entries are resources. Specifically, directories are
148-
not considered resources. Use `is_resource()` on each entry returned here
149-
to check if it is a resource or not.
150-
"""
151-
package = _common.get_package(package)
152-
reader = _common.get_resource_reader(package)
153-
if reader is not None:
154-
return _ensure_sequence(reader.contents())
155-
transversable = _common.from_package(package)
156-
if transversable.is_dir():
157-
return list(item.name for item in transversable.iterdir())
158-
return []
159-
160-
161144
@singledispatch
162145
def _ensure_sequence(iterable):
163146
return list(iterable)

importlib_resources/tests/test_resource.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ def test_contents(self):
3333
# are not germane to this test, so just filter them out.
3434
contents.discard('__pycache__')
3535
self.assertEqual(
36-
contents,
37-
{
36+
sorted(contents),
37+
[
3838
'__init__.py',
39-
'subdirectory',
40-
'utf-8.file',
4139
'binary.file',
40+
'subdirectory',
4241
'utf-16.file',
43-
},
42+
'utf-8.file',
43+
],
4444
)
4545

4646

0 commit comments

Comments
 (0)