Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 12-21-2011, 02:56 AM   #1
Ruskie_it
Fanatic
Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.
 
Posts: 538
Karma: 1000000
Join Date: Dec 2011
Location: Rome, Italy
Device: Kindle PW5, Kindle PW4, Kindle 4 NT
Making references to other fields in template functions

Hello everybody, this is my first attempt at dealing with python and with calibre programming / customization so apologies if the terminology is not correct.

I'm messing with the templates calibre uses while exporting books to devices. Currently my template is the following:

{series}/{author_sort}/{series_index:0>2s|| - }{title} - {authors}

Correct me if I am wrong, with this template if the book isn't part of a series ({series} is null) calibre creates a directory named after the book author, and in that folder it puts the book. If the book _is_ part of a series calibre creates a folder with the name of the series, a subfolder named after the author and in there it puts the book. Correct?

Now, what I would like to achieve is: if the book has NOT a series, create a top folder named after the author and place the book there. If it HAS a series, create a folder with that name, but do _NOT_ create a subfolder with the autor name!

Given that if the {series} field is empty calibre automatically ignores it, I thought I could do something like:

{series}/{author_sort:MAGIC_GOES_HERE}....

Where the magic should be: if field {series} is empty, put $ - the value of author_sort -, if {series} is not empty do not put anything.

But how can I get this? In my understanding if I do something like:

{author_sort:'ifempty(val,str)'} the functions ifempty refers to the value of author_sort, while I'd like to check {series}. Can I refer to the series field value in "val"? Is there a way to achieve that?

Thank you!!!
Rus

Last edited by Ruskie_it; 12-21-2011 at 03:04 AM.
Ruskie_it is offline   Reply With Quote
Old 12-21-2011, 03:25 AM   #2
DoctorOhh
US Navy, Retired
DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.
 
DoctorOhh's Avatar
 
Posts: 9,896
Karma: 13806776
Join Date: Feb 2009
Location: North Carolina
Device: Icarus Illumina XL HD, Kindle PaperWhite SE 11th Gen
Welcome to Mobileread, hopefully someone with more expertise than I will help you out with this question.

Moderator Notice
Moved out of Development sub-forum to the Library Management sub-forum. The description of the Library management sub-forum reads as follows: All about viewing, adding, and exporting books; managing metadata; using templates; and understanding the calibre directory structure.

As a note to all folks there are sticky posts at the top of every calibre forum, these posts are an excellent source of information and at least the titles should be given a quick once over before posting to see if they might be of help.

At the top of the Development forum is the following sticky post, PLEASE READ BEFORE POSTING - The purpose of this subforum, for some reason this post is often missed by folks posting in the development forum.
DoctorOhh is offline   Reply With Quote
Advert
Old 12-21-2011, 04:26 AM   #3
Ruskie_it
Fanatic
Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.
 
Posts: 538
Karma: 1000000
Join Date: Dec 2011
Location: Rome, Italy
Device: Kindle PW5, Kindle PW4, Kindle 4 NT
Hello dwthanny,

Thank you for moving it. However, one time that I really read the "read before posting"!!!!
I did, if I got confused is because one of the things that section is for (the post says) is:

Quote:
template python functions

and any other topic that is related to calibre coding. The hope is that having many 'developer eyes' on some question will help all of us improve both our own skills and calibre itself.
I thought it was that, coding... Even though I now realize that is referred to actual phyton functions and not calibre functions. Sorry!
Ruskie_it is offline   Reply With Quote
Old 12-21-2011, 04:38 AM   #4
DoctorOhh
US Navy, Retired
DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.DoctorOhh ought to be getting tired of karma fortunes by now.
 
DoctorOhh's Avatar
 
Posts: 9,896
Karma: 13806776
Join Date: Feb 2009
Location: North Carolina
Device: Icarus Illumina XL HD, Kindle PaperWhite SE 11th Gen
I'm glad to see you read the post ahead of time.

Quote:
Originally Posted by Ruskie_it View Post
Sorry!
No need to apologize, I get just as confused at times. Since I don't have the coding skills I just do a little housekeeping in forums and try to keep the non development posts out of the development area.

All I can do with regards to templates is point you to the calibre template language area of the manual. chaley did the heavy lifting in creating this feature and if he has time it is not unusual for him to help folks out.
DoctorOhh is offline   Reply With Quote
Old 12-21-2011, 04:59 AM   #5
Ruskie_it
Fanatic
Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.
 
Posts: 538
Karma: 1000000
Join Date: Dec 2011
Location: Rome, Italy
Device: Kindle PW5, Kindle PW4, Kindle 4 NT
Thanks again.
Since I had a little spare time today at work, I had another go at it. Here is how I figured out how it could be, I'm posting this here in case anyone is interested:

{series}/{author_sort:'test(ifempty(field('series'),""),"", $)'}/{series_index:0>2s|| - }{title} - {authors}

The heart of what I was trying to do is this:

{author_sort:'test(ifempty(field('series'),""),"", $)'}

Here's the logic (I did not find relational operators, otherwise it might have been simpler):

So, if {series} is empty, calibre won't create {series} folder in the first instance. After that, if series is empty, the function

ifempty(field('series'),"")

will return an empty string. This string will be thrown at the test function it is called within:

test(FUNCTION,"",$)

