From db3f5cb134935634c31ea8240d16a863c2337f30 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 9 Jan 2015 16:20:27 +0530 Subject: [PATCH] Edit Book: When importing files and the book has an orphaned entry in the manifest for that file, but the file itself is not present, rename the file instead of erroring out. Fixes #1407860 [cannot replace missing PDF](https://bugs.launchpad.net/calibre/+bug/1407860) --- src/calibre/ebooks/oeb/polish/container.py | 6 ++++++ src/calibre/gui2/tweak_book/boss.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/oeb/polish/container.py b/src/calibre/ebooks/oeb/polish/container.py index b1a873782c..21c7b445db 100644 --- a/src/calibre/ebooks/oeb/polish/container.py +++ b/src/calibre/ebooks/oeb/polish/container.py @@ -232,6 +232,12 @@ class Container(object): # {{{ self.dirty(self.opf_name) return item_id + def manifest_has_name(self, name): + ''' Return True if the manifest has an entry corresponding to name ''' + href = self.name_to_href(name, self.opf_name) + all_hrefs = {x.get('href') for x in self.opf_xpath('//opf:manifest/opf:item[@href]')} + return href in all_hrefs + def add_file(self, name, data, media_type=None, spine_index=None): ''' Add a file to this container. Entries for the file are automatically created in the OPF manifest and spine diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 8e029b071e..fb50e79854 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -432,7 +432,7 @@ class Boss(QObject): c = current_container() for path, name in files.iteritems(): i = 0 - while c.exists(name): + while c.exists(name) or c.manifest_has_name(name): i += 1 name, ext = name.rpartition('.')[0::2] name = '%s_%d.%s' % (name, i, ext)