This commit is contained in:
Kovid Goyal 2010-06-11 12:17:44 -06:00
commit 13acbf4459

View File

@ -147,6 +147,7 @@ class ITUNES(DevicePlugin):
ejected = False ejected = False
iTunes= None iTunes= None
iTunes_media = None iTunes_media = None
library_orphans = None
log = Log() log = Log()
manual_sync_mode = False manual_sync_mode = False
path_template = 'iTunes/%s - %s.epub' path_template = 'iTunes/%s - %s.epub'
@ -244,14 +245,13 @@ class ITUNES(DevicePlugin):
# Fetch a list of books from iPod device connected to iTunes # Fetch a list of books from iPod device connected to iTunes
# Fetch Library|Books
library_books = self._get_library_books()
if 'iPod' in self.sources: if 'iPod' in self.sources:
booklist = BookList(self.log) booklist = BookList(self.log)
cached_books = {} cached_books = {}
if isosx: if isosx:
library_books = self._get_library_books()
device_books = self._get_device_books() device_books = self._get_device_books()
book_count = float(len(device_books)) book_count = float(len(device_books))
for (i,book) in enumerate(device_books): for (i,book) in enumerate(device_books):
@ -281,11 +281,13 @@ class ITUNES(DevicePlugin):
if self.report_progress is not None: if self.report_progress is not None:
self.report_progress(i+1/book_count, _('%d of %d') % (i+1, book_count)) self.report_progress(i+1/book_count, _('%d of %d') % (i+1, book_count))
self._purge_orphans(cached_books)
elif iswindows: elif iswindows:
try: try:
pythoncom.CoInitialize() pythoncom.CoInitialize()
self.iTunes = win32com.client.Dispatch("iTunes.Application") self.iTunes = win32com.client.Dispatch("iTunes.Application")
library_books = self._get_library_books()
device_books = self._get_device_books() device_books = self._get_device_books()
book_count = float(len(device_books)) book_count = float(len(device_books))
for (i,book) in enumerate(device_books): for (i,book) in enumerate(device_books):
@ -315,6 +317,7 @@ class ITUNES(DevicePlugin):
if self.report_progress is not None: if self.report_progress is not None:
self.report_progress(i+1/book_count, self.report_progress(i+1/book_count,
_('%d of %d') % (i+1, book_count)) _('%d of %d') % (i+1, book_count))
self._purge_orphans(cached_books)
finally: finally:
pythoncom.CoUninitialize() pythoncom.CoUninitialize()
@ -1671,11 +1674,13 @@ class ITUNES(DevicePlugin):
def _get_library_books(self): def _get_library_books(self):
''' '''
Populate a dict of paths from iTunes Library|Books Populate a dict of paths from iTunes Library|Books
Windows assumes pythoncom wrapper
''' '''
if DEBUG: if DEBUG:
self.log.info("\n ITUNES._get_library_books()") self.log.info("\n ITUNES._get_library_books()")
library_books = {} library_books = {}
library_orphans = {}
lib = None lib = None
if isosx: if isosx:
@ -1708,15 +1713,14 @@ class ITUNES(DevicePlugin):
if DEBUG: if DEBUG:
self.log.info(" ignoring '%s' of type '%s'" % (book.name(), book.kind())) self.log.info(" ignoring '%s' of type '%s'" % (book.name(), book.kind()))
else: else:
# Remove calibre orphans # Collect calibre orphans - remnants of recipe uploads
path = self.path_template % (book.name(), book.artist())
if str(book.description()).startswith(self.description_prefix): if str(book.description()).startswith(self.description_prefix):
if book.location() == appscript.k.missing_value: if book.location() == appscript.k.missing_value:
library_orphans[path] = book
if DEBUG: if DEBUG:
self.log.info(" found calibre orphan '%s' in Library|Books" % book.name()) self.log.info(" found calibre orphan '%s' in Library|Books" % book.name())
#book.delete()
#continue
path = self.path_template % (book.name(), book.artist())
library_books[path] = book library_books[path] = book
if DEBUG: if DEBUG:
self.log.info(" adding %-30.30s [%s]" % (book.name(), book.kind())) self.log.info(" adding %-30.30s [%s]" % (book.name(), book.kind()))
@ -1729,9 +1733,9 @@ class ITUNES(DevicePlugin):
elif iswindows: elif iswindows:
lib = None lib = None
try: # try:
pythoncom.CoInitialize() # pythoncom.CoInitialize()
self.iTunes = win32com.client.Dispatch("iTunes.Application") # self.iTunes = win32com.client.Dispatch("iTunes.Application")
for source in self.iTunes.sources: for source in self.iTunes.sources:
if source.Kind == self.Sources.index('Library'): if source.Kind == self.Sources.index('Library'):
lib = source lib = source
@ -1764,24 +1768,24 @@ class ITUNES(DevicePlugin):
if DEBUG: if DEBUG:
self.log.info(" ignoring %-30.30s of type '%s'" % (book.Name, book.KindAsString)) self.log.info(" ignoring %-30.30s of type '%s'" % (book.Name, book.KindAsString))
else: else:
# Remove calibre orphans path = self.path_template % (book.Name, book.Artist)
# Collect calibre orphans
if book.Description.startswith(self.description_prefix): if book.Description.startswith(self.description_prefix):
if not book.Location: if not book.Location:
library_orphans[path] = book
if DEBUG: if DEBUG:
self.log.info(" found calibre orphan '%s' in Library|Books" % book.Name) self.log.info(" found calibre orphan '%s' in Library|Books" % book.Name)
#book.Delete()
#continue
path = self.path_template % (book.Name, book.Artist)
library_books[path] = book library_books[path] = book
if DEBUG: if DEBUG:
self.log.info(" adding %-30.30s [%s]" % (book.Name, book.KindAsString)) self.log.info(" adding %-30.30s [%s]" % (book.Name, book.KindAsString))
except: except:
if DEBUG: if DEBUG:
self.log.info(" no books in library") self.log.info(" no books in library")
finally: # finally:
pythoncom.CoUninitialize() # pythoncom.CoUninitialize()
self.library_orphans = library_orphans
return library_books return library_books
def _get_purchased_book_ids(self): def _get_purchased_book_ids(self):
@ -1904,6 +1908,45 @@ class ITUNES(DevicePlugin):
self.version[0],self.version[1],self.version[2])) self.version[0],self.version[1],self.version[2]))
self.log.info(" iTunes_media: %s" % self.iTunes_media) self.log.info(" iTunes_media: %s" % self.iTunes_media)
def _purge_orphans(self,cached_books):
'''
Scan self.library_orphans for any paths not on device
Remove any true orphans from iTunes
This occurs when recipes are uploaded in a previous session
and the book has since been deleted on the device
'''
if DEBUG:
self.log.info(" ITUNES._purge_orphans")
#self.log.info(" cached_books:\n %s" % "\n ".join(cached_books.keys()))
orphan_paths = {}
if isosx:
for orphan in self.library_orphans:
path = self.path_template % (self.library_orphans[orphan].name(),
self.library_orphans[orphan].artist())
orphan_paths[path] = self.library_orphans[orphan]
# Scan orphan_paths for paths not found in cached_books
for orphan in orphan_paths.keys():
if orphan not in cached_books:
if DEBUG:
self.log.info(" '%s' not found on device, removing from iTunes" % orphan)
self.iTunes.delete(orphan_paths[orphan])
elif iswindows:
for orphan in self.library_orphans:
path = self.path_template % (self.library_orphans[orphan].Name,
self.library_orphans[orphan].Artist)
orphan_paths[path] = self.library_orphans[orphan]
# Scan orphan_paths for paths not found in cached_books
for orphan in orphan_paths.keys():
if orphan not in cached_books:
if DEBUG:
self.log.info(" '%s' not found on device, removing from iTunes" % orphan)
orphan_paths[orphan].Delete()
def _remove_existing_copies(self,path,file,metadata): def _remove_existing_copies(self,path,file,metadata):
''' '''
''' '''