def update_metadata(ebook, new_opf): from calibre.ebooks.metadata.opf import get_metadata, set_metadata with ebook.open(ebook.opf_name, 'r+b') as stream, open(new_opf, 'rb') as ns: mi = get_metadata(ns)[0] mi.cover, mi.cover_data = None, (None, None) opfbytes = set_metadata(stream, mi, apply_null=True, update_timestamp=True)[0] stream.seek(0) stream.truncate() stream.write(opfbytes)
def main(opts, args, dbctx): if opts.list_fields: ans = get_fields(dbctx) prints('%-40s' % _('Title'), _('Field name'), '\n') for key, m in ans: prints('%-40s' % m['name'], key) return 0 def verify_int(x): try: int(x) return True except: return False if len(args) < 1 or not verify_int(args[0]): raise SystemExit( _('You must specify a record id as the ' 'first argument')) if len(args) < 2 and not opts.field: raise SystemExit(_('You must specify either a field or an OPF file')) book_id = int(args[0]) if len(args) > 1: opf = os.path.abspath(args[1]) if not os.path.exists(opf): raise SystemExit(_('The OPF file %s does not exist') % opf) with lopen(opf, 'rb') as stream: mi = get_metadata(stream)[0] if mi.cover: mi.cover = os.path.join(os.path.dirname(opf), os.path.relpath(mi.cover, getcwd())) final_mi = dbctx.run('set_metadata', 'opf', book_id, read_cover(mi)) if not final_mi: raise SystemExit( _('No book with id: %s in the database') % book_id) if opts.field: fields = {k: v for k, v in get_fields(dbctx)} fields['title_sort'] = fields['sort'] vals = {} for x in opts.field: field, val = x.partition(':')[::2] if field == 'sort': field = 'title_sort' if field not in fields: raise SystemExit(_('%s is not a known field' % field)) if field == 'cover': val = dbctx.path(os.path.abspath(os.path.expanduser(val))) else: val = field_from_string(field, val, fields[field]) vals[field] = val fvals = [] for field, val in sorted( # ensure series_index fields are set last iteritems(vals), key=lambda k: 1 if k[0].endswith('_index') else 0): if field.endswith('_index'): try: val = float(val) except Exception: raise SystemExit( 'The value %r is not a valid series index' % val) fvals.append((field, val)) final_mi = dbctx.run('set_metadata', 'fields', book_id, fvals) if not final_mi: raise SystemExit( _('No book with id: %s in the database') % book_id) prints(unicode_type(final_mi)) return 0
def main(opts, args, dbctx): if opts.list_fields: ans = get_fields(dbctx) prints('%-40s' % _('Title'), _('Field name'), '\n') for key, m in ans: prints('%-40s' % m['name'], key) return 0 def verify_int(x): try: int(x) return True except: return False if len(args) < 1 or not verify_int(args[0]): raise SystemExit(_( 'You must specify a record id as the ' 'first argument' )) if len(args) < 2 and not opts.field: raise SystemExit(_('You must specify either a field or an opf file')) book_id = int(args[0]) if len(args) > 1: opf = os.path.abspath(args[1]) if not os.path.exists(opf): raise SystemExit(_('The OPF file %s does not exist') % opf) with lopen(opf, 'rb') as stream: mi = get_metadata(stream)[0] if mi.cover: mi.cover = os.path.join(os.path.dirname(opf), os.path.relpath(mi.cover, os.getcwdu())) final_mi = dbctx.run('set_metadata', 'opf', book_id, read_cover(mi)) if not final_mi: raise SystemExit(_('No book with id: %s in the database') % book_id) if opts.field: fields = {k: v for k, v in get_fields(dbctx)} fields['title_sort'] = fields['sort'] vals = {} for x in opts.field: field, val = x.partition(':')[::2] if field == 'sort': field = 'title_sort' if field not in fields: raise SystemExit(_('%s is not a known field' % field)) if field == 'cover': val = dbctx.path(os.path.abspath(os.path.expanduser(val))) else: val = field_from_string(field, val, fields[field]) vals[field] = val fvals = [] for field, val in sorted( # ensure series_index fields are set last iteritems(vals), key=lambda k: 1 if k[0].endswith('_index') else 0): if field.endswith('_index'): try: val = float(val) except Exception: raise SystemExit('The value %r is not a valid series index' % val) fvals.append((field, val)) final_mi = dbctx.run('set_metadata', 'fields', book_id, fvals) if not final_mi: raise SystemExit(_('No book with id: %s in the database') % book_id) prints(unicode_type(final_mi)) return 0