Difference between revisions of "Example of Service in Python"

From Apache OpenOffice Wiki
Jump to: navigation, search
 
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
at fist, PyUNO bridge must be installed. E.g., I need to launch ping and see what errorcode it returns:
+
{{Extensions}}
  
<pre>[peet@mysql uno_packages]$ pwd
+
at first, PyUNO bridge must be installed. E.g., I need to launch ping and see what errorcode it returns:
/home/peet/OpenOffice.org1.1/user/uno_packages
+
 
[peet@mysql uno_packages]$ cat >pinger.py <<END_OF_CAT
+
<syntaxhighlight lang="python">
 
import os  
 
import os  
 
import uno  
 
import uno  
Line 40: Line 40:
 
g_ImplementationHelper = unohelper.ImplementationHelper()  
 
g_ImplementationHelper = unohelper.ImplementationHelper()  
 
g_ImplementationHelper.addImplementation( Pinger, "org.openoffice.comp.pyuno.Pinger", ("com.sun.star.task.Job",),)  
 
g_ImplementationHelper.addImplementation( Pinger, "org.openoffice.comp.pyuno.Pinger", ("com.sun.star.task.Job",),)  
END_OF_CAT
+
 
[peet@mysql uno_packages]$ /usr/lib/openoffice.org.rc2/program/pkgchk
+
</syntaxhighlight>
[peet@mysql uno_packages]$ export PYTHONPATH=/usr/lib/openoffice.org.rc2/program; ~/OpenOffice.org1.1/soffice</pre>
+
  
 
Since pkgchk I can use org.openoffice.comp.pyuno.Pinger e.g. in basic:
 
Since pkgchk I can use org.openoffice.comp.pyuno.Pinger e.g. in basic:
 +
<syntaxhighlight lang="oobas">
 
   dim worm as object  
 
   dim worm as object  
 
   worm = CreateUnoService("org.openoffice.comp.pyuno.Pinger")
 
   worm = CreateUnoService("org.openoffice.comp.pyuno.Pinger")
 +
</syntaxhighlight>
 +
 +
For 2.x the pkgchk has been replaced wiht unopkg which usually has a better compilation.
 +
 +
==Explanation of the code==
 +
First we need to import all the modules we need.
 +
<syntaxhighlight lang="python">
 +
import os
 +
import uno
 +
import unohelper</syntaxhighlight>
 +
 +
Second we will load the <code>XJob</code> within the <code>com.sun.star.task</code> this let us avoid being able to load OOo in ''listen mode''.
 +
 +
<syntaxhighlight lang="python">
 +
from com.sun.star.task import XJob </syntaxhighlight>
 +
 +
[[Category:Python]]
 +
 +
[[Category:Education]]

Latest revision as of 15:21, 14 March 2021

OOo Extensions project

Please view the wiki usage guidelines
before contributing.

Categories:

Pages:

Extensions on the main site

Extensions in other languages:
ES - FR - IT - JA - NL - OC -


at first, PyUNO bridge must be installed. E.g., I need to launch ping and see what errorcode it returns:

import os 
import uno 
import unohelper 
 
from com.sun.star.task import XJob 
 
class Pinger( unohelper.Base, XJob ): 
    def __init__( self, ctx ): 
        self.ctx = ctx 
 
    def execute( self, args ): 
        struct  = uno.createUnoStruct( "com.sun.star.beans.PropertyValue" ) 
        status  = -1 
        output  = "false" 
        pipeout = "Something goes wrong?" 
 
        for struct in args: 
                if struct.Name == 'command': 
                        pipe    = os.popen('{ ' + struct.Value + '; } 2>&1', 'r') 
                        pipeout = pipe.read() 
                        status  = pipe.close() 
                elif struct.Name == 'print': 
                        output  = struct.Value 
 
        if output != "false": 
                desktop = self.ctx.ServiceManager.createInstanceWithContext( "com.sun.star.frame.Desktop", self.ctx ) 
                model = desktop.getCurrentComponent() 
                text = model.Text 
                cursor = text.createTextCursor() 
                text.insertString( cursor, pipeout, 0 ) 
 
        return status 
 
 
g_ImplementationHelper = unohelper.ImplementationHelper() 
g_ImplementationHelper.addImplementation( Pinger, "org.openoffice.comp.pyuno.Pinger", ("com.sun.star.task.Job",),)

Since pkgchk I can use org.openoffice.comp.pyuno.Pinger e.g. in basic:

   dim worm as object 
   worm = CreateUnoService("org.openoffice.comp.pyuno.Pinger")

For 2.x the pkgchk has been replaced wiht unopkg which usually has a better compilation.

Explanation of the code

First we need to import all the modules we need.

import os 
import uno 
import unohelper

Second we will load the XJob within the com.sun.star.task this let us avoid being able to load OOo in listen mode.

 
from com.sun.star.task import XJob
Personal tools