diff --git a/src/calibre/devices/prs505/sony_cache.py b/src/calibre/devices/prs505/sony_cache.py index cc12edd735..181a18bcf9 100644 --- a/src/calibre/devices/prs505/sony_cache.py +++ b/src/calibre/devices/prs505/sony_cache.py @@ -388,23 +388,35 @@ class XMLCache(object): debug_print('WARNING: Some elements in the JSON cache were not' ' found in the XML cache') records = [x for x in records if x is not None] - ids = set() + # Ensure each book has an ID. for rec in records: - id = rec.get('id', None) - if id is None: + if rec.get('id', None) is None: rec.set('id', str(self.max_id(root)+1)) - id = rec.get('id', None) - ids.add(id) - # ids cannot contain None, so no reason to check + ids = [x.get('id', None) for x in records] + # Given that we set the ids, there shouldn't be any None's. But + # better to be safe... + if None in ids: + debug_print('WARNING: Some elements do not have ids') + ids = [x for x in ids if x is not None] playlist = self.get_or_create_playlist(bl_index, category) - # Reduce ids to books not already in the playlist + # Get the books currently in the playlist. We will need them to be + # sure to put back any books that were manually added. + playlist_ids = [] for item in playlist: id_ = item.get('id', None) if id_ is not None: - ids.discard(id_) - # Add the books in ids that were not already in the playlist - for id_ in ids: + playlist_ids.append(id_) + # Empty the playlist. We do this so that the playlist will have the + # order specified by get_collections + for item in list(playlist): + playlist.remove(item) + + # Get a list of ids not known by get_collections + extra_ids = [x for x in playlist_ids if x not in ids] + # Rebuild the collection in the order specified by get_collections. Then + # add the ids that get_collections didn't know about. + for id_ in ids + extra_ids: item = playlist.makeelement( '{%s}item'%self.namespaces[bl_index], nsmap=playlist.nsmap, attrib={'id':id_}) diff --git a/src/calibre/devices/usbms/books.py b/src/calibre/devices/usbms/books.py index 6204ee3ccb..42d0f3c863 100644 --- a/src/calibre/devices/usbms/books.py +++ b/src/calibre/devices/usbms/books.py @@ -174,8 +174,8 @@ class CollectionsBookList(BookList): if lpath not in collections_lpaths[category]: collections_lpaths[category].add(lpath) collections[category].append(book) - if attr == 'series': - series_categories.add(category) + if attr == 'series': + series_categories.add(category) # Sort collections for category, books in collections.items(): def tgetter(x):