Skip to content

Commit 3d73f64

Browse files
committed
Use helper functions for Python 3.11 and later.
1 parent 4e586a4 commit 3d73f64

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

domdf_python_tools/compat/importlib_resources.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,60 @@
1-
# noqa: D100
1+
# noqa: D100,DALL000
22

33
# stdlib
4+
import os
45
import sys
5-
from typing import TextIO
6+
from typing import Any, BinaryIO, TextIO
67

78
if sys.version_info[:2] < (3, 7): # pragma: no cover (py37+)
89
# 3rd party
910
import importlib_resources
10-
from importlib_resources._common import normalize_path
1111

1212
globals().update(importlib_resources.__dict__)
1313

14+
else: # pragma: no cover (<py39)
15+
# stdlib
16+
import importlib.resources
17+
globals().update(importlib.resources.__dict__)
18+
19+
if not ((3, 7) <= sys.version_info < (3, 11)): # pragma: no cover (py37 OR py38 OR py39 OR py310):
20+
21+
def _normalize_path(path: Any) -> str:
22+
"""
23+
Normalize a path by ensuring it is a string.
24+
25+
If the resulting string contains path separators, an exception is raised.
26+
"""
27+
28+
parent, file_name = os.path.split(str(path))
29+
if parent:
30+
raise ValueError(f'{path!r} must be only a file name')
31+
return file_name
32+
33+
def open_binary(package: importlib_resources.Package, resource: importlib_resources.Resource) -> BinaryIO:
34+
"""
35+
Return a file-like object opened for binary reading of the resource.
36+
"""
37+
38+
return (importlib_resources.files(package) / _normalize_path(resource)).open("rb")
39+
1440
def read_binary(package: importlib_resources.Package, resource: importlib_resources.Resource) -> bytes:
1541
"""
1642
Return the binary contents of the resource.
1743
"""
1844

19-
return (importlib_resources.files(package) / normalize_path(resource)).read_bytes()
45+
return (importlib_resources.files(package) / _normalize_path(resource)).read_bytes()
2046

2147
def open_text(
2248
package: importlib_resources.Package,
2349
resource: importlib_resources.Resource,
24-
encoding: str = 'utf-8',
25-
errors: str = 'strict',
50+
encoding: str = "utf-8",
51+
errors: str = "strict",
2652
) -> TextIO:
2753
"""
2854
Return a file-like object opened for text reading of the resource.
2955
"""
3056

31-
return (importlib_resources.files(package) / normalize_path(resource)).open(
57+
return (importlib_resources.files(package) / _normalize_path(resource)).open(
3258
'r',
3359
encoding=encoding,
3460
errors=errors,
@@ -37,17 +63,12 @@ def open_text(
3763
def read_text(
3864
package: importlib_resources.Package,
3965
resource: importlib_resources.Resource,
40-
encoding: str = 'utf-8',
41-
errors: str = 'strict',
66+
encoding: str = "utf-8",
67+
errors: str = "strict",
4268
) -> str:
4369
"""
4470
Return the decoded string of the resource.
4571
"""
4672

47-
with importlib_resources.open_text(package, resource, encoding, errors) as fp:
73+
with open_text(package, resource, encoding, errors) as fp:
4874
return fp.read()
49-
50-
else: # pragma: no cover (<py39)
51-
# stdlib
52-
import importlib.resources
53-
globals().update(importlib.resources.__dict__)

0 commit comments

Comments
 (0)