def test_identifiers(self): # {{{ def idt(val, scheme=None, iid=''): return '<dc:identifier id="{id}" {scheme}>{val}</dc:identifier>'.format(scheme=('opf:scheme="%s"'%scheme if scheme else ''), val=val, id=iid) def ri(root): return dict(read_identifiers(root, read_prefixes(root), default_refines)) for m, result in ( (idt('abc', 'ISBN'), {}), (idt('isbn:9780230739581'), {'isbn':['9780230739581']}), (idt('urn:isbn:9780230739581'), {'isbn':['9780230739581']}), (idt('9780230739581', 'ISBN'), {'isbn':['9780230739581']}), (idt('isbn:9780230739581', 'ISBN'), {'isbn':['9780230739581']}), (idt('key:val'), {'key':['val']}), (idt('url:http://x'), {'url':['http://x']}), (idt('a:1')+idt('a:2'), {'a':['1', '2']}), ): self.ae(result, ri(self.get_opf(m))) root = self.get_opf(metadata=idt('a:1')+idt('a:2')+idt('calibre:x')+idt('uuid:y')) mi = read_metadata(root) self.ae(mi.application_id, 'x') set_application_id(root, {}, default_refines, 'y') mi = read_metadata(root) self.ae(mi.application_id, 'y') root = self.get_opf(metadata=idt('i:1', iid='uid') + idt('r:1') + idt('o:1')) set_identifiers(root, read_prefixes(root), default_refines, {'i':'2', 'o':'2'}) self.ae({'i':['2', '1'], 'r':['1'], 'o':['2']}, ri(root)) self.ae(1, len(XPath('//dc:identifier[@id="uid"]')(root))) root = self.get_opf(metadata=idt('i:1', iid='uid') + idt('r:1') + idt('o:1')) set_identifiers(root, read_prefixes(root), default_refines, {'i':'2', 'o':'2'}, force_identifiers=True) self.ae({'i':['2', '1'], 'o':['2']}, ri(root)) root = self.get_opf(metadata=idt('i:1', iid='uid') + idt('r:1') + idt('o:1')) set_application_id(root, {}, default_refines, 'y') mi = read_metadata(root) self.ae(mi.application_id, 'y')
def test_prefix_parsing(self): # {{{ self.ae(parse_prefixes('foaf: http://xmlns.com/foaf/spec/\n dbp: http://dbpedia.org/ontology/'), {'foaf':'http://xmlns.com/foaf/spec/', 'dbp': 'http://dbpedia.org/ontology/'}) for raw, expanded in ( ('onix:xxx', reserved_prefixes['onix'] + ':xxx'), ('xxx:onix', 'xxx:onix'), ('xxx', 'xxx'), ): self.ae(expand_prefix(raw, reserved_prefixes.copy()), expanded) root = self.get_opf() ensure_prefix(root, read_prefixes(root), 'calibre', 'https://calibre-ebook.com') ensure_prefix(root, read_prefixes(root), 'marc', reserved_prefixes['marc']) self.ae(parse_prefixes(root.get('prefix')), {'calibre': 'https://calibre-ebook.com'})
def test_title(self): # {{{ def rt(root): return read_title(root, read_prefixes(root), read_refines(root)) def st(root, title, title_sort=None): set_title(root, read_prefixes(root), read_refines(root), title, title_sort) return rt(root) root = self.get_opf('''<dc:title/><dc:title id='t'>xxx</dc:title>''') self.ae(rt(root), 'xxx') self.ae(st(root, 'abc', 'cba'), 'abc') self.ae(read_title_sort(root, read_prefixes(root), read_refines(root)), 'cba') root = self.get_opf('''<dc:title>yyy</dc:title><dc:title id='t'>x xx </dc:title><meta refines='#t' property='title-type'>main</meta><meta name="calibre:title_sort" content="sorted"/>''') self.ae(rt(root), 'x xx') self.ae(read_title_sort(root, read_prefixes(root), read_refines(root)), 'sorted') self.ae(st(root, 'abc'), 'abc')
def test_dates(self): # {{{ from calibre.utils.date import utcnow def rl(root): p, r = read_prefixes(root), read_refines(root) return read_pubdate(root, p, r), read_timestamp(root, p, r) def st(root, pd, ts): p, r = read_prefixes(root), read_refines(root) set_pubdate(root, p, r, pd) set_timestamp(root, p, r, ts) return rl(root) def ae(root, y1=None, y2=None): x1, x2 = rl(root) for x, y in ((x1, y1), (x2, y2)): if y is None: self.assertIsNone(x) else: self.ae(y, getattr(x, 'year', None)) root = self.get_opf('''<dc:date>1999-3-2</dc:date><meta property="calibre:timestamp" scheme="dcterms:W3CDTF">2001</meta>''') ae(root, 1999, 2001) n = utcnow() q = n.replace(microsecond=0) self.ae(st(root, n, n), (n, q)) root = self.get_opf('''<dc:date>1999-3-2</dc:date><meta name="calibre:timestamp" content="2001-1-1"/>''') ae(root, 1999, 2001) root = self.get_opf('''<meta property="dcterms:modified">2003</meta>''') self.ae(read_last_modified(root, read_prefixes(root), read_refines(root)).year, 2003)
def test_raster_cover(self): # {{{ def rt(root): return read_raster_cover(root, read_prefixes(root), read_refines(root)) root = self.get_opf('<meta name="cover" content="cover"/>', '<item id="cover" media-type="image/jpeg" href="x.jpg"/>') self.ae('x.jpg', rt(root)) root = self.get_opf('<meta name="cover" content="cover"/>', '<item id="cover" media-type="image/jpeg" href="x.jpg"/><item media-type="image/jpeg" href="y.jpg" properties="cover-image"/>') self.ae('y.jpg', rt(root)) ensure_is_only_raster_cover(root, read_prefixes(root), read_refines(root), 'x.jpg') self.ae('x.jpg', rt(root)) self.ae(['x.jpg'], root.xpath('//*[@properties="cover-image"]/@href')) self.assertFalse(root.xpath('//*[@name]'))
def upgrade_metadata(root): data = Data() data.prefixes = read_prefixes(root) data.refines = read_refines(root) upgrade_identifiers(root, data) upgrade_title(root, data) upgrade_languages(root, data) upgrade_authors(root, data) upgrade_timestamp(root, data) upgrade_date(root, data) upgrade_rating(root, data) upgrade_series(root, data) upgrade_custom(root, data) upgrade_meta(root, data) remove_invalid_attrs_in_dc_metadata(root, data) pretty_print_opf(root)
def upgrade_metadata(root): data = Data() data.prefixes = read_prefixes(root) data.refines = read_refines(root) upgrade_identifiers(root, data) upgrade_title(root, data) upgrade_languages(root, data) upgrade_authors(root, data) upgrade_timestamp(root, data) upgrade_date(root, data) upgrade_rating(root, data) upgrade_series(root, data) upgrade_custom(root, data) upgrade_meta(root, data) upgrade_cover(root, data) remove_invalid_attrs_in_dc_metadata(root, data) set_last_modified(root, data.prefixes, data.refines) pretty_print_opf(root)
def st(root, val): set_publisher(root, read_prefixes(root), read_refines(root), val) return rt(root)
def st(root, pd, ts): set_pubdate(root, read_prefixes(root), read_refines(root), pd) set_timestamp(root, read_prefixes(root), read_refines(root), ts) return rl(root)
def st(root, val): set_comments(root, read_prefixes(root), read_refines(root), val) return rt(root)
def st(root, producers): set_book_producers(root, read_prefixes(root), read_refines(root), producers) return rl(root)
def rt(root): return read_series(root, read_prefixes(root), read_refines(root))
def ru(root): return read_user_metadata(root, read_prefixes(root), read_refines(root))
def rt(root): return read_raster_cover(root, read_prefixes(root), read_refines(root))
def rt(root): return read_publisher(root, read_prefixes(root), read_refines(root))
def rt(root): return read_comments(root, read_prefixes(root), read_refines(root))
def rl(root): return read_pubdate(root, read_prefixes(root), read_refines(root)), read_timestamp( root, read_prefixes(root), read_refines(root))
def rt(root): return read_tags(root, read_prefixes(root), read_refines(root))
def rt(root): return read_rating(root, read_prefixes(root), read_refines(root))
def st(root, val): set_tags(root, read_prefixes(root), read_refines(root), val) return rt(root)
def rt(root, name): f = globals()['read_' + name] return f(root, read_prefixes(root), read_refines(root))
def st(root, languages): set_languages(root, read_prefixes(root), read_refines(root), languages) return rl(root)
def st(root, val): set_rating(root, read_prefixes(root), read_refines(root), val) return rt(root)
def rl(root): return read_book_producers(root, read_prefixes(root), read_refines(root))
def rl(root): return read_pubdate(root, read_prefixes(root), read_refines(root)), read_timestamp(root, read_prefixes(root), read_refines(root))
def st(root, val, i): set_series(root, read_prefixes(root), read_refines(root), val, i) return rt(root)
def st(root, name, val): f = globals()['set_' + name] f(root, read_prefixes(root), read_refines(root), val) return rt(root, name)
def rl(root): return read_authors(root, read_prefixes(root), read_refines(root))
def rt(root): return read_title(root, read_prefixes(root), read_refines(root))
def st(root, authors): set_authors(root, read_prefixes(root), read_refines(root), authors) return rl(root)
def ri(root): return dict(read_identifiers(root, read_prefixes(root), default_refines))
def rl(root): return read_languages(root, read_prefixes(root), read_refines(root))
def su(root, val): set_user_metadata(root, read_prefixes(root), read_refines(root), val) return ru(root)
def ri(root): return dict( read_identifiers(root, read_prefixes(root), default_refines))
def st(root, title, title_sort=None): set_title(root, read_prefixes(root), read_refines(root), title, title_sort) return rt(root)