|
|
#1 |
|
Junior Member
![]() Posts: 8
Karma: 10
Join Date: Sep 2014
Location: Edinburgh
Device: Kindle
|
Writing a plugin and my method isn't being called
I'm trying to write a plugin which will zero the ratings on any books which are imported.
I thought this would be a simple job, but sadly it is proving a little more fiddly than I'd expect. Looking at the documentation I see that I need a FileTypePlugin, and there is the ability to call a method post-import, which sounds like it should do the right thing. My code looks like this: Code:
import os
from calibre.customize import FileTypePlugin
from calibre.ebooks.metadata.meta import get_metadata, set_metadata
class ZeroRatingsPlugin(FileTypePlugin):
name = 'Reset Rating Plugin'
description = 'When a book is imported set the rating to zero.'
supported_platforms = ['windows', 'osx', 'linux']
author = 'Mysql.'
version = (1, 0, 1)
file_types = set(['epub', 'mobi', 'lit', 'prc'])
on_import = True
on_postimport = True
minimum_calibre_version = (2,0,0)
def run(self, path_to_ebook):
'''
NOP
'''
print "ZeroRatings::run(%s)" % path_to_ebook
return path_to_ebook
def postimport( book_id, book_format, db):
'''
Find the path to the book, and zero the rating.
'''
print "ZeroRatings::postimport()"
if db is None:
from calibre.library import db
db = db() # initialize calibre library database
path_to_ebook = db.format_abspath(book_id, book_format, index_is_id=True)
print( "Path to book is %s" % path_to_ebook )
file = open(path_to_ebook, 'r+b')
print "Book is at " , path_to_ebook
ext = os.path.splitext(path_to_ebook)[-1][1:].lower()
mi = get_metadata(file, ext)
print "Current rating is:", mi.rating
mi.rating = 0
set_metadata(file, mi, ext)
When I import a book I see this Code:
ZeroRatings::run(/home/skx/test/foo.epub) That suggests that my Plugins run method is called but NOT the postimport method. I get the feeling I'm missing something obvious, but I'm not sure what that might be. |
|
|
|
|
|
#2 |
|
Junior Member
![]() Posts: 8
Karma: 10
Join Date: Sep 2014
Location: Edinburgh
Device: Kindle
|
I will say that if I follow the tutorial and update my code to have this:
Code:
def run(self, path_to_ebook):
'''
NOP
'''
print "ZeroRatings::run(%s)" % path_to_ebook
file = open(path_to_ebook, 'r+b')
ext = os.path.splitext(path_to_ebook)[-1][1:].lower()
mi = get_metadata(file, ext)
mi.rating = 0
set_metadata(file, mi, ext)
return path_to_ebook
|
|
|
|
|
|
#3 |
|
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,676
Karma: 28549304
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
If you want to zero the ratings, do this
db.new_api.set_field('rating', {book_id:0}) in the postimport method. |
|
|
|
|
|
#4 |
|
Junior Member
![]() Posts: 8
Karma: 10
Join Date: Sep 2014
Location: Edinburgh
Device: Kindle
|
Thanks, that seems cleaner and easier than my meta-date manipulation.
However it doesn't solve the specific problem I'm having - that the postimport method is not being called. I've upgraded to the latest, 2.3.0, release, and reset my ~/.config/calibre directroy. With those things done the following code shows output from "run" but not "postimport". Code:
import os
from calibre.customize import FileTypePlugin
from calibre.ebooks.metadata.meta import get_metadata, set_metadata
class RatingReset(FileTypePlugin):
name = 'Rating-Reset Plugin'
description = 'When a book is imported set the rating to zero.'
supported_platforms = ['windows', 'osx', 'linux']
author = 'Moi.'
version = (0, 0, 2)
file_types = set(['epub', 'mobi', 'lit', 'prc'])
on_postimport = True
minimum_calibre_version = (2,3,0)
print "RatingReset::loaded"
def run(self, path_to_ebook):
print "RatingReset::run(%s)" % path_to_ebook
return path_to_ebook
def postimport( book_id, book_format, db):
print "RatingReset::postimport()"
db.new_api.set_field('rating', {book_id:0})
|
|
|
|
|
|
#5 |
|
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,676
Karma: 28549304
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
You're missing a self in postimport()
|
|
|
|
|
|
#6 |
|
Junior Member
![]() Posts: 8
Karma: 10
Join Date: Sep 2014
Location: Edinburgh
Device: Kindle
|
Thanks for the prompt reply, again. I don't know how you have the patience.
Adding the missing "self" does not resolve the problem though, sadly: Xlib: extension "GLX" missing on display ":0.0". Code:
Could not initialize OpenGL for RasterGLSurface, reverting to RasterSurface. Worker Launch took: 0.129006147385 Job: 1 Read metadata (0 of 1) finished Read metadata (0 of 1) RatingReset::loaded RatingReset::run(/home/skx/tmp/tmp.epub) Code:
def run(self, path_to_ebook):
print "RatingReset::run(%s)" % path_to_ebook
return path_to_ebook
def postimport(self, book_id, book_format, db):
print "RatingReset::postimport()"
db.new_api.set_field('rating', {book_id:0})
|
|
|
|
|
|
#7 |
|
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,676
Karma: 28549304
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Hmm, I dont see anything else wrong. Perhaps the postimport hook for running plugins is broken, I will take a look at it when I next have a free moment.
|
|
|
|
|
|
#8 |
|
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,676
Karma: 28549304
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
There was indeeda bug, that affected postimport plugins when adding via the GUI only. You can add via
calibredb add file.epub and it should work, or wait for the fix: https://github.com/kovidgoyal/calibr...bc12e3919da763 |
|
|
|
|
|
#9 |
|
Junior Member
![]() Posts: 8
Karma: 10
Join Date: Sep 2014
Location: Edinburgh
Device: Kindle
|
Thank you - I was in the middle of adding tracing to the various source files.
I've pulled your recent commits and I can confirm that this resolves my issue. My my ResetRatings plugin is now working/complete. Last edited by plugin_dev; 09-26-2014 at 02:52 AM. Reason: git release / not release. |
|
|
|
![]() |
| Tags |
| plugin-development, plugins |
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Regarding using metadata objects in identify method of metadata download plugin api | aprekates | Development | 1 | 07-06-2014 04:35 AM |
| Writing a first plugin | Agama | Development | 50 | 07-07-2012 05:06 PM |
| Plugin not customizable: Plugin: HTML Output does not need customization | flyingfoxlee | Conversion | 2 | 02-24-2012 03:24 AM |
| Help with plugin writing | meme | Plugins | 2 | 01-21-2011 02:57 PM |
| Writing an interface action plugin | kiwidude | Plugins | 21 | 11-11-2010 05:11 PM |