Register Guidelines E-Books Search Today's Posts Mark Forums Read

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

Notices

Reply
 
Thread Tools Search this Thread
Old 11-18-2011, 03:40 PM   #1
smoothrolla
Member
smoothrolla began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Nov 2011
Device: kindle
How to automatically populate a tag with the decade from the pubdate via python

Hi all.

has anyone written any python code to get the decade from the pubdate year and put it into a custom column? ie 1910's 2000's etc

Im going to attempt one but i dont know half the commands for string manipulation

Any help appreciated!
smoothrolla is offline   Reply With Quote
Old 11-18-2011, 04:16 PM   #2
chaley
"chaley", not "charley"
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: 5,898
Karma: 1216548
Join Date: Jan 2010
Location: France
Device: Many android devices
Assuming you are writing a custom template function, the following gets the first three digits of the year with a trailing 0. It doesn't check for the 'undefined' date (1/1/0101).
Code:
def evaluate(self, formatter, kwargs, mi, locals):
	x = (mi.pubdate.year/10)*10
	return '%04d'%x
chaley is offline   Reply With Quote
Old 11-18-2011, 04:50 PM   #3
smoothrolla
Member
smoothrolla began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Nov 2011
Device: kindle
The code works great for me, cheers!

i have never used the mi parameter before
can i use this to access any of the metadata for the book?
ie mi.tags or mi.#anothercustomcolumn?
smoothrolla is offline   Reply With Quote
Old 11-19-2011, 05:33 AM   #4
smoothrolla
Member
smoothrolla began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Nov 2011
Device: kindle
is there any documentation on how to use mi and formatter? i have struggled to find any!
thanks
smoothrolla is offline   Reply With Quote
Old 11-19-2011, 07:09 AM   #5
chaley
"chaley", not "charley"
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: 5,898
Karma: 1216548
Join Date: Jan 2010
Location: France
Device: Many android devices
Quote:
Originally Posted by smoothrolla View Post
is there any documentation on how to use mi and formatter? i have struggled to find any!
thanks
The API for mi is documented in http://manual.calibre-ebook.com/template_ref.html#id73.

The only formatter method that is 'public' is
Code:
safe_format(self, fmt, kwargs, error_value, book)
  • fmt: the template string to evaluate
  • kwargs: a dict object containing key => value pairs. This is almost always the same as 'book' (below), but it can be different.
  • error_value: a string that is used if the template throws an exception
  • book: the metadata for the book in question, passed as mi to a formatter function. Can be None. Usually is the same as kwargs.
Notes:
  • kwargs is not always an mi structure. It should be accessed using
    Code:
    kwargs.get('key', defaultValue)
    where 'key' is the lookup key for the field/column in question.
  • book is either None or an instance of Metadata. There is no guarantee that the values obtained from book.get (mi.get) are the same as those returned by kwargs.get, although they usually are.
  • book does not equal kwargs for templates used in save-to-disk.
  • book does equal kwargs for templates used in the gui
  • book may or may not equal kwargs for templates used in send-to-device.
  • calling the formatter from a template function will evaluate the sub-template in the current context, sharing local template variables. If you don't want that, then create a new instance of formatter.
    Code:
    formatter.__class__().safe_format('yourTemplateString', kwargs, 'YourErrorText', mi)
Taking the above and defensive programming into consideration, the template I provided earlier is better written as
Code:
def evaluate(self, formatter, kwargs, mi, locals):
	x = kwargs.get('pubdate', None)
	if x is None:
		return ''
	x = (x.year/10)*10
	return '%04d'%x
If it matters to your task, you can check if you can use mi instead of kwargs by testing if kwargs == mi. You can check if kwargs is an instance if Metadata using
Code:
    from calibre.ebooks.metadata.book.base import Metadata
    if isinstance(kwargs, Metadata):
        do what you want to do
Finally, the best way to learn how to use these things is to look at the calibre source. See http://manual.calibre-ebook.com/develop.html. For example, mi makes reference to Field_Metadata, a dict describing each field/column in the calibre database. This dict is documented in src.calibre.library.field_metadata.py.
chaley is offline   Reply With Quote
Old 11-19-2011, 07:41 AM   #6
smoothrolla
Member
smoothrolla began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Nov 2011
Device: kindle
Thanks Chaley, really appreciate that info!
smoothrolla is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatically add tag if word/phrase found in ebook? eosrose Calibre 3 11-16-2011 07:48 AM
pubdate from PDF Metadata montymaverick Library Management 0 11-08-2011 08:29 AM
Quality (stars) doesn't populate jeffla376 Library Management 5 10-03-2011 11:06 PM
How do I populate a custom field on import? himitsuhieki Library Management 7 08-19-2011 04:37 PM
Using PubDate in print_version of custom news source mobilereader72 Calibre 4 05-30-2009 06:52 PM


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


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