Hentning af sessionsinformation

Behanding af LibreOffice-brugerprofil og -systemstier til delte moduler kan udføres i Python- eller i Basic-sprog. BeanShell-, Java-, JavaScript- ogPython-scripts placeringer kan udledes fra disse oplysninger.

Eksempler:

Med Python-skallen.

>>> from <the_module> import Session

>>> print(Session.SharedPythonScripts()) # statisk metode

>>> print(Session().UserName) # objektegenskab

>>> input(Session().UserProfile) # objektegenskab

Fra menuen Funktioner – Makroer – Kør makro....


        from <the_module> import Session
            
        def demo_session():
            import screen_io as ui
            ui.MsgBox(Session.Share(),title='Installation Share')  # statisk metode
            ui.Print(Session.SharedPythonScripts())  # statisk metode
            s = Session()  # opret session
            ui.MsgBox(s.UserName,title='Hello')  # objektegenskab
            ui.Print(s.UserPythonScripts)  # objektegenskab
            
        g_exportedScripts = (demo_session,)  # fællesmakroer
    

Med LibreOffice-Basic.


        Sub Session_example()
            Dim s As New Session ' instance of Session class
            Print "Placering af delte scripts", s.SharedScripts
            MsgBox s.UserName,,"Hallo"
            Print s.UserScripts, Chr(13), s.UserPythonScripts
        End Sub ' Session_example
    

Med COM/OLE og Visual Basic script-sprog.


        ' Tjenestehåndteringen er altid udgangspunktet.
        ' Hvis der ikke kører et office-program, opstartes et office-program.
        Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
        ' PathSubstitution-tjenesten viser information som udledes
        ' <UserProfile|Share>/Scripts/python-placeringer fra
        Set obj = sm.createInstance("com.sun.star.util.PathSubstitution")
            
        MsgBox CreateObject("WScript.Network").UserName,, "Hello"
        user = obj.getSubstituteVariableValue("$(user)")
        MsgBox user & "/Scripts",, "User scripts location"
        libO = Replace(obj.getSubstituteVariableValue("$(inst)"), "program/..", "Share")
        MsgBox libO & "/Scripts",, "Shared scripts location"
    

Python-sessionsklasse:


        import getpass, os, os.path, uno
            
        class Session():
            @staticmethod
            def substitute(var_name):
                ctx = uno.getComponentContext()
                ps = ctx.getServiceManager().createInstanceWithContext(
                    'com.sun.star.util.PathSubstitution', ctx)
                return ps.getSubstituteVariableValue(var_name)
            @staticmethod
            def Share():
                inst = uno.fileUrlToSystemPath(Session.substitute("$(prog)"))
                return os.path.normpath(inst.replace('program', "Share"))
            @staticmethod
            def SharedScripts():
                return ''.join([Session.Share(), os.sep, "Scripts"])
            @staticmethod
            def SharedPythonScripts():
                return ''.join([Session.SharedScripts(), os.sep, 'python'])
            @property  # alternativ til '$(username)'-variabel
            def UserName(self): return getpass.getuser()
            @property
            def UserProfile(self):
                return uno.fileUrlToSystemPath(Session.substitute("$(user)"))
            @property
            def UserScripts(self):
                return ''.join([self.UserProfile, os.sep, 'Scripts'])
            @property
            def UserPythonScripts(self):
                return ''.join([self.UserScripts, os.sep, "python"])
    
note

I modsætning til Basic udføres normalisering af stinavne i Python i sessionsklassen.


LibreOffice-Basic sessionsklasse:


        Option Explicit
        Option Compatible
        Option ClassModule
            
        Private _ps As Object ' Privat medlem
            
        Private Sub Class_Initialize()
            GlobalScope.BasicLibraries.LoadLibrary("Tools")
            Set _ps = CreateUnoService("com.sun.star.util.PathSubstitution")
        End Sub ' Konstruktør
            
        Private Sub Class_Terminate()
            _ps = Nothing
        End Sub ' Destruktør
            
        Public Property Get SharedScripts() As String
            Dim inst As String, shr As String
            inst = ConvertFromURL(_ps.getSubstituteVariableValue("$(prog)"))
            shr = Tools.Strings.ReplaceString(inst,"Share","program")
            SharedScripts = shr & GetPathSeparator() &"Scripts"
        End Property ' Session.sharedScripts
            
        Public Property Get SharedPythonScripts() As String
            sharedPythonScripts = sharedScripts() & GetPathSeparator() &"python"
        End Property ' Session.sharedPythonScripts
            
        Public Property Get UserName() As String ' Brugerkonto-navn
            userName = _ps.getSubstituteVariableValue("$(username)")
        End Property ' Session.userName
            
        Public Property Get UserProfile() As String ' Brugerprofil-systemsti
            userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
        End Property ' Session.userProfile
            
        Public Property Get UserScripts() As String ' Brugerscripts systemsti
            userScripts = userProfile() & GetPathSeparator() &"Scripts"
        End Property ' Session.userScripts
            
        Public Property Get UserPythonScripts() As String ' Bruger Python-scripts systemsti
            userPythonScripts = userScripts() & GetPathSeparator() &"python"
        End Property ' Session.userPythonScripts
    

Støt os venligst!