Skip to content

Commit

Permalink
legacy: move legacy API to separate module
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 Jun 26, 2021
1 parent ef2f7c2 commit e016e66
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 81 deletions.
7 changes: 5 additions & 2 deletions importlib_resources/__init__.py
Expand Up @@ -3,15 +3,18 @@
from ._common import (
as_file,
files,
Package,
Resource,
)

from ._legacy import (
contents,
open_binary,
read_binary,
open_text,
read_text,
is_resource,
path,
Package,
Resource,
)

from importlib_resources.abc import ResourceReader
Expand Down
80 changes: 1 addition & 79 deletions importlib_resources/_common.py
Expand Up @@ -6,8 +6,7 @@
import types
import importlib

from typing import Union, Any, Optional, Iterable, ContextManager
from typing.io import BinaryIO, TextIO
from typing import Union, Any, Optional
from .abc import ResourceReader, Traversable

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


# legacy API


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


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


def open_text(
package: Package,
resource: Resource,
encoding: str = 'utf-8',
errors: str = 'strict',
) -> TextIO:
"""Return a file-like object opened for text reading of the resource."""
return (files(package) / normalize_path(resource)).open(
'r', encoding=encoding, errors=errors
)


def read_text(
package: Package,
resource: Resource,
encoding: str = 'utf-8',
errors: str = 'strict',
) -> str:
"""Return the decoded string of the resource.
The decoding-related arguments have the same semantics as those of
bytes.decode().
"""
with open_text(package, resource, encoding, errors) as fp:
return fp.read()


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()]


def is_resource(package: Package, name: str) -> bool:
"""True if `name` is a resource inside `package`.
Directories are *not* resources.
"""
resource = normalize_path(name)
return any(
traversable.name == resource and traversable.is_file()
for traversable in files(package).iterdir()
)


def path(
package: Package,
resource: Resource,
) -> ContextManager[pathlib.Path]:
"""A context manager providing a file path object to the resource.
If the resource does not already exist on its own on the file system,
a temporary file will be created. If the file was created, the file
will be deleted upon exiting the context manager (no exception is
raised if the file was deleted prior to the context manager
exiting).
"""
return as_file(files(package) / normalize_path(resource))
85 changes: 85 additions & 0 deletions importlib_resources/_legacy.py
@@ -0,0 +1,85 @@
import os
import pathlib
import types

from typing import Union, Iterable, ContextManager
from typing.io import BinaryIO, TextIO

from . import _common

Package = Union[types.ModuleType, str]
Resource = Union[str, os.PathLike]


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')


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()


def open_text(
package: Package,
resource: Resource,
encoding: str = 'utf-8',
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(
'r', encoding=encoding, errors=errors
)


def read_text(
package: Package,
resource: Resource,
encoding: str = 'utf-8',
errors: str = 'strict',
) -> str:
"""Return the decoded string of the resource.
The decoding-related arguments have the same semantics as those of
bytes.decode().
"""
with open_text(package, resource, encoding, errors) as fp:
return fp.read()


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 _common.files(package).iterdir()]


def is_resource(package: Package, name: str) -> bool:
"""True if `name` is a resource inside `package`.
Directories are *not* resources.
"""
resource = _common.normalize_path(name)
return any(
traversable.name == resource and traversable.is_file()
for traversable in _common.files(package).iterdir()
)


def path(
package: Package,
resource: Resource,
) -> ContextManager[pathlib.Path]:
"""A context manager providing a file path object to the resource.
If the resource does not already exist on its own on the file system,
a temporary file will be created. If the file was created, the file
will be deleted upon exiting the context manager (no exception is
raised if the file was deleted prior to the context manager
exiting).
"""
return _common.as_file(_common.files(package) / _common.normalize_path(resource))

0 comments on commit e016e66

Please sign in to comment.