From 8bec5211c19513b8a55ecaad62a1c0177d36cea7 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 31 Aug 2012 21:50:23 +0530 Subject: [PATCH] Refactor to make build_template_regexp a utility function --- src/calibre/devices/__init__.py | 25 +++++++++++++++++++++- src/calibre/devices/mtp/base.py | 32 ++++++++--------------------- src/calibre/devices/usbms/driver.py | 23 +++------------------ 3 files changed, 35 insertions(+), 45 deletions(-) diff --git a/src/calibre/devices/__init__.py b/src/calibre/devices/__init__.py index 37ec55c149..ab772c6905 100644 --- a/src/calibre/devices/__init__.py +++ b/src/calibre/devices/__init__.py @@ -5,7 +5,7 @@ __copyright__ = '2008, Kovid Goyal ' Device drivers. ''' -import sys, time, pprint, operator +import sys, time, pprint, operator, re from functools import partial from StringIO import StringIO @@ -27,6 +27,29 @@ def strftime(epoch, zone=time.gmtime): src[2] = INVERSE_MONTH_MAP[int(src[2])] return ' '.join(src) +def build_template_regexp(template): + from calibre import prints + + def replfunc(match, seen=None): + v = match.group(1) + if v in ['authors', 'author_sort']: + v = 'author' + if v in ('title', 'series', 'series_index', 'isbn', 'author'): + if v not in seen: + seen.add(v) + return '(?P<' + v + '>.+?)' + return '(.+?)' + s = set() + f = partial(replfunc, seen=s) + + try: + template = template.rpartition('/')[2] + return re.compile(re.sub('{([^}]*)}', f, template) + '([_\d]*$)') + except: + prints(u'Failed to parse template: %r'%template) + template = u'{title} - {authors}' + return re.compile(re.sub('{([^}]*)}', f, template) + '([_\d]*$)') + def get_connected_device(): from calibre.customize.ui import device_plugins from calibre.devices.scanner import DeviceScanner diff --git a/src/calibre/devices/mtp/base.py b/src/calibre/devices/mtp/base.py index 3b71e40619..346a44e6e8 100644 --- a/src/calibre/devices/mtp/base.py +++ b/src/calibre/devices/mtp/base.py @@ -7,8 +7,7 @@ __license__ = 'GPL v3' __copyright__ = '2012, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import re -from functools import wraps, partial +from functools import wraps from calibre import prints from calibre.constants import DEBUG @@ -61,27 +60,12 @@ class MTPDeviceBase(DevicePlugin): return False def build_template_regexp(self): - return None - # TODO: Implement this - def replfunc(match, seen=None): - v = match.group(1) - if v in ['authors', 'author_sort']: - v = 'author' - if v in ('title', 'series', 'series_index', 'isbn', 'author'): - if v not in seen: - seen.add(v) - return '(?P<' + v + '>.+?)' - return '(.+?)' - s = set() - f = partial(replfunc, seen=s) - template = None - try: - template = self.save_template().rpartition('/')[2] - return re.compile(re.sub('{([^}]*)}', f, template) + '([_\d]*$)') - except: - prints(u'Failed to parse template: %r'%template) - template = u'{title} - {authors}' - return re.compile(re.sub('{([^}]*)}', f, template) + '([_\d]*$)') - + from calibre.devices import build_template_regexp + # TODO: Use the device specific template here + return build_template_regexp(self.default_save_template) + @property + def default_save_template(cls): + from calibre.library.save_to_disk import config + return config().parse().send_template diff --git a/src/calibre/devices/usbms/driver.py b/src/calibre/devices/usbms/driver.py index 12e30073ac..f6c7556fd8 100644 --- a/src/calibre/devices/usbms/driver.py +++ b/src/calibre/devices/usbms/driver.py @@ -10,7 +10,7 @@ driver. It is intended to be subclassed with the relevant parts implemented for a particular device. ''' -import os, re, time, json, functools, shutil +import os, time, json, shutil from itertools import cycle from calibre.constants import numeric_version @@ -404,25 +404,8 @@ class USBMS(CLI, Device): @classmethod def build_template_regexp(cls): - def replfunc(match, seen=None): - v = match.group(1) - if v in ['authors', 'author_sort']: - v = 'author' - if v in ('title', 'series', 'series_index', 'isbn', 'author'): - if v not in seen: - seen.add(v) - return '(?P<' + v + '>.+?)' - return '(.+?)' - s = set() - f = functools.partial(replfunc, seen=s) - template = None - try: - template = cls.save_template().rpartition('/')[2] - return re.compile(re.sub('{([^}]*)}', f, template) + '([_\d]*$)') - except: - prints(u'Failed to parse template: %r'%template) - template = u'{title} - {authors}' - return re.compile(re.sub('{([^}]*)}', f, template) + '([_\d]*$)') + from calibre.devices import build_template_regexp + return build_template_regexp(cls.save_template()) @classmethod def path_to_unicode(cls, path):