@@ -34,14 +34,14 @@ def _normalize_path(path):
3434 return file_name
3535
3636
37- def open (package , file_name , encoding = None , errors = None ):
37+ def open (package , resource , encoding = None , errors = None ):
3838 """Return a file-like object opened for reading of the resource."""
39- file_name = _normalize_path (file_name )
39+ resource = _normalize_path (resource )
4040 package = _get_package (package )
4141 # Using pathlib doesn't work well here due to the lack of 'strict' argument
4242 # for pathlib.Path.resolve() prior to Python 3.6.
4343 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 )
4545 full_path = os .path .abspath (relative_path )
4646 if encoding is None :
4747 args = dict (mode = 'rb' )
@@ -63,30 +63,30 @@ def open(package, file_name, encoding=None, errors=None):
6363 except (IOError , AttributeError ):
6464 package_name = package .__name__
6565 message = '{!r} resource not found in {!r}' .format (
66- file_name , package_name )
66+ resource , package_name )
6767 raise FileNotFoundError (message )
6868 else :
6969 return _wrap_file (BytesIO (data ), encoding , errors )
7070
7171
72- def read (package , file_name , encoding = 'utf-8' , errors = 'strict' ):
72+ def read (package , resource , encoding = 'utf-8' , errors = 'strict' ):
7373 """Return the decoded string of the resource.
7474
7575 The decoding-related arguments have the same semantics as those of
7676 bytes.decode().
7777 """
78- file_name = _normalize_path (file_name )
78+ resource = _normalize_path (resource )
7979 package = _get_package (package )
8080 # Note this is **not** builtins.open()!
81- with open (package , file_name ) as binary_file :
81+ with open (package , resource ) as binary_file :
8282 contents = binary_file .read ()
8383 if encoding is None :
8484 return contents
8585 return contents .decode (encoding = encoding , errors = errors )
8686
8787
8888@contextmanager
89- def path (package , file_name ):
89+ def path (package , resource ):
9090 """A context manager providing a file path object to the resource.
9191
9292 If the resource does not already exist on its own on the file system,
@@ -95,10 +95,10 @@ def path(package, file_name):
9595 raised if the file was deleted prior to the context manager
9696 exiting).
9797 """
98- file_name = _normalize_path (file_name )
98+ resource = _normalize_path (resource )
9999 package = _get_package (package )
100100 package_directory = Path (package .__file__ ).parent
101- file_path = package_directory / file_name
101+ file_path = package_directory / resource
102102 # If the file actually exists on the file system, just return it.
103103 # Otherwise, it's probably in a zip file, so we need to create a temporary
104104 # file and copy the contents into that file, hence the contextmanager to
@@ -107,7 +107,7 @@ def path(package, file_name):
107107 yield file_path
108108 else :
109109 # Note this is **not** builtins.open()!
110- with open (package , file_name ) as fileobj :
110+ with open (package , resource ) as fileobj :
111111 data = fileobj .read ()
112112 # Not using tempfile.NamedTemporaryFile as it leads to deeper 'try'
113113 # blocks due to the need to close the temporary file to work on Windows
@@ -124,13 +124,13 @@ def path(package, file_name):
124124 pass
125125
126126
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.
129129
130130 Directories are *not* resources.
131131 """
132132 package = _get_package (package )
133- _normalize_path (file_name )
133+ _normalize_path (name )
134134 try :
135135 package_contents = set (contents (package ))
136136 except OSError as error :
@@ -142,12 +142,12 @@ def is_resource(package, file_name):
142142 # worth it.
143143 raise # pragma: ge3
144144 return False
145- if file_name not in package_contents :
145+ if name not in package_contents :
146146 return False
147147 # Just because the given file_name lives as an entry in the package's
148148 # contents doesn't necessarily mean it's a resource. Directories are not
149149 # 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
151151 if path .is_file ():
152152 return True
153153 if path .is_dir ():
@@ -161,7 +161,7 @@ def is_resource(package, file_name):
161161 with ZipFile (archive_path ) as zf :
162162 toc = zf .namelist ()
163163 relpath = package_directory .relative_to (archive_path )
164- candidate_path = relpath / file_name
164+ candidate_path = relpath / name
165165 for entry in toc : # pragma: nobranch
166166 try :
167167 relative_to_candidate = Path (entry ).relative_to (candidate_path )
0 commit comments