[Solved] Python find extension path in user profile

Discussions about using 3rd party extension with OpenOffice.org

[Solved] Python find extension path in user profile

Postby Bastien » Mon Oct 05, 2015 5:25 pm


I've done a bunch of python scripts that are distributed in an extension made with ExtensionCompiler. Now it would be fantastic to have included in this extension a template used by these scripts.

How can I add some files (*.ott) in the extension? Is there a way to do that?

[EDIT] I found out that putting files in the extension archived allows these files to be present in the user profile. Now, my problem is rather being able to find this path !
Last edited by Bastien on Mon Oct 05, 2015 9:36 pm, edited 2 times in total.
LibreOffice on Ubuntu 18.04
Posts: 38
Joined: Mon May 12, 2014 2:45 pm

Re: Install special files within an extension

Postby Bastien » Mon Oct 05, 2015 8:04 pm

OK, I found how to get my path with basic.

Code: Select all   Expand viewCollapse view
function get_py_url(module as string, method as string)
dim ext_location as String
dim pip as object
   pip = GetDefaultContext.getByName("/singletons/com.sun.star.deployment.PackageInformationProvider")
   ext_location = pip.getpackagelocation(extension_name)
   if len(ext_location) then
      ext_location = FileNameOutOfPath(ext_location) & "/python/" & module & ".py$" & method & URL_Args & ":uno_packages"
      ext_location = module & ".py$" & method & URL_Args
   get_py_url = URL_Main & ext_location
End function

Can someone help me translating this in Python?
LibreOffice on Ubuntu 18.04
Posts: 38
Joined: Mon May 12, 2014 2:45 pm

Re: Python find special path files within an extension

Postby Bastien » Mon Oct 05, 2015 9:34 pm

Shame on me ! It was so easy ! Here is the python code to get an extension path in the user profile.

Code: Select all   Expand viewCollapse view
def get_extension_path(xscriptcontext):
   """MODULE = string like "module_name.extension_name"""
    ctx = xscriptcontext.getComponentContext()
    srv = ctx.getByName("/singletons/com.sun.star.deployment.PackageInformationProvider")
    return srv.getPackageLocation(MODULE)
LibreOffice on Ubuntu 18.04
Posts: 38
Joined: Mon May 12, 2014 2:45 pm

Re: [Solved] Python find extension path in user profile

Postby Villeroy » Tue Oct 06, 2015 5:41 pm

The code box contains the relevant code of my SpecialCells extension which opens the help file of that extension which is a Writer document.
Code: Select all   Expand viewCollapse view
def getConfigurationRoot(ctx,sNodePath):
    sProvider = "com.sun.star.configuration.ConfigurationProvider"
    sAccess   = "com.sun.star.configuration.ConfigurationAccess"
    aConfigProvider = createUnoService(ctx,sProvider)
    prp = uno.createUnoStruct("com.sun.star.beans.PropertyValue")
    prp.Name = "nodepath"
    prp.Value = sNodePath
    result = aConfigProvider.createInstanceWithArguments(sAccess, (prp,))
    return result

def getMacroExpander(ctx):
    btx = ctx.getServiceManager().createInstanceWithContext("com.sun.star.configuration.bootstrap.BootstrapContext", ctx)
    if btx:
        return btx.getValueByName("/singletons/com.sun.star.util.theMacroExpander")
        return ctx.getValueByName("/singletons/com.sun.star.util.theMacroExpander")
class ConfigProvider(XJobExecutor,unohelper.Base):
    RootNode = 'name.AndreasSaeger.SpecialCells.Localization/LocalizedStrings'
    HelpDoc = 'SpecialCells.sxw'
    Bookmark = '#_TOP'
    def __init__(self,ctx,):
            self.FileAccess = createUnoService(ctx, 'com.sun.star.ucb.SimpleFileAccess')
            self.Desktop = getDesktop(ctx)
            self.ConfRoot = getConfigurationRoot(ctx,ConfigProvider.RootNode)
            # %origin%/../help/SpecialCells.sxw
            # vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/SHZUe5_/SpecialCells.0.5.62.uno.zip/registry/../help/en/SpecialCells.sxw
            EXP = getMacroExpander(ctx)
            sMacroString = self.ConfRoot.getByName('HelpPath')
            sPrefix = "vnd.sun.star.expand:"
            sURL = EXP.expandMacros(sMacroString)
            n = len(sPrefix)
            if sURL.startswith(sPrefix):
                sURL = sURL[n:]
            self.HelpPath = sURL
    def getHelpURL(self,sHelpFile):
        """return complete URL if self.HelpPath/sHelpFile exists, else ''"""
        sResult = ''
            if self.HelpPath.endswith('/'):
                sURL = self.HelpPath + sHelpFile
                sURL = self.HelpPath +'/'+ sHelpFile

            if self.FileAccess.exists(sURL):
                sResult = sURL

        return sResult

    def trigger(self,arg=''):
        if self.HelpPath:
            sURL = self.getHelpURL(ConfigProvider.HelpDoc)
            self.loadHelpDocument(sURL + ConfigProvider.Bookmark)
    def loadHelpDocument(self,sURL,):
        '''Load writer document read-only'''
            prp = uno.createUnoStruct("com.sun.star.beans.PropertyValue")
            prp.Name = "ReadOnly"
            prp.Value = True
            oDoc = self.Desktop.loadComponentFromURL(sURL,"_default",0,(prp,))
            if oDoc:
                oFrame = oDoc.getCurrentController().getFrame()

if __name__ == "__main__":
    localContext = uno.getComponentContext()
    resolver = localContext.ServiceManager.createInstanceWithContext(
        "com.sun.star.bridge.UnoUrlResolver", localContext )
    ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
    oConfProvider = ConfigProvider(ctx,)
    p = oConfProvider.HelpPath
    print p
    fileURL = oConfProvider.getHelpURL('SpecialCells.sxw')
    print fileURL   
    # open the help file at bookmark #_DialogCellFormatRanges
    oConfProvider.trigger(fileURL +'#_DialogCellFormatRanges')

I used the __main__ routine for testing against a listening office instance. Class ConfigProvider gets initialized with the right component context of the listening office suite and determines the full path of the help file within the installed extension from its registered root node 'name.AndreasSaeger.SpecialCells.Localization/LocalizedStrings'
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, OpenOffice 4.x & LibreOffice 6.x
User avatar
Posts: 26417
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Return to Extensions

Who is online

Users browsing this forum: No registered users and 2 guests