From f945643acc4b3f935f2e91ef7f392a3fb65b5ced Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 15 Aug 2015 17:51:25 +0530 Subject: [PATCH] Ensure the short list of library names in the switch library dropdown is always sorted by frequency --- src/calibre/gui2/actions/choose_library.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/calibre/gui2/actions/choose_library.py b/src/calibre/gui2/actions/choose_library.py index e7c4d1b4b4..8b203a48df 100644 --- a/src/calibre/gui2/actions/choose_library.py +++ b/src/calibre/gui2/actions/choose_library.py @@ -5,7 +5,7 @@ __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import os, posixpath, weakref +import os, posixpath, weakref, sys from functools import partial from PyQt5.Qt import (QMenu, Qt, QInputDialog, QToolButton, QDialog, @@ -72,12 +72,12 @@ class LibraryUsageStats(object): # {{{ self.write_stats() return self.pretty(lpath) - def locations(self, db): + def locations(self, db, limit=None): lpath = self.canonicalize_path(db.library_path) locs = list(self.stats.keys()) if lpath in locs: locs.remove(lpath) - limit = tweaks['many_libraries'] + limit = tweaks['many_libraries'] if limit is None else limit key = sort_key if len(locs) > limit else lambda x:self.stats[x] locs.sort(key=key, reverse=len(locs)<=limit) for loc in locs: @@ -346,7 +346,10 @@ class ChooseLibraryAction(InterfaceAction): ac.setStatusTip(_('Remove: %s') % loc) qs_actions = [] - for i, x in enumerate(locations[:len(self.switch_actions)]): + locations_by_frequency = locations + if len(locations) >= tweaks['many_libraries']: + locations_by_frequency = list(self.stats.locations(db, limit=sys.maxsize)) + for i, x in enumerate(locations_by_frequency[:len(self.switch_actions)]): name, loc = x name = name.replace('&', '&&') ac = self.switch_actions[i]