If the value of the function is "" (that is if, and only if, the {series} field is empty, the function test will output the value of the author field, $; if the value is not empty, then test will return an empty value.

Therefore, when {series} has a value the whole template will be:

series_name/""/book_title (and calibre will convert it to series_name/book_title)

When {series} has not a value the template will be:

""/author_name/book_title (and calibre, I expect, will convert it to author_name/book_title)

If anyone has experience in coding, please correct or confirm... thank you!!!

Rus

Last edited by Ruskie_it; 12-21-2011 at 10:22 AM. Reason: wrong order of the test parameters
Ruskie_it is offline   Reply With Quote
Advert
Old 12-21-2011, 05:29 AM   #6
Ruskie_it
Fanatic
Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.
 
Posts: 538
Karma: 1000000
Join Date: Dec 2011
Location: Rome, Italy
Device: Kindle PW5, Kindle PW4, Kindle 4 NT
Yep, I tested it on a test folder, it works! :P
Ruskie_it is offline   Reply With Quote
Old 12-21-2011, 10:01 AM   #7
theducks
Well trained by Cats
theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.
 
theducks's Avatar
 
Posts: 31,046
Karma: 60358908
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
Quote:
Originally Posted by Ruskie_it View Post
Hello dwthanny,

Thank you for moving it. However, one time that I really read the "read before posting"!!!!
I did, if I got confused is because one of the things that section is for (the post says) is:



I thought it was that, coding... Even though I now realize that is referred to actual phyton functions and not calibre functions. Sorry!
It is easy to get confused on this one.
There is coding (as in user templates, recipes) and there is coding ( the core of Calibre, that we download).
Development is for the Big Coding
theducks is offline   Reply With Quote
Old 12-21-2011, 10:26 AM   #8
Ruskie_it
Fanatic
Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.
 
Posts: 538
Karma: 1000000
Join Date: Dec 2011
Location: Rome, Italy
Device: Kindle PW5, Kindle PW4, Kindle 4 NT
Quote:
Originally Posted by theducks View Post
It is easy to get confused on this one.
There is coding (as in user templates, recipes) and there is coding ( the core of Calibre, that we download).
Development is for the Big Coding
Yep, I got it now... But I'm a little too rusty for that, I do not do serious programming from my University days, and now as a SAP consultant I'm too busy as a system operator... :P
But coding a functional programming script as above was fun and mindbending enough.
Ruskie_it is offline   Reply With Quote
Old 12-21-2011, 01:12 PM   #9
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,440
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Glad you found template program mode!
Quote:
Originally Posted by Ruskie_it View Post
Here's the logic (I did not find relational operators, otherwise it might have been simpler):
The strcmp function provides comparison relationals. The test function provides equality relationals.
Quote:
Therefore, when {series} has a value the whole template will be:

series_name/""/book_title (and calibre will convert it to series_name/book_title)

When {series} has not a value the template will be:

""/author_name/book_title (and calibre, I expect, will convert it to author_name/book_title)
I assume that the above is in effect a requirements statement.

First, the /""/ will be converted to //, which will convert to /. As such, there isn't much point in generating it, although it doesn't hurt anything. Second, the test function can be used to test for != '', which eliminates the need for the subfunction by directly testing if series is not empty.

Combining the above with my understanding of your requirements, I think that this template satisfies your requirements.
Code:
{series:'test($, $, field('author_sort'))'}/{series_index:0>2s|| - }{title} - {authors}
chaley is offline   Reply With Quote
Old 12-21-2011, 03:39 PM   #10
Ruskie_it
Fanatic
Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.Ruskie_it ought to be getting tired of karma fortunes by now.
 
Posts: 538
Karma: 1000000
Join Date: Dec 2011
Location: Rome, Italy
Device: Kindle PW5, Kindle PW4, Kindle 4 NT
Quote:
Originally Posted by chaley View Post
Glad you found template program mode!The strcmp function provides comparison relationals. The test function provides equality relationals.
I see! Argh, yeah, I wasn't used to the subtleties of a functional language.

Quote:
I assume that the above is in effect a requirements statement.
I was trying to describe what my function does, but since it also is what I wanted to achieve yes, it's the requirement.
I want my book organized in folders on the device. When they are part of a series, I want them under a folder with the name of the series. When they are not, they should go under the name of the author.

Quote:
First, the /""/ will be converted to //, which will convert to /. As such, there isn't much point in generating it, although it doesn't hurt anything. Second, the test function can be used to test for != '', which eliminates the need for the subfunction by directly testing if series is not empty.
Yep, I used that because I didn't know how to write it better...
And I know I should have refined it at home and not in the office.
Yes, I think that moving the test in the series field makes it simpler and more elegant.
Ruskie_it is offline   Reply With Quote
Old 12-21-2011, 10:53 PM   #11
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,440
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
I was silly. I shouldn't have used "test". Better is "ifempty" as it avoids passing the same parameter twice, making things clearer and having better performance.
Code:
{series:'ifempty($, field('author_sort'))'}/{series_index:0>2s|| - }{title} - {authors}
chaley is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Does anyone use "general program mode" template functions? chaley Library Management 0 08-16-2011 11:54 AM
Removing class and id references Artha Sigil 10 07-24-2011 11:17 AM
Multiple references to the same footnote crutledge Sigil 8 08-23-2010 04:33 AM
multiple references troymc Sigil 3 05-06-2010 03:20 PM
Unutterably Silly Why all the Lovecraft references? Mobile Mason Lounge 3 12-03-2009 12:53 PM


All times are GMT -4. The time now is 10:21 AM.


MobileRead.com is a privately owned, operated and funded community.