@@ -34,14 +34,14 @@ def _normalize_path(path):
34
34
return file_name
35
35
36
36
37
- def open (package , file_name , encoding = None , errors = None ):
37
+ def open (package , resource , encoding = None , errors = None ):
38
38
"""Return a file-like object opened for reading of the resource."""
39
- file_name = _normalize_path (file_name )
39
+ resource = _normalize_path (resource )
40
40
package = _get_package (package )
41
41
# Using pathlib doesn't work well here due to the lack of 'strict' argument
42
42
# for pathlib.Path.resolve() prior to Python 3.6.
43
43
package_path = os .path .dirname (package .__file__ )
44
- relative_path = os .path .join (package_path , file_name )
44
+ relative_path = os .path .join (package_path , resource )
45
45
full_path = os .path .abspath (relative_path )
46
46
if encoding is None :
47
47
args = dict (mode = 'rb' )
@@ -63,30 +63,30 @@ def open(package, file_name, encoding=None, errors=None):
63
63
except (IOError , AttributeError ):
64
64
package_name = package .__name__
65
65
message = '{!r} resource not found in {!r}' .format (
66
- file_name , package_name )
66
+ resource , package_name )
67
67
raise FileNotFoundError (message )
68
68
else :
69
69
return _wrap_file (BytesIO (data ), encoding , errors )
70
70
71
71
72
- def read (package , file_name , encoding = 'utf-8' , errors = 'strict' ):
72
+ def read (package , resource , encoding = 'utf-8' , errors = 'strict' ):
73
73
"""Return the decoded string of the resource.
74
74
75
75
The decoding-related arguments have the same semantics as those of
76
76
bytes.decode().
77
77
"""
78
- file_name = _normalize_path (file_name )
78
+ resource = _normalize_path (resource )
79
79
package = _get_package (package )
80
80
# Note this is **not** builtins.open()!
81
- with open (package , file_name ) as binary_file :
81
+ with open (package , resource ) as binary_file :
82
82
contents = binary_file .read ()
83
83
if encoding is None :
84
84
return contents
85
85
return contents .decode (encoding = encoding , errors = errors )
86
86
87
87
88
88
@contextmanager
89
- def path (package , file_name ):
89
+ def path (package , resource ):
90
90
"""A context manager providing a file path object to the resource.
91
91
92
92
If the resource does not already exist on its own on the file system,
@@ -95,10 +95,10 @@ def path(package, file_name):
95
95
raised if the file was deleted prior to the context manager
96
96
exiting).
97
97
"""
98
- file_name = _normalize_path (file_name )
98
+ resource = _normalize_path (resource )
99
99
package = _get_package (package )
100
100
package_directory = Path (package .__file__ ).parent
101
- file_path = package_directory / file_name
101
+ file_path = package_directory / resource
102
102
# If the file actually exists on the file system, just return it.
103
103
# Otherwise, it's probably in a zip file, so we need to create a temporary
104
104
# file and copy the contents into that file, hence the contextmanager to
@@ -107,7 +107,7 @@ def path(package, file_name):
107
107
yield file_path
108
108
else :
109
109
# Note this is **not** builtins.open()!
110
- with open (package , file_name ) as fileobj :
110
+ with open (package , resource ) as fileobj :
111
111
data = fileobj .read ()
112
112
# Not using tempfile.NamedTemporaryFile as it leads to deeper 'try'
113
113
# blocks due to the need to close the temporary file to work on Windows
@@ -124,13 +124,13 @@ def path(package, file_name):
124
124
pass
125
125
126
126
127
- def is_resource (package , file_name ):
128
- """True if file_name is a resource inside package.
127
+ def is_resource (package , name ):
128
+ """True if name is a resource inside package.
129
129
130
130
Directories are *not* resources.
131
131
"""
132
132
package = _get_package (package )
133
- _normalize_path (file_name )
133
+ _normalize_path (name )
134
134
try :
135
135
package_contents = set (contents (package ))
136
136
except OSError as error :
@@ -142,12 +142,12 @@ def is_resource(package, file_name):
142
142
# worth it.
143
143
raise # pragma: ge3
144
144
return False
145
- if file_name not in package_contents :
145
+ if name not in package_contents :
146
146
return False
147
147
# Just because the given file_name lives as an entry in the package's
148
148
# contents doesn't necessarily mean it's a resource. Directories are not
149
149
# resources, so let's try to find out if it's a directory or not.
150
- path = Path (package .__file__ ).parent / file_name
150
+ path = Path (package .__file__ ).parent / name
151
151
if path .is_file ():
152
152
return True
153
153
if path .is_dir ():
@@ -161,7 +161,7 @@ def is_resource(package, file_name):
161
161
with ZipFile (archive_path ) as zf :
162
162
toc = zf .namelist ()
163
163
relpath = package_directory .relative_to (archive_path )
164
- candidate_path = relpath / file_name
164
+ candidate_path = relpath / name
165
165
for entry in toc : # pragma: nobranch
166
166
try :
167
167
relative_to_candidate = Path (entry ).relative_to (candidate_path )
0 commit comments