Getting Session Information

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.

Contoh:

Dengan Python shell.

>>> from <the_module> import Session

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

>>> print(Session().UserName) # object property

>>> input(Session().UserProfile) # object property

dariperkakas–makro - jalankan makro... menu

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()  # penciptaan instansi
    ui.MsgBox(s.UserName,title='Hello')  # object property
    ui.Print(s.UserPythonScripts)  # properti objek

g_exportedScripts = (demo_session,)  # public macros

Bekerja dengan Collabora Office Basic

Sub Session_example()
    Dim s As New Session ' instance of Session class
    Cetak "Bagikan skrip lokasi:" , s. Bagikan skrip
    MsgBox s.UserName,,"Halo"
    Print s.UserScripts, Chr(13), s.UserPythonScripts
End Sub ' Session_example

Menggunakan COM/OLE dan Bahasa pemrograman Visual basic.

' Pengelola layanan adalah selalu titik entri
'Jika tidak ada kantor yang beroperasi maka sebuah kantor dimulai
Set sm = WScript.CreateObject("com.sun.star.ServiceManager")
' Layanan PathSubstitution memperlihatkan informasi untuk disimpulkan
' <UserProfile|Share>/Skrip/python dari
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"

Kelas Sesi 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  # alternatif untuk variabel '$(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

Tidak seperti Basic, normalisasi pathname dilakukan dengan Python di dalam kelas Session


Collabora Office ~Basic...sesi kelas:

Option Explicit
Option Compatible
Option ClassModule

private _ps sebagai obyek 'anggota pribadi

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
Destructor Akhir Sub

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

Publik Properti Dapatkan NamaPengguna() sebagai String ' nama akun Pengguna
    userName = _ps.getSubstituteVariableValue("$(username)")
End Property ' Session.userName

Properti Publik mendapatkan UserProfile() Sebagai nama akun Pengguna String
    userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))
End Property ' Session.userProfile

Properti Publik mendapatkan UserProfile() Sebagai nama akun Pengguna String
    userScripts = userProfile() & GetPathSeparator() &"Scripts"
End Property ' Session.userScripts

Properti publik mendapatkan UserPythonScripts() Sebagai String ' Pengguna script Python jalur sistem
    userPythonScripts = userScripts() & GetPathSeparator() &"python"
End Property ' Session.userPythonScripts

Mohon dukung kami!