Kodi Community Forum

Full Version: Kodistubs (xbmcstubs) - code auto-completion and quick help for Python IDEs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
XBMCstubs were renamed to Kodistubs!

Started to learn how to create XBMC Python addons, I came across xbmcstubs project and started using it. Then I've decided to update it with the most recent information from the latest XBMC Python docs and my personal experience.

Currently Kodistubs are generated (indirectly) from Kodi source code so they pretty much exactly re-create Kodi Python API classes and functions. Now Kodistubs also include PEP-484 type annotations that provide information about actual arguments and return values of Kodi Python API functions and methods.

What is this and why do you need it?

Kodistubs are literally Python module stubs which mimic Kodi Python API modules. They contain all (hopefully) classes, methods, functions and constants declarations with docstrings, type annotations and minimal code not to raise syntax errors and basic exceptions.
If you using some Python IDE (PyCharm, Eclipse+Pydev, Python Tools for Visual Studio, PyScripter etc.), you can add those modules to Python import paths of your current Kodi addon project, and you will get code auto-completion, code inspection (if your IDE/code editor supports it), and quick help that greatly facilitates writing addon code and reduces the number of errors.


Where to add these stub modules in my IDE?

Simply runĀ pip install Kodistubs to install Kodistubs into your project's virtual environment.

Also it is strongly recommended to read Kodistubs documentation: http://romanvm.github.io/Kodistubs/
If you are documenting your code with Sphinx, this URL can also be used as intersphinx reference point for Kodi Python API.

You can find the latest Kodi Python stub modules in my Github repo.

Note: I try to keep Kodistubs in sync with Kodi Python API changes but it may happen that I miss something, so any comments, corrections, and pull requests are welcomed.

Warning: those stub modules do not include any useful code so don't try to run/debug your addon directly from IDE, unless you add some debug code to them. They are meant only to help writing addon code.

However, if you want to test your code outside Kodi (e.g. do unit testing), with Kodistubs you can use some Python mock library like mock or flexmock to mimic Kodi Python functions and classes. This will help you to eliminate at least some basic errors and bugs.
I've also added documentation for XBMC Python modules auto-generated from XBMCstubs docstrings: http://romanvm.github.io/xbmcstubs/docs
This is the most up to date XBMC Python documentation, because, frankly speaking, official documentation pages are far from perfect (and Gotham docs are really messy at some places).
Any comments or corrections are welcomed Smile
This looks awesome! I can't wait to try it out. Do you know if there's anything similar for the domain-specific xml that xbmc uses?
(2013-09-25, 03:08)jerimiah797 Wrote: [ -> ]Do you know if there's anything similar for the domain-specific xml that xbmc uses?

You mean auto-completion for XBMC skin files? I doubt such thing exists. With Python it's relatively simple due to full code introspection.
Yeah, I've only worked with xml in the context of xbmc files so i dont know that much about it. I'm wondering if there's an editor that supports some kind of xml dictionary so it can autocomplete only the tags that xbmc recognizes and flag tags that aren't in the 'dictionary'.
Could you tell me how i would add this to VS2012? I am still pretty new to this.
(2013-09-27, 17:21)Dhorse Wrote: [ -> ]Could you tell me how i would add this to VS2012? I am still pretty new to this.

I guess by "VS2012" you really mean Python Tools for Visual Studio. You need to download a ZIP from Gighub, unpack it to a folder of your convenience and add the folder with stub .py files to "Search Paths" of your PTVS project.
See the example below:

Image
can i ask if you found docstrings that needed fixing in xbmc source code?
because you stated:
Quote:because original docstrings include incorrect info in some cases.

if yes can we perhaps expect a PR to fix these?
(2013-09-27, 20:25)Martijn Wrote: [ -> ]can i ask if you found docstrings that needed fixing in xbmc source code?
because you stated:
Quote:because original docstrings include incorrect info in some cases.

if yes can we perhaps expect a PR to fix these?

I'm afraid, PR is not possible because the C++ files in question are not present on Github and are auto-generated during compilation. Those are:
\xbmc\interfaces\python\generated\
AddonModuleXbmc.cpp
AddonModuleXbmcaddon.cpp
AddonModuleXbmcgui.cpp
AddonModuleXbmcplugin.cpp
AddonModuleXbmcvfs.cpp

Also I need to note that to say that my C++ knowledge is very basic will be a great exaggeration.Smile
They are generated from the files located here:
https://github.com/xbmc/xbmc/blob/master...es/legacy/

example:
https://github.com/xbmc/xbmc/blob/master...rol.h#L417
https://github.com/xbmc/xbmc/blob/master...Xbmc.h#L41

If you grep through those files finding the "wrong" text you will end up on the file containing that text.

Edit:
already started on some a while ago. need to finish it
https://github.com/xbmc/xbmc/pull/3187
2 Martijn

Thanks for the explanation, I'll see what I can do.

P. S. Added some screenshots to the OP.
(2013-09-28, 15:24)Roman_V_M Wrote: [ -> ]2 Martijn

Thanks for the explanation, I'll see what I can do.

P. S. Added some screenshots to the OP.

Thanks. Will appreciate it Smile
I've tried out the stubs and they work in PyCharm, too. Here's how to install (per project, not globally)

Open the project folder in PyCharm. Select Settings, then click 'Project Structure' under Project Settings. Then click '+ Add Content Root', browse to where you have put the folder containing the stub files, and select the folder. The stubs folder can be anywhere on your machine, it doesn't have to be in your project (skin, addon, script, whatever) folder. Then click 'Apply', and then 'Okay'.

Thanks so much for this, Roman!!!! It's really saves a lot of time pouring over the pydocs and figuring out which methods can be used in a given context.

-Jerimiah

PS I'm working on the XML helper code and have made some progress defining the default xml namespace and associated tags and rules for an xbmc compliant window file. There are currently several methods to do this and I'm experimenting to find out the best way that doesn't confuse xbmc when it loads the xml files. So far, some methods don't work. I'll keep you posted.
2 jerimiah797

Thanks for the info. Could you also provide an example screenshot like the ones in the OP? I will update the OP with your info.

BTW, I've changed docstrings structure in xbmcgui module to provide better tooltips.
Here you go!

-Jerimiah

Image
Pages: 1 2 3 4