Skip to content

Commit

Permalink
use files() API in contents()
Browse files Browse the repository at this point in the history
Signed-off-by: Filipe Laíns <lains@riseup.net>
  • Loading branch information
FFY00 committed May 12, 2021
1 parent 193ede9 commit f68da1c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
2 changes: 1 addition & 1 deletion importlib_resources/__init__.py
Expand Up @@ -3,12 +3,12 @@
from ._common import (
as_file,
files,
contents,
)

from importlib_resources._py3 import (
Package,
Resource,
contents,
is_resource,
open_binary,
open_text,
Expand Down
15 changes: 14 additions & 1 deletion importlib_resources/_common.py
Expand Up @@ -6,7 +6,7 @@
import types
import importlib

from typing import Union, Any, Optional
from typing import Union, Any, Optional, Iterable
from .abc import ResourceReader

from ._compat import wrap_spec
Expand Down Expand Up @@ -112,3 +112,16 @@ def _(path):
Degenerate behavior for pathlib.Path objects.
"""
yield path


# legacy API


def contents(package: Package) -> Iterable[str]:
"""Return an iterable of entries in `package`.
Note that not all entries are resources. Specifically, directories are
not considered resources. Use `is_resource()` on each entry returned here
to check if it is a resource or not.
"""
return [path.name for path in files(package).iterdir()]
21 changes: 2 additions & 19 deletions importlib_resources/_py3.py
Expand Up @@ -8,7 +8,7 @@
from io import BytesIO, TextIOWrapper
from pathlib import Path
from types import ModuleType
from typing import ContextManager, Iterable, Union
from typing import ContextManager, Union
from typing import cast
from typing.io import BinaryIO, TextIO
from collections.abc import Sequence
Expand Down Expand Up @@ -135,29 +135,12 @@ def is_resource(package: Package, name: str) -> bool:
reader = _common.get_resource_reader(package)
if reader is not None:
return reader.is_resource(name)
package_contents = set(contents(package))
package_contents = set(_common.contents(package))
if name not in package_contents:
return False
return (_common.from_package(package) / name).is_file()


def contents(package: Package) -> Iterable[str]:
"""Return an iterable of entries in `package`.
Note that not all entries are resources. Specifically, directories are
not considered resources. Use `is_resource()` on each entry returned here
to check if it is a resource or not.
"""
package = _common.get_package(package)
reader = _common.get_resource_reader(package)
if reader is not None:
return _ensure_sequence(reader.contents())
transversable = _common.from_package(package)
if transversable.is_dir():
return list(item.name for item in transversable.iterdir())
return []


@singledispatch
def _ensure_sequence(iterable):
return list(iterable)
Expand Down
10 changes: 5 additions & 5 deletions importlib_resources/tests/test_resource.py
Expand Up @@ -33,14 +33,14 @@ def test_contents(self):
# are not germane to this test, so just filter them out.
contents.discard('__pycache__')
self.assertEqual(
contents,
{
sorted(contents),
[
'__init__.py',
'subdirectory',
'utf-8.file',
'binary.file',
'subdirectory',
'utf-16.file',
},
'utf-8.file',
],
)


Expand Down

0 comments on commit f68da1c

Please sign in to comment.