New template function: field_exists()

This commit is contained in:
Charles Haley 2021-01-17 13:56:55 +00:00
parent b7c4fcd711
commit cfbff3d98f
2 changed files with 16 additions and 1 deletions

View File

@ -390,6 +390,7 @@ parameters can be statements (sequences of expressions). Note that the definitiv
`[[` for the `{` character and `]]` for the '}' character; they are converted automatically. Note also that prefixes and suffixes `[[` for the `{` character and `]]` for the '}' character; they are converted automatically. Note also that prefixes and suffixes
(the `|prefix|suffix` syntax) cannot be used in the argument to this function when using Template Program Mode. (the `|prefix|suffix` syntax) cannot be used in the argument to this function when using Template Program Mode.
* ``field(name)`` -- returns the metadata field named by ``name``. * ``field(name)`` -- returns the metadata field named by ``name``.
* ``field_exists(field_name)`` -- checks if a field (column) named ``field_name`` exists, returning '1' if so and '' if not.
* ``finish_formatting(val, fmt, prefix, suffix)`` -- apply the format, * ``finish_formatting(val, fmt, prefix, suffix)`` -- apply the format,
prefix, and suffix to a value in the same way as done in a template like prefix, and suffix to a value in the same way as done in a template like
``{series_index:05.2f| - |- }``. This function is provided to ease ``{series_index:05.2f| - |- }``. This function is provided to ease

View File

@ -1894,6 +1894,19 @@ class BuiltinGlobals(BuiltinFormatterFunction):
# The globals function is implemented in-line in the formatter # The globals function is implemented in-line in the formatter
raise NotImplementedError() raise NotImplementedError()
class BuiltinFieldExists(BuiltinFormatterFunction):
name = 'field_exists'
arg_count = 1
category = 'If-then-else'
__doc__ = doc = _('field_exists(field_name) -- checks if a field '
'(column) named field_name exists, returning '
"'1' if so and '' if not.")
def evaluate(self, formatter, kwargs, mi, locals, field_name):
if field_name.lower() in mi.all_field_keys():
return '1'
return ''
_formatter_builtins = [ _formatter_builtins = [
BuiltinAdd(), BuiltinAnd(), BuiltinApproximateFormats(), BuiltinArguments(), BuiltinAdd(), BuiltinAnd(), BuiltinApproximateFormats(), BuiltinArguments(),
@ -1903,7 +1916,8 @@ _formatter_builtins = [
BuiltinCmp(), BuiltinConnectedDeviceName(), BuiltinConnectedDeviceUUID(), BuiltinContains(), BuiltinCmp(), BuiltinConnectedDeviceName(), BuiltinConnectedDeviceUUID(), BuiltinContains(),
BuiltinCount(), BuiltinCurrentLibraryName(), BuiltinCurrentLibraryPath(), BuiltinCount(), BuiltinCurrentLibraryName(), BuiltinCurrentLibraryPath(),
BuiltinDaysBetween(), BuiltinDivide(), BuiltinEval(), BuiltinFirstNonEmpty(), BuiltinDaysBetween(), BuiltinDivide(), BuiltinEval(), BuiltinFirstNonEmpty(),
BuiltinField(), BuiltinFinishFormatting(), BuiltinFirstMatchingCmp(), BuiltinFloor(), BuiltinField(), BuiltinFieldExists(),
BuiltinFinishFormatting(), BuiltinFirstMatchingCmp(), BuiltinFloor(),
BuiltinFormatDate(), BuiltinFormatNumber(), BuiltinFormatsModtimes(), BuiltinFormatDate(), BuiltinFormatNumber(), BuiltinFormatsModtimes(),
BuiltinFormatsPaths(), BuiltinFormatsSizes(), BuiltinFractionalPart(), BuiltinFormatsPaths(), BuiltinFormatsSizes(), BuiltinFractionalPart(),
BuiltinGlobals(), BuiltinGlobals(),