Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Plugins

Notices

Reply
 
Thread Tools Search this Thread
Old 01-22-2022, 03:16 PM   #1
un_pogaz
Chalut o/
un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.
 
un_pogaz's Avatar
 
Posts: 410
Karma: 145324
Join Date: Dec 2017
Device: Kobo
Plugin tip that change my life for the best you can not imagine

I am currently developing a plugin and I use Notepad++ which usually does the coffee.
But I've reached the limit of this solution, between the non syntax checking of silly errors and the lack of auto-completions, I decided to look for an IDE that could help me.

But here's the thing, the development of Calibre plugins has a critical element:
importing a custom package namespace at the root of yours modules.

All IDEs use the path of the file as source, or the path of the project file, as a starting point to build the namespace. Except for the Calibre plugins which already use a namespace at their "root".

So the IDE has to do a tricky thing : link/virtualize a Python namespace to a totally different folder.
calibre_plugins.epub_contributors_metadata <=> F:\Projet\Calibre plugins\ePub-Contributors-Metadata

... and nothing.
Impossible to find a solution. Without that, it's impossible to take advantage of the complete and dynamic auto-completion that gives the IDE this advantage I am looking for. You might as well go back to Notepad++ and write your plugin only from your head, knowing by heart everything you have written. And if we forgot a Colon... Calibre will tell us when it finds it.

But while searching on the internet how to do it, where obviously nobody seems to have solution to my problem , I found however many more pages that talk about the use of the upper relative namespace from ..your_module import your_function.
So, cool to know that Python is so powerful and versatile, that whatever your problem, if it involves the basic language elements, is implemented, even if its obscure and superfluous.
But that's not what I'm looking for, next!
[Note: all I learned from Python is for the Calibre plugins, so I'm still discovering a lot]

And then the information made its way... "If Python supports relative namespace up, it probably supports relative local paths, right?"
... Oh my god, if this works...

Let's try :
Code:
from calibre_plugins.epub_contributors_metadata.config import ICON, PREFS
from calibre_plugins.epub_contributors_metadata.common_utils import debug_print, set_plugin_icon_resources, get_icon, create_menu_action_unique
becomes:
Code:
from .config import ICON, PREFS
from .common_utils import debug_print, set_plugin_icon_resources, get_icon, create_menu_action_unique
Well, what does my IDE say:

It accepts
It finds and auto-completes my code correctly :

But... is Calibre too?

We ZIP the plugin, we launch calibre-debug.exe --gui-debug "calibre-debug.txt" and we pray. A lot

holy *loud profanaty*

it works, IT WORKS!!!

*click click click*

All work perfecly fine !

Oh boy, you can't imagine how much this will change my life and how happy I am. It's going to make my life so much easier to use a real IDE for plugin development, you can't imagine how much (although considering the size of the text I'm writing, maybe a little)

Obviously such a solution has its problems, for example if you use folders to classify your code, you'll have to play with relative namespace, but with a little bit of trickery and caution it can be managed.
But, I noticed that the vast majority of plugins, even the biggest and most complex ones, are flat: all in the same folder. That's how much using the complete namespace calibre_plugins.epub_contributors_metadata can be superfluous.

And, is at the discretion of each individual, I find it more elegant.

The only real little trick to know with this solution:
If you want to import the whole module, like import calibre_plugins.epub_contributors_metadata.templat es, the first idea import .templates doesn't work since "import" will not support relative namespace.
You will have to write from . import templates instead.

In conclusion: Use relative namespace, is more concise and versatile.

Last edited by un_pogaz; 01-22-2022 at 03:18 PM.
un_pogaz is offline   Reply With Quote
Reply

Tags
plugin, plugin development, python


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Koreader citation save tip or plugin for academic writing? amin021023 Android Devices 1 08-05-2020 05:31 PM
plugin to change pdf metadata? tsolignani Library Management 16 04-11-2013 07:58 PM
How do I change Plugin Images/Icons? Steppa Plugins 8 01-22-2013 06:37 PM
Accessories Motion L series Pen: when and how to change tip Hopi enTourage Archive 6 02-13-2011 03:33 PM
Useful tip: How to change the BD language AFTER you create a book HarryT Workshop 4 04-15-2009 12:36 AM


All times are GMT -4. The time now is 06:18 PM.


MobileRead.com is a privately owned, operated and funded community.