Uzyskiwanie informacji o sesji

Obliczanie ścieżek plików systemowych profilu użytkownika i udostępnionych modułów Collabora Office można wykonać za pomocą języków Python lub Basic. Na podstawie tych informacji można określić lokalizacje skryptów BeanShell, Java, JavaScript i Python.

Przykłady:

Za pomocą powłoki Pythona.

>>> from <the_module> import Session

>>> print(Session.SharedPythonScripts()) # metoda statyczna

>>> print(Session().UserName) # właściwość obiektu

>>> input(Session().UserProfile) # właściwość obiektu

Z menu Narzędzia - Makra - Uruchom makro….

from <the_module> import Session

def demo_session():
    import screen_io as ui
    ui.MsgBox(Session.Share(),title='Installation Share')  # metoda statyczna
    ui.Print(Session.SharedPythonScripts())  # metoda statyczna
    s = Session()  # tworzenie instancji
    ui.MsgBox(s.UserName,title='Hello')  # właściwość obiektu
    ui.Print(s.UserPythonScripts)  # właściwość obiektu

g_exportedScripts = (demo_session,)  # publiczne makra

Za pomocą Collabora Office Basic.

Sub Session_example()
    Dim s As New Session ' instance of Session class
    Print "Lokalizacja udostępnionych skryptów:", s.SharedScripts
    MsgBox s.UserName,,"Witaj"
    Print s.UserScripts, Chr(13), s.UserPythonScripts
End Sub ' Session_example

Korzystanie z COM/OLE i języka skryptowego Visual Basic

' Menedżer usługi jest zawsze punktem wejścia
' Jeśli nie ma działającego pakietu, zostanie uruchomiony
Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
' Usługa PathSubstitution wyświetla informacje do wnioskowania
' lokalizacji <ProfilUżytkownika|Udostępniony>/Scripts/python z
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"

Klasa Session Pythona:

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  # alternatywa dla zmiennej '$(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

W przeciwieństwie do Basic, normalizacja nazw ścieżek jest wykonywana w Pythonie wewnątrz klasy Session.


Klasa Session języka Basic Collabora Office:

Option Explicit
Option Compatible
Option ClassModule

Private _ps As Object ' Członek prywatny

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 ' Nazwa konta użytkownika
    userName = _ps.getSubstituteVariableValue("$(username)")
End Property ' Session.userName

Public Property Get UserProfile() As String ' Ścieżka systemowa profilu użytkownika
    userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
End Property ' Session.userProfile

Public Property Get UserScripts() As String ' Ścieżka systemowa skryptów użytkownika
    userScripts = userProfile() & GetPathSeparator() &"Scripts"
End Property ' Session.userScripts

Public Property Get UserPythonScripts() As String ' Ścieżka systemowa skryptów użytkownika Pythona
    userPythonScripts = userScripts() & GetPathSeparator() &"python"
End Property ' Session.userPythonScripts

Prosimy o wsparcie!