diff options
Diffstat (limited to 'helpcontent2/source/text/sbasic/python/python_session.xhp')
-rw-r--r-- | helpcontent2/source/text/sbasic/python/python_session.xhp | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/helpcontent2/source/text/sbasic/python/python_session.xhp b/helpcontent2/source/text/sbasic/python/python_session.xhp new file mode 100644 index 000000000..f8c378dee --- /dev/null +++ b/helpcontent2/source/text/sbasic/python/python_session.xhp @@ -0,0 +1,158 @@ +<?xml version="1.0" encoding="UTF-8"?> +<helpdocument version="1.0"> + <!-- + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + --> + <meta> + <topic id="text/sbasic/python/python_session"> + <title id="tit" xml-lang="en-US">Python_Session : Session class</title> + <filename>/text/sbasic/python/python_session.xhp</filename> + </topic> + </meta> + <body> + <bookmark branch="index" id="N0339"> + <bookmark_value>Session;UserName</bookmark_value> + <bookmark_value>Session;SharedScripts</bookmark_value> + <bookmark_value>Session;SharedPythonScripts</bookmark_value> + <bookmark_value>Session;UserProfile</bookmark_value> + <bookmark_value>Session;UserScripts</bookmark_value> + <bookmark_value>Session;UserPythonScripts</bookmark_value> + <bookmark_value>API;PathSubstitution</bookmark_value> + </bookmark> + <h1 id="N0340"><variable id="pythonsession"><link href="text/sbasic/python/python_session.xhp" name="session01">Getting Session Information</link></variable></h1> + <paragraph role="paragraph" id="N0341">Computing %PRODUCTNAME 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.</paragraph> + <h2 id="N0343">Examples:</h2> + <paragraph role="paragraph" id="N0344">With Python shell.</paragraph> + <paragraph role="paragraph" localize="false" id="N0345"><literal>>>> from <the_module> import Session</literal></paragraph> + <paragraph role="paragraph" xml-lang="en-US" id="N0346"><literal>>>> print(Session.SharedPythonScripts()) # static method</literal></paragraph> + <paragraph role="paragraph" xml-lang="en-US" id="N0347"><literal>>>> print(Session().UserName) # object property</literal></paragraph> + <paragraph role="paragraph" xml-lang="en-US" id="N0348"><literal>>>> input(Session().UserProfile) # object property</literal></paragraph> + <h3 id="N0349">From <menuitem>Tools – Macros - Run Macro</menuitem>... menu.</h3> + <pycode> + <paragraph role="pycode" localize="false" id="N0350">from <the_module> import Session</paragraph> + <paragraph role="pycode" localize="false" id="N0351"> </paragraph> + <paragraph role="pycode" localize="false" id="N0352">def demo_session():</paragraph> + <paragraph role="pycode" localize="false" id="N0353"> import screen_io as ui</paragraph> + <paragraph role="pycode" xml-lang="en-US" id="N0354"> ui.MsgBox(Session.Share(),title='Installation Share') # static method</paragraph> + <paragraph role="pycode" xml-lang="en-US" id="N0355"> ui.Print(Session.SharedPythonScripts()) # static method</paragraph> + <paragraph role="pycode" xml-lang="en-US" id="N0356"> s = Session() # instance creation</paragraph> + <paragraph role="pycode" xml-lang="en-US" id="N0357"> ui.MsgBox(s.UserName,title='Hello') # object property</paragraph> + <paragraph role="pycode" xml-lang="en-US" id="N0358"> ui.Print(s.UserPythonScripts) # object property</paragraph> + <paragraph role="pycode" localize="false" id="N0359"> </paragraph> + <paragraph role="pycode" xml-lang="en-US" id="N0360">g_exportedScripts = (demo_session,) # public macros</paragraph> + </pycode> + <h3 id="N0361">With %PRODUCTNAME Basic.</h3> + <bascode> + <paragraph role="bascode" localize="false" id="N0362">Sub Session_example()</paragraph> + <paragraph role="bascode" localize="false" id="N0363"> Dim s As New Session ' instance of Session class</paragraph> + <paragraph role="bascode" xml-lang="en-US" id="N0364"> Print "Shared scripts location:", s.SharedScripts</paragraph> + <paragraph role="bascode" xml-lang="en-US" id="N0365"> MsgBox s.UserName,,"Hello"</paragraph> + <paragraph role="bascode" localize="false" id="N0366"> Print s.UserScripts, Chr(13), s.UserPythonScripts</paragraph> + <paragraph role="bascode" localize="false" id="N0367">End Sub ' Session_example</paragraph> + </bascode> + <h3 id="N0368">Using COM/OLE and Visual Basic Scripting language.</h3> + <bascode> + <paragraph role="bascode" xml-lang="en-US" id="N0369">' The service manager is always the entry point</paragraph> + <paragraph role="bascode" xml-lang="en-US" id="N0370">' If there is no office running then an office is started up</paragraph> + <paragraph role="bascode" localize="false" id="N0371">Set sm = WScript.CreateObject("com.sun.star.ServiceManager")</paragraph> + <paragraph role="bascode" xml-lang="en-US" id="N0372">' PathSubstitution service exhibits information to infer</paragraph> + <paragraph role="bascode" xml-lang="en-US" id="N0373">' <UserProfile|Share>/Scripts/python locations from</paragraph> + <paragraph role="bascode" localize="false" id="N0374">Set obj = sm.createInstance("com.sun.star.util.PathSubstitution")</paragraph> + <paragraph role="bascode" localize="false" id="N0375"> </paragraph> + <paragraph role="bascode" localize="false" id="N0376">MsgBox CreateObject("WScript.Network").UserName,, "Hello"</paragraph> + <paragraph role="bascode" localize="false" id="N0377">user = obj.getSubstituteVariableValue("$(user)")</paragraph> + <paragraph role="bascode" localize="false" id="N0378">MsgBox user & "/Scripts",, "User scripts location"</paragraph> + <paragraph role="bascode" localize="false" id="N0379">libO = Replace(obj.getSubstituteVariableValue("$(inst)"), "program/..", "Share")</paragraph> + <paragraph role="bascode" localize="false" id="N0380">MsgBox libO & "/Scripts",, "Shared scripts location"</paragraph> + </bascode> + <h2 id="N0381">Python Session class:</h2> + <pycode> + <paragraph role="pycode" localize="false" id="N0382">import getpass, os, os.path, uno</paragraph> + <paragraph role="pycode" localize="false" id="N0383"> </paragraph> + <paragraph role="pycode" localize="false" id="N0384">class Session():</paragraph> + <paragraph role="pycode" localize="false" id="N0385"> @staticmethod</paragraph> + <paragraph role="pycode" localize="false" id="N0386"> def substitute(var_name):</paragraph> + <paragraph role="pycode" localize="false" id="N0387"> ctx = uno.getComponentContext()</paragraph> + <paragraph role="pycode" localize="false" id="N0388"> ps = ctx.getServiceManager().createInstanceWithContext(</paragraph> + <paragraph role="pycode" localize="false" id="N0389"> 'com.sun.star.util.PathSubstitution', ctx)</paragraph> + <paragraph role="pycode" localize="false" id="N0390"> return ps.getSubstituteVariableValue(var_name)</paragraph> + <paragraph role="pycode" localize="false" id="N0391"> @staticmethod</paragraph> + <paragraph role="pycode" localize="false" id="N0392"> def Share():</paragraph> + <paragraph role="pycode" localize="false" id="N0393"> inst = uno.fileUrlToSystemPath(Session.substitute("$(prog)"))</paragraph> + <paragraph role="pycode" localize="false" id="N0394"> return os.path.normpath(inst.replace('program', "Share"))</paragraph> + <paragraph role="pycode" localize="false" id="N0395"> @staticmethod</paragraph> + <paragraph role="pycode" localize="false" id="N0396"> def SharedScripts():</paragraph> + <paragraph role="pycode" localize="false" id="N0397"> return ''.join([Session.Share(), os.sep, "Scripts"])</paragraph> + <paragraph role="pycode" localize="false" id="N0398"> @staticmethod</paragraph> + <paragraph role="pycode" localize="false" id="N0399"> def SharedPythonScripts():</paragraph> + <paragraph role="pycode" localize="false" id="N0400"> return ''.join([Session.SharedScripts(), os.sep, 'python'])</paragraph> + <paragraph role="pycode" xml-lang="en-US" id="N0401"> @property # alternative to '$(username)' variable</paragraph> + <paragraph role="pycode" localize="false" id="N0402"> def UserName(self): return getpass.getuser()</paragraph> + <paragraph role="pycode" localize="false" id="N0403"> @property</paragraph> + <paragraph role="pycode" localize="false" id="N0404"> def UserProfile(self):</paragraph> + <paragraph role="pycode" localize="false" id="N0405"> return uno.fileUrlToSystemPath(Session.substitute("$(user)"))</paragraph> + <paragraph role="pycode" localize="false" id="N0406"> @property</paragraph> + <paragraph role="pycode" localize="false" id="N0407"> def UserScripts(self):</paragraph> + <paragraph role="pycode" localize="false" id="N0408"> return ''.join([self.UserProfile, os.sep, 'Scripts'])</paragraph> + <paragraph role="pycode" localize="false" id="N0409"> @property</paragraph> + <paragraph role="pycode" localize="false" id="N0410"> def UserPythonScripts(self):</paragraph> + <paragraph role="pycode" localize="false" id="N0411"> return ''.join([self.UserScripts, os.sep, "python"])</paragraph> + </pycode> + <note id="N0342">Unlike Basic, pathname normalization is performed with Python inside Session class.</note> + <h2 id="N0412">%PRODUCTNAME Basic Session class:</h2> + <bascode> + <paragraph role="bascode" localize="false" id="N0413">Option Explicit</paragraph> + <paragraph role="bascode" localize="false" id="N0414">Option Compatible</paragraph> + <paragraph role="bascode" localize="false" id="N0415">Option ClassModule</paragraph> + <paragraph role="bascode" localize="false" id="N0416"> </paragraph> + <paragraph role="bascode" xml-lang="en-US" id="N0417">Private _ps As Object ' Private member</paragraph> + <paragraph role="bascode" localize="false" id="N0418"> </paragraph> + <paragraph role="bascode" localize="false" id="N0419">Private Sub Class_Initialize()</paragraph> + <paragraph role="bascode" localize="false" id="N0420"> GlobalScope.BasicLibraries.LoadLibrary("Tools")</paragraph> + <paragraph role="bascode" localize="false" id="N0421"> Set _ps = CreateUnoService("com.sun.star.util.PathSubstitution")</paragraph> + <paragraph role="bascode" xml-lang="en-US" id="N0422">End Sub ' Constructor</paragraph> + <paragraph role="bascode" localize="false" id="N0423"> </paragraph> + <paragraph role="bascode" localize="false" id="N0424">Private Sub Class_Terminate()</paragraph> + <paragraph role="bascode" localize="false" id="N0425"> _ps = Nothing</paragraph> + <paragraph role="bascode" xml-lang="en-US" id="N0426">End Sub ' Destructor</paragraph> + <paragraph role="bascode" localize="false" id="N0427"> </paragraph> + <paragraph role="bascode" localize="false" id="N0428">Public Property Get SharedScripts() As String</paragraph> + <paragraph role="bascode" localize="false" id="N0429"> Dim inst As String, shr As String</paragraph> + <paragraph role="bascode" localize="false" id="N0430"> inst = ConvertFromURL(_ps.getSubstituteVariableValue("$(prog)"))</paragraph> + <paragraph role="bascode" localize="false" id="N0431"> shr = Tools.Strings.ReplaceString(inst,"Share","program")</paragraph> + <paragraph role="bascode" localize="false" id="N0432"> SharedScripts = shr & GetPathSeparator() &"Scripts"</paragraph> + <paragraph role="bascode" localize="false" id="N0433">End Property ' Session.sharedScripts</paragraph> + <paragraph role="bascode" localize="false" id="N0434"> </paragraph> + <paragraph role="bascode" localize="false" id="N0435">Public Property Get SharedPythonScripts() As String</paragraph> + <paragraph role="bascode" localize="false" id="N0436"> sharedPythonScripts = sharedScripts() & GetPathSeparator() &"python"</paragraph> + <paragraph role="bascode" localize="false" id="N0437">End Property ' Session.sharedPythonScripts</paragraph> + <paragraph role="bascode" localize="false" id="N0438"> </paragraph> + <paragraph role="bascode" xml-lang="en-US" id="N0439">Public Property Get UserName() As String ' User account name </paragraph> + <paragraph role="bascode" localize="false" id="N0440"> userName = _ps.getSubstituteVariableValue("$(username)")</paragraph> + <paragraph role="bascode" localize="false" id="N0441">End Property ' Session.userName</paragraph> + <paragraph role="bascode" localize="false" id="N0442"> </paragraph> + <paragraph role="bascode" xml-lang="en-US" id="N0443">Public Property Get UserProfile() As String ' User profile system path</paragraph> + <paragraph role="bascode" localize="false" id="N0444"> userProfile = ConvertFromURL(_ps.getSubstituteVariableValue("$(user)"))</paragraph> + <paragraph role="bascode" localize="false" id="N0445">End Property ' Session.userProfile</paragraph> + <paragraph role="bascode" localize="false" id="N0446"> </paragraph> + <paragraph role="bascode" xml-lang="en-US" id="N0447">Public Property Get UserScripts() As String ' User scripts system path</paragraph> + <paragraph role="bascode" localize="false" id="N0448"> userScripts = userProfile() & GetPathSeparator() &"Scripts"</paragraph> + <paragraph role="bascode" localize="false" id="N0449">End Property ' Session.userScripts</paragraph> + <paragraph role="bascode" localize="false" id="N0450"> </paragraph> + <paragraph role="bascode" xml-lang="en-US" id="N0451">Public Property Get UserPythonScripts() As String ' User Python scripts system path</paragraph> + <paragraph role="bascode" localize="false" id="N0452"> userPythonScripts = userScripts() & GetPathSeparator() &"python"</paragraph> + <paragraph role="bascode" localize="false" id="N0453">End Property ' Session.userPythonScripts</paragraph> + </bascode> + <section id="relatedtopics"> + <embed href="text/sbasic/python/python_locations.xhp#pythonlocations1"/> + <embed href="text/sbasic/python/python_import.xhp#pythonimporth1"/> + <embed href="text/sbasic/python/python_screen.xhp#ioscreen"/> + <embed href="text/sbasic/python/python_examples.xhp#pythonexamples2"/> + </section> +</body> +</helpdocument> |