From ae3c8a0a8a17b5cd3c427d7c62a4832631e18bb2 Mon Sep 17 00:00:00 2001 From: Alain Romedenne Date: Thu, 31 Jan 2019 14:51:22 +0100 Subject: [PATCH] Add Help page for Python session Change-Id: I01f1448176f5c95a5151bb3040e9c2ef53fcb7f1 Reviewed-on: https://gerrit.libreoffice.org/67214 Tested-by: Jenkins Reviewed-by: Olivier Hallot --- AllLangHelp_sbasic.mk | 1 + source/text/sbasic/python/python_session.xhp | 160 +++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 source/text/sbasic/python/python_session.xhp diff --git a/AllLangHelp_sbasic.mk b/AllLangHelp_sbasic.mk index e8c2c202ff..81718f70e2 100644 --- a/AllLangHelp_sbasic.mk +++ b/AllLangHelp_sbasic.mk @@ -375,6 +375,7 @@ $(eval $(call gb_AllLangHelp_add_helpfiles,sbasic,\ helpcontent2/source/text/sbasic/python/python_platform \ helpcontent2/source/text/sbasic/python/python_programming \ helpcontent2/source/text/sbasic/python/python_screen \ + helpcontent2/source/text/sbasic/python/python_session \ helpcontent2/source/text/sbasic/python/python_shell \ )) diff --git a/source/text/sbasic/python/python_session.xhp b/source/text/sbasic/python/python_session.xhp new file mode 100644 index 0000000000..e6a49c91eb --- /dev/null +++ b/source/text/sbasic/python/python_session.xhp @@ -0,0 +1,160 @@ + + + + + + Python_Session : Session class + /text/sbasic/python/python_session.xhp + + + + + Session;ComputerName + Session;SharedScripts + Session;SharedPythonScripts + Session;UserProfile + Session;UserScripts + Session;UserPythonScripts + +

Getting session information

+ To compute %PRODUCTNAME user profile and shared modules system file paths can be performed with Python and with Basic language. BeanShell, Java, JavaScript and Python scripts locations can be derived from this information. +

Examples:

+ With Python shell. + >>> from <the_module> import Session + >>> print(Session.SharedPythonScripts()) # class method + >>> print(Session().UserName) # object property + >>> input(Session().UserProfile) # object property +

From Tools – Macros - Run Macro... menu.

+ + from <the_module> import Session + + def demo_session(): + import screen_io as ui + ui.MsgBox(Session.Share(),title='Installation Share') # class method + ui.Print(Session.SharedPythonScripts()) # class method + s = Session() # instance creation + ui.MsgBox(s.UserName,title='Hello') # object property + ui.Print(s.UserPythonScripts) # object property + + g_exportedScripts = demo_session, # public macros + +

With %PRODUCTNAME Basic.

+ + Sub Session_example() + Dim s As New Session ' instance of Platform class + Print "Shared scripts location:", s.SharedScripts + MsgBox s.UserName,,"Hello" + Print s.UserScripts, Chr(13), s.UserPythonScripts + End Sub ' Session_example + +

Using COM/OLE and Visual Basic Scripting language.

+ + ' The service manager is always the entry point + ' If there is no office running then an office is started up + Set sm = WScript.CreateObject("com.sun.star.ServiceManager") + ' PathSubstitution service exhibits information to infer + ' <UserProfile|Share>/Scripts/python locations from + 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" + +

Python Session class:

+ + 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 # alternative to '$(username)' variable + 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"]) + + Unlike Basic, pathname normalization is performed with Python inside Session class. +

%PRODUCTNAME Basic Session class:

+ + Option Explicit + Option Compatible + Option ClassModule + + Private _ps As Object ' Private member + + 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 ' User account name + userName = _ps.getSubstituteVariableValue("$(username)") + End Property ' Session.userName + + Public Property Get UserProfile() As String ' User profile system path + userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)")) + End Property ' Session.userProfile + + Public Property Get UserScripts() As String ' User scripts system path + userScripts = userProfile() & GetPathSeparator() &"Scripts" + End Property ' Session.userScripts + + Public Property Get UserPythonScripts() As String ' User Python scripts system path + userPythonScripts = userScripts() & GetPathSeparator() &"python" + End Property ' Session.userPythonScripts + +
+ + Importing Python modules + + + Input/Output to Screen + + +
+ +