Skip to content
Snippets Groups Projects
Commit 0d4a44ca authored by ml bonhomme's avatar ml bonhomme :bee: Committed by Erwan Rouchet
Browse files

Check IIIF version @context property

parent b71d5b5a
No related branches found
No related tags found
1 merge request!363Check IIIF version @context property
Pipeline #138235 passed
......@@ -129,6 +129,24 @@ class IIIFParser(object):
if self.stream and not self.stream.closed:
self.stream.close()
def check_version(self):
"""
Raise an error if the manifest is IIIF v3
"""
context = next(
(
value
for prefix, event, value in ijson.parse(self.stream)
if prefix == "@context"
),
None,
)
if context and "http://iiif.io/api/presentation/3/context.json" in context:
raise ValueError(
"The import functionality does not support IIIF Presentation API version 3"
)
self.stream.seek(0)
def get_type(self):
"""
Get a parser class for the given IIIF object.
......@@ -155,6 +173,7 @@ class IIIFParser(object):
@staticmethod
def load(stream, *args, **kwargs):
p = IIIFParser(stream)
p.check_version()
cls = p.get_type()
return cls(stream, *args, **kwargs)
......
......@@ -24,6 +24,22 @@ class TestIIIFParser(TestCase):
IIIFParser(stream).close()
self.assertTrue(stream.closed)
def test_v3(self):
stream = BytesIO(
json.dumps(
{
"@context": "http://iiif.io/api/presentation/3/context.json",
"id": "something",
"type": "sc:Manifest",
}
).encode("utf-8")
)
with self.assertRaises(
ValueError,
msg="The import functionality does not support IIIF Presentation API version 3",
):
IIIFParser(stream).check_version()
def test_get_type(self):
stream = BytesIO(
json.dumps({"@id": "something", "@type": "sc:Manifest"}).encode("utf-8")
......@@ -43,7 +59,12 @@ class TestIIIFParser(TestCase):
def test_load(self):
stream = BytesIO(
json.dumps({"@id": "something", "@type": "sc:Manifest"}).encode("utf-8")
json.dumps(
{
"@id": "something",
"@type": "sc:Manifest",
}
).encode("utf-8")
)
self.assertIsInstance(IIIFParser.load(stream), ManifestParser)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment