Ottenere informazioni sulla sessione

L'elaborazione del profilo utente e dei percorsi dei file di sistema condivisi di Collabora Office si possono eseguire con il linguaggio Python o Basic. Le posizioni degli script BeanShell, Java, JavaScript e Python si possono derivare da queste informazioni.

Esempi:

Con shell Python.

>>> from <the_module> import Session

>>> print(Session.SharedPythonScripts()) # metodo statico

>>> print(Session().UserName) # proprietà dell'oggetto

>>> input(Session().UserProfile) # proprietà dell'oggetto

Dal menu Strumenti – Macro - Esegui macro.

from <the_module> import Session

def demo_session():
    import screen_io as ui
    ui.MsgBox(Session.Share(),title='Installation Share')  # metodo static
    ui.Print(Session.SharedPythonScripts())  # metodo statico
    s = Session()  # creazione istanza
    ui.MsgBox(s.UserName,title='Hello')  # proprietà dell'oggetto
    ui.Print(s.UserPythonScripts)  # proprietà dell'oggetto

g_exportedScripts = (demo_session,)  # macro pubbliche

Con Collabora Office Basic.

Sub Session_example()
    Dim s As New Session ' instance of Session class
    Print "Posizioni script condivise:", s.SharedScripts
    MsgBox s.UserName,,"Ciao"
    Print s.UserScripts, Chr(13), s.UserPythonScripts
End Sub ' Session_example

Uso di COM/OLE e del linguaggio Visual Basic Scripting.

' Il gestore del servizio è sempre il punto di accesso
' Se non esiste un compito in esecuzione allora viene fatto partire un compito
Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
' Il servizio PathSubstitution mostra le informazioni da dedurre
' posizioni da <UserProfile|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"

Classe di sessione Python:

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 alla variabile '$(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

A differenza di Basic, la normalizzazione del nome_percorso viene eseguita con Python dentro la classe di sessione.


Classe di sessione di Collabora Office Basic:

Option Explicit
Option Compatible
Option ClassModule

Private _ps As Object ' Membro privato

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

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

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 ' Nome account utente
    userName = _ps.getSubstituteVariableValue("$(username)")
End Property ' Session.userName

Public Property Get UserProfile() As String ' Percorso di sistema profilo utente
    userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
End Property ' Session.userProfile

Public Property Get UserScripts() As String ' Percorso di sistema script utente
    userScripts = userProfile() & GetPathSeparator() &"Scripts"
End Property ' Session.userScripts

Public Property Get UserPythonScripts() As String ' Percorso di sistema script Python
    userPythonScripts = userScripts() & GetPathSeparator() &"python"
End Property ' Session.userPythonScripts

Sosteneteci!