From fa95e6d8cfe493838d27e0836119af463e3ccbff Mon Sep 17 00:00:00 2001 From: Li Fanxi Date: Thu, 18 Aug 2011 16:52:44 +0800 Subject: [PATCH] [Feature] Add support for transferring PDF file to Bambook. --- src/calibre/devices/bambook/driver.py | 61 +++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/src/calibre/devices/bambook/driver.py b/src/calibre/devices/bambook/driver.py index b3a552b33c..5068a9c4d8 100644 --- a/src/calibre/devices/bambook/driver.py +++ b/src/calibre/devices/bambook/driver.py @@ -8,7 +8,7 @@ __docformat__ = 'restructuredtext en' Device driver for Sanda's Bambook ''' -import time, os, hashlib +import time, os, hashlib, shutil from itertools import cycle from calibre.devices.interface import DevicePlugin from calibre.devices.usbms.deviceconfig import DeviceConfig @@ -31,7 +31,7 @@ class BAMBOOK(DeviceConfig, DevicePlugin): ip = None - FORMATS = [ "snb" ] + FORMATS = [ "snb", "pdf" ] USER_CAN_ADD_NEW_FORMATS = False VENDOR_ID = 0x230b PRODUCT_ID = 0x0001 @@ -267,14 +267,59 @@ class BAMBOOK(DeviceConfig, DevicePlugin): for (i, f) in enumerate(files): self.report_progress((i+1) / float(len(files)), _('Transferring books to device...')) if not hasattr(f, 'read'): - if self.bambook.VerifySNB(f): - guid = self.bambook.SendFile(f, self.get_guid(metadata[i].uuid)) - if guid: - paths.append(guid) - else: - print "Send fail" + # Handle PDF File + if f[-3:].upper() == "PDF": + # Package the PDF file + with TemporaryDirectory() as tdir: + snbcdir = os.path.join(tdir, 'snbc') + snbfdir = os.path.join(tdir, 'snbf') + os.mkdir(snbcdir) + os.mkdir(snbfdir) + + tmpfile = open(os.path.join(snbfdir, 'book.snbf'), 'wb') + tmpfile.write(''' + + + + ZH-CN + + calibre + ''' + __appname__ + ' ' + __version__ + ''' + + + + + +''') + tmpfile.close() + tmpfile = open(os.path.join(snbfdir, 'toc.snbf'), 'wb') + tmpfile.write(''' + + 1 + + + + + +'''); + 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: print "book invalid" + if guid: + paths.append(guid) + else: + print "Send fail" + ret = zip(paths, cycle([on_card])) self.report_progress(1.0, _('Transferring books to device...')) return ret