Download calibre User Manual Release 2.7.0 Kovid Goyal

Transcript
calibre User Manual, Release 2.7.0
Contents
• Anatomy of a calibre plugin (page 198)
• A User Interface plugin (page 199)
– __init__.py (page 200)
– ui.py (page 201)
– main.py (page 202)
– Getting resources from the plugin zip file (page 205)
– Enabling user configuration of your plugin (page 205)
• Edit Book plugins (page 207)
– main.py (page 207)
• Adding translations to your plugin (page 210)
• The plugin API (page 210)
• Debugging plugins (page 211)
• More plugin examples (page 211)
• Sharing your plugins with others (page 211)
Note: This only applies to calibre releases >= 0.8.60
Anatomy of a calibre plugin
A calibre plugin is very simple, it’s just a zip file that contains some python code and any other resources like image
files needed by the plugin. Without further ado, let’s see a basic example.
Suppose you have an installation of calibre that you are using to self publish various e-documents in EPUB and MOBI
formats. You would like all files generated by calibre to have their publisher set as “Hello world”, here’s how to do it.
Create a file named __init__.py (this is a special name and must always be used for the main file of your plugin)
and enter the following Python code into it:
import os
from calibre.customize import FileTypePlugin
class HelloWorld(FileTypePlugin):
name
= 'Hello World Plugin' # Name of the plugin
description
= 'Set the publisher to Hello World for all new conversions'
supported_platforms = ['windows', 'osx', 'linux'] # Platforms this plugin will run on
author
= 'Acme Inc.' # The author of this plugin
version
= (1, 0, 0)
# The version number of this plugin
file_types
= set(['epub', 'mobi']) # The file types that this plugin will be applied to
on_postprocess
= True # Run this plugin after conversion is complete
minimum_calibre_version = (0, 7, 53)
def run(self, path_to_ebook):
from calibre.ebooks.metadata.meta import get_metadata, set_metadata
file = open(path_to_ebook, 'r+b')
ext = os.path.splitext(path_to_ebook)[-1][1:].lower()
mi = get_metadata(file, ext)
mi.publisher = 'Hello World'
set_metadata(file, mi, ext)
return path_to_ebook
That’s all. To add this code to calibre as a plugin, simply run the following in the directory in which you created
__init__.py:
198
Chapter 1. Sections