View Full Version : new plugins and using import


KevinH
05-09-2011, 11:44 AM
Hi Kovid, Other Plugin Developers

I am in the process of updating some older File Type plugins to the new plugin interface and I looked at your examples but still had a few questions.

From our old File Type plugin "_plugin.py" we could easily import other python code from our own zip archive by simply using "import" as long as we did it inside of the "run" method. So we never ran into any zipimport issues, since we never needed to do that.

Is using simple imports from within the run methods still the right way to import other code from our plugin zip archive for File Type Plugins?

I noticed you used some other approaches for UI demos where you defined a new class that did the main work but I was not sure if you needed/wanted that approach for the simpler file type plugins?

Thanks,

KevinH

KevinH
05-09-2011, 05:20 PM
Hi,

Just in case anyone else is interested in this question: based on trial and error with my own plugin and Calibre 0.8.0, it seems I misunderstood how use of import had changed in the new plugins setup. With recent calibre versions I can now use "import" anyplace (even for other files from the plugin zip) in __init.py__. So I no longer need to restrict the imports to be within the run method.

That is a very nice improvement indeed.

KevinH

kovidgoyal
05-09-2011, 08:35 PM
You can import anything from anywhere, and no you dont need multiple classes for file type plugins. Really, ll you need to migrate an old plugin is to rename the .py file to _init__.py and add the .txt file that gives the import name. Of course for maintainability of your plugin you should ideally split it up into multiple .py files, which the new import mechanism facilitates.

KevinH
05-09-2011, 09:37 PM
Hi Kovid,

Please forgive my ignorance here but just to be clear ...

By adding a "plugin-import-name-MYPLUGIN.txt" empty text file to my plugin zip archive, I am effectively saying the contents of my plugin zip file uses a namespace (or something like that) called "MYPLUGIN". This is done to prevent name clashes with other python files used inside calibre and/or inside other plugins, is that right?

Then in my __init__.py (or any of my other .py files in the plugin) I change my imports of other python files local to my plugin to use that "namespace" equivalent as follows:

from calibre_plugins.MYPLUGIN import file1

This would be equivalent to adding "import file1" in the run method under the old plugin interface.

Am I understanding things correctly?

Thanks,

KevinH

kovidgoyal
05-09-2011, 11:48 PM
Yes .