From 9665ef7649e1a6f9f4023c12a7fac15c92de0974 Mon Sep 17 00:00:00 2001 From: James Ralston Date: Wed, 29 Apr 2009 00:28:15 -0400 Subject: [PATCH] Added metadata from path fallback for jetbook --- src/calibre/devices/jetbook/driver.py | 38 ++++++++++++++++++++++----- src/calibre/gui2/device.py | 2 +- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/calibre/devices/jetbook/driver.py b/src/calibre/devices/jetbook/driver.py index fc98773f78..4eae09c208 100644 --- a/src/calibre/devices/jetbook/driver.py +++ b/src/calibre/devices/jetbook/driver.py @@ -4,12 +4,11 @@ __copyright__ = '2009, James Ralston ' Device driver for Ectaco Jetbook firmware >= JL04_v030e ''' -import os, shutil +import os, re, sys, shutil from itertools import cycle from calibre.devices.errors import FreeSpaceError -from calibre.devices.usbms.driver import USBMS -from calibre.devices.usbms.books import BookList +from calibre.devices.usbms.driver import USBMS, metadata_from_formats from calibre import sanitize_file_name as sanitize class JETBOOK(USBMS): @@ -36,6 +35,10 @@ class JETBOOK(USBMS): EBOOK_DIR_CARD = "Books" SUPPORTS_SUB_DIRS = True + JETBOOK_FILE_NAME_PATTERN = re.compile( + r'(?P.+)#(?P.+)' + ) + def upload_books(self, files, names, on_card=False, end_session=True, metadata=None): @@ -61,9 +64,9 @@ class JETBOOK(USBMS): if not os.path.exists(newpath): os.makedirs(newpath) - author = sanitize(mdata.get('authors','Unknown')) - title = sanitize(mdata.get('title', 'Unknown')) - (basename, fileext) = os.path.splitext(os.path.basename(names.next())) + author = sanitize(mdata.get('authors','Unknown')).replace(' ', '_') + title = sanitize(mdata.get('title', 'Unknown')).replace(' ', '_') + fileext = os.path.splitext(os.path.basename(names.next()))[1] fname = '%s#%s%s' % (author, title, fileext) filepath = os.path.join(newpath, fname) @@ -82,3 +85,26 @@ class JETBOOK(USBMS): return zip(paths, cycle([on_card])) + @classmethod + def metadata_from_path(cls, path): + + def check_unicode(txt): + txt = txt.replace('_', ' ') + if not isinstance(txt, unicode): + return txt.decode(sys.getfilesystemencoding(), 'replace') + + return txt + + mi = metadata_from_formats([path]) + + if (mi.title==_('Unknown') or mi.authors==[_('Unknown')]) \ + and '#' in mi.title: + fn = os.path.splitext(os.path.basename(path))[0] + match = cls.JETBOOK_FILE_NAME_PATTERN.match(fn) + if match is not None: + mi.title = check_unicode(match.group('title')) + authors = match.group('authors').split('&') + mi.authors = map(check_unicode, authors) + + return mi + diff --git a/src/calibre/gui2/device.py b/src/calibre/gui2/device.py index 03a6220f87..b2b9f14151 100644 --- a/src/calibre/gui2/device.py +++ b/src/calibre/gui2/device.py @@ -45,7 +45,7 @@ class DeviceJob(Job): class DeviceManager(Thread): - def __init__(self, connected_slot, job_manager, sleep_time=2): + def __init__(self, connected_slot, job_manager, sleep_time=20): ''' @param sleep_time: Time to sleep between device probes in millisecs @type sleep_time: integer