Skip to content

Commit b3a3481

Browse files
committed
Merge branch 'refactor/misc' into 'master'
Miscellaneous refactorings See merge request python-devs/importlib_resources!73
2 parents 72d52f5 + 89c3b07 commit b3a3481

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

importlib_resources/_py2.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ def _normalize_path(path):
3737
parent, file_name = os.path.split(str_path)
3838
if parent:
3939
raise ValueError("{!r} must be only a file name".format(path))
40-
else:
41-
return file_name
40+
return file_name
4241

4342

4443
def open_binary(package, resource):
@@ -68,8 +67,7 @@ def open_binary(package, resource):
6867
message = '{!r} resource not found in {!r}'.format(
6968
resource, package_name)
7069
raise FileNotFoundError(message)
71-
else:
72-
return BytesIO(data)
70+
return BytesIO(data)
7371

7472

7573
def open_text(package, resource, encoding='utf-8', errors='strict'):
@@ -113,27 +111,28 @@ def path(package, resource):
113111
package_directory = Path(package.__file__).parent
114112
file_path = package_directory / resource
115113
# If the file actually exists on the file system, just return it.
114+
if file_path.exists():
115+
yield file_path
116+
return
117+
116118
# Otherwise, it's probably in a zip file, so we need to create a temporary
117119
# file and copy the contents into that file, hence the contextmanager to
118120
# clean up the temp file resource.
119-
if file_path.exists():
120-
yield file_path
121-
else:
122-
with open_binary(package, resource) as fp:
123-
data = fp.read()
124-
# Not using tempfile.NamedTemporaryFile as it leads to deeper 'try'
125-
# blocks due to the need to close the temporary file to work on Windows
126-
# properly.
127-
fd, raw_path = tempfile.mkstemp()
121+
with open_binary(package, resource) as fp:
122+
data = fp.read()
123+
# Not using tempfile.NamedTemporaryFile as it leads to deeper 'try'
124+
# blocks due to the need to close the temporary file to work on Windows
125+
# properly.
126+
fd, raw_path = tempfile.mkstemp()
127+
try:
128+
os.write(fd, data)
129+
os.close(fd)
130+
yield Path(raw_path)
131+
finally:
128132
try:
129-
os.write(fd, data)
130-
os.close(fd)
131-
yield Path(raw_path)
132-
finally:
133-
try:
134-
os.remove(raw_path)
135-
except FileNotFoundError:
136-
pass
133+
os.remove(raw_path)
134+
except FileNotFoundError:
135+
pass
137136

138137

139138
def is_resource(package, name):

importlib_resources/_py3.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def _normalize_path(path) -> str:
5050
parent, file_name = os.path.split(str_path)
5151
if parent:
5252
raise ValueError('{!r} must be only a file name'.format(path))
53-
else:
54-
return file_name
53+
return file_name
5554

5655

5756
def _get_resource_reader(
@@ -96,8 +95,7 @@ def open_binary(package: Package, resource: Resource) -> BinaryIO:
9695
message = '{!r} resource not found in {!r}'.format(
9796
resource, package_name)
9897
raise FileNotFoundError(message)
99-
else:
100-
return BytesIO(data)
98+
return BytesIO(data)
10199

102100

103101
def open_text(package: Package,
@@ -146,33 +144,34 @@ def path(package: Package, resource: Resource) -> Iterator[Path]:
146144
package = _get_package(package)
147145
reader = _get_resource_reader(package)
148146
if reader is not None:
149-
try:
147+
with suppress(FileNotFoundError):
150148
yield Path(reader.resource_path(resource))
151149
return
152-
except FileNotFoundError:
153-
pass
154150
# Fall-through for both the lack of resource_path() *and* if
155151
# resource_path() raises FileNotFoundError.
156152
package_directory = Path(package.__spec__.origin).parent
157153
file_path = package_directory / resource
154+
# If the file actually exists on the file system, just return it.
158155
if file_path.exists():
159156
yield file_path
160-
else:
161-
with open_binary(package, resource) as fp:
162-
data = fp.read()
163-
# Not using tempfile.NamedTemporaryFile as it leads to deeper 'try'
164-
# blocks due to the need to close the temporary file to work on
165-
# Windows properly.
166-
fd, raw_path = tempfile.mkstemp()
167-
try:
168-
os.write(fd, data)
169-
os.close(fd)
170-
yield Path(raw_path)
171-
finally:
172-
try:
173-
os.remove(raw_path)
174-
except FileNotFoundError:
175-
pass
157+
return
158+
159+
# Otherwise, it's probably in a zip file, so we need to create a temporary
160+
# file and copy the contents into that file, hence the contextmanager to
161+
# clean up the temp file resource.
162+
with open_binary(package, resource) as fp:
163+
data = fp.read()
164+
# Not using tempfile.NamedTemporaryFile as it leads to deeper 'try'
165+
# blocks due to the need to close the temporary file to work on
166+
# Windows properly.
167+
fd, raw_path = tempfile.mkstemp()
168+
try:
169+
os.write(fd, data)
170+
os.close(fd)
171+
yield Path(raw_path)
172+
finally:
173+
with suppress(FileNotFoundError):
174+
os.remove(raw_path)
176175

177176

178177
def is_resource(package: Package, name: str) -> bool:

0 commit comments

Comments
 (0)