Page 1 of 1

[Solved] Python crashing on loading BASIC script

Posted: Wed Aug 05, 2015 2:56 pm
by sng
Hi all,

I am trying to load (and execute) a BASIC Sub from withing a python program.

I should tell you that I know absolutely nothing about python... just following examples :oops:

I am following this example : UNO component packaging modified as follows:

Code: Select all

import uno
import unohelper
from com.sun.star.task import XJobExecutor
from com.sun.star.uno import RuntimeException
 
class Wavelet( unohelper.Base, XJobExecutor ):
    def __init__( self, ctx ):
        self.ctx = ctx
 
    def crS(self,serviceName):
        """ gets a service from Uno """
        sm = uno.getComponentContext().ServiceManager
        result = sm.createInstance(serviceName)
        if not result:
            raise RuntimeException("\nService not available :\n" + serviceName, uno.getComponentContext())
        return result

    def trigger( self, args ):
        desktop = self.ctx.ServiceManager.createInstanceWithContext(
            "com.sun.star.frame.Desktop", self.ctx )
 
        doc = desktop.getCurrentComponent()
 
        try:
            search = doc.createSearchDescriptor()
            search.SearchRegularExpression = True
            search.SearchString = "\\<(k|s|v|z|o|u|i|a) "
 
            found = doc.findFirst( search )
            while found:
                found.String = found.String.replace( " ", u"\xa0" )
                found = doc.findNext( found.End, search)
 
        except:
            pass

        print("===> 1")
        _mspf = self.crS("com.sun.star.script.provider.MasterScriptProviderFactory")
        print("===> 2")
        """ scriptPro = _mspf.createScriptProvider("") """
        try:
            """ I found out that this may not always work... """
            scriptPro = doc.getScriptProvider()
        except:
            print("    **** Retrying ****")
            self.trigger( () )
            return

        if scriptPro is None:
            print("I am going to crash!!!")
        print("===> 3")
        try:
            Xscript = scriptPro.getScript("vnd.sun.star.script:AncientGreek.keybLayout.auto?language=Basic&location=application") 
            print("===> 4")
            if not Xscript:
                print("No script")
            a = None
            b = None
            c = None
            print("===> 5")
            Xscript.invoke( ( a, ), b, c )
            print("===> 6")
        except:
            print("===> 7")
            pass

g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(
        Wavelet,
        "name.vojta.openoffice.Wavelet",
        ("com.sun.star.task.Job",),)

if __name__ == "__main__":
    import os
 
    # Start OpenOffice.org, listen for connections and open testing document
    os.system( "/usr/lib/libreoffice/program/soffice '-accept=socket,host=localhost,port=2002;urp;' -writer ./WaveletTest.odt &" )
 
    # Get local context info
    localContext = uno.getComponentContext()
    resolver = localContext.ServiceManager.createInstanceWithContext(
        "com.sun.star.bridge.UnoUrlResolver", localContext )
 
    ctx = None
 
    # Wait until the OO.o starts and connection is established
    while ctx == None:
        try:
            ctx = resolver.resolve(
                "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
        except:
            pass
 
    print("===> Triggering...")
    # Trigger our job
    wavelet = Wavelet( ctx )
    wavelet.trigger( () )


The part that I have added starts at the line

Code: Select all

 
print("===> 1")



I run the python program with the command (linux):

Code: Select all

 
python ./Wavelet.py


And this is what I get:

Code: Select all

 
Warning: -accept=socket,host=localhost,port=2002;urp; is deprecated.  Use --accept=socket,host=localhost,port=2002;urp; instead.
Warning: -writer is deprecated.  Use --writer instead.
===> Triggering...
===> 1
===> 2
===> 3
Σφάλμα κατάτμησης (segmentation fault)


The line that crashes is

Code: Select all

 
Xscript = scriptPro.getScript("vnd.sun.star.script:AncientGreek.keybLayout.auto?language=Basic&location=application") 


The BASIC Sub that I try to load is part of an extension and resiedes under "My Macros"
Library: AncientGreek
Module: keybLayout
Sub: auto

and it actually shows a msgbox:

Code: Select all

 
Sub auto
    msgbox("Running...")
End Sub


What am i doing wrong?
Can anyone help?

Spiros

Re: Python crashing on loading BASIC script

Posted: Thu Aug 06, 2015 10:43 am
by sng
I have finally got it to work, based on the answer of B Marcelly on this old post: [SOLVED]Call Calc doc. Basic Sub from getScript impossible?
You must get the script provider from the document, not from the MasterScriptProviderFactory.
So making the following change did the trick:

Code: Select all

        """scriptPro = _mspf.createScriptProvider("")"""
        scriptPro = doc.getScriptProvider()
Marking as Solved and editing the code...

Spiros