Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No more pkg_resources #882

Merged
merged 8 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ deps-test:

# (Re)install the tool
install:
$(PIP) install -U pip wheel
$(PIP) install -U pip wheel setuptools fastentrypoints
for mod in $(BUILD_ORDER);do (cd $$mod ; $(PIP_INSTALL) .);done

# Install with pip install -e
Expand Down
2 changes: 1 addition & 1 deletion ocrd/ocrd/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Constants for ocrd.
"""
from pkg_resources import resource_filename
from ocrd_utils.package_resources import resource_filename

__all__ = [
'TMP_PREFIX',
Expand Down
4 changes: 2 additions & 2 deletions ocrd/ocrd/processor/builtin/dummy_processor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pylint: disable=missing-module-docstring,invalid-name
from os.path import join, basename
from pkg_resources import resource_string
from ocrd_utils.package_resources import resource_string

import click

Expand All @@ -17,7 +17,7 @@
)
from ocrd_modelfactory import page_from_file

OCRD_TOOL = parse_json_string_with_comments(resource_string(__name__, 'dummy/ocrd-tool.json').decode('utf8'))
OCRD_TOOL = parse_json_string_with_comments(resource_string(__name__, 'ocrd-tool.json').decode('utf8'))
kba marked this conversation as resolved.
Show resolved Hide resolved

class DummyProcessor(Processor):
"""
Expand Down
3 changes: 1 addition & 2 deletions ocrd/ocrd/workspace_bagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import re
import tempfile
import sys

from pkg_resources import get_distribution
from bagit import Bag, make_manifests # pylint: disable=no-name-in-module

from ocrd_utils import (
Expand All @@ -22,6 +20,7 @@
from ocrd_validators.constants import BAGIT_TXT, TMP_BAGIT_PREFIX, OCRD_BAGIT_PROFILE_URL
from ocrd_modelfactory import page_from_file
from ocrd_models.ocrd_page import to_xml
from ocrd_utils.package_resources import get_distribution

from .workspace import Workspace

Expand Down
2 changes: 1 addition & 1 deletion ocrd/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ opencv-python-headless
Flask
jsonschema
pyyaml
Deprecated == 1.2.0
Deprecated == 1.2.0
1 change: 1 addition & 0 deletions ocrd/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import fastentrypoints
from setuptools import setup, find_packages
from ocrd_utils import VERSION

Expand Down
2 changes: 1 addition & 1 deletion ocrd_models/ocrd_models/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Constants for ocrd_models.
"""
from pkg_resources import resource_string
from ocrd_utils.package_resources import resource_string
import re

__all__ = [
Expand Down
3 changes: 2 additions & 1 deletion ocrd_utils/ocrd_utils/constants.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""
Constants for ocrd_utils.
"""
from pkg_resources import get_distribution
from re import compile as regex_compile
from os import environ
from os.path import join, expanduser

from ocrd_utils.package_resources import get_distribution

__all__ = [
'EXT_TO_MIME',
'LOG_FORMAT',
Expand Down
50 changes: 50 additions & 0 deletions ocrd_utils/ocrd_utils/package_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import atexit
from contextlib import ExitStack
from pathlib import Path

try:
from importlib.resources import path, read_binary
except ImportError:
from importlib_resources import path, read_binary # type: ignore

try:
from importlib.metadata import distribution as get_distribution
except ImportError:
from importlib_metadata import distribution as get_distribution

# See https://importlib-resources.readthedocs.io/en/latest/migration.html#pkg-resources-resource-filename
_file_manager = ExitStack()
atexit.register(_file_manager.close)


def resource_filename(package: str, resource: str) -> Path:
"""
Reimplementation of the function with the same name from pkg_resources

Using importlib for better performance

package : str
The package from where to start looking for resource (often __name__)
resource : str
The resource to look up
"""
parent_package = package.rsplit('.',1)[0]
return _file_manager.enter_context(path(parent_package, resource))
kba marked this conversation as resolved.
Show resolved Hide resolved


def resource_string(package: str, resource: str) -> bytes:
"""
Reimplementation of the function with the same name from pkg_resources

Using importlib for better performance

package : str
The package from where to start looking for resource (often __name__)
resource : str
The resource to look up
"""
parent_package = package.rsplit('.',1)[0]
return read_binary(parent_package, resource)


__all__ = ['resource_filename', 'resource_string', 'get_distribution']
2 changes: 2 additions & 0 deletions ocrd_utils/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ Pillow >= 7.2.0
# tensorflow versions might require different versions
numpy
atomicwrites >= 1.3.0
importlib_metadata;python_version<'3.8'
importlib_resources;python_version<'3.8'
2 changes: 1 addition & 1 deletion ocrd_validators/ocrd_validators/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Constants for ocrd_validators.
"""
import yaml
from pkg_resources import resource_string, resource_filename
from ocrd_utils.package_resources import resource_string, resource_filename

__all__ = [
'OCRD_TOOL_SCHEMA',
Expand Down