Bambook driver: Allow direct rtansfer of PDF files to Bambook devices

This commit is contained in:
Kovid Goyal 2011-08-18 16:14:07 -06:00
commit e6401bf539

View File

@ -8,7 +8,7 @@ __docformat__ = 'restructuredtext en'
Device driver for Sanda's Bambook Device driver for Sanda's Bambook
''' '''
import time, os, hashlib import time, os, hashlib, shutil
from itertools import cycle from itertools import cycle
from calibre.devices.interface import DevicePlugin from calibre.devices.interface import DevicePlugin
from calibre.devices.usbms.deviceconfig import DeviceConfig from calibre.devices.usbms.deviceconfig import DeviceConfig
@ -31,7 +31,7 @@ class BAMBOOK(DeviceConfig, DevicePlugin):
ip = None ip = None
FORMATS = [ "snb" ] FORMATS = [ "snb", "pdf" ]
USER_CAN_ADD_NEW_FORMATS = False USER_CAN_ADD_NEW_FORMATS = False
VENDOR_ID = 0x230b VENDOR_ID = 0x230b
PRODUCT_ID = 0x0001 PRODUCT_ID = 0x0001
@ -267,14 +267,59 @@ class BAMBOOK(DeviceConfig, DevicePlugin):
for (i, f) in enumerate(files): for (i, f) in enumerate(files):
self.report_progress((i+1) / float(len(files)), _('Transferring books to device...')) self.report_progress((i+1) / float(len(files)), _('Transferring books to device...'))
if not hasattr(f, 'read'): if not hasattr(f, 'read'):
if self.bambook.VerifySNB(f): # Handle PDF File
guid = self.bambook.SendFile(f, self.get_guid(metadata[i].uuid)) if f[-3:].upper() == "PDF":
if guid: # Package the PDF file
paths.append(guid) with TemporaryDirectory() as tdir:
else: snbcdir = os.path.join(tdir, 'snbc')
print "Send fail" snbfdir = os.path.join(tdir, 'snbf')
os.mkdir(snbcdir)
os.mkdir(snbfdir)
tmpfile = open(os.path.join(snbfdir, 'book.snbf'), 'wb')
tmpfile.write('''<book-snbf version="1.0">
<head>
<name><![CDATA[''' + metadata[i].title + ''']]></name>
<author><![CDATA[''' + ' '.join(metadata[i].authors) + ''']]></author>
<language>ZH-CN</language>
<rights/>
<publisher>calibre</publisher>
<generator>''' + __appname__ + ' ' + __version__ + '''</generator>
<created/>
<abstract></abstract>
<cover/>
</head>
</book-snbf>
''')
tmpfile.close()
tmpfile = open(os.path.join(snbfdir, 'toc.snbf'), 'wb')
tmpfile.write('''<toc-snbf>
<head>
<chapters>1</chapters>
</head>
<body>
<chapter src="pdf1.pdf"><![CDATA[''' + metadata[i].title + ''']]></chapter>
</body>
</toc-snbf>
''');
tmpfile.close()
pdf_name = os.path.join(snbcdir, "pdf1.pdf")
shutil.copyfile(f, pdf_name)
with TemporaryFile('.snb') as snbfile:
if self.bambook.PackageSNB(snbfile, tdir) and self.bambook.VerifySNB(snbfile):
guid = self.bambook.SendFile(snbfile, self.get_guid(metadata[i].uuid))
elif f[-3:].upper() == 'SNB':
if self.bambook.VerifySNB(f):
guid = self.bambook.SendFile(f, self.get_guid(metadata[i].uuid))
else: else:
print "book invalid" print "book invalid"
if guid:
paths.append(guid)
else:
print "Send fail"
ret = zip(paths, cycle([on_card])) ret = zip(paths, cycle([on_card]))
self.report_progress(1.0, _('Transferring books to device...')) self.report_progress(1.0, _('Transferring books to device...'))
return ret return ret