diff options
author | Jean-Pierre Ledure <jp@ledure.be> | 2021-05-03 12:25:38 +0200 |
---|---|---|
committer | Jean-Pierre Ledure <jp@ledure.be> | 2021-05-03 15:45:51 +0200 |
commit | cec00cd70aa3899244106fcb3958b19c88e6db98 (patch) | |
tree | 26b035d99cad6332bc90b487ee60e78efcbc0fa5 /wizards | |
parent | c5a0b7af847a71fd50f713934b29305f8ce96c6b (diff) |
ScriptForge - (scriptforge.py) Fix dates transfer Basic <-> Python
Strings in CSV file recognized as dates are passed via
their ISO format, not in the default com.sun.star.util.DateTime
format. This required a fix in Basic ImportFromCSVFile().
Errors in user scripts can be announced to users from Python
with the same interface as from Basic: SF_Exception.RaiseFatal().
Error messages become translatable.
Used by SF_Exception.PythonShell(), display of APSO console,
when the APSO extension is not present.
Addition of the PYTHONSHELL error message in the po files.
Review of typos.
Change-Id: I52a19faa3773904bd37505fee780d517436485f2
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/115032
Tested-by: Jean-Pierre Ledure <jp@ledure.be>
Tested-by: Jenkins
Reviewed-by: Jean-Pierre Ledure <jp@ledure.be>
Diffstat (limited to 'wizards')
-rw-r--r-- | wizards/source/scriptforge/SF_Array.xba | 4 | ||||
-rw-r--r-- | wizards/source/scriptforge/SF_Exception.xba | 6 | ||||
-rw-r--r-- | wizards/source/scriptforge/SF_PythonHelper.xba | 6 | ||||
-rw-r--r-- | wizards/source/scriptforge/SF_Root.xba | 6 | ||||
-rw-r--r-- | wizards/source/scriptforge/po/ScriptForge.pot | 9 | ||||
-rw-r--r-- | wizards/source/scriptforge/po/en.po | 9 | ||||
-rw-r--r-- | wizards/source/scriptforge/python/scriptforge.py | 22 |
7 files changed, 50 insertions, 12 deletions
diff --git a/wizards/source/scriptforge/SF_Array.xba b/wizards/source/scriptforge/SF_Array.xba index b51bba63adbe..ea1500bdd057 100644 --- a/wizards/source/scriptforge/SF_Array.xba +++ b/wizards/source/scriptforge/SF_Array.xba @@ -846,6 +846,7 @@ REM ---------------------------------------------------------------------------- Public Function ImportFromCSVFile(Optional ByRef FileName As Variant _ , Optional ByVal Delimiter As Variant _ , Optional ByVal DateFormat As Variant _ + , Optional ByVal _IsoDate As Variant _ ) As Variant ''' Import the data contained in a comma-separated values (CSV) file ''' The comma may be replaced by any character @@ -862,6 +863,7 @@ Public Function ImportFromCSVFile(Optional ByRef FileName As Variant _ ''' The dash (-) may be replaced by a dot (.), a slash (/) or a space ''' Other date formats will be ignored ''' If "" (default), dates will be considered as strings +''' _IsoDate: when True, the execution is initiated from Python, do not convert dates to Date variables. Internal use only ''' Returns: ''' A 2D-array with each row corresponding with a single record read in the file ''' and each column corresponding with a field of the record @@ -898,6 +900,7 @@ Const cstSubArgs = "FileName, [Delimiter="",""], [DateF Check: If IsMissing(Delimiter) Or IsEmpty(Delimiter) Then Delimiter = "," If IsMissing(DateFormat) Or IsEmpty(DateFormat) Then DateFormat = "" + If IsMissing(_IsoDate) Or IsEmpty(_IsoDate) Then _IsoDate = False If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then If Not SF_Utils._ValidateFile(FileName, "FileName") Then GoTo Finally If Not SF_Utils._Validate(Delimiter, "Delimiter", V_STRING) Then GoTo Finally @@ -941,6 +944,7 @@ Try: iPosition = InStr(DateFormat, "MM") : iMonth = CInt(Mid(sItem, iPosition, 2)) iPosition = InStr(DateFormat, "DD") : iDay = CInt(Mid(sItem, iPosition, 2)) vItem = DateSerial(iYear, iMonth, iDay) + If _IsoDate Then vItem = SF_Utils._CDateToIso(vItem) ' Called from Python Else vItem = sItem End If diff --git a/wizards/source/scriptforge/SF_Exception.xba b/wizards/source/scriptforge/SF_Exception.xba index aae74fe98d42..ddfe56a069c8 100644 --- a/wizards/source/scriptforge/SF_Exception.xba +++ b/wizards/source/scriptforge/SF_Exception.xba @@ -122,6 +122,9 @@ Const TEXTFIELDERROR = "TEXTFIELDERROR" Const DBREADONLYERROR = "DBREADONLYERROR" Const SQLSYNTAXERROR = "SQLSYNTAXERROR" +' Python +Const PYTHONSHELLERROR = "PYTHONSHELLERROR" + REM ============================================================= PRIVATE MEMBERS ' User defined errors @@ -916,6 +919,9 @@ Try: Case SQLSYNTAXERROR ' SF_Database._ExecuteSql(SQL) sMessage = sLocation _ & "\n" & "\n" & .GetText("SQLSYNTAX", pvArgs(0)) + Case PYTHONSHELLERROR ' SF_Exception.PythonShell (Python only) + sMessage = sLocation _ + & "\n" & "\n" & .GetText("PYTHONSHELL") Case Else End Select End With diff --git a/wizards/source/scriptforge/SF_PythonHelper.xba b/wizards/source/scriptforge/SF_PythonHelper.xba index b7d8141325b2..52ccc1827e52 100644 --- a/wizards/source/scriptforge/SF_PythonHelper.xba +++ b/wizards/source/scriptforge/SF_PythonHelper.xba @@ -693,7 +693,7 @@ Try: ((CallType And vbMethod) + (CallType And cstRetArray)) = vbMethod + cstRetArray) Then Select Case sServiceName Case "ScriptForge.Array" - If Script = "ImportFromCSVFile" Then vReturn = SF_Array.ImportFromCSVFile(vArgs(0), vArgs(1), vArgs(2)) + If Script = "ImportFromCSVFile" Then vReturn = SF_Array.ImportFromCSVFile(vArgs(0), vArgs(1), vArgs(2), True) End Select ' Methods in usual modules are called by ExecuteBasicScript() except if they use a ParamArray @@ -788,12 +788,12 @@ Try: ' 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)) + If VarType(vReturn(i)) = V_DATE Then vReturn(i) = CDateToUnoDateTime(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)) + If VarType(vReturn(i, j)) = V_DATE Then vReturn(i, j) = CDateToUnoDateTime(vReturn(i, j)) Next j Next i End If diff --git a/wizards/source/scriptforge/SF_Root.xba b/wizards/source/scriptforge/SF_Root.xba index 7f07109dc055..39cf315bd3d2 100644 --- a/wizards/source/scriptforge/SF_Root.xba +++ b/wizards/source/scriptforge/SF_Root.xba @@ -923,6 +923,12 @@ Try: , Comment := "SF_Database can't interpret SQL statement\n" _ & "%1: The statement" _ ) + ' SF_Exception.PythonShell (Python only) + .AddText( Context := "PYTHONSHELL" _ + , MsgId := "The APSO extension could not be located in your LibreOffice installation." _ + , Comment := "SF_Exception.PythonShell error message" _ + & "APSO: to leave unchanged" _ + ) End With End If diff --git a/wizards/source/scriptforge/po/ScriptForge.pot b/wizards/source/scriptforge/po/ScriptForge.pot index 06f0c77c6e1a..012eebf29466 100644 --- a/wizards/source/scriptforge/po/ScriptForge.pot +++ b/wizards/source/scriptforge/po/ScriptForge.pot @@ -14,7 +14,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: https://bugs.libreoffice.org/enter_bug.cgi?product=LibreOffice&bug_status=UNCONFIRMED&component=UI\n" -"POT-Creation-Date: 2021-03-04 16:31:25\n" +"POT-Creation-Date: 2021-05-02 17:38:42\n" "PO-Revision-Date: YYYY-MM-DD HH:MM:SS\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <EMAIL@ADDRESS>\n" @@ -880,4 +880,11 @@ msgid "" "Check its syntax, table and/or field names, ...\n" "\n" "SQL Statement : « %1 »" +msgstr "" + +#. SF_Exception.PythonShell error messageAPSO: to leave unchanged +msgctxt "PYTHONSHELL" +msgid "" +"The APSO extension could not be located in your LibreOffice " +"installation." msgstr ""
\ No newline at end of file diff --git a/wizards/source/scriptforge/po/en.po b/wizards/source/scriptforge/po/en.po index 06f0c77c6e1a..012eebf29466 100644 --- a/wizards/source/scriptforge/po/en.po +++ b/wizards/source/scriptforge/po/en.po @@ -14,7 +14,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: https://bugs.libreoffice.org/enter_bug.cgi?product=LibreOffice&bug_status=UNCONFIRMED&component=UI\n" -"POT-Creation-Date: 2021-03-04 16:31:25\n" +"POT-Creation-Date: 2021-05-02 17:38:42\n" "PO-Revision-Date: YYYY-MM-DD HH:MM:SS\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <EMAIL@ADDRESS>\n" @@ -880,4 +880,11 @@ msgid "" "Check its syntax, table and/or field names, ...\n" "\n" "SQL Statement : « %1 »" +msgstr "" + +#. SF_Exception.PythonShell error messageAPSO: to leave unchanged +msgctxt "PYTHONSHELL" +msgid "" +"The APSO extension could not be located in your LibreOffice " +"installation." msgstr ""
\ No newline at end of file diff --git a/wizards/source/scriptforge/python/scriptforge.py b/wizards/source/scriptforge/python/scriptforge.py index 6c65f73d00ca..767cb9ae60ae 100644 --- a/wizards/source/scriptforge/python/scriptforge.py +++ b/wizards/source/scriptforge/python/scriptforge.py @@ -851,7 +851,7 @@ class SFScriptForge: def ImportFromPropertyValues(self, propertyvalues, overwrite = False): """ - nserts the contents of an array of PropertyValue objects into the current dictionary. + Inserts the contents of an array of PropertyValue objects into the current dictionary. PropertyValue Names are used as keys in the dictionary, whereas Values contain the corresponding values. Date-type values are converted to datetime.datetime instances. :param propertyvalues: a list.tuple containing com.sun.star.beans.PropertyValue objects @@ -914,14 +914,18 @@ class SFScriptForge: 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) + # Avoid using repr() on strings to not have backslashes * 4 + param = '\t'.join(list(map(lambda a: a.strip("'") if isinstance(a, str) else repr(a), + args))).expandtabs(tabsize = 4) return self.ExecMethod(self.vbMethod, 'DebugPrint', param) @classmethod def PythonShell(cls, variables = None): """ - Open an APSO python shell window - Thanks to its author Hanya - :param variables: Use PythonShell.(loc = globals()) to push the global dictionary to the shell window + Open an APSO python shell window - Thanks to its authors Hanya/Tsutomu Uchino/Hubert Lambert + :param variables: Typical use + PythonShell.({**globals(), **locals()}) + to push the global and local dictionaries to the shell window """ if variables is None: variables = locals() @@ -939,7 +943,8 @@ class SFScriptForge: kwargs['loc'].setdefault('XSCRIPTCONTEXT', uno) console(**kwargs) else: - raise RuntimeError('The APSO extension could not be located in your LibreOffice installation') + # The APSO extension could not be located in your LibreOffice installation + cls._RaiseFatal('SF_Exception.PythonShell', 'variables=None', 'PYTHONSHELLERROR') @classmethod def RaiseFatal(cls, errorcode, *args): @@ -949,7 +954,9 @@ class SFScriptForge: For INTERNAL USE only """ # Direct call because RaiseFatal forces an execution stop in Basic - return cls.SIMPLEEXEC('SF_Exception.RaiseFatal', errorcode, *args) + if len(args) == 0: + args = (None,) + return cls.SIMPLEEXEC('@SF_Exception.RaiseFatal', (errorcode, *args)) # With ParamArray @classmethod def _RaiseFatal(cls, sub, subargs, errorcode, *args): @@ -1397,6 +1404,7 @@ class SFScriptForge: @property def ActiveWindow(self): return self.ExecMethod(self.vbMethod, 'ActiveWindow') + activeWindow, activewindow = ActiveWindow, ActiveWindow def Activate(self, windowname = ''): return self.ExecMethod(self.vbMethod, 'Activate', windowname) @@ -1452,7 +1460,7 @@ class SFDatabases: pass # ######################################################################### - # SF_Document CLASS + # SF_Database CLASS # ######################################################################### class SF_Database(SFServices): """ |