wizards/source/scriptforge/SF_PythonHelper.xba | 27 ++++++++++++++++-- wizards/source/scriptforge/python/scriptforge.py | 34 ++++++++++++++++++++--- 2 files changed, 55 insertions(+), 6 deletions(-)
New commits: commit 8c04c9b3c726deb4bcab82aff0aec7d4f4ee6158 Author: Jean-Pierre Ledure <j...@ledure.be> AuthorDate: Tue Mar 16 16:57:28 2021 +0100 Commit: Jean-Pierre Ledure <j...@ledure.be> CommitDate: Wed Mar 17 18:11:23 2021 +0100 ScriptForge - (scriptforge.py) Array class The SF_Array (Basic) class is highly redundant with the Python tuple/list classes. Hence only 1 method is implemented in Python: ImportFromCSVFile() to have the same csv files imported identically in both environments. The returned value is a tuple of tuples. Restriction: dates are recognized but converted to their ISO format. Up to the user to interpret them correctly. Change-Id: I6611964a8083a9d7c6f9622b2aef64fe8b1491ea Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112585 Tested-by: Jean-Pierre Ledure <j...@ledure.be> Reviewed-by: Jean-Pierre Ledure <j...@ledure.be> diff --git a/wizards/source/scriptforge/SF_PythonHelper.xba b/wizards/source/scriptforge/SF_PythonHelper.xba index 38aaa0e4681b..7368390c97bf 100644 --- a/wizards/source/scriptforge/SF_PythonHelper.xba +++ b/wizards/source/scriptforge/SF_PythonHelper.xba @@ -547,8 +547,9 @@ Dim sObjectType As String ' Alias of object.ObjectType Dim bBasicClass As Boolean ' True when BasicObject is a class Dim sLibrary As String ' Library where the object belongs to Dim bUno As Boolean ' Return value is a UNO object +Dim iDims As Integer ' # of dims of vReturn Dim sess As Object : Set sess = ScriptForge.SF_Session -Dim i As Long +Dim i As Long, j As Long ' Conventional special input or output values Const cstNoArgs = "+++NOARGS+++", cstSymEmpty = "+++EMPTY+++", cstSymNull = "+++NULL+++", cstSymMissing = "+++MISSING+++" @@ -658,6 +659,15 @@ Try: If sObjectType = "SF_FileSystem" And Script = "GetFileModified" Then vReturn = SF_FileSystem.GetFileModified(vArgs(0)) End Select + ' Methods in usual modules using a 2D array or returning arrays are hardcoded as exceptions + ElseIf Not bBasicClass And _ + (((CallType And vbMethod) + (CallType And cstArgArray)) = vbMethod + cstArgArray Or _ + ((CallType And vbMethod) + (CallType And cstRetArray)) = vbMethod + cstRetArray) Then + Select Case sLibrary + Case "ScriptForge" + If sObjectType = "SF_Array" And Script = "ImportFromCSVFile" Then vReturn = SF_Array.ImportFromCSVFile(vArgs(0), vArgs(1), vArgs(2)) + End Select + ' Methods in usual modules are called by ExecuteBasicScript() except if they use a ParamArray ElseIf Not bBasicClass And (CallType And vbMethod) = vbMethod Then sScript = sLibrary & "." & sObjectType & "." & Script @@ -721,9 +731,22 @@ Try: ' Scalar If IsArray(vReturn) Then ReDim vReturnArray(0 To 2) + iDims = SF_Array.CountDims(vReturn) + ' Replace dates by ISO notation + If iDims = 1 Then + For i = LBound(vReturn) To UBound(vReturn) + If VarType(vReturn(i)) = V_DATE Then vReturn(i) = SF_Utils._CDateToIso(vReturn(i)) + Next i + ElseIf iDims = 2 Then + For i = LBound(vReturn, 1) To UBound(vReturn, 1) + For j = LBound(vReturn, 2) To UBound(vReturn, 2) + If VarType(vReturn(i, j)) = V_DATE Then vReturn(i, j) = SF_Utils._CDateToIso(vReturn(i, j)) + Next j + Next i + End If vReturnArray(0) = vReturn ' 2D arrays are flattened by the script provider when returning to Python vReturnArray(1) = VarType(vReturn) - vReturnArray(2) = SF_Array.CountDims(vReturn) + vReturnArray(2) = iDims ElseIf VarType(vReturn) = V_OBJECT And Not IsNull(vReturn) Then ' Uno or not Uno ?BuildPath bUno = False diff --git a/wizards/source/scriptforge/python/scriptforge.py b/wizards/source/scriptforge/python/scriptforge.py index 86e35167b83b..ed87679ee99f 100644 --- a/wizards/source/scriptforge/python/scriptforge.py +++ b/wizards/source/scriptforge/python/scriptforge.py @@ -362,7 +362,7 @@ class SFServices(object): vbGet, vbLet, vbMethod, vbSet = 2, 4, 1, 8 # CallByName constants flgDateRet = 128 # Invoked service method can return a date flgArrayArg = 512 # 1st argument can be a 2D array - flgArrayRet = 1024 # Invoked service method can return an array + flgArrayRet = 1024 # Invoked service method can return a 2D array flgUno = 256 # Invoked service method/property can return a UNO object # Basic class type moduleClass, moduleStandard = 2, 1 @@ -504,6 +504,31 @@ class SFServices(object): # SFScriptForge CLASS (alias of ScriptForge Basic library) ### # ##################################################################################################################### class SFScriptForge: + # ######################################################################### + # SF_Array CLASS + # ######################################################################### + class SF_Array(SFServices, metaclass = _Singleton): + """ + Provides a collection of methods for manipulating and transforming arrays of one dimension (vectors) + and arrays of two dimensions (matrices). This includes set operations, sorting, + importing to and exporting from text files. + The Python version of the service provides a single method: ImportFromCSVFile + """ + # Mandatory class properties for service registration + serviceimplementation = 'basic' + servicename = 'ScriptForge.Array' + servicesynonyms = ('array', 'scriptforge.array') + serviceproperties = dict() + propertysynonyms = SFServices._getAttributeSynonyms(serviceproperties) + + def ImportFromCSVFile(self, filename, delimiter = ',', dateformat = ''): + """ + Difference with the Basic version: dates are returned in their iso format, + not as any of the datetime objects. + """ + return self.Execute(self.vbMethod + self.flgArrayRet, 'ImportFromCSVFile', filename, delimiter, dateformat) + importFromCSVFile, importfromcsvfile = ImportFromCSVFile, ImportFromCSVFile + # ######################################################################### # SF_Basic CLASS # ######################################################################### @@ -787,7 +812,7 @@ class SFScriptForge: 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))) + 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 @@ -862,7 +887,7 @@ class SFScriptForge: fileexists, fileExists = FileExists, FileExists def Files(self, foldername, filter = ''): - return self.Execute(self.vbMethod + self.flgArrayRet, 'Files', foldername, filter) + return self.Execute(self.vbMethod, 'Files', foldername, filter) def FolderExists(self, foldername): return self.Execute(self.vbMethod, 'FolderExists', foldername) @@ -917,7 +942,7 @@ class SFScriptForge: return self.Execute(self.vbMethod, 'PickFolder', defaultfolder, freetext) def SubFolders(self, foldername, filter = ''): - return self.Execute(self.vbMethod + self.flgArrayRet, 'SubFolders', foldername, filter) + return self.Execute(self.vbMethod, 'SubFolders', foldername, filter) def _ConvertFromUrl(self, filename): # Alias for same function in FileSystem Basic module @@ -1169,6 +1194,7 @@ def CreateScriptService(service, *args): serv = ScriptForge.InvokeBasicService('SF_Services', SFServices.vbMethod, 'CreateScriptService', service, *args) return serv + createScriptService, createscriptservice = CreateScriptService, CreateScriptService _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits