06-21-2021, 09:25 PM | #1 |
Junior Member
Posts: 6
Karma: 10
Join Date: Jun 2014
Device: Android phone
|
Plugin dependencies
Hello, I thought I'd take a stab at making a google play books upload plugin for calibre.
I created a test python3 script that is able to upload a test epub file to my google drive after authenticating, getting permission, etc. using google-api-python-client and google-auth-oauthlib, etc. and made a gui calibre plugin to upload selected books on button press, but am having difficulty getting the python dependencies figured out. From looking at the WordDumb plugin example I see they are using pip to install their dependencies. Is that the best way to go about it? From reading https://gis.stackexchange.com/questi...python-library I thought maybe I'd just include the dependent .whl files inside the .zip, add them to the python path, and import that way, but that doesn't seem to work. When my plugin is loaded it complains that it can't find the googleapiclient module still with those paths in the python path. I guess if needed I can pip install the dependencies on any user's device, but that seems a bit overreaching. What's the best way to approach this? thanks, Jeremy Whiting |
06-21-2021, 10:27 PM | #2 |
creator of calibre
Posts: 44,317
Karma: 23661992
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
The plugin zip file is added to the sys.path if you do
with self: import your dependencies here I dont recall if python is capable of loading whl files from within zip files, probably safer to bundle the deps as ordinary python files. |
Advert | |
|
06-22-2021, 02:58 PM | #3 |
Junior Member
Posts: 6
Karma: 10
Join Date: Jun 2014
Device: Android phone
|
Thank you for the quick reply Kovid. I'm not quite sure what you mean by that. I took a stab at what you mean in action.py on my "implemtation" branch here, but the line with "with self:" gives an error:
Traceback (most recent call last): File "calibre_plugins.play_books_upload.action", line 110, in upload_books AttributeError: __enter__ https://github.com/jpwhiting/play-bo...action.py#L110 I've not seen "from self:" before in python examples anywhere, so am not quite sure what that is for, etc. If I try the imports without that, the upload action errors saying it can't find googleapiclient module which is there in the zip of the plugin. |
06-22-2021, 10:26 PM | #4 |
creator of calibre
Posts: 44,317
Karma: 23661992
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
self here is the plugin object not the interfaceaction object, if you want to do it in the InterfaceAction use with self.actual_plugin
|
06-23-2021, 12:17 AM | #5 |
Junior Member
Posts: 6
Karma: 10
Join Date: Jun 2014
Device: Android phone
|
Thanks for your help, but I'm still not following. action.py's PlayUploadAction class doesn't have any actual_plugin in it. Adding one with the same string value as __init__.py didn't seem to help either. It seems like the "with" keyword doesn't know what to do with a string (self.actual_plugin is defined as a string with value 'calibre_plugins.play_books_upload.action:PlayUplo adAction') or am I missing something?
Here's what I get with the force push I did to my implementation branch now: Traceback (most recent call last): File "calibre_plugins.play_books_upload.action", line 112, in upload_books AttributeError: __enter__ from reading this: The with statement is a control-flow structure whose basic structure is: with expression [as variable]: with-block The expression is evaluated, and it should result in an object that supports the context management protocol (that is, has __enter__() and __exit__() methods). I think self.actual_plugin which is just a string doesn't have that __enter__() or __exit__() method like a file for example would. |
Advert | |
|
06-23-2021, 12:28 AM | #6 |
creator of calibre
Posts: 44,317
Karma: 23661992
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Sorry got the name wrong, you want
self.interface_action_base_plugin |
06-23-2021, 12:39 PM | #7 |
Junior Member
Posts: 6
Karma: 10
Join Date: Jun 2014
Device: Android phone
|
Perfect. Works like a charm. Now to get the other kinks worked out. Thanks again kovid.
|
09-10-2021, 02:23 AM | #8 |
Junior Member
Posts: 6
Karma: 10
Join Date: Jun 2014
Device: Android phone
|
Ok, now after it working well for a while all of a sudden it has issues on the second time the action is triggered. I've got the following pattern:
with self.interface_action_base_plugin: import googleapiclient.discovery import google_auth_oauthlib.flow etc. On the second try the import fails because it's trying to import from a temp folder that has since gotten deleted. C:\\Users\\jeremy\\AppData\\Local\\Temp\\calibre_k r5ouawl\\6915rm5iplugin_unzip Is there some method to make the plugin unzip not get deleted when the first action finishes? Maybe I need to assign the result of the with operation to a class variable so it stays around until calibre closes or something? |
09-10-2021, 02:28 AM | #9 |
creator of calibre
Posts: 44,317
Karma: 23661992
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Manage it yourself if you need to import things repeatedly. See the __enter__ method of the Plugin class. Or just assign your imported things to some global variable the first time they are imported and use that.
|
09-10-2021, 09:39 AM | #10 |
Junior Member
Posts: 6
Karma: 10
Join Date: Jun 2014
Device: Android phone
|
Perfect. Extracting my dependencies next to my secrets file in a temp folder makes import once work as expected. Thanks kovidgoyal
|
Tags |
dependencies, pip, plugins, whl |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
koreader-cervantes dependencies appear to be corrupt | crudepatchwork | KOReader | 5 | 04-15-2019 08:12 PM |
Calibre Plugin with External Dependencies | ClashTheBunny | Development | 1 | 06-22-2015 10:41 AM |
Building libiMobileDevice and missing dependencies | ShellShock | Development | 2 | 10-26-2014 11:52 AM |
Dependencies | Bada Bing | Calibre | 3 | 03-11-2011 07:17 AM |
[Rant] New dependencies for 0.6.11? | Jellby | Calibre | 15 | 10-16-2009 10:17 AM |