11-10-2010, 03:54 PM | #1 |
Calibre Plugins Developer
Posts: 4,688
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
Writing an interface action plugin
One feature I would love to have are some context menu items for looking up various things from the selected book on external websites in a separate browser window. For instance looking up the author on fantasticfiction.co.uk (so I can see other titles by the author or missing books in series), or looking up the book on Amazon to read reviews on it.
Reading about the lovely "plugin architecture" I thought I would look into dabbling myself with it. I've "had a go" with trial & error but stumped I think with the wrapper class. Has anyone successfully attempted this and if so could they share their code or experiences? Is there an equivalent of the "HelloWorld" plugin example for an interface action? I have a bunch of questions but a simple example would solve most of them I am sure. This is what I have so far (simplified) - all classes in the same file: Filename: foo_plugin.py, Zipped into: foo_plugin.zip Code:
import webbrowser from calibre.customize import InterfaceActionBase from calibre.gui2.actions import InterfaceAction from calibre.gui2 import error_dialog class ActionFoo(InterfaceActionBase): name = 'Foo' actual_plugin = 'FooAction' class FooAction(InterfaceAction) name = ... etc with implementation Many thanks for help - first time at trying to write Python (.NET developer in the day job) so please be gentle Last edited by kiwidude; 11-10-2010 at 03:57 PM. |
11-10-2010, 04:25 PM | #2 |
Calibre Plugins Developer
Posts: 4,688
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
Ok, so I got one step further... I changed the code line for actual_plugin to have this value:
Code:
actual_plugin = 'calibre.gui2.actions.foo_plugin:FooAction' If I list the plugins using calibre-customize it appears in the list, and I can see the zip file in the Roaming\calibre\plugins folder. There seems to be some trick I am missing though to (a) get the plugin to appear back in the Preferences dialog after a restart, and (b) to get my menu items to actually appear (which may or may not be related to the implementation). I have tried copying directly an implementation from show_book_details.py into my FooAction class to prove it is not part of the code and it makes no difference? That version is now attached. Apologies for being a pain for asking. Last edited by kiwidude; 11-10-2010 at 04:54 PM. Reason: Added attachment |
Advert | |
|
11-10-2010, 05:38 PM | #3 |
creator of calibre
Posts: 44,565
Karma: 24495948
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
The InterfaceAction plugins aren't really designed to be loaded from zip files (this is because they depend on Qt, unlike most other plugin classes) that's why you have to jump through the "actual_plugin" hoops.
I'd suggest running calibre from source and implementing your plugin there. It's fairly trivial to run calibre from source, instructions in the user manual. |
11-10-2010, 06:43 PM | #4 |
Calibre Plugins Developer
Posts: 4,688
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
That's a shame.
Is there a way of getting just the 0.7.26 version of the code for me to run, rather than the trunk? I'm running on Windows btw. |
11-10-2010, 06:45 PM | #5 |
creator of calibre
Posts: 44,565
Karma: 24495948
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
you can check out any revision you like from the source code.
|
Advert | |
|
11-10-2010, 07:06 PM | #6 |
Calibre Plugins Developer
Posts: 4,688
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
Ummm... could you please be so kind as to give me a command line I could type for that? Not familiar with Bazaar or your repo structure sorry. I followed the instructions on the web page previously with
bzr branch lp:calibre |
11-10-2010, 07:07 PM | #7 |
creator of calibre
Posts: 44,565
Karma: 24495948
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
do
bzr revert -r revision_number in your checkout, get the revision_number you want with bzr log |
11-10-2010, 08:16 PM | #8 |
Calibre Plugins Developer
Posts: 4,688
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
Brilliant, thank you very much.
Ok, time for the really dumb question. I've setup my environment variable, got the right revision of code I want... But how do I actually run from source? |
11-10-2010, 08:26 PM | #9 |
Wizard
Posts: 4,553
Karma: 950151
Join Date: Nov 2008
Device: Sony PRS-950, iphone/ipad (Marvin/iBooks/QuickReader)
|
Once you have downloaded the source and set up the Environment variable correctly to specify its location, just start up Calibre in the normal way and the source is automatically picked up. Source files automatically supersede binary files if they are newer.
|
11-10-2010, 08:32 PM | #10 | |
Calibre Plugins Developer
Posts: 4,688
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
Quote:
At a command prompt when I type echo %CALIBRE_DEVELOP_FROM% it gives me back "c:\calibre" which is my root directory that has a src subdirectory with the changes in. Something else obvious I am doing wrong? |
|
11-10-2010, 08:38 PM | #11 |
creator of calibre
Posts: 44,565
Karma: 24495948
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
You need to use c:\calibre\src
|
11-10-2010, 08:53 PM | #12 |
Calibre Plugins Developer
Posts: 4,688
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
That would be the obvious thing Thanks kovid, running from source now.
|
11-10-2010, 09:35 PM | #13 |
Calibre Plugins Developer
Posts: 4,688
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
Ok, got my action plugged into the menu. For anyone else's info who follows in my footsteps (and for Kovid to tell me if I did wrong) I did these steps:
1. Add your implementation class file into \src\calibre\gui2\actions 2. Edit src\calibre\customize\builtins.py to add the wrapper class implementation pointing to the above. Also add the name of that wrapper class to the 'plugins' variable array. 3. Edit src\calibre\gui2\__init__.py to ensure the context menu item actually physically appears in the 'action-layout-context-menu' array in appropriate place you want it to appear in the menu. The value you add here matches that of the 'name' property of the implementation class. 4. Begin many iterations of opening and closing calibre as you learn how to write code in Python I've got a working (crude) implementation though of launching the webpage for fantasticfiction for the current selected author which was the first goal so stoked with that. Thanks for the help to get up and running! |
11-11-2010, 02:19 AM | #14 |
creator of calibre
Posts: 44,565
Karma: 24495948
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Note that 3 is not strictly necessary, you can control what appears in the context menus and toolbars via Preferences->Toolbars
|
11-11-2010, 08:34 AM | #15 |
Calibre Plugins Developer
Posts: 4,688
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
Ahhh yes, did not know that, thanks Kovid.
Job done... loving it! A shame I have to run from source (would be nicer as a zip plugin) but that I was able to add this so easily is a tribute to the quality of the existing codebase - great job Kovid and whoever else wrote Calibre. I've attached a screenshot for anyone interested in what I ended up with. The sub-menu options are currently defined in an array. You specify the name, icon and a url with tokens like {author}, {title} or {isbn} to be substituted by the plugin based on the selected row. You can also do empty rows for separators. If there was any interest by others in this becoming a patch to add to the trunk whoever wants to do it is welcome to the code, let me know. It would be nice to make the array able to be specified via the tweaks file or some other configuration, so users can manipulate the menu with other websites etc. I've not written this to be "bulletproof" being a Python newbie but it does the job for me. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
New Plugin Type Idea: Library Plugin | cgranade | Plugins | 3 | 09-15-2010 01:11 PM |
Device interface plugin - nook | badmadbug | Plugins | 2 | 09-06-2010 12:03 PM |
Manybooks new interface | mtravellerh | Upload Help | 0 | 11-30-2008 09:26 AM |
OS X Interface | irulan | Calibre | 5 | 09-13-2008 02:18 PM |
iLiad Interface Design | nathany | iRex Developer's Corner | 6 | 09-17-2007 03:05 PM |