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 5 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'))

class DummyProcessor(Processor):
"""
Expand Down
12 changes: 8 additions & 4 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 @@ -25,6 +23,12 @@

from .workspace import Workspace

try:
from importlib.metadata import version
except ImportError:
from importlib_metadata import version


tempfile.tempdir = '/tmp' # TODO hard-coded

BACKUPDIR = join('/tmp', TMP_BAGIT_PREFIX + 'backup')
Expand Down Expand Up @@ -123,8 +127,8 @@ def _set_bag_info(self, bag, total_bytes, total_files, ocrd_identifier, ocrd_man
bag.info['BagIt-Profile-Identifier'] = OCRD_BAGIT_PROFILE_URL
bag.info['Bag-Software-Agent'] = 'ocrd/core %s (bagit.py %s, bagit_profile %s) [cmdline: "%s"]' % (
VERSION, # TODO
get_distribution('bagit').version,
get_distribution('bagit_profile').version,
version('bagit'),
version('bagit_profile'),
' '.join(sys.argv))

bag.info['Ocrd-Identifier'] = ocrd_identifier
Expand Down
2 changes: 2 additions & 0 deletions ocrd/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ Flask
jsonschema
pyyaml
Deprecated == 1.2.0
importlib_metadata;python_version<'3.8'
importlib_resources;python_version<'3.8'
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
7 changes: 5 additions & 2 deletions ocrd_utils/ocrd_utils/constants.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""
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
try:
from importlib.metadata import version
except ImportError:
from importlib_metadata import version

__all__ = [
'EXT_TO_MIME',
Expand All @@ -22,7 +25,7 @@
'XDG_DATA_HOME',
]

VERSION = get_distribution('ocrd_utils').version
VERSION = version('ocrd_utils')

MIMETYPE_PAGE = 'application/vnd.prima.page+xml'

Expand Down
45 changes: 45 additions & 0 deletions ocrd_utils/ocrd_utils/package_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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


_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))


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']
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