Skip to content

Commit

Permalink
Move normalize_path to _legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Oct 22, 2021
1 parent 550ee51 commit 64d03de
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
15 changes: 1 addition & 14 deletions importlib_resources/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import types
import importlib

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

from ._compat import wrap_spec
Expand All @@ -23,19 +23,6 @@ def files(package):
return from_package(get_package(package))


def normalize_path(path):
# type: (Any) -> str
"""Normalize a path by ensuring it is a string.
If the resulting string contains path separators, an exception is raised.
"""
str_path = str(path)
parent, file_name = os.path.split(str_path)
if parent:
raise ValueError(f'{path!r} must be only a file name')
return file_name


def get_resource_reader(package):
# type: (types.ModuleType) -> Optional[ResourceReader]
"""
Expand Down
25 changes: 19 additions & 6 deletions importlib_resources/_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import types
import warnings

from typing import Union, Iterable, ContextManager, BinaryIO, TextIO
from typing import Union, Iterable, ContextManager, BinaryIO, TextIO, Any

from . import _common

Expand All @@ -27,16 +27,29 @@ def wrapper(*args, **kwargs):
return wrapper


def normalize_path(path):
# type: (Any) -> str
"""Normalize a path by ensuring it is a string.
If the resulting string contains path separators, an exception is raised.
"""
str_path = str(path)
parent, file_name = os.path.split(str_path)
if parent:
raise ValueError(f'{path!r} must be only a file name')
return file_name


@deprecated
def open_binary(package: Package, resource: Resource) -> BinaryIO:
"""Return a file-like object opened for binary reading of the resource."""
return (_common.files(package) / _common.normalize_path(resource)).open('rb')
return (_common.files(package) / normalize_path(resource)).open('rb')


@deprecated
def read_binary(package: Package, resource: Resource) -> bytes:
"""Return the binary contents of the resource."""
return (_common.files(package) / _common.normalize_path(resource)).read_bytes()
return (_common.files(package) / normalize_path(resource)).read_bytes()


@deprecated
Expand All @@ -47,7 +60,7 @@ def open_text(
errors: str = 'strict',
) -> TextIO:
"""Return a file-like object opened for text reading of the resource."""
return (_common.files(package) / _common.normalize_path(resource)).open(
return (_common.files(package) / normalize_path(resource)).open(
'r', encoding=encoding, errors=errors
)

Expand Down Expand Up @@ -85,7 +98,7 @@ def is_resource(package: Package, name: str) -> bool:
Directories are *not* resources.
"""
resource = _common.normalize_path(name)
resource = normalize_path(name)
return any(
traversable.name == resource and traversable.is_file()
for traversable in _common.files(package).iterdir()
Expand All @@ -105,4 +118,4 @@ def path(
raised if the file was deleted prior to the context manager
exiting).
"""
return _common.as_file(_common.files(package) / _common.normalize_path(resource))
return _common.as_file(_common.files(package) / normalize_path(resource))

0 comments on commit 64d03de

Please sign in to comment.