View Single Post
Old 02-22-2024, 07:46 PM   #1
calibricious
Junior Member
calibricious began at the beginning.
 
Posts: 3
Karma: 10
Join Date: Feb 2024
Device: Kobo Aura One
Incorrect number of arguments for function

Hello everyone,

Let me preface this with: I have about 24 hours experience with Calibre templating language, so forgive me if I am missing something obvious. I have tried to search the forum and the internet, but found nothing.

The problem

I have written a function maketitle which takes in 6 arguments. For almost all books, it works perfectly, but for a few select cases I get the error
Code:
EXCEPTION: Incorrect number of arguments for function maketitle
Details

Code:
def evaluate(self, formatter, kwargs, mi, locals, title, author, series, series_index, category, pairing):
[...]
  • The function takes in 6 arguments: title, author, series, series_index, category, pairing
    • "category" and "pairing" are custom created columns (values only ever contain 1 word with [A-Za-z]*)
  • I wrote the function in Python, but am trying to use it for formatting book titles when I send them to my Kobo. So I guess I'm using it in basic template mode (?)

Debugging

Since it is a basic template, I am running it on the field title
Code:
{title:maketitle({author},{series},{series_index},{#category},{#pairing})}
I am getting the following error code in some instances:
Code:
EXCEPTION: Incorrect number of arguments for function maketitle
I saw a file named after this error on my Kobo, so I decided to create a column with the same exact template, to visualise where it was going wrong.

I have noticed 2 things:
  1. It fails on 3 books (out of over 100) in the column
    • What these books have in common is that their author field contains commas (to be precise, exactly 1 comma)
  2. It fails on other books on the Kobo
    • I am unsure of how many or which, because every new error overwrites the previously generated file with the error title
    • I am sure these are files OTHER than the aforementioned 3 books, because I noticed it when I tried to send all files except for the ones with errors

I have also tested removing and adding fields, to see how many arguments it was effectively taking in. Removing 1 argument solved the issue for the 3 books.

My conclusion was that, at least for the column in Calibre, the comma in the authors list is being interpreted not literally but as part of the code. Therefore it is separating "authors" into multiple arguments sent into the function.

What I am looking for

1. Fields as literals (escaping)?

My first instinct was that I needed to pass the field names as literals, so they don't get split into multiple arguments.

I've tried the syntax
Code:
{title:maketitle(`{author}`,{series},{series_index},{#category},{#pairing})}
Nothing really changed. I also tried escaping everything (except for title). Still the same error
Code:
Incorrect number of arguments for function maketitle

2. Alternative way to use my Python functions in templates

I don't know much about Calibre's templating system, so maybe there's an easier way to do this. I'm open to suggestions.

All I want is to have my makefile function do its job, which is to determine the subdirectory where the book will be saved and establish which data goes into the title and which doesn't. I also want this to work for saving books to disk/device.
calibricious is offline   Reply With Quote