From eb0ebd4df04c58eac188db8a30f58d3c69ebc8f9 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Fri, 14 Jan 2011 10:50:13 +0000 Subject: [PATCH 1/2] Many small changes to the new formatter function stuff: 1) put isempty back. 2) use 'inspect' to get the source code for builtins so it can be displayed in the managemnet dialog box 3) change 'eval' to 'eval_' 4) do a better job of formatting exceptions 5) many changes to the help text 6) added a 'replace' box to the management dialog, so it is visually clear how to modify a function 7) got rid of a print statement --- .../gui2/preferences/template_functions.py | 71 ++++++++++----- .../gui2/preferences/template_functions.ui | 14 ++- src/calibre/utils/formatter.py | 9 +- src/calibre/utils/formatter_functions.py | 89 ++++++++++--------- 4 files changed, 113 insertions(+), 70 deletions(-) diff --git a/src/calibre/gui2/preferences/template_functions.py b/src/calibre/gui2/preferences/template_functions.py index efcf9e6379..2e16b0f4c3 100644 --- a/src/calibre/gui2/preferences/template_functions.py +++ b/src/calibre/gui2/preferences/template_functions.py @@ -25,37 +25,49 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): template function is written in python. It takes information from the book, processes it in some way, then returns a string result. Functions defined here are usable in templates in the same way that builtin - functions are usable. The function must be named evaluate, and must - have the signature shown below.

-

evaluate(self, formatter, kwargs, mi, locals, your_arguments) + functions are usable. The function must be named evaluate, and + must have the signature shown below.

+

evaluate(self, formatter, kwargs, mi, locals, your parameters) → returning a unicode string

-

The arguments to evaluate are: +

The parameters of the evaluate function are:

- The following example function looks for various values in the tags - metadata field, returning those values that appear in tags. + The following example function checks the value of the field. If the + field is not empty, the field's value is returned, otherwise the value + EMPTY is returned.

+        name: my_ifempty
+        arg count: 1
+        doc: my_ifempty(val) -- return val if it is not empty, otherwise the string 'EMPTY'
+        program code:
         def evaluate(self, formatter, kwargs, mi, locals, val):
-            awards=['allbooks', 'PBook', 'ggff']
-            return ', '.join([t for t in kwargs.get('tags') if t in awards])
-        
+ if val: + return val + else: + return 'EMPTY' + This function can be called in any of the three template program modes: +