+
+ def get_sem_coords(self, sem):
+ area = sem.find("div[@type='rect']")
+ if area is None:
+ area = sem.find("div[@type='whole']")
+ return [[0, 0], [-1, -1]]
+
+ def has_all_props(node, props):
+ return reduce(and_, map(lambda prop: prop in node.attrib, props))
+
+ if has_all_props(area, ['x1', 'x2', 'y1', 'y2']) == False:
+ return None
+
+ def n(prop): return int(area.get(prop))
+ return [[n('x1'), n('y1')], [n('x2'), n('y2')]]
+
+
+ def partiter(self):
+ """
+ Iterates the parts of this picture and returns them and their metadata
+ """
+ # omg no support for //sem[(@type='theme') or (@type='object')] ?
+ for part in list(self.edoc.iterfind("//sem[@type='theme']")) + list(self.edoc.iterfind("//sem[@type='object']")):
+ pd = {}
+ pd['type'] = part.get('type')
+
+ coords = self.get_sem_coords(part)
+ if coords is None: continue
+ pd['coords'] = coords
+
+ def want_unicode(x):
+ if not isinstance(x, unicode): return x.decode('utf-8')
+ else: return x
+ pd['object'] = part.attrib['type'] == 'object' and want_unicode(part.attrib.get('object', u'')) or None
+ pd['themes'] = part.attrib['type'] == 'theme' and [part.attrib.get('theme', u'')] or []
+ yield pd
+
+ def load_frame_info(self):
+ k = self.edoc.find("//sem[@object='kadr']")
+
+ if k is not None:
+ clip = self.get_sem_coords(k)
+ self.frame = clip
+ frm = Element("sem", {"type": "frame"})
+ frm.append(k.iter("div").next())
+ self.edoc.getroot().append(frm)
+ k.getparent().remove(k)
+ else:
+ frm = self.edoc.find("//sem[@type='frame']")
+ if frm:
+ self.frame = self.get_sem_coords(frm)
+ else:
+ self.frame = None
+ return self