Create and Call Uno Service in Python

Java, C++, C#, Delphi, ??? - Using the UNO bridges

Create and Call Uno Service in Python

Postby saleem145 » Tue Dec 18, 2012 6:36 pm

Hello,

I have an Addin and an Addon. Addin is in C++ and the Addon is python.

In the addon I am trying the following code,

service = CreateUnoService("my_module.MyService1")
service.Initialize(a,b,c,d)

(The initialize routine is defined in service, and can be called succressfully from basic)

It doesn't work. The second line tanks....I am not good at python so I do not know how to print errors....

try:
service.Initialize(a,b,c,d)
except Exception as inst:
f = open('/tmp/err.log', 'w')
f.write('saleem\n')
f.write(type(inst))

It does create the file and put my name in it, but the next line does not do anything.....

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
saleem145
 
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Create and Call Uno Service in Python

Postby Villeroy » Tue Dec 18, 2012 6:41 pm

Code: Select all   Expand viewCollapse view
class Office:
    '''Frequently used methods in office context'''
    def __init__(self, ctx=uno.getComponentContext()):
        self.ctx = ctx
        self.smgr = self.ctx.ServiceManager
       
    def createUnoService(self, service):
        return self.smgr.createInstance(service)

    def getDesktop(self):
        return self.smgr.createInstanceWithContext("com.sun.star.frame.Desktop",self.ctx)

    def getCurrentComponent(self):
        return self.getDesktop().getCurrentComponent()

    def getCurrentFrame(self):
        return self.getDesktop().getCurrentFrame()

    def getCurrentComponentWindow(self):
        return self.getCurrentFrame().getComponentWindow()

    def getCurrentContainerWindow(self):
        return self.getCurrentFrame().getContainerWindow()

    def getCurrentController(self):
        return self.getCurrentFrame().getController()

    def callMRI(self,obj=None):
        '''Create an instance of MRI inspector and inspect the given object (default is selection)'''
        if not obj:
            obj = self.getCurrentController().getSelection()
        mri = self.createUnoService("mytools.Mri")
        mri.inspect(obj)
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28431
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Create and Call Uno Service in Python

Postby Jan_J » Tue Dec 18, 2012 6:43 pm

It does create the file and put my name in it, but the next line does not do anything.....
The second line should save type (i.e. name of class) of your exception. Make sure that the file is closed properly after writing. Either
Code: Select all   Expand viewCollapse view
f = open(.....)
f.write(...)
f.write(...)
f.close()
or (even better)
Code: Select all   Expand viewCollapse view
with open(.....) as f:
    f.write(...)
    f.write(...)
Besides, you are trying to write to the file something that is not a string... (type is not a string). Try rather
Code: Select all   Expand viewCollapse view
f.write(repr(type(inst)))
JJ ∙ http://forum.openoffice.org/pl/
LO (6.1|6.0) ∙ AOO 4.1.5 ∙ Python (3.7|2.7) ∙ Unicode 11 ∙ LᴬTeX 2ε ∙ XML ∙ Unix tools ∙ Linux (2.6|3.x) ∙ Fedora ∙ CentOS ∙ SUSE
Jan_J
 
Posts: 143
Joined: Wed Apr 29, 2009 1:42 pm
Location: Poland

Re: Create and Call Uno Service in Python

Postby saleem145 » Tue Dec 18, 2012 6:56 pm

service = CreateUnoService("my_module.MyService1")
service.Initialize(a,b,c,d)

The exception throw is of type --

exceptions.AttributeError

Any idea what that means?? What else could I add to the logging to shed further light on whats going wrong??

Thanks,

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
saleem145
 
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Create and Call Uno Service in Python

Postby Villeroy » Tue Dec 18, 2012 7:27 pm

How did you define def CreateUnoService?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28431
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to External Programs

Who is online

Users browsing this forum: No registered users and 2 guests