From: Marcin Koziej Date: Wed, 21 Dec 2011 12:01:41 +0000 (+0100) Subject: WLPicture parts iterator (partiter) X-Git-Tag: 1.7~177 X-Git-Url: https://git.mdrn.pl/librarian.git/commitdiff_plain/97ed31c1ea6c21e0ac6cad0bc25a3cf63ecdd1ad?ds=sidebyside;hp=--cc WLPicture parts iterator (partiter) --- 97ed31c1ea6c21e0ac6cad0bc25a3cf63ecdd1ad diff --git a/librarian/picture.py b/librarian/picture.py index 70b372f..b770030 100644 --- a/librarian/picture.py +++ b/librarian/picture.py @@ -153,3 +153,28 @@ class WLPicture(object): def image_file(self, *args, **kwargs): return open(self.image_path, *args, **kwargs) + + def partiter(self): + """ + Iterates the parts of this picture and returns them and their metadata + """ + for part in self.edoc.iter("div"): + pd = {} + pd['type'] = part.get('type') + if pd['type'] == 'area': + pd['coords'] = ((int(part.get('x1')), int(part.get('y1'))), + (int(part.get('x2')), int(part.get('y2')))) + + pd['themes'] = [] + pd['object'] = None + parent = part + while True: + parent = parent.getparent() + if parent is None: + break + if parent.tag == 'sem': + if parent.get('type') == 'theme': + pd['themes'] += map(unicode.strip, unicode(parent.get('theme')).split(',')) + elif parent.get('type') == 'object' and not pd['object']: + pd['object'] = parent.get('name') + yield pd diff --git a/tests/files/picture/angelus-novus.xml b/tests/files/picture/angelus-novus.xml index 032545d..b3b9ccb 100644 --- a/tests/files/picture/angelus-novus.xml +++ b/tests/files/picture/angelus-novus.xml @@ -24,14 +24,16 @@ lat - - + +
+ +
- +
- +
diff --git a/tests/test_picture.py b/tests/test_picture.py index 4e3b252..35f2cf9 100644 --- a/tests/test_picture.py +++ b/tests/test_picture.py @@ -38,3 +38,22 @@ def test_wlpicture(): f = wlp.image_file('r') f.close() + +def test_picture_parts(): + wlp = picture.WLPicture.from_file(open(get_fixture('picture', 'angelus-novus.xml'))) + parts = list(wlp.partiter()) + assert len(parts) == 5, "there should be %d parts of the picture" % 5 + motifs = set() + names = set() + + for p in parts: + for m in p['motifs']: + motifs.add(m) + for p in parts: + if p['object']: + names.add(p['object']) + + assert motifs == set([u'anioł historii', u'spojrzenie']), "missing motifs, got: %s" % motifs + assert names == set([u'obraz cały', u'skrzydło']), 'missing objects, got: %s' % names + +