On my Linux system XSCRIPTCONTEXT is defined in pythonscript.py
The entire object hierarchy comes from a framework called “Unified Network Objects” (UNO).
Thanks, I located the pythonscript.py
file. I would like PyLance (or the language server) to by default read this file. But I can only add directories to that one (where it finds the imported modules).
I guess the UNO bindings are generated by C/C++, and somehow exposed to the Python interpreter. And that there are no corresponding Python source files (?) .
All languages use the same Unified Network Object. All it takes is a little bit of glue code.
Yes, and somehow Python can then use this code:
from com.sun.star.util import XModifyListener
But there is nowhere in the file system a file com/sun/star/util.py
(as would be for most other import statements).
How can an external tool (like a language server) get hold of the same information as the Python interpreter ?
Maybe I need to search this piece from other sources…
https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1util.html
[Tutorial] Introduction into object inspection with MRI
Both links are interesting and bring me new knowledge. The MRI is an interesting tool (I had used it but the articles points out new useful things to me).
You could say my original question was more how to make the code analyzer in VS Code go quiet - by resolving the import paths. I do use the output of the code analyzer - as it picks up bugs. It gets a bit overloaded now by these unnecessary references to UNO function calls it cannot resolve.
I’ll live with it for now.
So your question is more on configuration of pylance. I could find how to silence missing imports via google (pylance ignore missing imports) at Disable specific Pylance linting messages in VS Code settings.json like with "python.linting.pylintArgs" - Stack Overflow
Thanks. I really would like it to be able to resolve those calls. But if there’s no way, then it is so. Your suggestion removes the symptoms (and disables this whole category of warnings from the code analyzer) - better than no solution at all.
Time passes by…
.
One could check the project at the following link. I don’t use it, but it seem to integrate some glue-code to the LibreOffice-API to python.
XSCRIPTCONTEXT global variable is only available within LO on open documents.
Debugging remote Python from IDES such as PyCharm or VS requires the setup of LO as a Service. In which case one needs to create an XSCRIPTCONTEXT adapter - see Python Basics - or to substitute XSCRIPTCONTEXT features importing UNO.
The latter is described in programming with Python scripts
Nonsense… from a »remote« interactive python-session via Jupyter-notebook:
import uno
from pythonscript import ScriptContext
PIPENAME = "your_pipename_here" # todo: start Lo in pipe-mode
args = f"pipe,name={PIPENAME}"
local = uno.getComponentContext()
resolver = local.ServiceManager.createInstance("com.sun.star.bridge.UnoUrlResolver")
ctx = resolver.resolve( f"uno:{args};"
"urp;"
"StarOffice.ComponentContext")
createUnoService = ctx.ServiceManager.createInstance
XSCRIPTCONTEXT = ScriptContext( ctx , None, None )
- after executing the code above there is »XSCRIPTCONTEXT« available!
- also the method »createUnoService« which is mostly the very same as its BASIC-counterpart
- also code-completition|suggestions after typing:
some_object.<tab>
ps. and YES, both PyCharm and VSCode provide Plugins for working directly with jupyter-notebook-files
Thanks Karolus
In your example XSCRIPTCONTEXT is instantiated - therefore still absent as a global variable - meaning it’s more or less substituted as suggested.
My prayer: Can you provide instructions as to how to setup an “interactive Python-session via Jupyter-notebook”?
in the context linux with LO from your distro linked to the distro-python-interpreter?:
in a terminal:
## DONT use pip with sudo!!!
pip install notebook --break-system-packages --upgrade
#create a dedicated folder for LO-related notebooks:
mkdir ~/.config/libreoffice/4/user/ipython
cd ~/.config/libreoffice/4/user/ipython
jupyter-notebook
The last command triggers a jupyter-dashboard in your webbrowser[1], from there you may continue by your own
[1] It runs completly local without calls to somewhere in the Internet
[2] in some distros you may need first:
sudo apt-get install libreoffice-script-provider-python
sudo apt-get install python3-zmq
…
in the context linux with Libreoffice via flatpak:
1. create python-venv for LO-Flatpak:
1.1 change to LO-Runtime, initialize the venv and install »notebook«:
… $ flatpak run --command=bash org.libreoffice.LibreOffice
… $ python -m venv /var/data/python
… $ cd /var/data/python
… $ source bin/activate
(python) … $ pip install pip --upgrade
(python) … $ pip install notebook --upgrade
2. … create a shellscript with content:
#!/usr/bin/bash
# URE_BOOTSTRAP……fundamentalrc is mandatory for uno-imports
# extra-PATH to »jupyter-notebook«
# extra-PYTHONPATH to the libreoffice/program install-directory
/usr/bin/flatpak run \
--env=URE_BOOTSTRAP=vnd.sun.star.pathname:/app/libreoffice/program/fundamentalrc \
--env=PATH=$PATH:/var/data/python/bin \
--env=PYTHONPATH=$PYTHONPATH:/app/libreoffice/program \
--command=/var/data/python/bin/jupyter-notebook \
--file-forwarding \
org.libreoffice.LibreOffice
make the script executeable
create a .desktop file with command /path/to/your/shellscript.sh
and a working-directory to your …flatpak-user-config/ipython
btw. @sberg : could you place such instructions into flatpak-Libreoffice-ReadMe ?
Happy !
Thank you!
YES…I lost some hairs to get the stuff straight forward in the flatpak-case
Its not »substituted« its the very same as the global »XSCRIPTCONTEXT« which is initialized inside a Libreoffice-process.
(read the source $(instdir)/program/pythonscript.py )
Nice finding, but I’m not sure where it would be best served from. The flatpak’s /app/libreoffice/readmes/README_en-US
(and its translations) is just the same as the vanilla LO version, and presumably obscure to find, and IMO at a wrong level for “programmability” findings like this anyway.
Maybe in the wiki-page, wich also lists other “setup”-information for IDE, and covers XSCRIPTCONTEXT
https://wiki.documentfoundation.org/Macros/Python_Basics#What’s_in_a_Macro