def set_metadata_opf2(root, cover_prefix, mi, opf_version, cover_data=None, apply_null=False, update_timestamp=False, force_identifiers=False, add_missing_cover=True): mi = MetaInformation(mi) for x in ('guide', 'toc', 'manifest', 'spine'): setattr(mi, x, None) opf = OPF(None, preparsed_opf=root, read_toc=False) if mi.languages: mi.languages = normalize_languages(list(opf.raw_languages) or [], mi.languages) opf.smart_update(mi, apply_null=apply_null) if getattr(mi, 'uuid', None): opf.application_id = mi.uuid if apply_null or force_identifiers: opf.set_identifiers(mi.get_identifiers()) else: orig = opf.get_identifiers() orig.update(mi.get_identifiers()) opf.set_identifiers({k:v for k, v in orig.iteritems() if k and v}) if update_timestamp and mi.timestamp is not None: opf.timestamp = mi.timestamp raster_cover = opf.raster_cover if raster_cover is None and cover_data is not None and add_missing_cover: guide_raster_cover = opf.guide_raster_cover i = None if guide_raster_cover is not None: i = guide_raster_cover raster_cover = i.get('href') else: if cover_prefix and not cover_prefix.endswith('/'): cover_prefix += '/' name = cover_prefix + 'cover.jpg' i = create_manifest_item(opf.root, name, 'cover') if i is not None: raster_cover = name if i is not None: if opf_version.major < 3: [x.getparent().remove(x) for x in opf.root.xpath('//*[local-name()="meta" and @name="cover"]')] m = opf.create_metadata_element('meta', is_dc=False) m.set('name', 'cover'), m.set('content', i.get('id')) else: for x in opf.root.xpath('//*[local-name()="item" and contains(@properties, "cover-image")]'): x.set('properties', x.get('properties').replace('cover-image', '').strip()) i.set('properties', 'cover-image') with pretty_print: return opf.render(), raster_cover
def set_metadata_opf2(root, cover_prefix, mi, opf_version, cover_data=None, apply_null=False, update_timestamp=False, force_identifiers=False, add_missing_cover=True): mi = MetaInformation(mi) for x in ('guide', 'toc', 'manifest', 'spine'): setattr(mi, x, None) opf = OPF(None, preparsed_opf=root, read_toc=False) if mi.languages: mi.languages = normalize_languages(list(opf.raw_languages) or [], mi.languages) opf.smart_update(mi, apply_null=apply_null) if getattr(mi, 'uuid', None): opf.application_id = mi.uuid if apply_null or force_identifiers: opf.set_identifiers(mi.get_identifiers()) else: orig = opf.get_identifiers() orig.update(mi.get_identifiers()) opf.set_identifiers({k:v for k, v in orig.items() if k and v}) if update_timestamp and mi.timestamp is not None: opf.timestamp = mi.timestamp raster_cover = opf.raster_cover if raster_cover is None and cover_data is not None and add_missing_cover: guide_raster_cover = opf.guide_raster_cover i = None if guide_raster_cover is not None: i = guide_raster_cover raster_cover = i.get('href') else: if cover_prefix and not cover_prefix.endswith('/'): cover_prefix += '/' name = cover_prefix + 'cover.jpg' i = create_manifest_item(opf.root, name, 'cover') if i is not None: raster_cover = name if i is not None: if opf_version.major < 3: [x.getparent().remove(x) for x in opf.root.xpath('//*[local-name()="meta" and @name="cover"]')] m = opf.create_metadata_element('meta', is_dc=False) m.set('name', 'cover'), m.set('content', i.get('id')) else: for x in opf.root.xpath('//*[local-name()="item" and contains(@properties, "cover-image")]'): x.set('properties', x.get('properties').replace('cover-image', '').strip()) i.set('properties', 'cover-image') with pretty_print: return opf.render(), raster_cover
def apply_metadata(root, mi, cover_prefix='', cover_data=None, apply_null=False, update_timestamp=False, force_identifiers=False, add_missing_cover=True): prefixes, refines = read_prefixes(root), read_refines(root) current_mi = read_metadata(root) if apply_null: def ok(x): return True else: def ok(x): return not mi.is_null(x) if ok('identifiers'): set_identifiers(root, prefixes, refines, mi.identifiers, force_identifiers=force_identifiers) if ok('title'): set_title(root, prefixes, refines, mi.title, mi.title_sort) if ok('languages'): set_languages(root, prefixes, refines, mi.languages) if ok('book_producer'): set_book_producers(root, prefixes, refines, (mi.book_producer, )) aus = string_to_authors(mi.author_sort or '') authors = [] for i, aut in enumerate(mi.authors): authors.append(Author(aut, aus[i] if i < len(aus) else None)) if authors or apply_null: set_authors(root, prefixes, refines, authors) if ok('pubdate'): set_pubdate(root, prefixes, refines, mi.pubdate) if update_timestamp and mi.timestamp is not None: set_timestamp(root, prefixes, refines, mi.timestamp) if ok('comments'): set_comments(root, prefixes, refines, mi.comments) if ok('publisher'): set_publisher(root, prefixes, refines, mi.publisher) if ok('tags'): set_tags(root, prefixes, refines, mi.tags) if ok('rating') and mi.rating is not None and mi.rating > 0.1: set_rating(root, prefixes, refines, mi.rating) if ok('series'): set_series(root, prefixes, refines, mi.series, mi.series_index or 1) if ok('author_link_map'): set_author_link_map(root, prefixes, refines, getattr(mi, 'author_link_map', None)) if ok('user_categories'): set_user_categories(root, prefixes, refines, getattr(mi, 'user_categories', None)) # We ignore apply_null for the next two to match the behavior with opf2.py if mi.application_id: set_application_id(root, prefixes, refines, mi.application_id) if mi.uuid: set_uuid(root, prefixes, refines, mi.uuid) current_mi.remove_stale_user_metadata(mi) new_user_metadata, current_user_metadata = mi.get_all_user_metadata( True), current_mi.get_all_user_metadata(True) missing = object() for key in tuple(new_user_metadata): meta = new_user_metadata.get(key) if meta is None: if apply_null: new_user_metadata[key] = None continue dt = meta.get('datatype') if dt == 'text' and meta.get('is_multiple'): val = mi.get(key, []) if val or apply_null: current_user_metadata[key] = meta elif dt in {'int', 'float', 'bool'}: val = mi.get(key, missing) if val is missing: if apply_null: current_user_metadata[key] = meta elif apply_null or val is not None: current_user_metadata[key] = meta elif apply_null or not mi.is_null(key): current_user_metadata[key] = meta set_user_metadata(root, prefixes, refines, current_user_metadata) raster_cover = read_raster_cover(root, prefixes, refines) if not raster_cover and cover_data and add_missing_cover: if cover_prefix and not cover_prefix.endswith('/'): cover_prefix += '/' name = cover_prefix + 'cover.jpg' i = create_manifest_item(root, name, 'cover') if i is not None: ensure_is_only_raster_cover(root, prefixes, refines, name) raster_cover = name pretty_print_opf(root) return raster_cover
def apply_metadata(root, mi, cover_prefix='', cover_data=None, apply_null=False, update_timestamp=False, force_identifiers=False, add_missing_cover=True): prefixes, refines = read_prefixes(root), read_refines(root) current_mi = read_metadata(root) if apply_null: def ok(x): return True else: def ok(x): return not mi.is_null(x) if ok('identifiers'): set_identifiers(root, prefixes, refines, mi.identifiers, force_identifiers=force_identifiers) if ok('title'): set_title(root, prefixes, refines, mi.title, mi.title_sort) if ok('languages'): set_languages(root, prefixes, refines, mi.languages) if ok('book_producer'): set_book_producers(root, prefixes, refines, (mi.book_producer,)) aus = string_to_authors(mi.author_sort or '') authors = [] for i, aut in enumerate(mi.authors): authors.append(Author(aut, aus[i] if i < len(aus) else None)) if authors or apply_null: set_authors(root, prefixes, refines, authors) if ok('pubdate'): set_pubdate(root, prefixes, refines, mi.pubdate) if update_timestamp and mi.timestamp is not None: set_timestamp(root, prefixes, refines, mi.timestamp) if ok('comments'): set_comments(root, prefixes, refines, mi.comments) if ok('publisher'): set_publisher(root, prefixes, refines, mi.publisher) if ok('tags'): set_tags(root, prefixes, refines, mi.tags) if ok('rating') and mi.rating > 0.1: set_rating(root, prefixes, refines, mi.rating) if ok('series'): set_series(root, prefixes, refines, mi.series, mi.series_index or 1) if ok('author_link_map'): set_author_link_map(root, prefixes, refines, getattr(mi, 'author_link_map', None)) if ok('user_categories'): set_user_categories(root, prefixes, refines, getattr(mi, 'user_categories', None)) # We ignore apply_null for the next two to match the behavior with opf2.py if mi.application_id: set_application_id(root, prefixes, refines, mi.application_id) if mi.uuid: set_uuid(root, prefixes, refines, mi.uuid) new_user_metadata, current_user_metadata = mi.get_all_user_metadata(True), current_mi.get_all_user_metadata(True) missing = object() for key in tuple(new_user_metadata): meta = new_user_metadata.get(key) if meta is None: if apply_null: new_user_metadata[key] = None continue dt = meta.get('datatype') if dt == 'text' and meta.get('is_multiple'): val = mi.get(key, []) if val or apply_null: current_user_metadata[key] = meta elif dt in {'int', 'float', 'bool'}: val = mi.get(key, missing) if val is missing: if apply_null: current_user_metadata[key] = meta elif apply_null or val is not None: current_user_metadata[key] = meta elif apply_null or not mi.is_null(key): current_user_metadata[key] = meta set_user_metadata(root, prefixes, refines, current_user_metadata) raster_cover = read_raster_cover(root, prefixes, refines) if not raster_cover and cover_data and add_missing_cover: if cover_prefix and not cover_prefix.endswith('/'): cover_prefix += '/' name = cover_prefix + 'cover.jpg' i = create_manifest_item(root, name, 'cover') if i is not None: ensure_is_only_raster_cover(root, prefixes, refines, name) raster_cover = name pretty_print_opf(root) return raster_cover