From a9f4ab2346c78c63d60478036c4ddec0ececdf46 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 5 Feb 2009 17:00:04 -0800 Subject: [PATCH] Minor fixes --- src/calibre/ebooks/lrf/meta.py | 7 ++++++- src/calibre/ebooks/metadata/__init__.py | 13 ++++++++----- src/calibre/ebooks/metadata/cli.py | 21 +++++++++++++++++---- src/calibre/ebooks/metadata/opf.xml | 6 +++--- src/calibre/ebooks/metadata/opf2.py | 23 ++++++++++++++++++++++- 5 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/calibre/ebooks/lrf/meta.py b/src/calibre/ebooks/lrf/meta.py index 331e101ddd..322835f470 100644 --- a/src/calibre/ebooks/lrf/meta.py +++ b/src/calibre/ebooks/lrf/meta.py @@ -229,6 +229,9 @@ def get_metadata(stream): mi.author = lrf.author.strip() mi.comments = lrf.free_text.strip() mi.category = lrf.category.strip()+', '+lrf.classification.strip() + tags = [x.strip() for x in mi.category.split(',') if x.strip()] + if tags: + mi.tags = tags mi.publisher = lrf.publisher.strip() mi.cover_data = lrf.get_cover() try: @@ -624,7 +627,9 @@ def set_metadata(stream, mi): lrf.title = mi.title if mi.authors: lrf.author = ', '.join(mi.authors) - if mi.category: + if mi.tags: + lrf.category = mi.tags[0] + if getattr(mi, 'category', False): lrf.category = mi.category if mi.comments: lrf.free_text = mi.comments diff --git a/src/calibre/ebooks/metadata/__init__.py b/src/calibre/ebooks/metadata/__init__.py index 063e56190b..e3c434342a 100644 --- a/src/calibre/ebooks/metadata/__init__.py +++ b/src/calibre/ebooks/metadata/__init__.py @@ -185,7 +185,7 @@ class MetaInformation(object): @staticmethod def copy(mi): ans = MetaInformation(mi.title, mi.authors) - for attr in ('author_sort', 'title_sort', 'comments', + for attr in ('author_sort', 'title_sort', 'comments', 'category', 'publisher', 'series', 'series_index', 'rating', 'isbn', 'tags', 'cover_data', 'application_id', 'guide', 'manifest', 'spine', 'toc', 'cover', 'language', 'book_producer'): @@ -210,7 +210,7 @@ class MetaInformation(object): #: mi.cover_data = (ext, data) self.cover_data = getattr(mi, 'cover_data', (None, None)) - for x in ('author_sort', 'title_sort', 'comments', 'publisher', + for x in ('author_sort', 'title_sort', 'comments', 'category', 'publisher', 'series', 'series_index', 'rating', 'isbn', 'language', 'application_id', 'manifest', 'toc', 'spine', 'guide', 'cover', 'book_producer', @@ -228,7 +228,7 @@ class MetaInformation(object): if mi.authors and mi.authors[0] != _('Unknown'): self.authors = mi.authors - for attr in ('author_sort', 'title_sort', 'comments', + for attr in ('author_sort', 'title_sort', 'comments', 'category', 'publisher', 'series', 'series_index', 'rating', 'isbn', 'application_id', 'manifest', 'spine', 'toc', 'cover', 'language', 'guide', 'book_producer'): @@ -251,10 +251,11 @@ class MetaInformation(object): return '%d'%x if int(x) == x else '%.2f'%x def __unicode__(self): - ans = [ fmt('Title', self.title) ] + ans = [] def fmt(x, y): ans.append(u'%-20s: %s'%(unicode(x), unicode(y))) - + + fmt('Title', self.title) if self.title_sort: fmt('Title sort', self.title_sort) if self.authors: @@ -264,6 +265,8 @@ class MetaInformation(object): fmt('Publisher', self.publisher) if getattr(self, 'book_producer', False): fmt('Book Producer', self.book_producer) + if self.category: + ans += u'Category : ' + unicode(self.category) + u'\n' if self.comments: fmt('Comments', self.comments) if self.isbn: diff --git a/src/calibre/ebooks/metadata/cli.py b/src/calibre/ebooks/metadata/cli.py index 75b541d9c9..4101f34047 100644 --- a/src/calibre/ebooks/metadata/cli.py +++ b/src/calibre/ebooks/metadata/cli.py @@ -26,6 +26,7 @@ from calibre.customize.ui import metadata_readers, metadata_writers from calibre.ebooks.metadata.meta import get_metadata, set_metadata from calibre.ebooks.metadata import string_to_authors, authors_to_sort_string, \ title_sort, MetaInformation +from calibre.ebooks.lrf.meta import LRFMetaFile from calibre import prints def config(): @@ -50,6 +51,8 @@ def config(): help=_('Set the ebook description.')) c.add_opt('publisher', ['-p', '--publisher'], help=_('Set the ebook publisher.')) + c.add_opt('category', ['--category'], + help=_('Set the book category.')) c.add_opt('series', ['-s', '--series'], help=_('Set the series this ebook belongs to.')) c.add_opt('series_index', ['-i', '--index'], @@ -75,6 +78,9 @@ def config(): help=_('Read metadata from the specified OPF file and use it to ' 'set metadata in the ebook. Metadata specified on the' 'command line will override metadata read from the OPF file')) + + c.add_opt('lrf_bookid', ['--lrf-bookid'], + help=_('Set the BookID in LRF files')) return c def filetypes(): @@ -102,12 +108,12 @@ def do_set_metadata(opts, mi, stream, stream_type): for pref in config().option_set.preferences: if pref.name in ('to_opf', 'from_opf', 'authors', 'title_sort', - 'author_sort', 'get_cover', 'cover', 'tags'): + 'author_sort', 'get_cover', 'cover', 'tags', + 'lrf_bookid'): continue val = getattr(opts, pref.name, None) if val is not None: - setattr(mi, pref.name, getattr()) - + setattr(mi, pref.name, val) if getattr(opts, 'authors', None) is not None: mi.authors = string_to_authors(opts.authors) mi.author_sort = authors_to_sort_string(mi.authors) @@ -158,11 +164,18 @@ def main(args=sys.argv): do_set_metadata(opts, mi, stream, stream_type) stream.seek(0) stream.flush() + lrf = None + if stream_type == 'lrf': + if opts.lrf_bookid is not None: + lrf = LRFMetaFile(stream) + lrf.book_id = opts.lrf_bookid mi = get_metadata(stream, stream_type) - prints(_('Changed metadata')+'::') + prints('\n' + _('Changed metadata') + '::') metadata = unicode(mi) metadata = '\t'+'\n\t'.join(metadata.split('\n')) prints(metadata) + if lrf is not None: + prints('\tBookID:', lrf.book_id) if opts.to_opf is not None: from calibre.ebooks.metadata.opf2 import OPFCreator diff --git a/src/calibre/ebooks/metadata/opf.xml b/src/calibre/ebooks/metadata/opf.xml index d95268f306..703e82b5c1 100644 --- a/src/calibre/ebooks/metadata/opf.xml +++ b/src/calibre/ebooks/metadata/opf.xml @@ -6,13 +6,13 @@ > - ${mi.title} + ${mi.title} ${author} - ${'%s (%s)'%(__appname__, __version__)} [http://${__appname__}.kovidgoyal.net] + ${'%s (%s)'%(__appname__, __version__)} [http://${__appname__}.kovidgoyal.net] ${mi.application_id} ${mi.language if mi.language else 'UND'} - ${mi.category} + ${mi.category} ${mi.comments} ${mi.publisher} ${mi.isbn} diff --git a/src/calibre/ebooks/metadata/opf2.py b/src/calibre/ebooks/metadata/opf2.py index 718d615e71..f051ad8568 100644 --- a/src/calibre/ebooks/metadata/opf2.py +++ b/src/calibre/ebooks/metadata/opf2.py @@ -414,6 +414,7 @@ class OPF(object): metadata_path = XPath('descendant::*[re:match(name(), "metadata", "i")]') metadata_elem_path = XPath('descendant::*[re:match(name(), concat($name, "$"), "i") or (re:match(name(), "meta$", "i") and re:match(@name, concat("^calibre:", $name, "$"), "i"))]') + title_path = XPath('descendant::*[re:match(name(), "title", "i")]') authors_path = XPath('descendant::*[re:match(name(), "creator", "i") and (@role="aut" or @opf:role="aut" or (not(@role) and not(@opf:role)))]') bkp_path = XPath('descendant::*[re:match(name(), "contributor", "i") and (@role="bkp" or @opf:role="bkp")]') tags_path = XPath('descendant::*[re:match(name(), "subject", "i")]') @@ -503,7 +504,7 @@ class OPF(object): def set_text(self, elem, content): if elem.tag == self.META: - elem.attib['content'] = content + elem.attrib['content'] = content else: elem.text = content @@ -645,6 +646,26 @@ class OPF(object): return property(fget=fget, fset=fset) + @apply + def title_sort(): + + def fget(self): + matches = self.title_path(self.metadata) + if matches: + for match in matches: + ans = match.get('{%s}file-as'%self.NAMESPACES['opf'], None) + if not ans: + ans = match.get('file-as', None) + if ans: + return ans + + def fset(self, val): + matches = self.title_path(self.metadata) + if matches: + matches[0].set('file-as', unicode(val)) + + return property(fget=fget, fset=fset) + @apply def tags():