Refactor deprecated pkg_resources usage

- Locate resources relative to `__file__`
- Support egg/zip with `open_file_for_reading`
This commit is contained in:
Thomas Holder
2023-12-10 11:59:21 +01:00
parent a62e2a4dae
commit 50f62c61fb
4 changed files with 51 additions and 10 deletions

29
tests/test_input.py Normal file
View File

@@ -0,0 +1,29 @@
import propka.input as m
import zipfile
def test_open_file_for_reading(tmp_path):
path = tmp_path / "tmp.txt"
path.write_text("One\nTwo\nThree\n")
# str
with m.open_file_for_reading(str(path)) as outer:
assert outer.read() == "One\nTwo\nThree\n"
assert outer.closed
# Path
with m.open_file_for_reading(path) as outer:
# TextIO
with m.open_file_for_reading(outer) as inner:
assert inner.readline() == "One\n"
assert not outer.closed
assert outer.readline() == "Two\n"
assert outer.closed
def test_open_file_for_reading__zipfile(tmp_path):
zippath = tmp_path / "tmp.zip"
arcname = "foo/bar.txt"
with zipfile.ZipFile(zippath, "w") as ziphandle:
ziphandle.writestr(arcname, "One\nTwo\nThree\n")
with m.open_file_for_reading(zippath / arcname) as outer:
assert outer.readline() == "One\n"
assert outer.closed