mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Use ProxyMetadata in formatter functions for virt libs, ondevice, book_size, and approximate_formats. This saves the assignment in get_metadata()
This commit is contained in:
parent
6a488a3320
commit
8467baea33
@ -214,8 +214,8 @@ class Cache(object):
|
|||||||
default_value='dummy')
|
default_value='dummy')
|
||||||
mi.title_sort = self._field_for('sort', book_id,
|
mi.title_sort = self._field_for('sort', book_id,
|
||||||
default_value=_('Unknown'))
|
default_value=_('Unknown'))
|
||||||
mi.book_size = self._field_for('size', book_id, default_value=0)
|
# mi.book_size = self._field_for('size', book_id, default_value=0)
|
||||||
mi.ondevice_col = self._field_for('ondevice', book_id, default_value='')
|
# mi.ondevice_col = self._field_for('ondevice', book_id, default_value='')
|
||||||
mi.last_modified = self._field_for('last_modified', book_id,
|
mi.last_modified = self._field_for('last_modified', book_id,
|
||||||
default_value=n)
|
default_value=n)
|
||||||
formats = self._field_for('formats', book_id)
|
formats = self._field_for('formats', book_id)
|
||||||
@ -227,7 +227,7 @@ class Cache(object):
|
|||||||
mi.format_metadata = FormatMetadata(self, book_id, formats)
|
mi.format_metadata = FormatMetadata(self, book_id, formats)
|
||||||
good_formats = FormatsList(formats, mi.format_metadata)
|
good_formats = FormatsList(formats, mi.format_metadata)
|
||||||
mi.formats = good_formats
|
mi.formats = good_formats
|
||||||
mi.db_approx_formats = formats
|
# mi.db_approx_formats = formats
|
||||||
mi.has_cover = _('Yes') if self._field_for('cover', book_id,
|
mi.has_cover = _('Yes') if self._field_for('cover', book_id,
|
||||||
default_value=False) else ''
|
default_value=False) else ''
|
||||||
mi.tags = list(self._field_for('tags', book_id, default_value=()))
|
mi.tags = list(self._field_for('tags', book_id, default_value=()))
|
||||||
@ -1413,6 +1413,7 @@ class Cache(object):
|
|||||||
def refresh_ondevice(self):
|
def refresh_ondevice(self):
|
||||||
self.fields['ondevice'].clear_caches()
|
self.fields['ondevice'].clear_caches()
|
||||||
self.clear_search_caches()
|
self.clear_search_caches()
|
||||||
|
self.clear_composite_caches()
|
||||||
|
|
||||||
@read_api
|
@read_api
|
||||||
def tags_older_than(self, tag, delta=None, must_have_tag=None, must_have_authors=None):
|
def tags_older_than(self, tag, delta=None, must_have_tag=None, must_have_authors=None):
|
||||||
|
@ -355,4 +355,6 @@ class ProxyMetadata(Metadata):
|
|||||||
um = ga(self, '_user_metadata')
|
um = ga(self, '_user_metadata')
|
||||||
return frozenset(ALL_METADATA_FIELDS.union(um.iterkeys()))
|
return frozenset(ALL_METADATA_FIELDS.union(um.iterkeys()))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _proxy_metadata(self):
|
||||||
|
return self
|
||||||
|
@ -453,7 +453,7 @@ class BooksModel(QAbstractTableModel): # {{{
|
|||||||
|
|
||||||
def get_book_display_info(self, idx):
|
def get_book_display_info(self, idx):
|
||||||
mi = self.db.get_metadata(idx)
|
mi = self.db.get_metadata(idx)
|
||||||
mi.size = mi.book_size
|
mi.size = mi._proxy_metadata.book_size
|
||||||
mi.cover_data = ('jpg', self.cover(idx))
|
mi.cover_data = ('jpg', self.cover(idx))
|
||||||
mi.id = self.db.id(idx)
|
mi.id = self.db.id(idx)
|
||||||
mi.field_metadata = self.db.field_metadata
|
mi.field_metadata = self.db.field_metadata
|
||||||
|
@ -640,15 +640,22 @@ class BuiltinApproximateFormats(BuiltinFormatterFunction):
|
|||||||
'although it probably is. '
|
'although it probably is. '
|
||||||
'This function can be called in template program mode using '
|
'This function can be called in template program mode using '
|
||||||
'the template "{:\'approximate_formats()\'}". '
|
'the template "{:\'approximate_formats()\'}". '
|
||||||
'Note that format names are always uppercase, as in EPUB.'
|
'Note that format names are always uppercase, as in EPUB. '
|
||||||
|
'This function works only in the GUI. If you want to use these values '
|
||||||
|
'in save-to-disk or send-to-device templates then you '
|
||||||
|
'must make a custom "Column built from other columns", use '
|
||||||
|
'the function in that column\'s template, and use that '
|
||||||
|
'column\'s value in your save/send templates'
|
||||||
)
|
)
|
||||||
|
|
||||||
def evaluate(self, formatter, kwargs, mi, locals):
|
def evaluate(self, formatter, kwargs, mi, locals):
|
||||||
fmt_data = mi.get('db_approx_formats', [])
|
if hasattr(mi, '_proxy_metadata'):
|
||||||
if not fmt_data:
|
fmt_data = mi._proxy_metadata.db_approx_formats
|
||||||
return ''
|
if not fmt_data:
|
||||||
data = sorted(fmt_data)
|
return ''
|
||||||
return ','.join(v.upper() for v in data)
|
data = sorted(fmt_data)
|
||||||
|
return ','.join(v.upper() for v in data)
|
||||||
|
return _('This function can be used only in the GUI')
|
||||||
|
|
||||||
class BuiltinFormatsModtimes(BuiltinFormatterFunction):
|
class BuiltinFormatsModtimes(BuiltinFormatterFunction):
|
||||||
name = 'formats_modtimes'
|
name = 'formats_modtimes'
|
||||||
@ -902,27 +909,42 @@ class BuiltinBooksize(BuiltinFormatterFunction):
|
|||||||
name = 'booksize'
|
name = 'booksize'
|
||||||
arg_count = 0
|
arg_count = 0
|
||||||
category = 'Get values from metadata'
|
category = 'Get values from metadata'
|
||||||
__doc__ = doc = _('booksize() -- return value of the size field')
|
__doc__ = doc = _('booksize() -- return value of the size field. '
|
||||||
|
'This function works only in the GUI. If you want to use this value '
|
||||||
|
'in save-to-disk or send-to-device templates then you '
|
||||||
|
'must make a custom "Column built from other columns", use '
|
||||||
|
'the function in that column\'s template, and use that '
|
||||||
|
'column\'s value in your save/send templates')
|
||||||
|
|
||||||
def evaluate(self, formatter, kwargs, mi, locals):
|
def evaluate(self, formatter, kwargs, mi, locals):
|
||||||
if mi.book_size is not None:
|
if hasattr(mi, '_proxy_metadata'):
|
||||||
try:
|
try:
|
||||||
return str(mi.book_size)
|
v = mi._proxy_metadata.book_size
|
||||||
|
if v is not None:
|
||||||
|
return str(mi._proxy_metadata.book_size)
|
||||||
|
return ''
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
return ''
|
return ''
|
||||||
|
return _('This function can be used only in the GUI')
|
||||||
|
|
||||||
class BuiltinOndevice(BuiltinFormatterFunction):
|
class BuiltinOndevice(BuiltinFormatterFunction):
|
||||||
name = 'ondevice'
|
name = 'ondevice'
|
||||||
arg_count = 0
|
arg_count = 0
|
||||||
category = 'Get values from metadata'
|
category = 'Get values from metadata'
|
||||||
__doc__ = doc = _('ondevice() -- return Yes if ondevice is set, otherwise return '
|
__doc__ = doc = _('ondevice() -- return Yes if ondevice is set, otherwise return '
|
||||||
'the empty string')
|
'the empty string. This function works only in the GUI. If you want to '
|
||||||
|
'use this value in save-to-disk or send-to-device templates then you '
|
||||||
|
'must make a custom "Column built from other columns", use '
|
||||||
|
'the function in that column\'s template, and use that '
|
||||||
|
'column\'s value in your save/send templates')
|
||||||
|
|
||||||
def evaluate(self, formatter, kwargs, mi, locals):
|
def evaluate(self, formatter, kwargs, mi, locals):
|
||||||
if mi.ondevice_col:
|
if hasattr(mi, '_proxy_metadata'):
|
||||||
return _('Yes')
|
if mi._proxy_metadata.ondevice_col:
|
||||||
return ''
|
return _('Yes')
|
||||||
|
return ''
|
||||||
|
return _('This function can be used only in the GUI')
|
||||||
|
|
||||||
class BuiltinSeriesSort(BuiltinFormatterFunction):
|
class BuiltinSeriesSort(BuiltinFormatterFunction):
|
||||||
name = 'series_sort'
|
name = 'series_sort'
|
||||||
@ -1259,9 +1281,6 @@ class BuiltinVirtualLibraries(BuiltinFormatterFunction):
|
|||||||
'column\'s value in your save/send templates')
|
'column\'s value in your save/send templates')
|
||||||
|
|
||||||
def evaluate(self, formatter, kwargs, mi, locals_):
|
def evaluate(self, formatter, kwargs, mi, locals_):
|
||||||
from calibre.db.lazy import ProxyMetadata
|
|
||||||
if isinstance(mi, ProxyMetadata):
|
|
||||||
return mi.virtual_libraries
|
|
||||||
if hasattr(mi, '_proxy_metadata'):
|
if hasattr(mi, '_proxy_metadata'):
|
||||||
return mi._proxy_metadata.virtual_libraries
|
return mi._proxy_metadata.virtual_libraries
|
||||||
return _('This function can be used only in the GUI')
|
return _('This function can be used only in the GUI')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user