View Single Post
Old 09-05-2011, 03:09 PM   #1
kacir
Wizard
kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.kacir ought to be getting tired of karma fortunes by now.
 
kacir's Avatar
 
Posts: 3,463
Karma: 10684861
Join Date: May 2006
Device: PocketBook 360, before it was Sony Reader, cassiopeia A-20
Patch for small enhancement of functionality.

Dear developers.

For a long time I had a wish that there was a functionality like "Similar books" -> "Books by the same author", but it would search just author surname and it wouldn't use the default search string author:"=FirstName Surname", but only author:"Surname".

So today, I got pisse^H^H^H^H^Hmotivated enough and I made a change [jumping excitedly up and down].
I have downloaded the source, figured the way to do the 'print "Hello World"' change from Tutorial and run the result. Then I located the desired piece of code and I have made the change. It seems to work. I am surprised how quickly it went. I was expecting that it would take me *much* longer to figure it all out.

It is my first ever change to Calibre and I am pretty sure that my effort is very similar to a blind, lame elephant blundering through the proverbial china shop. But, now I have my very own customised version of Calibre that does exactly what I wanted it to do.

Sorry for not submitting a "proper" patch, but that is something I have never done before, so here is my change in a form of one commented changed source file.

File: /home/kacir/work/calibre/src/calibre/gui2/actions/similar_books.py
Code:
#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai

__license__   = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

from functools import partial

from PyQt4.Qt import QToolButton

from calibre.gui2.actions import InterfaceAction

#### Begin added by Kacir ####
import re
# I am not sure if this is really necessary, but it works this way. Most probably the re module is imported somewhere else, but I am not skilled in Python programming
#### END added by Kacir ####

class SimilarBooksAction(InterfaceAction):

    name = 'Similar Books'
    action_spec = (_('Similar books...'), None, None, None)
    popup_type = QToolButton.InstantPopup
    action_type = 'current'
    action_add_menu = True

    def genesis(self):
        m = self.qaction.menu()
        for text, icon, target, shortcut in [
        (_('Books by same author'), 'user_profile.png', 'authors', _('Alt+A')),
        #### Begin added by Kacir ####
        (_('Books by same author surname'), 'user_profile.png', 'surname', _('Alt+W')),
        #### END added by Kacir ####
        (_('Books in this series'), 'books_in_series.png', 'series',
            _('Alt+Shift+S')),
        (_('Books by this publisher'), 'publisher.png', 'publisher', _('Alt+P')),
        (_('Books with the same tags'), 'tags.png', 'tag', _('Alt+T')),]:
            ac = self.create_action(spec=(text, icon, None, shortcut),
                    attr=target)
            m.addAction(ac)
            ac.triggered.connect(partial(self.show_similar_books, target))
        self.qaction.setMenu(m)

    def show_similar_books(self, type, *args):
        search, join = [], ' '
        idx = self.gui.library_view.currentIndex()
        if not idx.isValid():
            return
        row = idx.row()
        if type == 'series':
            series = idx.model().db.series(row)
            if series:
                search = ['series:"'+series+'"']
        elif type == 'publisher':
            publisher = idx.model().db.publisher(row)
            if publisher:
                search = ['publisher:"'+publisher+'"']
        elif type == 'tag':
            tags = idx.model().db.tags(row)
            if tags:
                search = ['tag:"='+t+'"' for t in tags.split(',')]
        elif type in ('author', 'authors'):
            authors = idx.model().db.authors(row)
            if authors:
                search = ['author:"='+a.strip().replace('|', ',')+'"' \
                                for a in authors.split(',')]
                join = ' or '
        #### Begin added by Kacir ####
        elif type == 'surname':
            authors = idx.model().db.authors(row)
            if authors:
                search = ['author:"'+re.sub(r'(.*) ([^ ]*)',r'\2',a.strip().replace('|', ','))+'"' \
                                for a in authors.split(',')]
                join = ' or '
        #### END added by Kacir ####
        if search:
            self.gui.search.set_search_string(join.join(search),
                    store_in_history=True)
Is there a chance of this little patch (after being cleared tidied and vetted by experts, of course) making into official tree? Pretty please.
kacir is offline   Reply With Quote