Skip to content

Commit

Permalink
Merge pull request #200 from markpbaggett/recipe_0014
Browse files Browse the repository at this point in the history
Add Recipe 0014 and add add_item() to AccompanyingCanvas
  • Loading branch information
glenrobson authored Dec 5, 2024
2 parents 73fe84b + c779de8 commit 07c867a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
10 changes: 10 additions & 0 deletions docs/recipes/0014-accompanyingcanvas.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# "Audio Presentation with Accompanying Image"
| | **Cookbook URLs** |
|--------------|-------------------|
| **Recipe:** | [https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/](https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/) |
| **JSON-LD:** | [https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/manifest.json](https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas/manifest.json) |

### Method 1 - Use AccompanyingCanvas and add_item() helper
```python
--8<-- "docs/recipes/scripts/0014-accompanyingcanvas-method1.py"
```
62 changes: 62 additions & 0 deletions docs/recipes/scripts/0014-accompanyingcanvas-method1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from iiif_prezi3 import Manifest, ResourceItem, AnnotationPage, Annotation, config, AccompanyingCanvas

base_url = "https://iiif.io/api/cookbook/recipe/0014-accompanyingcanvas"

config.configs['helpers.auto_fields.AutoLang'].auto_lang = "en"
accompanying_canvas = AccompanyingCanvas(
id=f"{base_url}/canvas/accompanying",
label="First page of score for Gustav Mahler, Symphony No. 3",
height=998,
width=772,
)
manifest = Manifest(
id=f"{base_url}/manifest.json",
label="Partial audio recording of Gustav Mahler's _Symphony No. 3_",
)
ac_anno_body = ResourceItem(
id="https://iiif.io/api/image/3.0/example/reference/4b45bba3ea612ee46f5371ce84dbcd89-mahler-0/full/,998/0/default.jpg",
type="Image",
format="image/jpeg",
height=998,
width=772,
)
ac_anno_body.make_service(
id="https://iiif.io/api/image/3.0/example/reference/4b45bba3ea612ee46f5371ce84dbcd89-mahler-0",
type="ImageService3",
profile="level1"
)
ac_anno_page = AnnotationPage(
id=f"{base_url}/canvas/accompanying/annotation/page"
)
ac_anno = Annotation(
id=f"{base_url}/canvas/accompanying/annotation/image",
motivation="painting",
body=ac_anno_body,
target=f"{base_url}/canvas/accompanying"
)
ac_anno_page.add_item(ac_anno)
accompanying_canvas.add_item(ac_anno_page)
canvas = manifest.make_canvas(
id=f"{base_url}/canvas/p1",
label="Gustav Mahler, Symphony No. 3, CD 1",
duration=1985.024,
accompanyingCanvas=accompanying_canvas
)
anno_body = ResourceItem(
id="https://fixtures.iiif.io/audio/indiana/mahler-symphony-3/CD1/medium/128Kbps.mp4",
type="Sound",
format="video/mp4",
duration=1985.024,
)
anno_page = AnnotationPage(
id=f"{base_url}/canvas/page/p1"
)
anno = Annotation(
id=f"{base_url}/canvas/page/annotation/segment1-audio",
motivation="painting",
body=anno_body,
target=canvas.id
)
anno_page.add_item(anno)
canvas.add_item(anno_page)
print(manifest.json(indent=2))
8 changes: 4 additions & 4 deletions iiif_prezi3/helpers/add_item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from ..loader import monkeypatch_schema
from ..skeleton import (AnnotationPage, Canvas, Collection, Manifest, Range,
Reference)
from ..skeleton import (AccompanyingCanvas, AnnotationPage, Canvas, Collection,
Manifest, Range, Reference)


class AddItem:
Expand Down Expand Up @@ -37,5 +37,5 @@ def add_item_by_reference(self, item):
self.items = self.items # Force Pydantic to validate?


monkeypatch_schema([Collection, Manifest, Canvas, Range, AnnotationPage, Reference], AddItem)
monkeypatch_schema([Collection, Range, Canvas, AnnotationPage], AddItemByReference)
monkeypatch_schema([Collection, Manifest, Canvas, Range, AnnotationPage, Reference, AccompanyingCanvas], AddItem)
monkeypatch_schema([Collection, Range, Canvas, AnnotationPage, AccompanyingCanvas], AddItemByReference)
11 changes: 9 additions & 2 deletions tests/test_add_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from pydantic import ValidationError

from iiif_prezi3 import (Annotation, AnnotationPage, Canvas, Collection,
CollectionRef, Manifest, ManifestRef, ResourceItem)
from iiif_prezi3 import (AccompanyingCanvas, Annotation, AnnotationPage,
Canvas, Collection, CollectionRef, Manifest,
ManifestRef, ResourceItem)


class AddItemTests(unittest.TestCase):
Expand All @@ -16,6 +17,7 @@ def setUp(self):
self.ca2 = Canvas(label="second canvas", type="Canvas")
self.ap = AnnotationPage()
self.a = Annotation(target=self.c.id)
self.ac = AccompanyingCanvas()

def test_add_item(self):
"""Test that a Canvas added to an empty Manifest creates and populates items."""
Expand Down Expand Up @@ -74,3 +76,8 @@ def test_add_invalid_reference(self):
"""Test that adding an invalid reference type fails."""
with self.assertRaises(ValidationError):
self.c2.add_item_by_reference(self.ca)

def test_add_item_to_accompanying_canvas(self):
"""Test that an item can be added to an items list in Accompanying Canvas."""
self.ac.add_item(self.ap)
self.assertIsInstance(self.ac.items[0], AnnotationPage)

0 comments on commit 07c867a

Please sign in to comment.