Skip to content

Commit

Permalink
fix RefResolver with latest versions of jsonschema (relates to python…
Browse files Browse the repository at this point in the history
  • Loading branch information
fmigneault committed Jul 6, 2023
1 parent bcdf134 commit 57e8909
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Fixes:
------
- Fix broken Docker build of ``weaver-worker`` image due to unresolved ``docker-ce-cli`` package.
Installation is updated according to the reference documentation (https://docs.docker.com/engine/install/debian/).
- Fix incorrect stream reader type (``bytes`` instead of ``str``) for some handlers in ``open_module_resource_file``.
- Fix invalid ``jsonschema.validators.RefResolver`` reference in ``jsonschema>=4.18.0`` caused by refactor
(see https://github.com/python-jsonschema/jsonschema/blob/main/CHANGELOG.rst#v4180
and `python-jsonschema/jsonschema#1049 <https://github.com/python-jsonschema/jsonschema/pull/1049>`_).

.. _changes_4.30.0:

Expand Down
12 changes: 9 additions & 3 deletions weaver/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import functools
import importlib.util
import inspect
import io
import logging
import os
import posixpath
Expand Down Expand Up @@ -33,7 +34,6 @@
from botocore.config import Config as S3Config
from bs4 import BeautifulSoup
from celery.app import Celery
from jsonschema.validators import RefResolver as JsonSchemaRefResolver
from mypy_boto3_s3.literals import RegionName
from pyramid.config import Configurator
from pyramid.exceptions import ConfigurationError
Expand Down Expand Up @@ -67,6 +67,11 @@
from weaver.warning import TimeZoneInfoAlreadySetWarning
from weaver.xml_util import HTML_TREE_BUILDER, XML

try: # refactor in jsonschema==4.18.0
from jsonschema.validators import _RefResolver as JsonSchemaRefResolver # pylint: disable=E0611
except ImportError:
from jsonschema.validators import RefResolver as JsonSchemaRefResolver # pylint: disable=E0611

if TYPE_CHECKING:
import importlib.abc
from types import FrameType, ModuleType
Expand Down Expand Up @@ -1055,7 +1060,7 @@ def import_target(target, default_root=None):


def open_module_resource_file(module, file_path):
# type: (Union[str, ModuleType], str) -> IO[bytes]
# type: (Union[str, ModuleType], str) -> IO[str]
"""
Opens a resource (data file) from an installed module.
Expand All @@ -1070,7 +1075,8 @@ def open_module_resource_file(module, file_path):
reader = loader.get_resource_reader() # type: importlib.abc.ResourceReader # noqa
except AttributeError:
reader = loader # noqa
return reader.open_resource(file_path)
buffer = reader.open_resource(file_path)
return io.TextIOWrapper(buffer, encoding="utf-8")
except AttributeError:
path = os.path.join(module.__path__[0], file_path)
return open(path, mode="r", encoding="utf-8")
Expand Down

0 comments on commit 57e8909

Please sign in to comment.