Pridobivanje informacij o seji

Izračunavanje poti sistemskih datotek uporabniškega profila in skupnih modulov Collabora Office je mogoče izvesti s programskima jezikoma Python in Basic. Iz teh informacij je moč razbrati mesta skriptov BeanShell, Java, JavaScript in Python.

Primeri:

Z lupino Python.

>>> from <the_module> import Session

>>> print(Session.SharedPythonScripts()) # statična metoda

>>> print(Session().UserName) # lastnost predmeta

>>> input(Session().UserProfile) # lastnost predmeta

Izberite Orodja – Makri – Zaženi makro.

from <the_module> import Session

def demo_session():
    import screen_io as ui
    ui.MsgBox(Session.Share(),title=‘Namestitvena mapa za skupno rabo’)  # statična metoda
    ui.Print(Session.SharedPythonScripts())  # statična metoda
    s = Session()  # ustvarjanje instance
    ui.MsgBox(s.UserName,title=‘Pozdravljeni’)  # lastnost predmeta
    ui.Print(s.UserPythonScripts)  # lastnost predmeta

g_exportedScripts = (demo_session,)  # javni makri

Z Collabora Office Basicom.

Sub Session_example()
    Dim s As New Session ' instance of Session class
    Print "Mesto skriptov v skupni rabi:", s.SharedScripts
    MsgBox s.UserName,,"Pozdravljeni"
    Print s.UserScripts, Chr(13), s.UserPythonScripts
End Sub ' Session_example

Z uporabo COM/OLE in skriptnega jezika Visual Basic.

' Upravitelj storitev je vedno vstopna točka
' Če pisarniški paket ni zagnan, se s tem zažene
Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
' Storitev PathSubstitution prikaže informacije, iz katerih pridobiva
' mesta iz <UporabniškiProfil|Share>/Scripts/python
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"

Razred Pythona Session:

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  # alternativa za spremenljivko '$(username)'
    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

Za razliko od Basica se normalizacija imena poti izvede s Pythonom v okviru razreda Session.


Razred Session Collabora Office Basic:

Option Explicit
Option Compatible
Option ClassModule

Private _ps As Object ' Zasebni član

Private Sub Class_Initialize()
    GlobalScope.BasicLibraries.LoadLibrary("Tools")
    Set _ps = CreateUnoService("com.sun.star.util.PathSubstitution")
End Sub ' Konstruktor

Private Sub Class_Terminate()
    _ps = Nothing
End Sub ' Destruktor

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 ' Ime uporabniškega računa
    userName = _ps.getSubstituteVariableValue("$(username)")
End Property ' Session.userName

Public Property Get UserProfile() As String ' Sistemska pot do uporabniškega profila
    userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
End Property ' Session.userProfile

Public Property Get UserScripts() As String ' Sistemska pot do uporabniških skriptov
    userScripts = userProfile() & GetPathSeparator() &"Scripts"
End Property ' Session.userScripts

Public Property Get UserPythonScripts() As String ' Sistemska pot do uporabniških skriptov v Pythonu
    userPythonScripts = userScripts() & GetPathSeparator() &"python"
End Property ' Session.userPythonScripts

Podprite nas!