From 72d2a7e372a7da1d60f86464c58a8b50417b9aa4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 20 Dec 2013 23:31:53 +0530 Subject: [PATCH] Edit book: Handle epub files which contain opf files with no tags --- src/calibre/ebooks/oeb/polish/container.py | 34 ++++++++++++---------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/container.py b/src/calibre/ebooks/oeb/polish/container.py index 25e757ee07..185e408a80 100644 --- a/src/calibre/ebooks/oeb/polish/container.py +++ b/src/calibre/ebooks/oeb/polish/container.py @@ -692,21 +692,25 @@ class Container(object): # {{{ return item def format_opf(self): - mdata = self.opf_xpath('//opf:metadata')[0] - mdata.text = '\n ' - remove = set() - for child in mdata: - child.tail = '\n ' - try: - if (child.get('name', '').startswith('calibre:') and - child.get('content', '').strip() in {'{}', ''}): - remove.add(child) - except AttributeError: - continue # Happens for XML comments - for child in remove: - mdata.remove(child) - if len(mdata) > 0: - mdata[-1].tail = '\n ' + try: + mdata = self.opf_xpath('//opf:metadata')[0] + except IndexError: + pass + else: + mdata.text = '\n ' + remove = set() + for child in mdata: + child.tail = '\n ' + try: + if (child.get('name', '').startswith('calibre:') and + child.get('content', '').strip() in {'{}', ''}): + remove.add(child) + except AttributeError: + continue # Happens for XML comments + for child in remove: + mdata.remove(child) + if len(mdata) > 0: + mdata[-1].tail = '\n ' # Ensure name comes before content, needed for Nooks for meta in self.opf_xpath('//opf:meta[@name="cover"]'): if 'content' in meta.attrib: