Skip to content

Commit 2a4b00d

Browse files
committed
Extract _path_from_reader helper for cleaner implementation
1 parent adea250 commit 2a4b00d

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

importlib_resources/_py3.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ def get(package: Package, resource: Resource) -> trees.Traversable:
138138
return trees.from_package(package) / resource
139139

140140

141-
@contextmanager
142141
def path(package: Package, resource: Resource) -> Iterator[Path]:
143142
"""A context manager providing a file path object to the resource.
144143
@@ -148,17 +147,22 @@ def path(package: Package, resource: Resource) -> Iterator[Path]:
148147
raised if the file was deleted prior to the context manager
149148
exiting).
150149
"""
151-
norm_resource = _normalize_path(resource)
152150
reader = _get_resource_reader(_get_package(package))
153-
if reader is not None:
154-
with suppress(FileNotFoundError):
155-
yield Path(reader.resource_path(norm_resource))
156-
return
157-
opener_reader = reader.open_resource(norm_resource)
158-
with trees._tempfile(opener_reader.read) as res:
159-
yield res
160-
return
161-
with trees.as_file(get(package, resource)) as res:
151+
return (
152+
_path_from_reader(reader, resource)
153+
if reader else
154+
trees.as_file(get(package, resource))
155+
)
156+
157+
158+
@contextmanager
159+
def _path_from_reader(reader, resource):
160+
norm_resource = _normalize_path(resource)
161+
with suppress(FileNotFoundError):
162+
yield Path(reader.resource_path(norm_resource))
163+
return
164+
opener_reader = reader.open_resource(norm_resource)
165+
with trees._tempfile(opener_reader.read) as res:
162166
yield res
163167

164168

0 commit comments

Comments
 (0)