diff --git a/src/calibre/devices/apple/driver.py b/src/calibre/devices/apple/driver.py index cc4d39d3c5..5a0496e863 100644 --- a/src/calibre/devices/apple/driver.py +++ b/src/calibre/devices/apple/driver.py @@ -24,6 +24,7 @@ from calibre.utils.logging import Log from calibre.utils.zipfile import ZipFile from PIL import Image as PILImage +from lxml import etree if isosx: try: @@ -2515,23 +2516,23 @@ class ITUNES(DriverBase): fnames = zf_opf.namelist() opf = [x for x in fnames if '.opf' in x][0] if opf: - opf_raw = cStringIO.StringIO(zf_opf.read(opf)) - soup = BeautifulSoup(opf_raw.getvalue()) - opf_raw.close() - - # Touch existing calibre timestamp - md = soup.find('metadata') - if md: - ts = md.find('meta',attrs={'name':'calibre:timestamp'}) - if ts: - timestamp = ts['content'] - old_ts = parse_date(timestamp) - metadata.timestamp = datetime.datetime(old_ts.year, old_ts.month, old_ts.day, old_ts.hour, - old_ts.minute, old_ts.second, old_ts.microsecond+1, old_ts.tzinfo) - else: - metadata.timestamp = now() - if DEBUG: - self.log.info(" add timestamp: %s" % metadata.timestamp) + opf_tree = etree.fromstring(zf_opf.read(opf)) + ns_map = opf_tree.nsmap.keys() + for item in ns_map: + ns = opf_tree.nsmap[item] + md_el = opf_tree.find(".//{%s}metadata" % ns) + if md_el is not None: + ts = md_el.find('.//{%s}meta[@name="calibre:timestamp"]') + if ts: + timestamp = ts.get('content') + old_ts = parse_date(timestamp) + metadata.timestamp = datetime.datetime(old_ts.year, old_ts.month, old_ts.day, old_ts.hour, + old_ts.minute, old_ts.second, old_ts.microsecond+1, old_ts.tzinfo) + else: + metadata.timestamp = now() + if DEBUG: + self.log.info(" add timestamp: %s" % metadata.timestamp) + break else: metadata.timestamp = now() if DEBUG: @@ -2839,7 +2840,7 @@ class ITUNES(DriverBase): def _xform_metadata_via_plugboard(self, book, format): ''' Transform book metadata from plugboard templates ''' if DEBUG: - self.log.info(" ITUNES._update_metadata_from_plugboard()") + self.log.info(" ITUNES._xform_metadata_via_plugboard()") if self.plugboard_func: pb = self.plugboard_func(self.DEVICE_PLUGBOARD_NAME, format, self.plugboards) diff --git a/src/calibre/library/catalog.py b/src/calibre/library/catalog.py index cb55b2318d..f3640af4f0 100644 --- a/src/calibre/library/catalog.py +++ b/src/calibre/library/catalog.py @@ -1481,23 +1481,36 @@ class EPUB_MOBI(CatalogPlugin): current_author = authors[0] for (i,author) in enumerate(authors): if author != current_author and i: - # Exit if author matches previous, but author_sort doesn't match if author[0] == current_author[0]: - error_msg = _(''' -Inconsistent Author Sort values for Author '{0}': -'{1}' <> '{2}', -unable to build catalog.\n -Select all books by '{0}', apply correct Author Sort value in Edit Metadata dialog, -then rebuild the catalog.\n''').format(author[0],author[1],current_author[1]) - self.opts.log.warn('\n*** Metadata error ***') - self.opts.log.warn(error_msg) + if self.opts.fmt == 'mobi': + # Exit if building MOBI + error_msg = _( +'''Inconsistent Author Sort values for +Author '{0}': +'{1}' <> '{2}' +Unable to build MOBI catalog.\n +Select all books by '{0}', apply correct Author Sort value in Edit Metadata dialog, then rebuild the catalog.\n''').format(author[0],author[1],current_author[1]) + self.opts.log.warn('\n*** Metadata error ***') + self.opts.log.warn(error_msg) + + self.error.append('Author Sort mismatch') + self.error.append(error_msg) + return False + else: + # Warning if building non-MOBI + if not self.error: + self.error.append('Author Sort mismatch') + + error_msg = _( +'''Warning: inconsistent Author Sort values for +Author '{0}': +'{1}' <> '{2}'\n''').format(author[0],author[1],current_author[1]) + self.opts.log.warn('\n*** Metadata warning ***') + self.opts.log.warn(error_msg) + self.error.append(error_msg) - self.error.append('Metadata error') - self.error.append(error_msg) - return False current_author = author - self.booksByAuthor = sorted(self.booksByAuthor, key=self.booksByAuthorSorter_author_sort) # Build the unique_authors set from existing data @@ -2135,7 +2148,7 @@ then rebuild the catalog.\n''').format(author[0],author[1],current_author[1]) if author_count == 1: divOpeningTag.insert(dotc, pBookTag) dotc += 1 - else: + elif divRunningTag: divRunningTag.insert(drtc,pBookTag) drtc += 1