diff options
author | Jean-Pierre Ledure <jp@ledure.be> | 2021-03-16 11:44:44 +0100 |
---|---|---|
committer | Jean-Pierre Ledure <jp@ledure.be> | 2021-03-16 13:08:05 +0100 |
commit | 8570a945f2c77ea0231acd0d110016a336dbf9c7 (patch) | |
tree | e180f4c9dc0b1f2bfe3f24cd5ae54c5743683a0c /wizards | |
parent | 1e9b97dc1f795da63c92b0169415a4f455d9d014 (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.xba | 44 | ||||
-rw-r--r-- | wizards/source/scriptforge/SF_PythonHelper.xba | 8 | ||||
-rw-r--r-- | wizards/source/scriptforge/SF_Root.xba | 2 | ||||
-rw-r--r-- | wizards/source/scriptforge/python/scriptforge.py | 57 |
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 ' ScriptForge.SF_Exception.ConsoleToFile REM ----------------------------------------------------------------------------- +Public Sub DebugDisplay(ParamArray pvArgs() As Variant) +''' Display the list of arguments in a readable form in a message box +''' Arguments are separated by a LINEFEED character +''' The maximum length of each individual argument = 1024 characters +''' Args: +''' Any number of arguments of any type +''' Examples: +''' SF_Exception.DebugDisplay(a, Array(1, 2, 3), , "line1" & Chr(10) & "Line2", DateSerial(2020, 04, 09)) + +Dim sOutputMsg As String ' Line to display +Dim sOutputCon As String ' Line to write in console +Dim sArgMsg As String ' Single argument +Dim sArgCon As String ' Single argument +Dim i As Integer +Const cstTab = 4 +Const cstMaxLength = 1024 +Const cstThisSub = "Exception.DebugDisplay" +Const cstSubArgs = "Arg0, [Arg1, ...]" + + If SF_Utils._ErrorHandling() Then On Local Error Goto Finally ' Never interrupt processing + SF_Utils._EnterFunction(cstThisSub, cstSubArgs) +Try: + ' Build new console line + sOutputMsg = "" : sOutputCon = "" + For i = 0 To UBound(pvArgs) + If IsError(pvArgs(i)) Then pvArgs(i) = "" + sArgMsg = Iif(i = 0, "", SF_String.sfNEWLINE) & SF_Utils._Repr(pvArgs(i), cstMaxLength) 'Do not use SF_String.Represent() + sArgCon = Iif(i = 0, "", SF_String.sfTAB) & SF_Utils._Repr(pvArgs(i), cstMaxLength) + sOutputMsg = sOutputMsg & sArgMsg + sOutputCon = sOutputCon & sArgCon + Next i + + ' Add to actual console + _SF_._AddToConsole(SF_String.ExpandTabs(sOutputCon, cstTab)) + ' Display the message + MsgBox(sOutputMsg, MB_OK + MB_ICONINFORMATION, "DebugDisplay") + +Finally: + SF_Utils._ExitFunction(cstThisSub) + Exit Sub +End Sub ' ScriptForge.SF_Exception.DebugDisplay + +REM ----------------------------------------------------------------------------- Public Sub DebugPrint(ParamArray pvArgs() As Variant) ''' Print the list of arguments in a readable form in the console ''' 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 ' Line to write in console Dim sArg As String ' Single argument -Dim sMainSub As String ' 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 _ ''' Example: (Python code) ''' a = bas.MsgBox ('Please press a button:', bas.MB_EXCLAMATION, 'Dear User') -Dim sMsg As String ' Return value +Dim iMsg As Integer ' Return value Const cstThisSub = "Basic.MsgBox" Const cstSubArgs = "text, [dialogtype=0], [dialogtitle]" If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch - sMsg = "" + 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 ' Alias of psLine ' Add a timestamp to the line and insert it (without date) sLine = Mid(SF_Utils._Repr(Now()), 12) & " -> " & psLine - ConsoleLines(lConsole + 1) = Mid(SF_Utils._Repr(Now()), 12) & " -> " & psLine + ConsoleLines(lConsole + 1) = sLine ' 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): |