def add_or_replace_jacket(container):
 name = find_existing_jacket(container)
 found = True
 if (name is None):
  jacket_item = container.generate_item(u'jacket.xhtml', id_prefix=u'jacket')
  name = container.href_to_name(jacket_item.get(u'href'), container.opf_name)
  found = False
 if found:
  remove_jacket_images(container, name)
 replace_jacket(container, name)
 if (not found):
  index = 0
  sp = container.abspath_to_name(container.spine_items.next())
  if (sp == find_cover_page(container)):
   index = 1
  itemref = container.opf.makeelement(OPF(u'itemref'), idref=jacket_item.get(u'id'))
  container.insert_into_xml(container.opf_xpath(u'//opf:spine')[0], itemref, index=index)
 return found

def add_or_replace_jacket ( container ) : name = find_existing_jacket ( container ) found = True if ( name is None ) : jacket_item = container . generate_item ( u'jacket.xhtml' , id_prefix = u'jacket' ) name = container . href_to_name ( jacket_item . get ( u'href' ) , container . opf_name ) found = False if found : remove_jacket_images ( container , name ) replace_jacket ( container , name ) if ( not found ) : index = 0 sp = container . abspath_to_name ( container . spine_items . next ( ) ) if ( sp == find_cover_page ( container ) ) : index = 1 itemref = container . opf . makeelement ( OPF ( u'itemref' ) , idref = jacket_item . get ( u'id' ) ) container . insert_into_xml ( container . opf_xpath ( u'//opf:spine' ) [ 0 ] , itemref , index = index ) return found
either create a new jacket from the books metadata or replace an existing jacket .