Skip to content

Commit 5e79870

Browse files
authored
Remove pkg_resources (fixes #650) (#690)
1 parent 99e86b6 commit 5e79870

File tree

6 files changed

+50
-33
lines changed

6 files changed

+50
-33
lines changed

gramps_webapi/api/image.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
import os
2424
import shutil
2525
import tempfile
26+
from importlib.resources import as_file, files
2627
from pathlib import Path
2728
from typing import BinaryIO, Callable
2829

2930
import ffmpeg
3031
from pdf2image import convert_from_path
3132
from PIL import Image, ImageOps
3233
from PIL.Image import Image as ImageType
33-
from pkg_resources import resource_filename # type: ignore[import-untyped]
3434

3535
from gramps_webapi.const import MIME_PDF
3636
from gramps_webapi.types import FilenameOrPath
@@ -230,12 +230,11 @@ def detect_faces(stream: BinaryIO) -> list[tuple[float, float, float, float]]:
230230
assert cv_image is not None, "cv_image is None" # for type checker
231231

232232
# Load the YuNet model
233-
model_path = resource_filename(
234-
"gramps_webapi", "data/face_detection_yunet_2023mar.onnx"
235-
)
236-
face_detector = cv2.FaceDetectorYN.create(
237-
model_path, "", (320, 320), score_threshold=0.5
238-
)
233+
ref = files("gramps_webapi") / "data/face_detection_yunet_2023mar.onnx"
234+
with as_file(ref) as model_path:
235+
face_detector = cv2.FaceDetectorYN.create(
236+
str(model_path), "", (320, 320), score_threshold=0.5
237+
)
239238

240239
# Set input image size for YuNet
241240
height, width, _ = cv_image.shape

gramps_webapi/const.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,40 @@
1919

2020
"""Constants for the web API."""
2121

22+
import atexit
2223
import shutil
24+
from contextlib import ExitStack
25+
from importlib.resources import as_file, files
2326

2427
import gramps.gen.lib as lib
2528
from gramps.gen.plug import CATEGORY_DRAW, CATEGORY_GRAPHVIZ, CATEGORY_TEXT
26-
from pkg_resources import resource_filename # type: ignore[import-untyped]
2729

2830
from ._version import __version__ as VERSION
2931

3032
# the value of the TREE config option that enables multi-tree support
3133
TREE_MULTI = "*"
3234

33-
# files
34-
TEST_CONFIG = resource_filename("gramps_webapi", "data/test.cfg")
35-
TEST_AUTH_CONFIG = resource_filename("gramps_webapi", "data/test_auth.cfg")
36-
TEST_EXAMPLE_GRAMPS_CONFIG = resource_filename(
35+
# Helper function to get resource file paths with managed cleanup
36+
_file_manager = ExitStack()
37+
atexit.register(_file_manager.close)
38+
39+
40+
def _get_resource_path(package: str, resource_path: str) -> str:
41+
"""Get the file system path for a package resource with managed cleanup."""
42+
ref = files(package) / resource_path
43+
return str(_file_manager.enter_context(as_file(ref)))
44+
45+
46+
# Configuration files
47+
TEST_CONFIG = _get_resource_path("gramps_webapi", "data/test.cfg")
48+
TEST_AUTH_CONFIG = _get_resource_path("gramps_webapi", "data/test_auth.cfg")
49+
TEST_EXAMPLE_GRAMPS_CONFIG = _get_resource_path(
3750
"gramps_webapi", "data/example_gramps.cfg"
3851
)
39-
TEST_EXAMPLE_GRAMPS_AUTH_CONFIG = resource_filename(
52+
TEST_EXAMPLE_GRAMPS_AUTH_CONFIG = _get_resource_path(
4053
"gramps_webapi", "data/example_gramps_auth.cfg"
4154
)
42-
TEST_EMPTY_GRAMPS_AUTH_CONFIG = resource_filename(
55+
TEST_EMPTY_GRAMPS_AUTH_CONFIG = _get_resource_path(
4356
"gramps_webapi", "data/empty_gramps_auth.cfg"
4457
)
4558

tests/test_cli.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,15 @@ def test_search_reindex_full_notree(self):
215215
def test_search_reindex_with_broken_progress_callback(self):
216216
"""Test that would catch the original bug where progress_callback_count was undefined."""
217217
tree = WebDbManager(name=self.name).dirname
218-
218+
219219
# Temporarily break the progress callback by patching it to raise an error
220-
with patch('gramps_webapi.__main__.progress_callback_count_factory') as mock_factory:
221-
mock_factory.side_effect = NameError("name 'progress_callback_count' is not defined")
222-
220+
with patch(
221+
"gramps_webapi.__main__.progress_callback_count_factory"
222+
) as mock_factory:
223+
mock_factory.side_effect = NameError(
224+
"name 'progress_callback_count' is not defined"
225+
)
226+
223227
result = self.runner.invoke(
224228
cli,
225229
[

tests/test_endpoints/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020
"""Tests for the `gramps_webapi.api` module."""
2121

2222
import os
23-
import shutil
23+
from importlib.resources import as_file, files
2424
from unittest.mock import patch
2525

2626
import yaml
2727
from jsonschema import RefResolver
28-
from pkg_resources import resource_filename
2928

3029
from gramps_webapi.api.search import get_search_indexer
3130
from gramps_webapi.app import create_app
@@ -41,8 +40,10 @@
4140
from gramps_webapi.dbmanager import WebDbManager
4241
from tests import TEST_GRAMPSHOME, ExampleDbSQLite
4342

44-
with open(resource_filename("gramps_webapi", "data/apispec.yaml")) as file_handle:
45-
API_SCHEMA = yaml.safe_load(file_handle)
43+
ref = files("gramps_webapi") / "data/apispec.yaml"
44+
with as_file(ref) as file_path:
45+
with open(file_path, encoding="utf-8") as file_handle:
46+
API_SCHEMA = yaml.safe_load(file_handle)
4647

4748
API_RESOLVER = RefResolver(base_uri="", referrer=API_SCHEMA, store={"": API_SCHEMA})
4849

tests/test_schema.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
"""Tests for the `gramps_webapi.api` module."""
2121

2222
import unittest
23+
from importlib.resources import as_file, files
2324

2425
import yaml
25-
from jsonschema import Draft4Validator, validate
26-
from pkg_resources import resource_filename
26+
from jsonschema import Draft4Validator
2727

2828

2929
class TestSchema(unittest.TestCase):
@@ -32,9 +32,9 @@ class TestSchema(unittest.TestCase):
3232
def test_schema(self):
3333
"""Check schema for validity."""
3434
# check it loads okay
35-
with open(
36-
resource_filename("gramps_webapi", "data/apispec.yaml")
37-
) as file_handle:
38-
api_schema = yaml.safe_load(file_handle)
35+
ref = files("gramps_webapi") / "data/apispec.yaml"
36+
with as_file(ref) as file_path:
37+
with open(file_path, encoding="utf-8") as file_handle:
38+
api_schema = yaml.safe_load(file_handle)
3939
# check structure
4040
Draft4Validator.check_schema(api_schema)

tests/test_version.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
"""Test consistent version numbers."""
2121

2222
import unittest
23+
from importlib.resources import as_file, files
2324

2425
import yaml
25-
from pkg_resources import resource_filename
2626

2727
from gramps_webapi import __version__
2828

@@ -32,8 +32,8 @@ class TestVersion(unittest.TestCase):
3232

3333
def test_version(self):
3434
"""Test version in setup and apispec are equal."""
35-
with open(
36-
resource_filename("gramps_webapi", "data/apispec.yaml")
37-
) as file_handle:
38-
schema = yaml.safe_load(file_handle)
35+
ref = files("gramps_webapi") / "data/apispec.yaml"
36+
with as_file(ref) as file_path:
37+
with open(file_path, encoding="utf-8") as file_handle:
38+
schema = yaml.safe_load(file_handle)
3939
self.assertEqual(__version__, schema["info"]["version"])

0 commit comments

Comments
 (0)