diff --git a/snovault/embed.py b/snovault/embed.py index 20d0c8ce5..fb6d3e590 100644 --- a/snovault/embed.py +++ b/snovault/embed.py @@ -1,14 +1,14 @@ import logging from copy import deepcopy from posixpath import join -from pyramid.compat import ( - native_, - unquote_bytes_to_wsgi, -) from pyramid.httpexceptions import HTTPNotFound, HTTPServerError import pyramid.request from .crud_views import collection_add as sno_collection_add from .interfaces import COLLECTIONS, CONNECTION +from .pyramid_compat import ( + native_, + unquote_bytes_to_wsgi, +) from .resources import Collection from .schema_utils import validate_request from dcicutils.misc_utils import check_true diff --git a/snovault/pyramid_compat.py b/snovault/pyramid_compat.py new file mode 100644 index 000000000..8c38e8c38 --- /dev/null +++ b/snovault/pyramid_compat.py @@ -0,0 +1,23 @@ +# Not that pyramid.compat disappeared with pyramid 2.0.02 (during move to Python 3.12). + +from urllib.parse import unquote_to_bytes + +text_type = str + + +def ascii_native_(s): + if isinstance(s, text_type): + s = s.encode('ascii') + return str(s, 'ascii', 'strict') + + +def native_(s, encoding='latin-1', errors='strict'): + """ If ``s`` is an instance of ``text_type``, return + ``s``, otherwise return ``str(s, encoding, errors)``""" + if isinstance(s, text_type): + return s + return str(s, encoding, errors) + + +def unquote_bytes_to_wsgi(bytestring): + return unquote_to_bytes(bytestring).decode('latin-1') diff --git a/snovault/tests/test_views.py b/snovault/tests/test_views.py index ab641ea1b..6e7fccc06 100644 --- a/snovault/tests/test_views.py +++ b/snovault/tests/test_views.py @@ -4,10 +4,10 @@ from base64 import b64encode from jsonschema import Draft202012Validator -# from pyramid.compat import ascii_native_ from uuid import uuid4 from ..interfaces import TYPES +from ..pyramid_compat import ascii_native_ from ..util import mappings_use_nested from .testing_views import ( @@ -19,12 +19,6 @@ TYPE_NAMES = ['TestingPostPutPatchSno', 'TestingDownload'] -# pyramid.compat disappeared with pyramid 2.0.02 (during move to Python 3.12). -def ascii_native_(s): - if isinstance(s, str): - s = s.encode('ascii') - return str(s, 'ascii', 'strict') - def get_parameterized_names(): """ Get all item types from schema names """