From 967285b9f6dcc495b1f892587724732c51960323 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 23 Aug 2011 11:33:33 -0600 Subject: [PATCH] When automatically computing author sort from author's name, if the name contains certain words like Inc., Company, Team, etc. use the author name as the sort string directly. The list of such words can be controlled via Preferences->Tweaks. Fixes #797895 (author name sort order copy keywords) --- resources/default_tweaks.py | 6 ++++++ src/calibre/ebooks/metadata/__init__.py | 7 +++++++ src/calibre/gui2/metadata/basic_widgets.py | 17 +++++++++++++++-- src/calibre/gui2/metadata/single.py | 7 ++++++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/resources/default_tweaks.py b/resources/default_tweaks.py index 12731a8c42..f11a0b7bc0 100644 --- a/resources/default_tweaks.py +++ b/resources/default_tweaks.py @@ -62,10 +62,16 @@ authors_completer_append_separator = False # The author name suffixes are words that are ignored when they occur at the # end of an author name. The case of the suffix is ignored and trailing # periods are automatically handled. +# The author name copy words are a set of words which if they occur in an +# author name cause the automatically geenrated author sort string to be +# identical to the author name. This means that the sort for a string like Acme +# Inc. will be Acme Inc. instead of Inc., Acme author_sort_copy_method = 'comma' author_name_suffixes = ('Jr', 'Sr', 'Inc', 'Ph.D', 'Phd', 'MD', 'M.D', 'I', 'II', 'III', 'IV', 'Junior', 'Senior') +author_name_copywords = ('Corporation', 'Company', 'Co.', 'Agency', 'Council', + 'Committee', 'Inc.', 'Institute', 'Society', 'Club', 'Team') #: Use author sort in Tag Browser # Set which author field to display in the tags pane (the list of authors, diff --git a/src/calibre/ebooks/metadata/__init__.py b/src/calibre/ebooks/metadata/__init__.py index 2c26d011b7..a9816db5ae 100644 --- a/src/calibre/ebooks/metadata/__init__.py +++ b/src/calibre/ebooks/metadata/__init__.py @@ -36,8 +36,15 @@ def author_to_author_sort(author, method=None): return author if method is None: method = tweaks['author_sort_copy_method'] + + ltoks = frozenset(x.lower() for x in tokens) + copy_words = frozenset(x.lower() for x in tweaks['author_name_copywords']) + if ltoks.intersection(copy_words): + method = u'copy' + if method == u'copy': return author + suffixes = set([x.lower() for x in tweaks['author_name_suffixes']]) suffixes |= set([x+u'.' for x in suffixes]) diff --git a/src/calibre/gui2/metadata/basic_widgets.py b/src/calibre/gui2/metadata/basic_widgets.py index 29f6fffa0b..3ec34938af 100644 --- a/src/calibre/gui2/metadata/basic_widgets.py +++ b/src/calibre/gui2/metadata/basic_widgets.py @@ -308,7 +308,7 @@ class AuthorSortEdit(EnLineEdit): LABEL = _('Author s&ort:') def __init__(self, parent, authors_edit, autogen_button, db, - copy_a_to_as_action, copy_as_to_a_action): + copy_a_to_as_action, copy_as_to_a_action, a_to_as, as_to_a): EnLineEdit.__init__(self, parent) self.authors_edit = authors_edit self.db = db @@ -333,6 +333,8 @@ class AuthorSortEdit(EnLineEdit): autogen_button.clicked.connect(self.auto_generate) copy_a_to_as_action.triggered.connect(self.auto_generate) copy_as_to_a_action.triggered.connect(self.copy_to_authors) + a_to_as.triggered.connect(self.author_to_sort) + as_to_a.triggered.connect(self.sort_to_author) self.update_state() @dynamic_property @@ -389,10 +391,21 @@ class AuthorSortEdit(EnLineEdit): def auto_generate(self, *args): au = unicode(self.authors_edit.text()) - au = re.sub(r'\s+et al\.$', '', au) + au = re.sub(r'\s+et al\.$', '', au).strip() authors = string_to_authors(au) self.current_val = self.db.author_sort_from_authors(authors) + def author_to_sort(self, *args): + au = unicode(self.authors_edit.text()) + au = re.sub(r'\s+et al\.$', '', au).strip() + if au: + self.current_val = au + + def sort_to_author(self, *args): + aus = self.current_val + if aus: + self.authors_edit.current_val = [aus] + def initialize(self, db, id_): self.current_val = db.author_sort(id_, index_is_id=True) diff --git a/src/calibre/gui2/metadata/single.py b/src/calibre/gui2/metadata/single.py index dc3983171b..a2666b0351 100644 --- a/src/calibre/gui2/metadata/single.py +++ b/src/calibre/gui2/metadata/single.py @@ -130,10 +130,15 @@ class MetadataSingleDialogBase(ResizableDialog): ac = m.addAction(QIcon(I('forward.png')), _('Set author sort from author')) ac2 = m.addAction(QIcon(I('back.png')), _('Set author from author sort')) ac3 = m.addAction(QIcon(I('user_profile.png')), _('Manage authors')) + ac4 = m.addAction(QIcon(I('next.png')), + _('Copy author to author sort')) + ac5 = m.addAction(QIcon(I('previous.png')), + _('Copy author sort to author')) + b.setMenu(m) self.authors = AuthorsEdit(self, ac3) self.author_sort = AuthorSortEdit(self, self.authors, b, self.db, ac, - ac2) + ac2, ac4, ac5) self.basic_metadata_widgets.extend([self.authors, self.author_sort]) self.swap_title_author_button = QToolButton(self)