Add a reference to the plugin object in the Tool class and also improve

documentation a little
This commit is contained in:
Kovid Goyal 2014-08-04 10:25:33 +05:30
parent 8b6aae9068
commit 04e4c25a90
4 changed files with 37 additions and 9 deletions

View File

@ -196,12 +196,4 @@ Viewer plugins
:members:
:member-order: bysource
Edit Book plugins
--------------------
.. autoclass:: calibre.gui2.tweak_book.plugin.Tool
:show-inheritance:
:members:
:member-order: bysource

View File

@ -121,6 +121,15 @@ Working with the Table of Contents
.. autofunction:: create_inline_toc
Edit Book Tool
--------------------
.. autoclass:: calibre.gui2.tweak_book.plugin.Tool
:show-inheritance:
:members:
:member-order: bysource
Controlling the editor's user interface
-----------------------------------------

View File

@ -39,6 +39,8 @@ class Plugin(object): # {{{
Useful methods:
* :meth:`temporary_file`
* :meth:`__enter__`
* :meth:`load_resources`
'''
#: List of platforms this plugin works on
@ -263,6 +265,12 @@ class Plugin(object): # {{{
return False
def __enter__(self, *args):
'''
Add this plugin to the python path so that it's contents become directly importable.
Useful when bundling large python libraries into the plugin. Use it like this::
with plugin:
import something
'''
if self.plugin_path is not None:
from calibre.utils.zipfile import ZipFile
zf = ZipFile(self.plugin_path)
@ -272,6 +280,7 @@ class Plugin(object): # {{{
for ext in ('pyd', 'so', 'dll', 'dylib'):
if ext in extensions:
zip_safe = False
break
if zip_safe:
sys.path.insert(0, self.plugin_path)
self.sys_insertion_path = self.plugin_path

View File

@ -17,6 +17,22 @@ from calibre.gui2.tweak_book.boss import get_boss
class Tool(object):
'''
.. module:: calibre.gui2.tweak_book.plugin.Tool
The base class for individual tools in an Edit Book plugin. Useful members include:
* ``self.plugin``: A reference to the :class:`calibre.customize.Plugin` object to which this tool belongs.
* self. :attr:`boss`
* self. :attr:`gui`
Methods that must be overridden in sub classes:
* :meth:`create_action`
* :meth:`register_shortcut`
'''
#: Set this to a unique name it will be used as a key
name = None
@ -103,7 +119,9 @@ def load_plugin_tools(plugin):
else:
for x in vars(main).itervalues():
if isinstance(x, type) and x is not Tool and issubclass(x, Tool):
yield x()
ans = x()
ans.plugin = plugin
yield ans
def plugin_action_sid(plugin, tool, for_toolbar=True):
return plugin.name + tool.name + ('toolbar' if for_toolbar else 'menu')