03-29-2014, 09:39 PM | #1 |
Member
Posts: 21
Karma: 104
Join Date: Oct 2013
Device: none
|
Getting PyCharm code completion while writing Calibre plugins
First of all, I am a complete newbie when it comes to writing Calibre plugins. All I've done so far is take kiwidude's Count Pages plugin and blast in the ability to also count the # of occurrences of a (weighted) list of words. (To show how lame my implementation is my weighted list is just a dict of regexs with the weights as the values written directly in his statistics.py file. I didn't bother to write a GUI for this since it's easy enough for me to just edit the source and reload the plugin )
Anyway, I've discovered a way to use the very nice (and free) JetBrains PyCharm 3.1.1 Community Edition IDE that lets you see code completion for Calibre objects while writing plugins. Unfortunately, I don't know how to use the PyCharm Debugger on a Calibre plugin (it's probably impossible?). The basic idea is to download the source to Calibre, extract the src\Calibre directory --- or even all the subfolders of src --- into a .zip file, and then setup a PyCharm Python Interpreter to have that zip file in its path. (For whatever reason, I couldn't directly use C:\Program Files\calibre2\pylib.zip? I couldn't even in unpack it correctly since it seems to be some weird zip that doesn't let you unpack while preserving its relative paths?) Then you'll get code completion for anything in the calibre module. For example, typing "from calibre." will show things like: constants in a popup. And of course PyCharm already supports PyQt4 so you get code completion when writing your QWidget derived class.db ebooks gui2 utils Here's the details for those interested. Assuming you have calibre.zip (just the src\Calibre --- or maybe src if you also want completions of odf, cherrypy, regex, etc? --- tree of calibre-1.29.0.tar.xz) do this:
Code:
from calibre_plugins.count_pages.common_utils import (get_library_uuid, CustomColumnComboBox, KeyboardConfigDialog, KeyValueComboBox, PrefsViewerDialog) workaround is to change this to just: Code:
from common_utils import (get_library_uuid, CustomColumnComboBox, KeyboardConfigDialog, KeyValueComboBox, PrefsViewerDialog) Hope this is of some interest to people Last edited by trying; 03-29-2014 at 10:03 PM. Reason: fix error |
03-29-2014, 10:21 PM | #2 |
creator of calibre
Posts: 43,775
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
You can just do this:
Code:
try: from calibre_plugins.count_pages.common_utils import (get_library_uuid, CustomColumnComboBox, KeyboardConfigDialog, KeyValueComboBox, PrefsViewerDialog) except ImportError: from common_utils import (get_library_uuid, CustomColumnComboBox, KeyboardConfigDialog, KeyValueComboBox, PrefsViewerDialog) Code:
import sys in_calibre = hasattr(sys, 'extensions_location') Last edited by kovidgoyal; 03-29-2014 at 11:48 PM. |
03-29-2014, 10:46 PM | #3 |
creator of calibre
Posts: 43,775
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
As for debugging, I have written over half a million lines of python code without once needing to use a debugger, but if you really want to, you can probably setup remote debugging, as described here:
http://blog.jetbrains.com/pycharm/20...-with-pycharm/ |
03-29-2014, 11:17 PM | #4 |
Member
Posts: 21
Karma: 104
Join Date: Oct 2013
Device: none
|
Yes the try: except ImportError: technique does work
For the other one, you really meant to say : Code:
import sys
in_calibre = hasattr(sys, 'extensions_location')
|
03-29-2014, 11:50 PM | #5 |
creator of calibre
Posts: 43,775
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
calibre_basename and friends only exist if you are running a frozen build of calibre. extensions_location always exists even if you are running calibre directly from source.
|
03-30-2014, 05:01 AM | #6 |
creator of calibre
Posts: 43,775
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Just for giggles I added a remote python debugger to calibre. https://github.com/kovidgoyal/calibr...fd9c65f8c8bffb
|
03-30-2014, 05:47 AM | #7 | |
Member
Posts: 21
Karma: 104
Join Date: Oct 2013
Device: none
|
Quote:
I spent a few hours and got debugging Calibre v1.29 plugins to work with PyCharm. Unfortunately it is only possible with PyCharm Professional, since you need the ability to create Python Remote Debug configurations which is not possible with the free PyCharm Community Edition. Since there is already the HowTo: Debug calibre with Eclipse/PyDev thread (which I just discovered) there probably won't be much demand for debugging with PyCharm Pro. For those interested, the process is vaguely similar. You start up a Debugger server and add code to your plugin to call settrace(). However, PyCharm also requires what they call "Path mappings" that are currently a little buggy (at least on Windows 7). If they fix this I might start another thread that explains how to debug Calibre with PyCharm Pro in more detail. |
|
Tags |
pycharm |
Thread Tools | Search this Thread |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
calibre plugins | whizbo | Calibre | 3 | 09-20-2013 04:08 PM |
Aura HD Calibre Plugins and Tricks | thoreau | Kobo Reader | 1 | 05-06-2013 02:49 PM |
Calibre Portable like the Calibre on PC (espec. settings and plugins)? | manawydan | Devices | 6 | 01-23-2013 06:45 AM |
Best Calibre plugins/tweaks | baronrus | Kobo Reader | 2 | 05-24-2011 11:09 PM |
Free ebook from Microsoft Press: Writing Secure Code, 2nd Ed | atidball | Deals and Resources (No Self-Promotion or Affiliate Links) | 11 | 08-30-2008 02:05 PM |