GwR revisions apple driver 0.5

This commit is contained in:
GRiker 2010-06-10 09:03:23 -06:00
parent e78d1dafc1
commit 7a67294ae7

View File

@ -114,6 +114,7 @@ class ITUNES(DevicePlugin):
# Properties # Properties
cached_books = {} cached_books = {}
cache_dir = os.path.join(config_dir, 'caches', 'itunes') cache_dir = os.path.join(config_dir, 'caches', 'itunes')
description_prefix = "added by calibre"
ejected = False ejected = False
iTunes= None iTunes= None
iTunes_media = None iTunes_media = None
@ -620,6 +621,7 @@ class ITUNES(DevicePlugin):
if DEBUG: if DEBUG:
self.log.info("ITUNES.remove_books_from_metadata()") self.log.info("ITUNES.remove_books_from_metadata()")
for path in paths: for path in paths:
self._dump_cached_book(self.cached_books[path])
if self.cached_books[path]['lib_book']: if self.cached_books[path]['lib_book']:
# Remove from the booklist # Remove from the booklist
for i,book in enumerate(booklists[0]): for i,book in enumerate(booklists[0]):
@ -881,6 +883,7 @@ class ITUNES(DevicePlugin):
'author': metadata[i].author[0], 'author': metadata[i].author[0],
'lib_book': lb_added, 'lib_book': lb_added,
'dev_book': db_added } 'dev_book': db_added }
self._dump_cached_books(header="after upload_books()")
# Report progress # Report progress
if self.report_progress is not None: if self.report_progress is not None:
@ -1015,11 +1018,11 @@ class ITUNES(DevicePlugin):
def _add_library_book(self,file, metadata): def _add_library_book(self,file, metadata):
''' '''
assumes pythoncom wrapper windows assumes pythoncom wrapper
''' '''
self.log.info(" ITUNES._add_library_book()") self.log.info(" ITUNES._add_library_book()")
if isosx: if isosx:
print "to be implemented" added = self.iTunes.add(appscript.mactypes.File(file))
elif iswindows: elif iswindows:
lib = self.iTunes.LibraryPlaylist lib = self.iTunes.LibraryPlaylist
@ -1095,6 +1098,28 @@ class ITUNES(DevicePlugin):
self.log.info(" ITUNES._cover_to_thumb()") self.log.info(" ITUNES._cover_to_thumb()")
thumb = None thumb = None
if metadata.cover: if metadata.cover:
if isosx:
cover_data = open(metadata.cover,'rb')
if lb_added:
lb_added.artworks[1].data_.set(cover_data.read())
if db_added:
# The following command generates an error, but the artwork does in fact
# get sent to the device. Seems like a bug in Apple's automation interface
try:
db_added.artworks[1].data_.set(cover_data.read())
except:
if DEBUG:
self.log.warning(" iTunes automation interface generated an error"
" when adding artwork to '%s'" % metadata.title)
#import traceback
#traceback.print_exc()
#from calibre import ipython
#ipython(user_ns=locals())
pass
elif iswindows:
if lb_added: if lb_added:
if lb_added.Artwork.Count: if lb_added.Artwork.Count:
lb_added.Artwork.Item(1).SetArtworkFromFile(metadata.cover) lb_added.Artwork.Item(1).SetArtworkFromFile(metadata.cover)
@ -1246,6 +1271,31 @@ class ITUNES(DevicePlugin):
self.log.info("%-40.40s %-30.30s" % self.log.info("%-40.40s %-30.30s" %
(book.title, book.author)) (book.title, book.author))
def _dump_cached_book(self, cached_book, header=None):
'''
'''
if header:
msg = '%s' % header
self.log.info(msg)
self.log.info( "%s" % ('-' * len(msg)))
if isosx:
self.log.info("%-40.40s %-30.30s %-10.10s %-10.10s" %
('title',
'author',
'lib_book',
'dev_book'))
self.log.info("%-40.40s %-30.30s %-10.10s %-10.10s" %
(cached_book['title'],
cached_book['author'],
str(cached_book['lib_book'])[-9:],
str(cached_book['dev_book'])[-9:]))
elif iswindows:
self.log.info("%-40.40s %-30.30s" %
(cached_book['title'],
cached_book['author']))
self.log.info()
def _dump_cached_books(self, header=None): def _dump_cached_books(self, header=None):
''' '''
''' '''
@ -1254,6 +1304,11 @@ class ITUNES(DevicePlugin):
self.log.info(msg) self.log.info(msg)
self.log.info( "%s" % ('-' * len(msg))) self.log.info( "%s" % ('-' * len(msg)))
if isosx: if isosx:
self.log.info("%-40.40s %-30.30s %-10.10s %-10.10s" %
('title',
'author',
'lib_book',
'dev_book'))
for cb in self.cached_books.keys(): for cb in self.cached_books.keys():
self.log.info("%-40.40s %-30.30s %-10.10s %-10.10s" % self.log.info("%-40.40s %-30.30s %-10.10s %-10.10s" %
(self.cached_books[cb]['title'], (self.cached_books[cb]['title'],
@ -1411,6 +1466,7 @@ class ITUNES(DevicePlugin):
else: else:
return thumb_data return thumb_data
self.log.info(" ITUNES._generate_thumbnail()")
if isosx: if isosx:
try: try:
# Resize the cover # Resize the cover
@ -1424,18 +1480,16 @@ class ITUNES(DevicePlugin):
# Cache the tagged thumb # Cache the tagged thumb
if DEBUG: if DEBUG:
self.log.info("ITUNES._generate_thumbnail(): generated thumb for '%s', caching" % book.name()) self.log.info(" generated thumb for '%s', caching" % book.name())
zfw.writestr(thumb_path, thumb.getvalue()) zfw.writestr(thumb_path, thumb.getvalue())
zfw.close() zfw.close()
return thumb.getvalue() return thumb.getvalue()
except: except:
self.log.error("ITUNES._generate_thumbnail(): error generating thumb for '%s'" % book.name()) self.log.error(" error generating thumb for '%s'" % book.name())
return None return None
elif iswindows: elif iswindows:
if DEBUG:
self.log.info("ITUNES._generate_thumbnail()")
if not book.Artwork.Count: if not book.Artwork.Count:
if DEBUG: if DEBUG:
self.log.info(" no artwork available") self.log.info(" no artwork available")
@ -1547,6 +1601,8 @@ class ITUNES(DevicePlugin):
''' '''
assumes pythoncom wrapper assumes pythoncom wrapper
''' '''
if DEBUG:
self.log.info(" ITUNES._get_device_books_playlist()")
if iswindows: if iswindows:
if 'iPod' in self.sources: if 'iPod' in self.sources:
pl = None pl = None
@ -1623,13 +1679,21 @@ 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
if str(book.description()).startswith(self.description_prefix):
if book.location() == appscript.k.missing_value:
if DEBUG: if DEBUG:
self.log.info(" adding %-30.30s [%s]" % (book.name(), book.kind())) self.log.info(" deleting calibre orphan '%s' from Library|Books" % book.name())
book.delete()
continue
path = self.path_template % (book.name(), book.artist()) path = self.path_template % (book.name(), book.artist())
library_books[path] = book library_books[path] = book
if DEBUG:
self.log.info(" adding %-30.30s [%s]" % (book.name(), book.kind()))
else: else:
if DEBUG: if DEBUG:
self.log.info('No Library playlists') self.log.info(' no Library playlists')
else: else:
if DEBUG: if DEBUG:
self.log.info(' no Library found') self.log.info(' no Library found')
@ -1671,10 +1735,18 @@ 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
if book.Description.startswith(self.description_prefix):
if not book.Location:
if DEBUG: if DEBUG:
self.log.info(" adding %-30.30s [%s]" % (book.Name, book.KindAsString)) self.log.info(" deleting calibre orphan '%s' from Library|Books" % book.Name)
book.Delete()
continue
path = self.path_template % (book.Name, book.Artist) path = self.path_template % (book.Name, book.Artist)
library_books[path] = book library_books[path] = book
if DEBUG:
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")
@ -1835,7 +1907,6 @@ class ITUNES(DevicePlugin):
if DEBUG: if DEBUG:
self.log.info(" deleting %s" % cached_book['dev_book']) self.log.info(" deleting %s" % cached_book['dev_book'])
result = cached_book['dev_book'].delete() result = cached_book['dev_book'].delete()
print "result: %s" % result
elif iswindows: elif iswindows:
dev_pl = self._get_device_books_playlist() dev_pl = self._get_device_books_playlist()
@ -1856,12 +1927,14 @@ class ITUNES(DevicePlugin):
We only want to delete stored copies if the file is stored in iTunes We only want to delete stored copies if the file is stored in iTunes
We don't want to delete files stored outside of iTunes We don't want to delete files stored outside of iTunes
''' '''
if DEBUG:
self.log.info(" ITUNES._remove_from_iTunes():")
if isosx: if isosx:
storage_path = os.path.split(cached_book['lib_book'].location().path) storage_path = os.path.split(cached_book['lib_book'].location().path)
if cached_book['lib_book'].location().path.startswith(self.iTunes_media): if cached_book['lib_book'].location().path.startswith(self.iTunes_media):
title_storage_path = storage_path[0] title_storage_path = storage_path[0]
if DEBUG: if DEBUG:
self.log.info("ITUNES._remove_from_iTunes():")
self.log.info(" removing title_storage_path: %s" % title_storage_path) self.log.info(" removing title_storage_path: %s" % title_storage_path)
try: try:
shutil.rmtree(title_storage_path) shutil.rmtree(title_storage_path)
@ -1892,15 +1965,13 @@ class ITUNES(DevicePlugin):
Assume we're wrapped in a pythoncom Assume we're wrapped in a pythoncom
Windows stores the book under a common author directory, so we just delete the .epub Windows stores the book under a common author directory, so we just delete the .epub
''' '''
if DEBUG:
self.log.info("ITUNES._remove_from_iTunes():\n '%s'" % cached_book['title'])
book = self._find_library_book(cached_book) book = self._find_library_book(cached_book)
if book: if book:
path = book.Location path = book.Location
storage_path = os.path.split(book.Location) storage_path = os.path.split(book.Location)
if book.Location.startswith(self.iTunes_media): if book.Location.startswith(self.iTunes_media):
if DEBUG: if DEBUG:
self.log.info("ITUNES._remove_from_iTunes():")
self.log.info(" removing '%s' at %s" % self.log.info(" removing '%s' at %s" %
(cached_book['title'], path)) (cached_book['title'], path))
try: try:
@ -1995,13 +2066,13 @@ class ITUNES(DevicePlugin):
pass pass
if lb_added: if lb_added:
lb_added.description.set("added by calibre %s" % strftime('%Y-%m-%d %H:%M:%S')) lb_added.description.set("%s %s" % (self.description_prefix,strftime('%Y-%m-%d %H:%M:%S')))
lb_added.enabled.set(True) lb_added.enabled.set(True)
lb_added.sort_artist.set(metadata.author_sort.title()) lb_added.sort_artist.set(metadata.author_sort.title())
lb_added.sort_name.set(this_book.title_sorter) lb_added.sort_name.set(this_book.title_sorter)
if db_added: if db_added:
db_added.description.set("added by calibre %s" % strftime('%Y-%m-%d %H:%M:%S')) db_added.description.set("%s %s" % (self.description_prefix,strftime('%Y-%m-%d %H:%M:%S')))
db_added.enabled.set(True) db_added.enabled.set(True)
db_added.sort_artist.set(metadata.author_sort.title()) db_added.sort_artist.set(metadata.author_sort.title())
db_added.sort_name.set(this_book.title_sorter) db_added.sort_name.set(this_book.title_sorter)
@ -2036,14 +2107,13 @@ class ITUNES(DevicePlugin):
pass pass
if lb_added: if lb_added:
lb_added.Description = ("added by calibre %s" % strftime('%Y-%m-%d %H:%M:%S')) lb_added.Description = ("%s %s" % (self.description_prefix,strftime('%Y-%m-%d %H:%M:%S')))
lb_added.Enabled = True lb_added.Enabled = True
lb_added.SortArtist = (metadata.author_sort.title()) lb_added.SortArtist = (metadata.author_sort.title())
lb_added.SortName = (this_book.title_sorter) lb_added.SortName = (this_book.title_sorter)
if db_added: if db_added:
db_added.Description = ("added by calibre %s" % strftime('%Y-%m-%d %H:%M:%S')) db_added.Description = ("%s %s" % (self.description_prefix,strftime('%Y-%m-%d %H:%M:%S')))
db_added.Enabled = True
db_added.SortArtist = (metadata.author_sort.title()) db_added.SortArtist = (metadata.author_sort.title())
db_added.SortName = (this_book.title_sorter) db_added.SortName = (this_book.title_sorter)