Obtener información de la sesión

Computing Collabora Office user profile and shared modules system file paths can be performed with Python or with Basic languages. BeanShell, Java, JavaScript and Python scripts locations can be derived from this information.

Ejemplos:

Con el intérprete de Python

>>> from <the_module> import Session

>>> print(Session.SharedPythonScripts()) # static method

>>> print(Session().UserName) # propiedad de objeto

>>> input(Session().UserProfile) # propiedad de objeto

Desde el menú Herramientas ▸ Macros ▸ Ejecutar macro.

from <the_module> import Session

def demo_session():
    import screen_io as ui
    ui.MsgBox(Session.Share(),title='Installation Share')  # static method
    ui.Print(Session.SharedPythonScripts())  # static method
    s = Session()  # creación de instancia
    ui.MsgBox(s.UserName,title='Hola')  # propiedad de objeto
    ui.Print(s.UserPythonScripts)  # propiedad de objeto

g_exportedScripts = (demo_session,)  # macros públicas

Con Collabora Office Basic

Sub Session_example()
    Dim s As New Session ' instance of Session class
    Print "Ubicación de secuencias compartidas:", s.SharedScripts
    MsgBox s.UserName,,"Hola"
    Print s.UserScripts, Chr(13), s.UserPythonScripts
End Sub ' Session_example

Mediante COM/OLE y el lenguaje VBScript

' El gestor de servicios es siempre el punto de entrada
' Si no hay ninguna instancia en funcionamiento, se pone en marcha una.
Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
' El servicio PathSubstitution muestra información para inferir
' <UserProfile|Share>/Scripts/python ubicaciones desde
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"

Clase Session de 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 a la variable «$(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 diferencia de Basic, la normalización de nombres de rutas de acceso se realiza con Python en la clase Session.


Clase Session de Collabora Office Basic:

Option Explicit
Option Compatible
Option ClassModule

Private _ps As Object ' miembro privado

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

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

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 ' nombre de cuenta de usuario
    userName = _ps.getSubstituteVariableValue("$(username)")
End Property ' Session.userName

Public Property Get UserProfile() As String ' ruta de perfil de usuario en el sistema
    userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
End Property ' Session.userProfile

Public Property Get UserScripts() As String ' ruta de secuencias de órdenes de usuario en el sistema
    userScripts = userProfile() & GetPathSeparator() &"Scripts"
End Property ' Session.userScripts

Public Property Get UserPythonScripts() As String ' ruta de secuencias de órdenes en Python de usuario en el sistema
    userPythonScripts = userScripts() & GetPathSeparator() &"python"
End Property ' Session.userPythonScripts

¡Necesitamos su ayuda!