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

Replaced depreciated pkg_resources with importlib.resources in precalculated_text_measurer.py for Python 3.12 support #45

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def unit(session):
'install',
[
'Jinja2==3.0.0',
'Pillow==8.3.2', # Oldest version that supports Python 3.7 to 3.10.
'requests==2.22.0',
'xmldiff==2.4'
'Pillow==10.1', # Oldest version that supports Python 3.12.
'requests==2.32.3', # Oldest version that supports Python 3.12
'xmldiff==2.6' # Oldest version that supports Python 3.12
])
def compatibility(session, install):
"""Run the unit test suite with each support library and Python version."""
Expand Down
30 changes: 17 additions & 13 deletions pybadges/precalculated_text_measurer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import io
import json
import pkg_resources
import importlib.resources as resources
from typing import cast, Mapping, TextIO, Type

from pybadges import text_measurer
Expand Down Expand Up @@ -74,19 +74,23 @@ def default(cls) -> 'PrecalculatedTextMeasurer':
if cls._default_cache is not None:
return cls._default_cache

if pkg_resources.resource_exists(__name__, 'default-widths.json.xz'):
resource_name_xz = 'default-widths.json.xz'
resource_name_json = 'default-widths.json'

resource_package = resources.files(__name__)
resource_xz_path = resource_package / resource_name_xz
resource_json_path = resource_package / resource_name_json

if resource_xz_path.exists():
import lzma
with pkg_resources.resource_stream(__name__,
'default-widths.json.xz') as f:
with lzma.open(f, "rt") as g:
cls._default_cache = PrecalculatedTextMeasurer.from_json(
cast(TextIO, g))
with resources.as_file(resource_xz_path) as path:
with lzma.open(path, "rt") as f:
cls._default_cache = PrecalculatedTextMeasurer.from_json(cast(TextIO, f))
return cls._default_cache
elif resource_json_path.exists():
with resources.as_file(resource_json_path) as path:
with open(path, 'r', encoding='utf-8') as f:
cls._default_cache = PrecalculatedTextMeasurer.from_json(f)
return cls._default_cache
elif pkg_resources.resource_exists(__name__, 'default-widths.json'):
with pkg_resources.resource_stream(__name__,
'default-widths.json') as f:
cls._default_cache = PrecalculatedTextMeasurer.from_json(
io.TextIOWrapper(f, encoding='utf-8'))
return cls._default_cache
else:
raise ValueError('could not load default-widths.json')
11 changes: 5 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def replace_relative_with_absolute(match):
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
Expand All @@ -67,17 +66,17 @@ def replace_relative_with_absolute(match):
},
long_description=get_long_description(),
long_description_content_type='text/markdown',
python_requires='>=3.4',
install_requires=['Jinja2>=3,<4', 'requests>=2.22.0,<3'],
python_requires='>=3.8',
install_requires=['Jinja2>=3,<4', 'requests>=2.32.3,<3'],
extras_require={
'pil-measurement': ['Pillow>=6,<10'],
'pil-measurement': ['Pillow>=10.1'],
'dev': [
'Flask>=2.0', # For server tests.
'Flask>=2.0', # For server tests.
'fonttools>=3.26',
'nox',
'Pillow>=5',
'pytest>=3.6',
'xmldiff>=2.4'
'xmldiff>=2.6'
],
},
license='Apache-2.0',
Expand Down