mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 02:34:06 -04:00
Add tweaks to control which custom columns the content server displays. Various bug fixes. More template language documentation
This commit is contained in:
commit
db5ba4113f
@ -63,8 +63,9 @@ function render_book(book) {
|
||||
if (tags) {
|
||||
t = tags.split(':&:', 2);
|
||||
m = parseInt(t[0]);
|
||||
tall = t[1].split(',');
|
||||
t = t[1].split(',', m);
|
||||
if (t.length == m) t[m] = '...'
|
||||
if (tall.length > m) t[m] = '...'
|
||||
title += 'Tags=[{0}] '.format(t.join(','));
|
||||
}
|
||||
custcols = book.attr("custcols").split(',')
|
||||
|
@ -145,6 +145,24 @@ add_new_book_tags_when_importing_books = False
|
||||
# Set the maximum number of tags to show per book in the content server
|
||||
max_content_server_tags_shown=5
|
||||
|
||||
# Set custom metadata fields that the content server will or will not display.
|
||||
# content_server_will_display is a list of custom fields to be displayed.
|
||||
# content_server_wont_display is a list of custom fields not to be displayed.
|
||||
# wont_display has priority over will_display.
|
||||
# The special value '*' means all custom fields.
|
||||
# Defaults:
|
||||
# content_server_will_display = ['*']
|
||||
# content_server_wont_display = ['']
|
||||
# Examples:
|
||||
# To display only the custom fields #mytags and #genre:
|
||||
# content_server_will_display = ['#mytags', '#genre']
|
||||
# content_server_wont_display = ['']
|
||||
# To display all fields except #mycomments:
|
||||
# content_server_will_display = ['*']
|
||||
# content_server_wont_display['#mycomments']
|
||||
content_server_will_display = ['*']
|
||||
content_server_wont_display = ['']
|
||||
|
||||
|
||||
# Set the maximum number of sort 'levels' that calibre will use to resort the
|
||||
# library after certain operations such as searches or device insertion. Each
|
||||
|
@ -34,9 +34,10 @@ NULL_VALUES = {
|
||||
field_metadata = FieldMetadata()
|
||||
|
||||
class SafeFormat(TemplateFormatter):
|
||||
def get_value(self, key, args, mi):
|
||||
|
||||
def get_value(self, key, args, kwargs):
|
||||
try:
|
||||
ign, v = mi.format_field(key.lower(), series_with_index=False)
|
||||
ign, v = self.book.format_field(key.lower(), series_with_index=False)
|
||||
if v is None:
|
||||
return ''
|
||||
if v == '':
|
||||
@ -100,7 +101,9 @@ class Metadata(object):
|
||||
cf['#value#'] = 'RECURSIVE_COMPOSITE FIELD ' + field
|
||||
cf['#value#'] = composite_formatter.safe_format(
|
||||
cf['display']['composite_template'],
|
||||
self, _('TEMPLATE ERROR')).strip()
|
||||
self,
|
||||
_('TEMPLATE ERROR'),
|
||||
self).strip()
|
||||
return d['#value#']
|
||||
|
||||
raise AttributeError(
|
||||
|
@ -75,7 +75,8 @@ class JsonCodec(object):
|
||||
self.field_metadata = FieldMetadata()
|
||||
|
||||
def encode_to_file(self, file, booklist):
|
||||
json.dump(self.encode_booklist_metadata(booklist), file, indent=2, encoding='utf-8')
|
||||
file.write(json.dumps(self.encode_booklist_metadata(booklist),
|
||||
indent=2, encoding='utf-8'))
|
||||
|
||||
def encode_booklist_metadata(self, booklist):
|
||||
result = []
|
||||
|
@ -232,7 +232,7 @@ class AddAction(InterfaceAction):
|
||||
# metadata for this book to the device. This sets the uuid to the
|
||||
# correct value. Note that set_books_in_library might sync_booklists
|
||||
self.gui.set_books_in_library(booklists=[model.db], reset=True)
|
||||
model.reset()
|
||||
self.gui.refresh_ondevice()
|
||||
|
||||
def add_books_from_device(self, view):
|
||||
rows = view.selectionModel().selectedRows()
|
||||
|
@ -721,14 +721,16 @@ class DeviceMixin(object): # {{{
|
||||
self.device_manager.device.__class__.get_gui_name()+\
|
||||
_(' detected.'), 3000)
|
||||
self.device_connected = device_kind
|
||||
self.refresh_ondevice_info (device_connected = True, reset_only = True)
|
||||
self.library_view.set_device_connected(self.device_connected)
|
||||
self.refresh_ondevice (reset_only = True)
|
||||
else:
|
||||
self.device_connected = None
|
||||
self.status_bar.device_disconnected()
|
||||
if self.current_view() != self.library_view:
|
||||
self.book_details.reset_info()
|
||||
self.location_manager.update_devices()
|
||||
self.refresh_ondevice_info(device_connected=False)
|
||||
self.library_view.set_device_connected(self.device_connected)
|
||||
self.refresh_ondevice()
|
||||
|
||||
def info_read(self, job):
|
||||
'''
|
||||
@ -760,9 +762,9 @@ class DeviceMixin(object): # {{{
|
||||
self.card_b_view.set_editable(self.device_manager.device.CAN_SET_METADATA)
|
||||
self.sync_news()
|
||||
self.sync_catalogs()
|
||||
self.refresh_ondevice_info(device_connected = True)
|
||||
self.refresh_ondevice()
|
||||
|
||||
def refresh_ondevice_info(self, device_connected, reset_only = False):
|
||||
def refresh_ondevice(self, reset_only = False):
|
||||
'''
|
||||
Force the library view to refresh, taking into consideration new
|
||||
device books information
|
||||
@ -770,7 +772,7 @@ class DeviceMixin(object): # {{{
|
||||
self.book_on_device(None, reset=True)
|
||||
if reset_only:
|
||||
return
|
||||
self.library_view.set_device_connected(device_connected)
|
||||
self.library_view.model().refresh_ondevice()
|
||||
|
||||
# }}}
|
||||
|
||||
@ -803,7 +805,7 @@ class DeviceMixin(object): # {{{
|
||||
self.book_on_device(None, reset=True)
|
||||
# We need to reset the ondevice flags in the library. Use a big hammer,
|
||||
# so we don't need to worry about whether some succeeded or not.
|
||||
self.refresh_ondevice_info(device_connected=True, reset_only=False)
|
||||
self.refresh_ondevice(reset_only=False)
|
||||
|
||||
def dispatch_sync_event(self, dest, delete, specific):
|
||||
rows = self.library_view.selectionModel().selectedRows()
|
||||
@ -1300,7 +1302,7 @@ class DeviceMixin(object): # {{{
|
||||
if not self.set_books_in_library(self.booklists(), reset=True):
|
||||
self.upload_booklists()
|
||||
self.book_on_device(None, reset=True)
|
||||
self.refresh_ondevice_info(device_connected = True)
|
||||
self.refresh_ondevice()
|
||||
|
||||
view = self.card_a_view if on_card == 'carda' else \
|
||||
self.card_b_view if on_card == 'cardb' else self.memory_view
|
||||
|
@ -167,7 +167,7 @@ class MetadataBulkDialog(QDialog, Ui_MetadataBulkDialog):
|
||||
self.tag_editor_button.clicked.connect(self.tag_editor)
|
||||
self.autonumber_series.stateChanged[int].connect(self.auto_number_changed)
|
||||
|
||||
if len(db.custom_column_label_map) == 0:
|
||||
if len(db.custom_field_keys(include_composites=False)) == 0:
|
||||
self.central_widget.removeTab(1)
|
||||
else:
|
||||
self.create_custom_column_editors()
|
||||
|
@ -120,6 +120,9 @@ class BooksModel(QAbstractTableModel): # {{{
|
||||
|
||||
def set_device_connected(self, is_connected):
|
||||
self.device_connected = is_connected
|
||||
self.refresh_ondevice()
|
||||
|
||||
def refresh_ondevice(self):
|
||||
self.db.refresh_ondevice()
|
||||
self.refresh() # does a resort()
|
||||
self.research()
|
||||
@ -129,7 +132,7 @@ class BooksModel(QAbstractTableModel): # {{{
|
||||
|
||||
def set_database(self, db):
|
||||
self.db = db
|
||||
self.custom_columns = self.db.field_metadata.get_custom_field_metadata()
|
||||
self.custom_columns = self.db.field_metadata.custom_field_metadata()
|
||||
self.column_map = list(self.orig_headers.keys()) + \
|
||||
list(self.custom_columns)
|
||||
def col_idx(name):
|
||||
|
@ -21,7 +21,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
||||
def genesis(self, gui):
|
||||
self.gui = gui
|
||||
db = self.gui.library_view.model().db
|
||||
self.custcols = copy.deepcopy(db.field_metadata.get_custom_field_metadata())
|
||||
self.custcols = copy.deepcopy(db.field_metadata.custom_field_metadata())
|
||||
|
||||
self.column_up.clicked.connect(self.up_column)
|
||||
self.column_down.clicked.connect(self.down_column)
|
||||
|
@ -427,6 +427,8 @@ class CustomColumns(object):
|
||||
data = self.custom_column_label_map[label]
|
||||
if num is not None:
|
||||
data = self.custom_column_num_map[num]
|
||||
if data['datatype'] == 'composite':
|
||||
return None
|
||||
if not data['editable']:
|
||||
raise ValueError('Column %r is not editable'%data['label'])
|
||||
table, lt = self.custom_table_names(data['num'])
|
||||
|
@ -539,8 +539,8 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
||||
def standard_field_keys(self):
|
||||
return self.field_metadata.standard_field_keys()
|
||||
|
||||
def custom_field_keys(self):
|
||||
return self.field_metadata.custom_field_keys()
|
||||
def custom_field_keys(self, include_composites=True):
|
||||
return self.field_metadata.custom_field_keys(include_composites)
|
||||
|
||||
def all_field_keys(self):
|
||||
return self.field_metadata.all_field_keys()
|
||||
@ -554,6 +554,12 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
||||
def search_term_to_field_key(self, term):
|
||||
return self.field_metadata.search_term_to_key(term)
|
||||
|
||||
def custom_field_metadata(self, include_composites=True):
|
||||
return self.field_metadata.custom_field_metadata(include_composites)
|
||||
|
||||
def all_metadata(self):
|
||||
return self.field_metadata.all_metadata()
|
||||
|
||||
def metadata_for_field(self, key):
|
||||
return self.field_metadata[key]
|
||||
|
||||
@ -565,7 +571,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
||||
for book_id in book_ids:
|
||||
if not self.data.has_id(book_id):
|
||||
continue
|
||||
mi = self.get_metadata(book_id, index_is_id=True, get_cover=True)
|
||||
mi = self.get_metadata(book_id, index_is_id=True, get_cover=False)
|
||||
# Always set cover to cover.jpg. Even if cover doesn't exist,
|
||||
# no harm done. This way no need to call dirtied when
|
||||
# cover is set/removed
|
||||
|
@ -358,10 +358,14 @@ class FieldMetadata(dict):
|
||||
if self._tb_cats[k]['kind']=='field' and
|
||||
not self._tb_cats[k]['is_custom']]
|
||||
|
||||
def custom_field_keys(self):
|
||||
return [k for k in self._tb_cats.keys()
|
||||
if self._tb_cats[k]['kind']=='field' and
|
||||
self._tb_cats[k]['is_custom']]
|
||||
def custom_field_keys(self, include_composites=True):
|
||||
res = []
|
||||
for k in self._tb_cats.keys():
|
||||
fm = self._tb_cats[k]
|
||||
if fm['kind']=='field' and fm['is_custom'] and \
|
||||
(fm['datatype'] != 'composite' or include_composites):
|
||||
res.append(k)
|
||||
return res
|
||||
|
||||
def all_field_keys(self):
|
||||
return [k for k in self._tb_cats.keys() if self._tb_cats[k]['kind']=='field']
|
||||
@ -402,19 +406,15 @@ class FieldMetadata(dict):
|
||||
return self.custom_label_to_key_map[label]
|
||||
raise ValueError('Unknown key [%s]'%(label))
|
||||
|
||||
def get_custom_fields(self):
|
||||
return [l for l in self._tb_cats if self._tb_cats[l]['is_custom']]
|
||||
|
||||
def all_metadata(self):
|
||||
l = {}
|
||||
for k in self._tb_cats:
|
||||
l[k] = self._tb_cats[k]
|
||||
return l
|
||||
|
||||
def get_custom_field_metadata(self):
|
||||
def custom_field_metadata(self, include_composites=True):
|
||||
l = {}
|
||||
for k in self._tb_cats:
|
||||
if self._tb_cats[k]['is_custom']:
|
||||
for k in self.custom_field_keys(include_composites):
|
||||
l[k] = self._tb_cats[k]
|
||||
return l
|
||||
|
||||
|
@ -108,8 +108,12 @@ class SafeFormat(TemplateFormatter):
|
||||
'''
|
||||
def get_value(self, key, args, kwargs):
|
||||
try:
|
||||
if kwargs[key.lower()]:
|
||||
return kwargs[key.lower()]
|
||||
b = self.book.get_user_metadata(key, False)
|
||||
key = key.lower()
|
||||
if b is not None and b['datatype'] == 'composite':
|
||||
return self.vformat(b['display']['composite_template'], [], kwargs)
|
||||
if kwargs[key]:
|
||||
return self.sanitize(kwargs[key.lower()])
|
||||
return ''
|
||||
except:
|
||||
return ''
|
||||
@ -159,7 +163,8 @@ def get_components(template, mi, id, timefmt='%b %Y', length=250,
|
||||
elif custom_metadata[key]['datatype'] == 'bool':
|
||||
format_args[key] = _('yes') if format_args[key] else _('no')
|
||||
|
||||
components = safe_formatter.safe_format(template, format_args, '')
|
||||
components = safe_formatter.safe_format(template, format_args, '', mi,
|
||||
sanitize=sanitize_func)
|
||||
components = [x.strip() for x in components.split('/') if x.strip()]
|
||||
components = [sanitize_func(x) for x in components if x]
|
||||
if not components:
|
||||
|
@ -7,7 +7,7 @@ __docformat__ = 'restructuredtext en'
|
||||
|
||||
import os
|
||||
|
||||
from calibre.utils.config import Config, StringConfig, config_dir
|
||||
from calibre.utils.config import Config, StringConfig, config_dir, tweaks
|
||||
|
||||
|
||||
listen_on = '0.0.0.0'
|
||||
@ -46,6 +46,16 @@ def server_config(defaults=None):
|
||||
'to disable grouping.'))
|
||||
return c
|
||||
|
||||
def custom_fields_to_display(db):
|
||||
ckeys = db.custom_field_keys()
|
||||
yes_fields = set(tweaks['content_server_will_display'])
|
||||
no_fields = set(tweaks['content_server_wont_display'])
|
||||
if '*' in yes_fields:
|
||||
yes_fields = set(ckeys)
|
||||
if '*' in no_fields:
|
||||
no_fields = set(ckeys)
|
||||
return frozenset(yes_fields - no_fields)
|
||||
|
||||
def main():
|
||||
from calibre.library.server.main import main
|
||||
return main()
|
||||
|
@ -13,6 +13,7 @@ from lxml import html
|
||||
from lxml.html.builder import HTML, HEAD, TITLE, LINK, DIV, IMG, BODY, \
|
||||
OPTION, SELECT, INPUT, FORM, SPAN, TABLE, TR, TD, A, HR
|
||||
|
||||
from calibre.library.server import custom_fields_to_display
|
||||
from calibre.library.server.utils import strftime, format_tag_string
|
||||
from calibre.ebooks.metadata import fmt_sidx
|
||||
from calibre.constants import __appname__
|
||||
@ -197,7 +198,7 @@ class MobileServer(object):
|
||||
self.sort(items, sort, (order.lower().strip() == 'ascending'))
|
||||
|
||||
CFM = self.db.field_metadata
|
||||
CKEYS = [key for key in sorted(CFM.get_custom_fields(),
|
||||
CKEYS = [key for key in sorted(custom_fields_to_display(self.db),
|
||||
cmp=lambda x,y: cmp(CFM[x]['name'].lower(),
|
||||
CFM[y]['name'].lower()))]
|
||||
# This method uses its own book dict, not the Metadata dict. The loop
|
||||
|
@ -17,6 +17,7 @@ import routes
|
||||
from calibre.constants import __appname__
|
||||
from calibre.ebooks.metadata import fmt_sidx
|
||||
from calibre.library.comments import comments_to_html
|
||||
from calibre.library.server import custom_fields_to_display
|
||||
from calibre.library.server.utils import format_tag_string
|
||||
from calibre import guess_type
|
||||
from calibre.utils.ordered_dict import OrderedDict
|
||||
@ -277,7 +278,7 @@ class AcquisitionFeed(NavFeed):
|
||||
db):
|
||||
NavFeed.__init__(self, id_, updated, version, offsets, page_url, up_url)
|
||||
CFM = db.field_metadata
|
||||
CKEYS = [key for key in sorted(CFM.get_custom_fields(),
|
||||
CKEYS = [key for key in sorted(custom_fields_to_display(db),
|
||||
cmp=lambda x,y: cmp(CFM[x]['name'].lower(),
|
||||
CFM[y]['name'].lower()))]
|
||||
for item in items:
|
||||
|
@ -11,6 +11,7 @@ import cherrypy
|
||||
from lxml.builder import ElementMaker
|
||||
from lxml import etree
|
||||
|
||||
from calibre.library.server import custom_fields_to_display
|
||||
from calibre.library.server.utils import strftime, format_tag_string
|
||||
from calibre.ebooks.metadata import fmt_sidx
|
||||
from calibre.constants import preferred_encoding
|
||||
@ -94,7 +95,7 @@ class XMLServer(object):
|
||||
c = kwargs.pop('comments')
|
||||
|
||||
CFM = self.db.field_metadata
|
||||
CKEYS = [key for key in sorted(CFM.get_custom_fields(),
|
||||
CKEYS = [key for key in sorted(custom_fields_to_display(self.db),
|
||||
cmp=lambda x,y: cmp(CFM[x]['name'].lower(),
|
||||
CFM[y]['name'].lower()))]
|
||||
custcols = []
|
||||
|
@ -7,9 +7,9 @@ The |app| template language
|
||||
=======================================================
|
||||
|
||||
The |app| template language is used in various places. It is used to control the folder structure and file name when saving files from the |app| library to the disk or eBook reader.
|
||||
It is used to define "virtual" columns that contain data from other columns and so on.
|
||||
It is also used to define "virtual" columns that contain data from other columns and so on.
|
||||
|
||||
In essence, the template language is very simple. The basic idea is that a template consists of names in curly brackets that are then replaced by the corresponding metadata from the book being processed. So, for example, the default template used for saving books to device in |app| is::
|
||||
The basic template language is very simple, but has very powerful advanced features. The basic idea is that a template consists of names in curly brackets that are then replaced by the corresponding metadata from the book being processed. So, for example, the default template used for saving books to device in |app| is::
|
||||
|
||||
{author_sort}/{title}/{title} - {authors}
|
||||
|
||||
@ -17,7 +17,9 @@ For the book "The Foundation" by "Isaac Asimov" it will become::
|
||||
|
||||
Asimov, Isaac/The Foundation/The Foundation - Isaac Asimov
|
||||
|
||||
You can use all the various metadata fields available in calibre in a template, including the custom columns you have created yourself. To find out the template name for a column sinply hover your mouse over the column header. Names for custom fields (columns you have created yourself) are always prefixed by an #. For series type fields, there is always an additional field named ``series_index`` that becomes the series index for that series. So if you have a custom series field named #myseries, there will also be a field named #myseries_index. In addition to the column based fields, you also can use::
|
||||
You can use all the various metadata fields available in calibre in a template, including any custom columns you have created yourself. To find out the template name for a column simply hover your mouse over the column header. Names for custom fields (columns you have created yourself) always have a # as the first character. For series type custom fields, there is always an additional field named ``#seriesname_index`` that becomes the series index for that series. So if you have a custom series field named #myseries, there will also be a field named #myseries_index.
|
||||
|
||||
In addition to the column based fields, you also can use::
|
||||
|
||||
{formats} - A list of formats available in the calibre library for a book
|
||||
{isbn} - The ISBN number of the book
|
||||
@ -26,7 +28,7 @@ If a particular book does not have a particular piece of metadata, the field in
|
||||
|
||||
{author_sort}/{series}/{title} {series_index}
|
||||
|
||||
will become::
|
||||
If a book has a series, the template will produce::
|
||||
|
||||
{Asimov, Isaac}/Foundation/Second Foundation - 3
|
||||
|
||||
@ -42,33 +44,90 @@ Advanced formatting
|
||||
|
||||
You can do more than just simple substitution with the templates. You can also conditionally include text and control how the substituted data is formatted.
|
||||
|
||||
Regarding conditionally including text: there are cases where you might want to have text appear in the output only if a field is not empty. A common case is series and series_index, where you want either nothing or the two values with a hyphen between them. Calibre handles this case using a special field syntax.
|
||||
For example, assume you want to use the template
|
||||
First, conditionally including text. There are cases where you might want to have text appear in the output only if a field is not empty. A common case is series and series_index, where you want either nothing or the two values with a hyphen between them. Calibre handles this case using a special field syntax.
|
||||
|
||||
For example, assume you want to use the template::
|
||||
|
||||
{series} - {series_index} - {title}
|
||||
|
||||
Unfortunately, if the book has no series, the answer will be '- - title'. Many people would rather it be simply 'title', without the hyphens. To do this, use the extended syntax {some_text|field|other_text}. When you use this syntax, if field has the value SERIES then the result will be some_textSERIESother_text. If field has no value, then the result will be the empty string (nothing). Using this syntax, we can solve the above series problem with the template::
|
||||
If the book has no series, the answer will be '- - title'. Many people would rather the result be simply 'title', without the hyphens. To do this, use the extended syntax ``{field:|prefix_text|suffix_text}``. When you use this syntax, if field has the value SERIES then the result will be prefix_textSERIESsuffix_text. If field has no value, then the result will be the empty string (nothing). The prefix and suffix can contain blanks.
|
||||
|
||||
{series}{ - |series_index| - }{title}
|
||||
Using this syntax, we can solve the above series problem with the template::
|
||||
|
||||
The hyphens will be included only if the book has a series index. Note: you must either use no | characters or both of them. Using one, such as in {field| - }, is not allowed. It is OK to not provide any text for one side or the other, such as in {\|series\| - }. Using {\|title\|} is the same as using {title}.
|
||||
{series}{series_index:| - | - }{title}
|
||||
|
||||
Now to formatting. Suppose you wanted to ensure that the series_index is always formatted as three digits with leading zeros. This would do the trick::
|
||||
The hyphens will be included only if the book has a series index.
|
||||
|
||||
Notes: you must include the : character if you want to use a prefix or a suffix. You must either use no \| characters or both of them; using one, as in ``{field:| - }``, is not allowed. It is OK not to provide any text for one side or the other, such as in ``{series:|| - }``. Using ``{title:||}`` is the same as using ``{title}``.
|
||||
|
||||
Second: formatting. Suppose you wanted to ensure that the series_index is always formatted as three digits with leading zeros. This would do the trick::
|
||||
|
||||
{series_index:0>3s} - Three digits with leading zeros
|
||||
|
||||
If instead of leading zeros you want leading spaces, use::
|
||||
|
||||
{series_index:>3s} - Thre digits with leading spaces
|
||||
{series_index:>3s} - Three digits with leading spaces
|
||||
|
||||
For trailing zeros, use::
|
||||
|
||||
{series_index:0<3s} - Three digits with trailing zeros
|
||||
|
||||
|
||||
If you want only the first two letters of the data to be rendered, use::
|
||||
If you want only the first two letters of the data, use::
|
||||
|
||||
{author_sort:.2} - Only the first two letter of the author sort name
|
||||
|
||||
The |app| template language comes from python and for more details on the syntax of these advanced formatting operations, look at the `Python documentation <http://docs.python.org/library/string.html#format-string-syntax>`_.
|
||||
|
||||
Advanced features
|
||||
------------------
|
||||
|
||||
Using templates in custom columns
|
||||
----------------------------------
|
||||
|
||||
There are sometimes cases where you want to display metadata that |app| does not normally display, or to display data in a way different from how |app| normally does. For example, you might want to display the ISBN, a field that |app| does not display. You can use custom columns for this. To do so, you create a column with the type 'column built from other columns' (hereafter called composite columns), enter a template, and |app| will display in the column the result of evaluating that template. To display the isbn, create the column and enter ``{isbn}`` into the template box. To display a column containing the values of two series custom columns separated by a comma, use ``{#series1:||,}{#series2}``.
|
||||
|
||||
Composite columns can use any template option, including formatting.
|
||||
|
||||
You cannot change the data contained in a composite column. If you edit a composite column by double-clicking on any item, you will open the template for editing, not the underlying data. Editing the template on the GUI is a quick way of testing and changing composite columns.
|
||||
|
||||
Using functions in templates
|
||||
-----------------------------
|
||||
|
||||
Suppose you want to display the value of a field in upper case, when that field is normally in title case. You can do this (and many more things) using the functions available for templates. For example, to display the title in upper case, use ``{title:uppercase()}``. To display it in title case, use ``{title:titlecase()}``.
|
||||
|
||||
Function references replace the formatting specification, going after the : and before the first ``|`` or the closing ``}``. Functions must always end with ``()``. Some functions take extra values (arguments), and these go inside the ``()``.
|
||||
|
||||
The syntax for using functions is ``{field:function(arguments)}``, or ``{field:function(arguments)|prefix|suffix}``. Argument values cannot contain a comma, because it is used to separate arguments. Functions return the value of the field used in the template, suitably modified.
|
||||
|
||||
The functions available are:
|
||||
|
||||
* ``lowercase()`` -- return value of the field in lower case.
|
||||
* ``uppercase()`` -- return the value of the field in upper case.
|
||||
* ``titlecase()`` -- return the value of the field in title case.
|
||||
* ``capitalize()`` -- return the value as capitalized.
|
||||
* ``ifempty(text)`` -- if the field is not empty, return the value of the field. Otherwise return `text`.
|
||||
* ``test(text if not empty, text if empty)`` -- return `text if not empty` if the field is not empty, otherwise return `text if empty`.
|
||||
* ``shorten(left chars, middle text, right chars)`` -- Return a shortened version of the field, consisting of `left chars` characters from the beginning of the field, followed by `middle text`, followed by `right chars` characters from the end of the string. `Left chars` and `right chars` must be integers. For example, assume the title of the book is `Ancient English Laws in the Times of Ivanhoe`, and you want it to fit in a space of at most 15 characters. If you use ``{title:shorten(9,-,5)}``, the result will be `Ancient E-nhoe`. If the field's length is less than ``left chars`` + ``right chars`` + the length of ``middle text``, then the field will be used intact. For example, the title `The Dome` would not be changed.
|
||||
* ``lookup(field if not empty, field if empty)`` -- like test, except the arguments are field (metadata) names, not text. The value of the appropriate field will be fetched and used. Note that because composite columns are fields, you can use this function in one composite field to use the value of some other composite field. This is extremely useful when constructing variable save paths (more later).
|
||||
* ``re(pattern, replacement)`` -- return the field after applying the regular expression. All instances of `pattern` are replaced with `replacement`. As in all of |app|, these are python-compatible regular expressions.
|
||||
|
||||
Special notes for save/send templates
|
||||
-------------------------------------
|
||||
|
||||
Special processing is applied when a template is used in a `save to disk` or `send to device` template. The values of the fields are cleaned, replacing characters that are special to file systems with underscores, including slashes. This means that field text cannot be used to create folders. However, slashes are not changed in prefix or suffix strings, so slashes in these strings will cause folders to be created. Because of this, you can create variable-depth folder structure.
|
||||
|
||||
For example, assume we want the folder structure `series/series_index - title`, with the caveat that if series does not exist, then the title should be in the top folder. The template to do this is::
|
||||
|
||||
{series:||/}{series_index:|| - }{title}
|
||||
|
||||
The slash and the hyphen appear only if series is not empty.
|
||||
|
||||
The lookup function lets us do even fancier processing. For example, assume we want the following: if a book has a series, then we want the folder structure `series/series index - title.fmt`. If the book does not have a series, then we want the folder structure `genre/author_sort/title.fmt`. If the book has no genre, use 'Unknown'. We want two completely different paths, depending on the value of series.
|
||||
|
||||
To accomplish this, we:
|
||||
1. Create a composite field (call it AA) containing ``{series:||}/{series_index} - {title'}``. If the series is not empty, then this template will produce `series/series_index - title`.
|
||||
2. Create a composite field (call it BB) containing ``{#genre:ifempty(Unknown)}/{author_sort}/{title}``. This template produces `genre/author_sort/title`, where an empty genre is replaced wuth `Unknown`.
|
||||
3. Set the save template to ``{series:lookup(AA,BB)}``. This template chooses composite field AA if series is not empty, and composite field BB if series is empty. We therefore have two completely different save paths, depending on whether or not `series` is empty.
|
||||
|
||||
|
||||
|
@ -6,25 +6,36 @@ Created on 23 Sep 2010
|
||||
|
||||
import re, string
|
||||
|
||||
def _lookup(val, mi, field_if_set, field_not_set):
|
||||
if hasattr(mi, 'format_field'):
|
||||
if val:
|
||||
return mi.format_field(field_if_set.strip())[1]
|
||||
else:
|
||||
return mi.format_field(field_not_set.strip())[1]
|
||||
else:
|
||||
if val:
|
||||
return mi.get(field_if_set.strip(), '')
|
||||
else:
|
||||
return mi.get(field_not_set.strip(), '')
|
||||
class TemplateFormatter(string.Formatter):
|
||||
'''
|
||||
Provides a format function that substitutes '' for any missing value
|
||||
'''
|
||||
|
||||
def _ifempty(val, mi, value_if_empty):
|
||||
def __init__(self):
|
||||
string.Formatter.__init__(self)
|
||||
self.book = None
|
||||
self.kwargs = None
|
||||
self.sanitize = None
|
||||
|
||||
def _lookup(self, val, field_if_set, field_not_set):
|
||||
if val:
|
||||
return self.vformat('{'+field_if_set.strip()+'}', [], self.kwargs)
|
||||
else:
|
||||
return self.vformat('{'+field_not_set.strip()+'}', [], self.kwargs)
|
||||
|
||||
def _test(self, val, value_if_set, value_not_set):
|
||||
if val:
|
||||
return value_if_set
|
||||
else:
|
||||
return value_not_set
|
||||
|
||||
def _ifempty(self, val, value_if_empty):
|
||||
if val:
|
||||
return val
|
||||
else:
|
||||
return value_if_empty
|
||||
|
||||
def _shorten(val, mi, leading, center_string, trailing):
|
||||
def _shorten(self, val, leading, center_string, trailing):
|
||||
l = int(leading)
|
||||
t = int(trailing)
|
||||
if len(val) > l + len(center_string) + t:
|
||||
@ -32,25 +43,25 @@ def _shorten(val, mi, leading, center_string, trailing):
|
||||
else:
|
||||
return val
|
||||
|
||||
class TemplateFormatter(string.Formatter):
|
||||
'''
|
||||
Provides a format function that substitutes '' for any missing value
|
||||
'''
|
||||
def _re(self, val, pattern, replacement):
|
||||
return re.sub(pattern, replacement, val)
|
||||
|
||||
functions = {
|
||||
'uppercase' : (0, lambda x: x.upper()),
|
||||
'lowercase' : (0, lambda x: x.lower()),
|
||||
'titlecase' : (0, lambda x: x.title()),
|
||||
'capitalize' : (0, lambda x: x.capitalize()),
|
||||
'uppercase' : (0, lambda s,x: x.upper()),
|
||||
'lowercase' : (0, lambda s,x: x.lower()),
|
||||
'titlecase' : (0, lambda s,x: x.title()),
|
||||
'capitalize' : (0, lambda s,x: x.capitalize()),
|
||||
'ifempty' : (1, _ifempty),
|
||||
'lookup' : (2, _lookup),
|
||||
're' : (2, _re),
|
||||
'shorten' : (3, _shorten),
|
||||
'test' : (2, _test),
|
||||
}
|
||||
|
||||
format_string_re = re.compile(r'^(.*)\|(.*)\|(.*)$')
|
||||
compress_spaces = re.compile(r'\s+')
|
||||
|
||||
def get_value(self, key, args, mi):
|
||||
def get_value(self, key, args, kwargs):
|
||||
raise Exception('get_value must be implemented in the subclass')
|
||||
|
||||
|
||||
@ -79,9 +90,9 @@ class TemplateFormatter(string.Formatter):
|
||||
(func[0] > 0 and func[0] != len(args)):
|
||||
raise ValueError('Incorrect number of arguments for function '+ fmt[0:p])
|
||||
if func[0] == 0:
|
||||
val = func[1](val, self.mi)
|
||||
val = func[1](self, val)
|
||||
else:
|
||||
val = func[1](val, self.mi, *args)
|
||||
val = func[1](self, val, *args)
|
||||
else:
|
||||
val = string.Formatter.format_field(self, val, fmt)
|
||||
if not val:
|
||||
@ -89,11 +100,13 @@ class TemplateFormatter(string.Formatter):
|
||||
return prefix + val + suffix
|
||||
|
||||
def vformat(self, fmt, args, kwargs):
|
||||
self.mi = kwargs
|
||||
ans = string.Formatter.vformat(self, fmt, args, kwargs)
|
||||
return self.compress_spaces.sub(' ', ans).strip()
|
||||
|
||||
def safe_format(self, fmt, kwargs, error_value):
|
||||
def safe_format(self, fmt, kwargs, error_value, book, sanitize=None):
|
||||
self.kwargs = kwargs
|
||||
self.book = book
|
||||
self.sanitize = sanitize
|
||||
try:
|
||||
ans = self.vformat(fmt, [], kwargs).strip()
|
||||
except:
|
||||
|
Loading…
x
Reference in New Issue
Block a user