mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Make template processing more robust (I hope)
This commit is contained in:
parent
8324d42162
commit
50aae924fc
@ -8,6 +8,7 @@ __docformat__ = 'restructuredtext en'
|
|||||||
import copy, traceback
|
import copy, traceback
|
||||||
|
|
||||||
from calibre import prints
|
from calibre import prints
|
||||||
|
from calibre.constants import DEBUG
|
||||||
from calibre.ebooks.metadata.book import SC_COPYABLE_FIELDS
|
from calibre.ebooks.metadata.book import SC_COPYABLE_FIELDS
|
||||||
from calibre.ebooks.metadata.book import SC_FIELDS_COPY_NOT_NULL
|
from calibre.ebooks.metadata.book import SC_FIELDS_COPY_NOT_NULL
|
||||||
from calibre.ebooks.metadata.book import STANDARD_METADATA_FIELDS
|
from calibre.ebooks.metadata.book import STANDARD_METADATA_FIELDS
|
||||||
@ -50,6 +51,8 @@ class SafeFormat(TemplateFormatter):
|
|||||||
return ''
|
return ''
|
||||||
return v
|
return v
|
||||||
except:
|
except:
|
||||||
|
if DEBUG:
|
||||||
|
traceback.print_exc()
|
||||||
return key
|
return key
|
||||||
|
|
||||||
composite_formatter = SafeFormat()
|
composite_formatter = SafeFormat()
|
||||||
@ -320,8 +323,8 @@ class Metadata(object):
|
|||||||
else:
|
else:
|
||||||
self.set(dest, val)
|
self.set(dest, val)
|
||||||
except:
|
except:
|
||||||
|
if DEBUG:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
pass
|
|
||||||
|
|
||||||
# Old Metadata API {{{
|
# Old Metadata API {{{
|
||||||
def print_all_attributes(self):
|
def print_all_attributes(self):
|
||||||
|
@ -340,7 +340,7 @@ class DeviceManager(Thread): # {{{
|
|||||||
cpb = None
|
cpb = None
|
||||||
|
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
prints('Using plugboard', ext, dev_name, cpb)
|
prints('Device using plugboard', ext, dev_name, cpb)
|
||||||
if ext:
|
if ext:
|
||||||
try:
|
try:
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
|
@ -8,6 +8,8 @@ __docformat__ = 'restructuredtext en'
|
|||||||
|
|
||||||
import os, traceback, cStringIO, re
|
import os, traceback, cStringIO, re
|
||||||
|
|
||||||
|
from calibre import prints
|
||||||
|
from calibre.constants import DEBUG
|
||||||
from calibre.utils.config import Config, StringConfig, tweaks
|
from calibre.utils.config import Config, StringConfig, tweaks
|
||||||
from calibre.utils.formatter import TemplateFormatter
|
from calibre.utils.formatter import TemplateFormatter
|
||||||
from calibre.utils.filenames import shorten_components_to, supports_long_names, \
|
from calibre.utils.filenames import shorten_components_to, supports_long_names, \
|
||||||
@ -118,7 +120,7 @@ class SafeFormat(TemplateFormatter):
|
|||||||
try:
|
try:
|
||||||
b = self.book.get_user_metadata(key, False)
|
b = self.book.get_user_metadata(key, False)
|
||||||
except:
|
except:
|
||||||
print 'save_to_disk get value exception'
|
if DEBUG:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
b = None
|
b = None
|
||||||
|
|
||||||
@ -129,13 +131,13 @@ class SafeFormat(TemplateFormatter):
|
|||||||
self.composite_values[key] = \
|
self.composite_values[key] = \
|
||||||
self.vformat(b['display']['composite_template'], [], kwargs)
|
self.vformat(b['display']['composite_template'], [], kwargs)
|
||||||
return self.composite_values[key]
|
return self.composite_values[key]
|
||||||
if kwargs[key]:
|
if key in kwargs:
|
||||||
return self.sanitize(kwargs[key])
|
return kwargs[key].replace('/', '_').replace('\\', '_')
|
||||||
return ''
|
return ''
|
||||||
except:
|
except:
|
||||||
print 'save_to_disk general exception'
|
if DEBUG:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return ''
|
return key
|
||||||
|
|
||||||
safe_formatter = SafeFormat()
|
safe_formatter = SafeFormat()
|
||||||
|
|
||||||
@ -182,8 +184,8 @@ def get_components(template, mi, id, timefmt='%b %Y', length=250,
|
|||||||
elif custom_metadata[key]['datatype'] == 'bool':
|
elif custom_metadata[key]['datatype'] == 'bool':
|
||||||
format_args[key] = _('yes') if format_args[key] else _('no')
|
format_args[key] = _('yes') if format_args[key] else _('no')
|
||||||
|
|
||||||
components = safe_formatter.safe_format(template, format_args, '', mi,
|
components = safe_formatter.safe_format(template, format_args,
|
||||||
sanitize=sanitize_func)
|
'G_C-EXCEPTION!', mi)
|
||||||
components = [x.strip() for x in components.split('/') if x.strip()]
|
components = [x.strip() for x in components.split('/') if x.strip()]
|
||||||
components = [sanitize_func(x) for x in components if x]
|
components = [sanitize_func(x) for x in components if x]
|
||||||
if not components:
|
if not components:
|
||||||
@ -267,7 +269,8 @@ def save_book_to_disk(id, db, root, opts, length):
|
|||||||
cpb = cpb[dev_name]
|
cpb = cpb[dev_name]
|
||||||
else:
|
else:
|
||||||
cpb = None
|
cpb = None
|
||||||
#prints('Using plugboard:', fmt, cpb)
|
if DEBUG:
|
||||||
|
prints('Save-to-disk using plugboard:', fmt, cpb)
|
||||||
data = db.format(id, fmt, index_is_id=True)
|
data = db.format(id, fmt, index_is_id=True)
|
||||||
if data is None:
|
if data is None:
|
||||||
continue
|
continue
|
||||||
@ -285,6 +288,7 @@ def save_book_to_disk(id, db, root, opts, length):
|
|||||||
newmi = mi
|
newmi = mi
|
||||||
set_metadata(stream, newmi, fmt)
|
set_metadata(stream, newmi, fmt)
|
||||||
except:
|
except:
|
||||||
|
if DEBUG:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
stream.seek(0)
|
stream.seek(0)
|
||||||
data = stream.read()
|
data = stream.read()
|
||||||
|
@ -4,7 +4,9 @@ Created on 23 Sep 2010
|
|||||||
@author: charles
|
@author: charles
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import re, string
|
import re, string, traceback
|
||||||
|
|
||||||
|
from calibre.constants import DEBUG
|
||||||
|
|
||||||
class TemplateFormatter(string.Formatter):
|
class TemplateFormatter(string.Formatter):
|
||||||
'''
|
'''
|
||||||
@ -19,7 +21,6 @@ class TemplateFormatter(string.Formatter):
|
|||||||
string.Formatter.__init__(self)
|
string.Formatter.__init__(self)
|
||||||
self.book = None
|
self.book = None
|
||||||
self.kwargs = None
|
self.kwargs = None
|
||||||
self.sanitize = None
|
|
||||||
|
|
||||||
def _lookup(self, val, field_if_set, field_not_set):
|
def _lookup(self, val, field_if_set, field_not_set):
|
||||||
if val:
|
if val:
|
||||||
@ -99,7 +100,7 @@ class TemplateFormatter(string.Formatter):
|
|||||||
return fmt, '', ''
|
return fmt, '', ''
|
||||||
return matches.groups()
|
return matches.groups()
|
||||||
except:
|
except:
|
||||||
import traceback
|
if DEBUG:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return fmt, '', ''
|
return fmt, '', ''
|
||||||
|
|
||||||
@ -139,14 +140,15 @@ class TemplateFormatter(string.Formatter):
|
|||||||
ans = string.Formatter.vformat(self, fmt, args, kwargs)
|
ans = string.Formatter.vformat(self, fmt, args, kwargs)
|
||||||
return self.compress_spaces.sub(' ', ans).strip()
|
return self.compress_spaces.sub(' ', ans).strip()
|
||||||
|
|
||||||
def safe_format(self, fmt, kwargs, error_value, book, sanitize=None):
|
def safe_format(self, fmt, kwargs, error_value, book):
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
self.book = book
|
self.book = book
|
||||||
self.sanitize = sanitize
|
|
||||||
self.composite_values = {}
|
self.composite_values = {}
|
||||||
try:
|
try:
|
||||||
ans = self.vformat(fmt, [], kwargs).strip()
|
ans = self.vformat(fmt, [], kwargs).strip()
|
||||||
except:
|
except:
|
||||||
|
if DEBUG:
|
||||||
|
traceback.print_exc()
|
||||||
ans = error_value
|
ans = error_value
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user