From dca6c268edadf96a6b1c667e4e76c79da6c9ecee Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 23 Jun 2016 14:24:03 +0530 Subject: [PATCH] Implement dc:publisher --- src/calibre/ebooks/metadata/opf3.py | 22 ++++++++++++++++++++++ src/calibre/ebooks/metadata/opf3_test.py | 14 +++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/metadata/opf3.py b/src/calibre/ebooks/metadata/opf3.py index d3e54f156f..fac445efb3 100644 --- a/src/calibre/ebooks/metadata/opf3.py +++ b/src/calibre/ebooks/metadata/opf3.py @@ -547,6 +547,26 @@ def set_comments(root, prefixes, refines, val): m.append(c) # }}} +# Publisher {{{ + +@simple_text +def read_publisher(root, prefixes, refines): + for dc in XPath('./opf:metadata/dc:publisher')(root): + if dc.text: + return dc.text + +def set_publisher(root, prefixes, refines, val): + for dc in XPath('./opf:metadata/dc:publisher')(root): + remove_element(dc, refines) + m = XPath('./opf:metadata')(root)[0] + if val: + val = val.strip() + if val: + c = m.makeelement(DC('publisher')) + c.text = normalize_whitespace(val) + m.append(c) +# }}} + def read_metadata(root): ans = Metadata(_('Unknown'), [_('Unknown')]) prefixes, refines = read_prefixes(root), read_refines(root) @@ -579,6 +599,7 @@ def read_metadata(root): if not is_date_undefined(lm): ans.last_modified = lm ans.comments = read_comments(root, prefixes, refines) or ans.comments + ans.publisher = read_publisher(root, prefixes, refines) or ans.publisher return ans def get_metadata(stream): @@ -598,6 +619,7 @@ def apply_metadata(root, mi, cover_prefix='', cover_data=None, apply_null=False, set_pubdate(root, prefixes, refines, mi.pubdate) set_timestamp(root, prefixes, refines, mi.timestamp) set_comments(root, prefixes, refines, mi.comments) + set_publisher(root, prefixes, refines, mi.publisher) pretty_print_opf(root) diff --git a/src/calibre/ebooks/metadata/opf3_test.py b/src/calibre/ebooks/metadata/opf3_test.py index 4b985324d4..5c32a8db42 100644 --- a/src/calibre/ebooks/metadata/opf3_test.py +++ b/src/calibre/ebooks/metadata/opf3_test.py @@ -16,7 +16,7 @@ from calibre.ebooks.metadata.opf3 import ( read_authors, Author, set_authors, ensure_prefix, read_prefixes, read_book_producers, set_book_producers, read_timestamp, set_timestamp, read_pubdate, set_pubdate, CALIBRE_PREFIX, read_last_modified, read_comments, - set_comments + set_comments, read_publisher, set_publisher ) TEMPLATE = '''{metadata}''' % CALIBRE_PREFIX # noqa @@ -181,6 +181,18 @@ class TestOPF3(unittest.TestCase): return rt(root) root = self.get_opf('''<span>one</span> xxx''') self.ae('one\nxxx', rt(root)) + self.ae('p', st(root, 'p ')) + # }}} + + def test_publisher(self): # {{{ + def rt(root): + return read_publisher(root, read_prefixes(root), read_refines(root)) + def st(root, val): + set_publisher(root, read_prefixes(root), read_refines(root), val) + return rt(root) + root = self.get_opf(''' one xxx''') + self.ae('one', rt(root)) + self.ae('p', st(root, 'p ')) # }}} # Run tests {{{