diff --git a/manual/plugins.rst b/manual/plugins.rst index f609589c70..f430ebf791 100644 --- a/manual/plugins.rst +++ b/manual/plugins.rst @@ -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 - diff --git a/manual/polish.rst b/manual/polish.rst index 43f10dc85b..9537ceabda 100644 --- a/manual/polish.rst +++ b/manual/polish.rst @@ -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 ----------------------------------------- diff --git a/src/calibre/customize/__init__.py b/src/calibre/customize/__init__.py index 38f61d49ff..7569dc107e 100644 --- a/src/calibre/customize/__init__.py +++ b/src/calibre/customize/__init__.py @@ -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 diff --git a/src/calibre/gui2/tweak_book/plugin.py b/src/calibre/gui2/tweak_book/plugin.py index af77d58a58..1c4bf4675d 100644 --- a/src/calibre/gui2/tweak_book/plugin.py +++ b/src/calibre/gui2/tweak_book/plugin.py @@ -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')