20
20
21
21
Package = Union [types .ModuleType , str ]
22
22
if sys .version_info >= (3 , 6 ):
23
- FileName = Union [str , os .PathLike ]
23
+ FileName = Union [str , os .PathLike ] # pragma: ge36
24
24
else :
25
- FileName = str
25
+ FileName = str # pragma: le35
26
26
27
27
28
28
def _get_package (package ) -> types .ModuleType :
29
29
if hasattr (package , '__spec__' ):
30
30
if package .__spec__ .submodule_search_locations is None :
31
- raise TypeError ("{!r} is not a package" .format (package .__spec__ .name ))
31
+ raise TypeError ("{!r} is not a package" .format (
32
+ package .__spec__ .name ))
32
33
else :
33
34
return package
34
35
else :
@@ -69,13 +70,13 @@ def open(package: Package, file_name: FileName) -> BinaryIO:
69
70
# importlib.machinery loaders are and an AttributeError for
70
71
# get_data() will make it clear what is needed from the loader.
71
72
loader = typing .cast (importlib .abc .ResourceLoader ,
72
- package .__spec__ .loader )
73
+ package .__spec__ .loader )
73
74
try :
74
75
data = loader .get_data (full_path )
75
76
except IOError :
76
77
package_name = package .__spec__ .name
77
- message = '{!r} resource not found in {!r}' .format (file_name ,
78
- package_name )
78
+ message = '{!r} resource not found in {!r}' .format (
79
+ file_name , package_name )
79
80
raise FileNotFoundError (message )
80
81
else :
81
82
return io .BytesIO (data )
@@ -92,8 +93,8 @@ def read(package: Package, file_name: FileName, encoding: str = 'utf-8',
92
93
package = _get_package (package )
93
94
# Note this is **not** builtins.open()!
94
95
with open (package , file_name ) as binary_file :
95
- # Decoding from io.TextIOWrapper() instead of str.decode() in hopes that
96
- # the former will be smarter about memory usage.
96
+ # Decoding from io.TextIOWrapper() instead of str.decode() in hopes
97
+ # that the former will be smarter about memory usage.
97
98
text_file = io .TextIOWrapper (binary_file , encoding = encoding ,
98
99
errors = errors )
99
100
return text_file .read ()
@@ -119,8 +120,8 @@ def path(package: Package, file_name: FileName) -> Iterator[pathlib.Path]:
119
120
return
120
121
except FileNotFoundError :
121
122
pass
122
- # Fall-through for both the lack of resource_path() *and* if resource_path()
123
- # raises FileNotFoundError.
123
+ # Fall-through for both the lack of resource_path() *and* if
124
+ # resource_path() raises FileNotFoundError.
124
125
package_directory = pathlib .Path (package .__spec__ .origin ).parent
125
126
file_path = package_directory / file_name
126
127
if file_path .exists ():
0 commit comments