View Single Post
Old 07-08-2022, 11:09 AM   #120
davidfor
Grand Sorcerer
davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.davidfor ought to be getting tired of karma fortunes by now.
 
Posts: 24,905
Karma: 47303824
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
Yes, the Python functions. I have had one working and copied between my libraries for a while. I was able to add the sample and it was listed. But, after restarting calibre, it disappeared. But, it was in the database and the original functions had disappeared.

For the library, I was looking at, the database had:

Code:
[
  [
    "rating_as_stars", 
    "rating_as_stars(x) -- returns x number of stars.", 
    1, 
    "def evaluate(self, formatter, kwargs, mi, locals, r):\n\ttry:\n\t\tx = int(r) * '\u2605'\n\texcept:\n\t\tx = ''\n\treturn x"
  ], 
  [
    "counts_for_comments", 
    "counts_for_comments(x) -- returns the word and page count formatted for the comment.\n\nThis will look like:\n<p><b>Word count:</b> nnnn <b>Page count:</b> nnnn</p>\n\nIf either count is missing, that part of the string will not be included. If neither exist, the result is empty.", 
    -1, 
    "def evaluate(self, formatter, kwargs, mi, locals):\n\twords = getattr(mi, '#words', None)\n\tpages = getattr(mi, '#pages', None)\n    counts_str = None\n    if words:\n\t    words_str = \"<b>Word count:</b> {0:,}\".format(words)\n        counts_str = words_str\n    if pages:\n\t    pages_str = \"<b>Page count:</b> {0:,}\".format(pages)\n        counts_str = counts_str + ' ' + pages_str if counts_str else pages_str\n    if counts_str:\n        counts_str = '<p>' + counts_str + '</p>' \n\treturn counts_str"
  ]
]
These have been there and working for some tie.

And now, after adding "my_ifempty" and restarting, I have:
Code:
[
  [
    "my_ifempty",
    "y_ifempty(val) -- return val if it is not empty, otherwise the string 'EMPTY'",
    1,
    "def evaluate(self, formatter, kwargs, mi, locals, val):\n            if val:\n                return val\n            else:\n                return 'EMPTY'"
  ]
]

When I added the tweak and restarted, the log showed:

Code:
[0.27] Initializing db...

from calibre.utils.formatter_functions import FormatterUserFunction
from calibre.utils.formatter_functions import formatter_functions
class UserFunction(FormatterUserFunction):
    def evaluate(self, formatter, kwargs, mi, locals, val):
                if val:
                    return val
                else:
                    return 'EMPTY'
[0.34] db initialized
]

And then after the plugins had finished initializing:

Code:
from calibre.utils.formatter_functions import FormatterUserFunction
from calibre.utils.formatter_functions import formatter_functions
class UserFunction(FormatterUserFunction):
    def evaluate(self, formatter, kwargs, mi, locals, val):
                if val:
                    return val
                else:
                    return 'EMPTY'
I always run from source with a few changes that I like. I did try it directly and the added function was not in the list.

I'll try the other things tomorrow.
davidfor is offline