Example of Service in Python

From Apache OpenOffice Wiki
Revision as of 18:43, 6 December 2006 by Jza (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

[peet@mysql uno_packages]$ pwd 
/home/peet/OpenOffice.org1.1/user/uno_packages 
[peet@mysql uno_packages]$ cat >pinger.py <<END_OF_CAT 
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",),) 
END_OF_CAT 
[peet@mysql uno_packages]$ /usr/lib/openoffice.org.rc2/program/pkgchk 
[peet@mysql uno_packages]$ export PYTHONPATH=/usr/lib/openoffice.org.rc2/program; ~/OpenOffice.org1.1/soffice

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")
Personal tools