Python 3 class returns null when using contructor

422 views Asked by At

I started Libre-Office Calc with the following command:

$ libreoffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"

import uno

# Class so I don't have to do this crap over and over again...
class UnoStruct():
    localContext = None
    resolver = None
    ctx = None
    smgr = None
    desktop = None
    model = None
    def __init__(self ):
        print("BEGIN: constructor")
        # get the uno component context from the PyUNO runtime
        localContext = uno.getComponentContext()

        # create the UnoUrlResolver
        resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )

        # connect to the running office
        ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
        smgr = ctx.ServiceManager

        # get the central desktop object
        desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)

        # access the current writer document
        model = desktop.getCurrentComponent()

        print("END: constructor")

And then I call it with:

myUno = UnoStruct()
BEGIN: constructor
END: constructor

And attempt to get it with

active_sheet = myUno.model.CurrentController.ActiveSheet

AttributeError: 'NoneType' object has no attribute 'CurrentController'

and it appears that the model is None (null)

>>> active_sheet = myUno.model
>>> print( myUno.model )
None
>>> print( myUno )
<__main__.UnoStruct object at 0x7faea8e06748>

So what happened to it in the constructor? Shouldn't it still be there? I'm trying to avoid the boiler plate code.

2

There are 2 answers

3
Kirill Bulygin On BEST ANSWER

I would add to the answer of Barros that you declare localContext = None, resolver = None, etc. as class variables. So the modified code is this (if you need all these variables as instance variables):

class UnoStruct():
    def __init__(self ):
        # get the uno component context from the PyUNO runtime
        self.localContext = uno.getComponentContext()

        # create the UnoUrlResolver
        self.resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )

        # connect to the running office
        self.ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
        self.smgr = ctx.ServiceManager

        # get the central desktop object
        self.desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)

        # access the current writer document
        self.model = desktop.getCurrentComponent()
0
Rafael Barros On

You need to be explicit:

   self.model = desktop.getCurrentComponent()

The model variable inside __init__ is local to that method, and won't be attached to the instance self unless you assign it.

When you defined model right under the class, but outside the __init__ method you defined a class attribute, which will be in all instances of that class.

Without that, when you accessed myUno.model you'd be facing an AttributeError.