mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #289
This commit is contained in:
parent
5107f19f08
commit
5b80e7034e
@ -106,6 +106,15 @@ class Book(object):
|
|||||||
return self.root + self.rpath
|
return self.root + self.rpath
|
||||||
return property(fget=fget, doc=doc)
|
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/"):
|
def __init__(self, node, tags=[], prefix="", root="/Data/media/"):
|
||||||
self.elem = node
|
self.elem = node
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
@ -123,6 +132,7 @@ def fix_ids(media, cache):
|
|||||||
Adjust ids in cache to correspond with media.
|
Adjust ids in cache to correspond with media.
|
||||||
'''
|
'''
|
||||||
media.purge_empty_playlists()
|
media.purge_empty_playlists()
|
||||||
|
media.reorder_playlists()
|
||||||
if cache.root:
|
if cache.root:
|
||||||
sourceid = media.max_id()
|
sourceid = media.max_id()
|
||||||
cid = sourceid + 1
|
cid = sourceid + 1
|
||||||
@ -144,6 +154,7 @@ class BookList(_BookList):
|
|||||||
|
|
||||||
def __init__(self, root="/Data/media/", sfile=None):
|
def __init__(self, root="/Data/media/", sfile=None):
|
||||||
_BookList.__init__(self)
|
_BookList.__init__(self)
|
||||||
|
self.tag_order = {}
|
||||||
self.root = self.document = self.proot = None
|
self.root = self.document = self.proot = None
|
||||||
if sfile:
|
if sfile:
|
||||||
sfile.seek(0)
|
sfile.seek(0)
|
||||||
@ -280,6 +291,8 @@ class BookList(_BookList):
|
|||||||
self.append(book)
|
self.append(book)
|
||||||
self.set_next_id(cid+1)
|
self.set_next_id(cid+1)
|
||||||
if self.prefix and info.has_key('tags'): # Playlists only supportted in main memory
|
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'])
|
self.set_playlists(book.id, info['tags'])
|
||||||
|
|
||||||
|
|
||||||
@ -334,7 +347,34 @@ class BookList(_BookList):
|
|||||||
continue
|
continue
|
||||||
return ans
|
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):
|
def write(self, stream):
|
||||||
""" Write XML representation of DOM tree to C{stream} """
|
""" Write XML representation of DOM tree to C{stream} """
|
||||||
|
@ -113,6 +113,15 @@ class Book(object):
|
|||||||
return self.mountpath + self.rpath
|
return self.mountpath + self.rpath
|
||||||
return property(fget=fget, doc=doc)
|
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=""):
|
def __init__(self, node, mountpath, tags, prefix=""):
|
||||||
self.elem = node
|
self.elem = node
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
@ -134,6 +143,7 @@ class BookList(_BookList):
|
|||||||
self.root_element = self.document.documentElement
|
self.root_element = self.document.documentElement
|
||||||
self.mountpath = mountpath
|
self.mountpath = mountpath
|
||||||
records = self.root_element.getElementsByTagName('records')
|
records = self.root_element.getElementsByTagName('records')
|
||||||
|
self.tag_order = {}
|
||||||
|
|
||||||
if records:
|
if records:
|
||||||
self.prefix = 'xs1:'
|
self.prefix = 'xs1:'
|
||||||
@ -202,6 +212,8 @@ class BookList(_BookList):
|
|||||||
book.datetime = ctime
|
book.datetime = ctime
|
||||||
self.append(book)
|
self.append(book)
|
||||||
if info.has_key('tags'):
|
if info.has_key('tags'):
|
||||||
|
if info.has_key('tag order'):
|
||||||
|
self.tag_order.update(info['tag order'])
|
||||||
self.set_tags(book, info['tags'])
|
self.set_tags(book, info['tags'])
|
||||||
|
|
||||||
def _delete_book(self, node):
|
def _delete_book(self, node):
|
||||||
@ -329,6 +341,35 @@ class BookList(_BookList):
|
|||||||
src = self.document.toxml('utf-8') + '\n'
|
src = self.document.toxml('utf-8') + '\n'
|
||||||
stream.write(src.replace("'", '''))
|
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):
|
def fix_ids(main, card):
|
||||||
'''
|
'''
|
||||||
Adjust ids the XML databases.
|
Adjust ids the XML databases.
|
||||||
@ -358,6 +399,8 @@ def fix_ids(main, card):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
item.parentNode.removeChild(item)
|
item.parentNode.removeChild(item)
|
||||||
item.unlink()
|
item.unlink()
|
||||||
|
|
||||||
|
db.reorder_playlists()
|
||||||
|
|
||||||
regen_ids(main)
|
regen_ids(main)
|
||||||
regen_ids(card)
|
regen_ids(card)
|
||||||
|
@ -236,12 +236,17 @@ class BooksModel(QAbstractTableModel):
|
|||||||
tags = []
|
tags = []
|
||||||
else:
|
else:
|
||||||
tags = tags.split(',')
|
tags = tags.split(',')
|
||||||
|
series = self.db.series(row)
|
||||||
|
if series is not None:
|
||||||
|
tags.append(series)
|
||||||
mi = {
|
mi = {
|
||||||
'title' : self.db.title(row),
|
'title' : self.db.title(row),
|
||||||
'authors' : au,
|
'authors' : au,
|
||||||
'cover' : self.db.cover(row),
|
'cover' : self.db.cover(row),
|
||||||
'tags' : tags,
|
'tags' : tags,
|
||||||
}
|
}
|
||||||
|
if series is not None:
|
||||||
|
mi['tag order'] = {series:self.db.books_in_series_of(row)}
|
||||||
|
|
||||||
metadata.append(mi)
|
metadata.append(mi)
|
||||||
return metadata
|
return metadata
|
||||||
|
Loading…
x
Reference in New Issue
Block a user