summaryrefslogtreecommitdiff
path: root/wizards
diff options
context:
space:
mode:
authorJean-Pierre Ledure <jp@ledure.be>2021-03-16 11:44:44 +0100
committerJean-Pierre Ledure <jp@ledure.be>2021-03-16 13:08:05 +0100
commit8570a945f2c77ea0231acd0d110016a336dbf9c7 (patch)
treee180f4c9dc0b1f2bfe3f24cd5ae54c5743683a0c /wizards
parent1e9b97dc1f795da63c92b0169415a4f455d9d014 (diff)
ScriptForge - (scriptforge.py) Exception class
The SF_Exception class is not intended to replace or complement the builtin Python exception handling. Hence it implements only a subset of the corresponding Basic service. - no properties: they are fully related to Basic error trapping - next methods are available: Console, ConsoleClear, ConsoleToFile, DebugPrint and RaiseFatal (reserved for internal use) Restriction: Console() does not work in non-modal mode. DebugPrint formats its messages by using the repr() builtin method. DebugPrint outputs from Basic or from Python are mixed within the same console. The other methods call their Basic counterpart. A new method has been added both in Python and Basic: DebugDisplay It combines DebugPrint with a display in a message box of the arguments. Change-Id: Iba94ab951b95b409c881ac17560476d6e9bc2fc2 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112570 Tested-by: Jean-Pierre Ledure <jp@ledure.be> Reviewed-by: Jean-Pierre Ledure <jp@ledure.be>
Diffstat (limited to 'wizards')
-rw-r--r--wizards/source/scriptforge/SF_Exception.xba44
-rw-r--r--wizards/source/scriptforge/SF_PythonHelper.xba8
-rw-r--r--wizards/source/scriptforge/SF_Root.xba2
-rw-r--r--wizards/source/scriptforge/python/scriptforge.py57
4 files changed, 105 insertions, 6 deletions
diff --git a/wizards/source/scriptforge/SF_Exception.xba b/wizards/source/scriptforge/SF_Exception.xba
index 18d562b21e7b..ba9ce109ecc8 100644
--- a/wizards/source/scriptforge/SF_Exception.xba
+++ b/wizards/source/scriptforge/SF_Exception.xba
@@ -392,6 +392,49 @@ Catch:
End Function &apos; ScriptForge.SF_Exception.ConsoleToFile
REM -----------------------------------------------------------------------------
+Public Sub DebugDisplay(ParamArray pvArgs() As Variant)
+&apos;&apos;&apos; Display the list of arguments in a readable form in a message box
+&apos;&apos;&apos; Arguments are separated by a LINEFEED character
+&apos;&apos;&apos; The maximum length of each individual argument = 1024 characters
+&apos;&apos;&apos; Args:
+&apos;&apos;&apos; Any number of arguments of any type
+&apos;&apos;&apos; Examples:
+&apos;&apos;&apos; SF_Exception.DebugDisplay(a, Array(1, 2, 3), , &quot;line1&quot; &amp; Chr(10) &amp; &quot;Line2&quot;, DateSerial(2020, 04, 09))
+
+Dim sOutputMsg As String &apos; Line to display
+Dim sOutputCon As String &apos; Line to write in console
+Dim sArgMsg As String &apos; Single argument
+Dim sArgCon As String &apos; Single argument
+Dim i As Integer
+Const cstTab = 4
+Const cstMaxLength = 1024
+Const cstThisSub = &quot;Exception.DebugDisplay&quot;
+Const cstSubArgs = &quot;Arg0, [Arg1, ...]&quot;
+
+ If SF_Utils._ErrorHandling() Then On Local Error Goto Finally &apos; Never interrupt processing
+ SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
+Try:
+ &apos; Build new console line
+ sOutputMsg = &quot;&quot; : sOutputCon = &quot;&quot;
+ For i = 0 To UBound(pvArgs)
+ If IsError(pvArgs(i)) Then pvArgs(i) = &quot;&quot;
+ sArgMsg = Iif(i = 0, &quot;&quot;, SF_String.sfNEWLINE) &amp; SF_Utils._Repr(pvArgs(i), cstMaxLength) &apos;Do not use SF_String.Represent()
+ sArgCon = Iif(i = 0, &quot;&quot;, SF_String.sfTAB) &amp; SF_Utils._Repr(pvArgs(i), cstMaxLength)
+ sOutputMsg = sOutputMsg &amp; sArgMsg
+ sOutputCon = sOutputCon &amp; sArgCon
+ Next i
+
+ &apos; Add to actual console
+ _SF_._AddToConsole(SF_String.ExpandTabs(sOutputCon, cstTab))
+ &apos; Display the message
+ MsgBox(sOutputMsg, MB_OK + MB_ICONINFORMATION, &quot;DebugDisplay&quot;)
+
+Finally:
+ SF_Utils._ExitFunction(cstThisSub)
+ Exit Sub
+End Sub &apos; ScriptForge.SF_Exception.DebugDisplay
+
+REM -----------------------------------------------------------------------------
Public Sub DebugPrint(ParamArray pvArgs() As Variant)
&apos;&apos;&apos; Print the list of arguments in a readable form in the console
&apos;&apos;&apos; Arguments are separated by a TAB character (simulated by spaces)
@@ -403,7 +446,6 @@ Public Sub DebugPrint(ParamArray pvArgs() As Variant)
Dim sOutput As String &apos; Line to write in console
Dim sArg As String &apos; Single argument
-Dim sMainSub As String &apos; Temporary storage for main function
Dim i As Integer
Const cstTab = 4
Const cstMaxLength = 1024
diff --git a/wizards/source/scriptforge/SF_PythonHelper.xba b/wizards/source/scriptforge/SF_PythonHelper.xba
index c54d799ae282..38aaa0e4681b 100644
--- a/wizards/source/scriptforge/SF_PythonHelper.xba
+++ b/wizards/source/scriptforge/SF_PythonHelper.xba
@@ -481,21 +481,21 @@ Public Function PyMsgBox(ByVal Text As Variant _
&apos;&apos;&apos; Example: (Python code)
&apos;&apos;&apos; a = bas.MsgBox (&apos;Please press a button:&apos;, bas.MB_EXCLAMATION, &apos;Dear User&apos;)
-Dim sMsg As String &apos; Return value
+Dim iMsg As Integer &apos; Return value
Const cstThisSub = &quot;Basic.MsgBox&quot;
Const cstSubArgs = &quot;text, [dialogtype=0], [dialogtitle]&quot;
If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
- sMsg = &quot;&quot;
+ iMsg = -1
Check:
SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
Try:
- sMsg = MsgBox(Text, DialogType, DialogTitle)
+ iMsg = MsgBox(Text, DialogType, DialogTitle)
Finally:
- PyMsgBox = sMsg
+ PyMsgBox = iMsg
SF_Utils._ExitFunction(cstThisSub)
Exit Function
Catch:
diff --git a/wizards/source/scriptforge/SF_Root.xba b/wizards/source/scriptforge/SF_Root.xba
index fcbba9039596..7ba27eb3c4ec 100644
--- a/wizards/source/scriptforge/SF_Root.xba
+++ b/wizards/source/scriptforge/SF_Root.xba
@@ -160,7 +160,7 @@ Dim sLine As String &apos; Alias of psLine
&apos; Add a timestamp to the line and insert it (without date)
sLine = Mid(SF_Utils._Repr(Now()), 12) &amp; &quot; -&gt; &quot; &amp; psLine
- ConsoleLines(lConsole + 1) = Mid(SF_Utils._Repr(Now()), 12) &amp; &quot; -&gt; &quot; &amp; psLine
+ ConsoleLines(lConsole + 1) = sLine
&apos; Add the new line to the actual (probably non-modal) console, if active
If Not IsNull(ConsoleDialog) Then
diff --git a/wizards/source/scriptforge/python/scriptforge.py b/wizards/source/scriptforge/python/scriptforge.py
index 9baabf42b0bf..86e35167b83b 100644
--- a/wizards/source/scriptforge/python/scriptforge.py
+++ b/wizards/source/scriptforge/python/scriptforge.py
@@ -751,6 +751,63 @@ class SFScriptForge:
importFromPropertyValues, importfrompropertyvalues = ImportFromPropertyValues, ImportFromPropertyValues
# #########################################################################
+ # SF_Exception CLASS
+ # #########################################################################
+ class SF_Exception(SFServices, metaclass = _Singleton):
+ """
+ The Exception service is a collection of methods for code debugging and error handling.
+
+ The Exception service console stores events, variable values and information about errors.
+ Use the console when the Python shell is not available, for example in Calc user defined functions (UDF)
+ or during events processing.
+ Use DebugPrint() method to aggregate additional user data of any type.
+
+ Console entries can be dumped to a text file or visualized in a dialogue.
+ """
+ # Mandatory class properties for service registration
+ serviceimplementation = 'basic'
+ servicename = 'ScriptForge.Exception'
+ servicesynonyms = ('exception', 'scriptforge.exception')
+ serviceproperties = dict()
+ propertysynonyms = SFServices._getAttributeSynonyms(serviceproperties)
+
+ def Console(self, modal = True):
+ # Modal is always True in Python: Basic execution lasts only the time to display the box
+ return self.Execute(self.vbMethod, 'Console', True)
+ console = Console
+
+ def ConsoleClear(self, keep = 0):
+ return self.Execute(self.vbMethod, 'ConsoleClear', keep)
+ consoleClear, consoleclear = ConsoleClear, ConsoleClear
+
+ def ConsoleToFile(self, filename):
+ return self.Execute(self.vbMethod, 'ConsoleToFile', filename)
+ consoleToFile, consoletofile = ConsoleToFile, ConsoleToFile
+
+ def DebugDisplay(self, *args):
+ # Arguments are concatenated in a single string similar to what the Python print() function would produce
+ self.DebugPrint(*args)
+ param = '\n'.join(list(map(lambda a : a.strip("'") if isinstance(a, str) else repr(a), args)))
+ bas = CreateScriptService('ScriptForge.Basic')
+ return bas.MsgBox(param, bas.MB_OK + bas.MB_ICONINFORMATION, 'DebugDisplay')
+ debugDisplay, debugdisplay = DebugDisplay, DebugDisplay
+
+ def DebugPrint(self, *args):
+ # Arguments are concatenated in a single string similar to what the Python print() function would produce
+ param = '\t'.join(list(map(repr, args))).expandtabs(tabsize = 4)
+ return self.Execute(self.vbMethod, 'DebugPrint', param)
+ debugPrint, debugprint = DebugPrint, DebugPrint
+
+ def RaiseFatal(self, errorcode, *args):
+ """
+ Generate a run-time error caused by an anomaly in a user script detected by ScriptForge
+ The message is logged in the console. The execution is STOPPED
+ For INTERNAL USE only
+ """
+ # Direct call because RaiseFatal forces an execution stop in Basic
+ return self.SIMPLEEXEC('SF_Exception.RaiseFatal', errorcode, *args)
+
+ # #########################################################################
# SF_FileSystem CLASS
# #########################################################################
class SF_FileSystem(SFServices, metaclass = _Singleton):