Page 1 of 1

[Solved] Python find extension path in user profile

Posted: Mon Oct 05, 2015 5:25 pm
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 ! 

Re: Install special files within an extension

Posted: Mon Oct 05, 2015 8:04 pm
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?

Re: Python find special path files within an extension

Posted: Mon Oct 05, 2015 9:34 pm
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)

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

Posted: Tue Oct 06, 2015 5:41 pm
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'