|
|
Python-UNO bridge- Translations- Introduction - Download - State - Tutorial -- PyUNO Installation -- PyUNO Bridge Modes -- More examples - UNO language binding -- UNO type mapping -- Implementing UNO objects -- Implementing UNO components -- Out parameter handling -- Exception handling -- current context support (since OOo 2.0.2) -- unohelper module -- logging (since OOo 2.0.2) -- Implementing UNO components with multiple source files (since OOo 2.4) - Dependencies - Bootstrapping in non-OOo environments - Replacing the python runtime - Regressiontests - External references - FAQ (read this FIRST when you have problems) - Known pyuno extensions for OOo - PyUNO needs you ! - Authors - License TranslationsFind here a shortened Spanish version of this document. IntroductionThe Python-UNO bridge allows to
You can find the most current version of this document from http://udk.openoffice.org/python/python-bridge.html DownloadYou can also download this documentation for offline work. Download pyuno-doc.zip ( less than 0.5 MB). StateThe Python-UNO bridge is feature complete, but has not been used extensively, so it may contain some bugs. It is now integrated in the OpenOffice.org source trees. (OpenOffice.org 1.0.x is not supported.) The documentation in its current state is targeted at developers who have already some experience with OpenOffice.org API and with some other programming language (Java/C++/StarBasic). It is recommended that you read that some background information from the developer manual before looking at the specifics of python. PyUNO tutorial for OpenOffice.orgThis tutorial shows, how the PyUNO bridge can be used to automate OpenOffice.org. This is not an OpenOffice.org tutorial, there is lots of resources available in the office development kit and the developer manual.PyUNO InstallationSince OpenOffice1.1, PyUNO is included in the default installation.PyUNO bridge modesPyUNO can be used in three different modes:
Examples
UNO Language bindingIn the following you find the full description about how UNO features are mapped to the python language.UNO Type mapping
Implementing UNO objectsOne may use python classes to implement UNO objects. Instances of a python class may then be passed as argument to UNO calls where anys or concrete interfaces are specified.To be an UNO object, a python class MUST implement the com.sun.star.lang.XTypeProvider interface by implementing two methods getTypes() and getImplementationId(), which inform the python-UNO bridge, which concrete UNO interfaces the python class implements. The getTypes() function defines, which interfaces are implemented by the class.
To make this easier, there exists a
Implementing Python UNO componentsThere exists a loader for python components. It allows to create instances of python classes not just within the python process but in every arbitrary UNO process including OpenOffice.org. The python loader loads the python runtime on demand if it is not already loaded and executes python code within the root python interpreter. If the reader is unfamiliar with the component registration process, it should visit the OpenOffice.org developer manual for a comprehensive explanation. The python loader currently supports the following protocols for incoming urls :
After the module has been imported, the python loader looks for a module-global variable
with the name
You can of course also use the
, but note, that this command creates a copy of the file (when the script changes,it must be redeployed using the above command). The component can be instantiated e.g. from OpenOffice.org Basic with
Out parameter handlingUNO out parameters are handled through the python multiple return value
feature. For
pure outparameters, a dummy
A python UNO object implements such a method the following way:
This also emphasizes, that out-parameters are quite close to multiple return values (though the semantic association of a inout parameter gets lost). However, note that
Exception handlingThe Python-UNO bridge uses the common Python exception handling mechanism. For every UNO exception, a concrete exception class is generated on the fly (see above type mapping table for an explanation how to do this).Example for catching
Example for throwing
current context supportNEW SINCE OOo 2.0.2pyuno supports the uno current context concept. There exist the functions uno.getCurrentContext() und uno.setCurrentContext( newContext ). Furthermore, there exists a class unohelper.CurrentContext. The constructor accepts a hashmap with name/value pairs and the former context for delegation. Usage pattern:
unohelper moduleThe unohelper.py module contains some extra functions/classes, which are nice to use with pyuno, but not mandatory. This paragraph lists some of the unohelper.py features.
LoggingNEW SINCE OOo 2.0.2The pyuno bridge can now log every call bridged between python and uno. This may be a useful help when you need to debug or profile your code. There are two environment variables, which activate logging:
(Since OOo 2.4) Implementing UNO components with multiple source filesBefore the pythonloader tries to load a new python unocomponent, it looks beside the uno component for a file with the name pythonpath.zip or a directory named pythonpath . If it exists, it puts it into sys.path (if it is not already in there) and then tries to load the given component. Note, that the unocomponent file itself is not within PYTHONPATH and thus cannot be reimported by other modules.This now means that python uno components can be implemented with an arbirtrary number of python source files which can be deployed/undeployed via the uno package mechanism. It also means, that you can now use the unohelper.Base implementation even if you have defined your own interface types (by lazy loading the new types so that they don't get used during registration process).
Have a look at the sample calc addin to see how it works. Note that there are some negative side effects:
DependenciesThis chapter is most interesting for people who want to use the Python-UNO bridge independently from OpenOffice.org. Unlike the Java or C++ UNO binding, the python UNO binding is not self contained. It requires the C++ UNO binding and additional scripting components. These additional components currently live in the shared libraries typeconverter.uno, invocation.uno, corereflection.uno, introspection.uno, invocadapt.uno, proxyfac.uno, pythonloader.uno (on windows typeconverter.uno.dll,...; unix typeconverter.uno.so,...). Often, the components for setting up an interprocess connection are also required. These are uuresolver.uno, connector.uno, remotebridge.uno, bridgefac.uno shared libraries. The path environment variables ( LD_LIBRARY_PATH on Unix, PATH on Windows) must point to a directory, where the core UNO libraries, the above listed components and the pyuno shared library is located. (On Unix, there exists two files: libpyuno.so containing the code and a pyuno.so which is needed for importing a native python module). Additionally, the python module uno.py, unohelper.py and pythonloader.py must be located in a directory, which is listed in the PYTHONPATH environment variable. Bootstrapping pyuno from the python executableWhen the uno module gets first imported from an arbitrary python script, it must bootstrap a properly prepared UNO component context.
As the python programmer can't (and probably doesn't want to) give parameters while importing a module, the python-uno binding uses the pyuno[rc|.ini] file located beside the pyuno shared library to bootstrap the UNO context (see uno bootstrap variable concept). The bootstrap variables UNO_SERVICES must point to a registry file where the components, given above, were registered. PYUNOLIBDIR is a special bootstrap variable, which contains the path to the currently used pyuno shared library. Example:
If the above preconditions are fulfilled, the script can simply be started with $ python myscript.py
Sometimes it is preferable to mention the librarynames of the desired
components directly within the script instead of preparing a registry
(however note that the above mentioned bootstrap components always needs
to be registered in a registry).
This can be achieved by using the function
Example:
Replacing the python runtime with your system's python installationOOo by default ships with the Python-2.2.2 core runtime. This is fine for most users, but some hackers (or Linux distributors) may want to replace the runtime with the python system's installation, which may contain more optional packages that you want to use in python.The replacement is a little complicated however you just need an installed python and office. WindowsOn windows, you can only use python-2.2. If you want to use python-2.3, you must recompile the pyuno module with python-2.3 (see below)
LinuxOn Linux, you can use both use python-2.2 or python 2.3, but when using the latter, you get a warning on stderr (informing you about the version mismatch) when starting python or the office, to avoid the warning, you need to rebuild pyuno with python-2.3 (see below), however I haven't noticed any difficulties because of the version mismatch.
TestingYou should now be able to start system's python and type 'import uno'. If this works fine, use pkgchk to deploy your script, for example the above swritercomp.py in OpenOffice.org (Tip: add a print sys.version to it). If this works fine, python should work well in OpenOffice.org itself.I did only some rudimentary tests, but I didn't notice any significant problems. Let us know, if you have some. Note that the Bibus project uses an extended python 2.2.2 with the wxPython/wxWindows extension for the GUI. Rebuilding pyunoYou'll need to install OOo buildenv to do this. In the shell, replace the the PYTHONPATH variable properly, e.g.setenv PYTHONPATH /usr/local/lib/python2.3:.:/usr/local/lib/python2.3/lib-dynloadMake sure, that system's python is in the PATH variable. Build the office (or at least all components, pyuno depends on) but leave out the python module. In the pyuno module itself, you should only build pyuno/source/module, pyuno/source/loader and pyuno/test, leave out the zipcore directory. You'll need to modify the pyuno/source/module/makefile.mk and pyuno/source/loader/makefile.mk. Replace the CFLAGS+= line with CFLAGS+=-I/usr/local/include/python2.3 and all occurrences of -lpython with -lpython2.3. When the test runs fine, you can now replace pyuno.so, libpyuno.so and pythonloader.uno.so in the office with your rebuilt version. RegressiontestsIn case you have modified python or pyuno, you should at least run the following regression tests.
External references
Frequently Asked Questions
Known PyUNO extensions for OpenOffice.orgPackages listed here can be taken as a demo of what is possible with pyuno. Let me know, if you are aware of other extensions using pyuno.
PyUNO needs YOU !PyUNO is currently (and will be in future) maintained by myself in my spare time. My main aim for pyuno is to provide a good integration of OpenOffice.org's component model into python. Some guys on dev@udk.openoffice.org demand to have a more feature rich python runtime in OpenOffice.org and an integration with the system's python installation. While this is an understandable demand, it is not one of my favourite topics to work on and it involves quite a lot of work. As I also spend time on the creation of a postgresql driver for OpenOffice.org, there is simply no time left for this task. So I am looking for other volunteers such as you to fill this gap. In case you are interested, let me know via the dev@udk.openoffice.org mailing list or drop me a mail privately. I currently see the following main tasks
AuthorsThe UNO python bridge was initially created by Ralph Thomas and is now maintained by Joerg Budischewski. Christian Zagrodnick sent in some very useful patches. Many unmentioned porters made it possible to have pyuno on all platforms supported by OOo. Last updated $Date: 2008/04/03 06:40:42 $ Please use the dev@udk.openoffice.org mailing list for further questions. LicenseThis document is available under PDL (Public Documentation License). |




