1
- # noqa: D100
1
+ # noqa: D100,DALL000
2
2
3
3
# stdlib
4
+ import os
4
5
import sys
5
- from typing import TextIO
6
+ from typing import Any , BinaryIO , TextIO
6
7
7
8
if sys .version_info [:2 ] < (3 , 7 ): # pragma: no cover (py37+)
8
9
# 3rd party
9
10
import importlib_resources
10
- from importlib_resources ._common import normalize_path
11
11
12
12
globals ().update (importlib_resources .__dict__ )
13
13
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
+
14
40
def read_binary (package : importlib_resources .Package , resource : importlib_resources .Resource ) -> bytes :
15
41
"""
16
42
Return the binary contents of the resource.
17
43
"""
18
44
19
- return (importlib_resources .files (package ) / normalize_path (resource )).read_bytes ()
45
+ return (importlib_resources .files (package ) / _normalize_path (resource )).read_bytes ()
20
46
21
47
def open_text (
22
48
package : importlib_resources .Package ,
23
49
resource : importlib_resources .Resource ,
24
- encoding : str = ' utf-8' ,
25
- errors : str = ' strict' ,
50
+ encoding : str = " utf-8" ,
51
+ errors : str = " strict" ,
26
52
) -> TextIO :
27
53
"""
28
54
Return a file-like object opened for text reading of the resource.
29
55
"""
30
56
31
- return (importlib_resources .files (package ) / normalize_path (resource )).open (
57
+ return (importlib_resources .files (package ) / _normalize_path (resource )).open (
32
58
'r' ,
33
59
encoding = encoding ,
34
60
errors = errors ,
@@ -37,17 +63,12 @@ def open_text(
37
63
def read_text (
38
64
package : importlib_resources .Package ,
39
65
resource : importlib_resources .Resource ,
40
- encoding : str = ' utf-8' ,
41
- errors : str = ' strict' ,
66
+ encoding : str = " utf-8" ,
67
+ errors : str = " strict" ,
42
68
) -> str :
43
69
"""
44
70
Return the decoded string of the resource.
45
71
"""
46
72
47
- with importlib_resources . open_text (package , resource , encoding , errors ) as fp :
73
+ with open_text (package , resource , encoding , errors ) as fp :
48
74
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