[Solved] Python find extension path in user profile

Discussions about using 3rd party extension with OpenOffice.org
Post Reply
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

[Solved] Python find extension path in user profile

Post by Bastien »

Hi,

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 6.2.3.2 on Ubuntu 19.04
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: Install special files within an extension

Post by Bastien »

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

Code: Select all

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"
 		else
 		ext_location = module & ".py$" & method & URL_Args 
	endif
	get_py_url = URL_Main & ext_location
End function
Can someone help me translating this in Python?
LibreOffice 6.2.3.2 on Ubuntu 19.04
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: Python find special path files within an extension

Post by Bastien »

Here is the python code to get an extension path in the user profile.

Code: Select all

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)
Last edited by Bastien on Tue Feb 18, 2020 10:44 am, edited 1 time in total.
LibreOffice 6.2.3.2 on Ubuntu 19.04
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Post by Villeroy »

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

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")
    else:
        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,):
        try:
            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
        except:
            pass
            
    def getHelpURL(self,sHelpFile):
        """return complete URL if self.HelpPath/sHelpFile exists, else ''"""
        sResult = ''
        try:
            if self.HelpPath.endswith('/'):
                sURL = self.HelpPath + sHelpFile
            else:
                sURL = self.HelpPath +'/'+ sHelpFile

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

        except:
            pass
        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'''
        try:
            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()
                oFrame.activate()
                oFrame.getContainerWindow().toFront()
        except:
            pass

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 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply