diff --git a/src/libprs500/devices/prs500/books.py b/src/libprs500/devices/prs500/books.py index 00c3d306d7..30a64da3a6 100644 --- a/src/libprs500/devices/prs500/books.py +++ b/src/libprs500/devices/prs500/books.py @@ -106,6 +106,15 @@ class Book(object): return self.root + self.rpath return property(fget=fget, doc=doc) + @apply + def db_id(): + doc = '''The database id in the application database that this file corresponds to''' + def fget(self): + match = re.search(r'_(\d+)$', self.rpath.rpartition('.')[0]) + if match: + return int(match.group(1)) + return property(fget=fget, doc=doc) + def __init__(self, node, tags=[], prefix="", root="/Data/media/"): self.elem = node self.prefix = prefix @@ -123,6 +132,7 @@ def fix_ids(media, cache): Adjust ids in cache to correspond with media. ''' media.purge_empty_playlists() + media.reorder_playlists() if cache.root: sourceid = media.max_id() cid = sourceid + 1 @@ -144,6 +154,7 @@ class BookList(_BookList): def __init__(self, root="/Data/media/", sfile=None): _BookList.__init__(self) + self.tag_order = {} self.root = self.document = self.proot = None if sfile: sfile.seek(0) @@ -280,6 +291,8 @@ class BookList(_BookList): self.append(book) self.set_next_id(cid+1) if self.prefix and info.has_key('tags'): # Playlists only supportted in main memory + if info.has_key('tag order'): + self.tag_order.update(info['tag order']) self.set_playlists(book.id, info['tags']) @@ -334,7 +347,34 @@ class BookList(_BookList): continue return ans - + def book_by_id(self, id): + for book in self: + if str(book.id) == str(id): + return book + + def reorder_playlists(self): + for title in self.tag_order.keys(): + pl = self.playlist_by_title(title) + if not pl: + continue + db_ids = [i.getAttribute('id') for i in pl.childNodes] + pl_book_ids = [self.book_by_id(i.getAttribute('id')).db_id for i in pl.childNodes] + map = {} + for i, j in zip(pl_book_ids, db_ids): + map[i] = j + pl_book_ids = [i for i in pl_book_ids if i is not None] + ordered_ids = [i for i in self.tag_order[title] if i in pl_book_ids] + + if len(ordered_ids) < len(pl.childNodes): + continue + children = [i for i in pl.childNodes] + for child in children: + pl.removeChild(child) + child.unlink() + for id in ordered_ids: + item = self.document.createElement(self.prefix+'item') + item.setAttribute('id', str(map[id])) + pl.appendChild(item) def write(self, stream): """ Write XML representation of DOM tree to C{stream} """ diff --git a/src/libprs500/devices/prs505/books.py b/src/libprs500/devices/prs505/books.py index c8773f7f49..fabb75af61 100644 --- a/src/libprs500/devices/prs505/books.py +++ b/src/libprs500/devices/prs505/books.py @@ -113,6 +113,15 @@ class Book(object): return self.mountpath + self.rpath return property(fget=fget, doc=doc) + @apply + def db_id(): + doc = '''The database id in the application database that this file corresponds to''' + def fget(self): + match = re.search(r'_(\d+)$', self.rpath.rpartition('.')[0]) + if match: + return int(match.group(1)) + return property(fget=fget, doc=doc) + def __init__(self, node, mountpath, tags, prefix=""): self.elem = node self.prefix = prefix @@ -134,6 +143,7 @@ class BookList(_BookList): self.root_element = self.document.documentElement self.mountpath = mountpath records = self.root_element.getElementsByTagName('records') + self.tag_order = {} if records: self.prefix = 'xs1:' @@ -202,6 +212,8 @@ class BookList(_BookList): book.datetime = ctime self.append(book) if info.has_key('tags'): + if info.has_key('tag order'): + self.tag_order.update(info['tag order']) self.set_tags(book, info['tags']) def _delete_book(self, node): @@ -329,6 +341,35 @@ class BookList(_BookList): src = self.document.toxml('utf-8') + '\n' stream.write(src.replace("'", ''')) + def book_by_id(self, id): + for book in self: + if str(book.id) == str(id): + return book + + def reorder_playlists(self): + for title in self.tag_order.keys(): + pl = self.playlist_by_title(title) + if not pl: + continue + db_ids = [i.getAttribute('id') for i in pl.childNodes] + pl_book_ids = [self.book_by_id(i.getAttribute('id')).db_id for i in pl.childNodes] + map = {} + for i, j in zip(pl_book_ids, db_ids): + map[i] = j + pl_book_ids = [i for i in pl_book_ids if i is not None] + ordered_ids = [i for i in self.tag_order[title] if i in pl_book_ids] + + if len(ordered_ids) < len(pl.childNodes): + continue + children = [i for i in pl.childNodes] + for child in children: + pl.removeChild(child) + child.unlink() + for id in ordered_ids: + item = self.document.createElement(self.prefix+'item') + item.setAttribute('id', str(map[id])) + pl.appendChild(item) + def fix_ids(main, card): ''' Adjust ids the XML databases. @@ -358,6 +399,8 @@ def fix_ids(main, card): except KeyError: item.parentNode.removeChild(item) item.unlink() + + db.reorder_playlists() regen_ids(main) regen_ids(card) diff --git a/src/libprs500/gui2/library.py b/src/libprs500/gui2/library.py index 4e45a2c830..0b7aac0b3e 100644 --- a/src/libprs500/gui2/library.py +++ b/src/libprs500/gui2/library.py @@ -236,12 +236,17 @@ class BooksModel(QAbstractTableModel): tags = [] else: tags = tags.split(',') + series = self.db.series(row) + if series is not None: + tags.append(series) mi = { 'title' : self.db.title(row), 'authors' : au, 'cover' : self.db.cover(row), 'tags' : tags, - } + } + if series is not None: + mi['tag order'] = {series:self.db.books_in_series_of(row)} metadata.append(mi) return metadata