mirror of
https://github.com/kovidgoyal/calibre.git
synced 2026-03-10 03:43:40 -04:00
DRYer
This commit is contained in:
parent
6c5a8762ee
commit
4007463a19
@ -74,6 +74,7 @@ def implementation(
|
||||
data[field] = {k: v.get('isbn') or '' for k, v in x.items()}
|
||||
continue
|
||||
if field == 'template':
|
||||
from calibre.utils.formatter import TEMPLATE_ERROR
|
||||
if not template:
|
||||
data['template'] = _('Template not allowed') if is_remote else _('No template specified')
|
||||
continue
|
||||
@ -84,7 +85,7 @@ def implementation(
|
||||
formatter = SafeFormat()
|
||||
for book_id in book_ids:
|
||||
mi = db.get_proxy_metadata(book_id)
|
||||
vals[book_id] = formatter.safe_format(template, {}, 'TEMPLATE ERROR', mi, global_vars=global_vars)
|
||||
vals[book_id] = formatter.safe_format(template, {}, TEMPLATE_ERROR, mi, global_vars=global_vars)
|
||||
data['template'] = vals
|
||||
continue
|
||||
field = field.replace('*', '#')
|
||||
|
||||
@ -17,6 +17,7 @@ from calibre.db.write import Writer
|
||||
from calibre.ebooks.metadata import author_to_author_sort, rating_to_stars, title_sort
|
||||
from calibre.utils.config_base import tweaks
|
||||
from calibre.utils.date import UNDEFINED_DATE, clean_date_for_sort, parse_date
|
||||
from calibre.utils.formatter import TEMPLATE_ERROR
|
||||
from calibre.utils.icu import sort_key
|
||||
from calibre.utils.localization import calibre_langcode_to_name
|
||||
|
||||
@ -299,7 +300,7 @@ class CompositeField(OneToOneField):
|
||||
' INTERNAL USE ONLY. DO NOT USE THIS OUTSIDE THIS CLASS! '
|
||||
db = self.db_weakref()
|
||||
ans = formatter.safe_format(
|
||||
self.metadata['display']['composite_template'], mi, _('TEMPLATE ERROR'),
|
||||
self.metadata['display']['composite_template'], mi, TEMPLATE_ERROR,
|
||||
mi, column_name=self._composite_name, template_cache=template_cache,
|
||||
template_functions=self.get_template_functions(),
|
||||
global_vars={rendering_composite_name:'1'}, database=db).strip()
|
||||
|
||||
@ -147,11 +147,12 @@ class Metadata:
|
||||
d = _data['user_metadata'][field]
|
||||
val = d['#value#']
|
||||
if val is None and d['datatype'] == 'composite':
|
||||
from calibre.utils.formatter import TEMPLATE_ERROR
|
||||
d['#value#'] = 'RECURSIVE_COMPOSITE FIELD (Metadata) ' + field
|
||||
val = d['#value#'] = self.formatter.safe_format(
|
||||
d['display']['composite_template'],
|
||||
self,
|
||||
_('TEMPLATE ERROR'),
|
||||
TEMPLATE_ERROR,
|
||||
self, column_name=field,
|
||||
template_cache=self.template_cache).strip()
|
||||
return val
|
||||
|
||||
@ -125,9 +125,10 @@ class DatabaseImages(pictureflow.FlowImages):
|
||||
return self.model.count()
|
||||
|
||||
def render_template(self, template, index, db):
|
||||
from calibre.utils.formatter import TEMPLATE_ERROR
|
||||
book_id = self.model.id(index)
|
||||
mi = db.get_proxy_metadata(book_id)
|
||||
return mi.formatter.safe_format(template, mi, _('TEMPLATE ERROR'), mi, template_cache=self.template_cache)
|
||||
return mi.formatter.safe_format(template, mi, TEMPLATE_ERROR, mi, template_cache=self.template_cache)
|
||||
|
||||
def caption(self, index):
|
||||
if self.ignore_image_requests:
|
||||
|
||||
@ -73,6 +73,7 @@ from calibre.gui2.library.caches import CoverThumbnailCache, Thumbnailer
|
||||
from calibre.gui2.library.models import BooksModel
|
||||
from calibre.gui2.momentum_scroll import MomentumScrollMixin
|
||||
from calibre.gui2.palette import dark_palette, light_palette
|
||||
from calibre.utils.formatter import TEMPLATE_ERROR
|
||||
from calibre.utils.icu import numeric_sort_key
|
||||
from calibre.utils.img import resize_to_fit
|
||||
from calibre.utils.iso8601 import UNDEFINED_DATE
|
||||
@ -84,7 +85,6 @@ from calibre_extensions.progress_indicator import contrast_ratio
|
||||
|
||||
|
||||
# Utility functions {{{
|
||||
TEMPLATE_ERROR = _('TEMPLATE ERROR')
|
||||
|
||||
|
||||
def random_from_id(book_id: int, limit: int = 21) -> int:
|
||||
|
||||
@ -24,6 +24,8 @@ from calibre.utils.icu import strcmp
|
||||
from calibre.utils.localization import _
|
||||
from polyglot.builtins import error_message
|
||||
|
||||
TEMPLATE_ERROR = _('TEMPLATE ERROR')
|
||||
|
||||
|
||||
def default_template_error_reporter(e: Exception, fmt, kwargs, book, column_name: str | None):
|
||||
if DEBUG:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user