Skip to content

Commit 19f7d1e

Browse files
committed
Merge branch 'master' into feature/traversable
2 parents 3de48de + e76da57 commit 19f7d1e

File tree

8 files changed

+49
-60
lines changed

8 files changed

+49
-60
lines changed

.gitlab-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ qa:
1111

1212
tests:
1313
script:
14-
- tox -e py27-nocov,py34-nocov,py35-nocov,py36-nocov
14+
- tox -e py27,py34,py35,py36,py37,py38
1515

1616
coverage:
1717
script:

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2017-2018 Brett Cannon, Barry Warsaw
1+
Copyright 2017-2019 Brett Cannon, Barry Warsaw
22

33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

importlib_resources/_py2.py

Lines changed: 21 additions & 22 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):
@@ -220,7 +219,7 @@ def contents(package):
220219
relpath = package_directory.relative_to(archive_path)
221220
with ZipFile(archive_path) as zf:
222221
toc = zf.namelist()
223-
subdirs_seen = set() # type: Set
222+
subdirs_seen = set()
224223
subdirs_returned = []
225224
for filename in toc:
226225
path = Path(filename)

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:

importlib_resources/docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
# General information about the project.
5555
project = 'importlib_resources'
56-
copyright = '2017-2018, Brett Cannon, Barry Warsaw'
56+
copyright = '2017-2019, Brett Cannon, Barry Warsaw'
5757
author = 'Brett Cannon, Barry Warsaw'
5858

5959
# The version info for the project you're documenting, acts as replacement for

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = importlib_resources
3-
version = attr: version.__version__
3+
version = file: importlib_resources/version.txt
44
author = Barry Warsaw
55
author_email = [email protected]
66
url = http://importlib-resources.readthedocs.io/

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[tox]
22
# Don't cover Python 3.7 since this is just a shim for that version. Do at
33
# least make sure we don't regress!
4-
envlist = {py27,py34,py35,py36}-{nocov,cov,diffcov},{py37,py38}-nocov,qa,docs
4+
envlist = {py27,py34,py35,py36}{,-cov,-diffcov},{py37,py38},qa,docs
55
skip_missing_interpreters = True
66

77

88
[testenv]
99
commands =
10-
nocov: python -m unittest discover
10+
!cov,!diffcov: python -m unittest discover
1111
cov,diffcov: python -m coverage run {[coverage]rc} -m unittest discover {posargs}
1212
cov,diffcov: python -m coverage combine {[coverage]rc}
1313
cov: python -m coverage html {[coverage]rc}

version.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)