diff options
author | Jean-Pierre Ledure <jp@ledure.be> | 2022-07-05 17:47:56 +0200 |
---|---|---|
committer | Jean-Pierre Ledure <jp@ledure.be> | 2022-07-06 12:27:09 +0200 |
commit | 7c0f0afd58c00211236b138ddd4804099c5aec83 (patch) | |
tree | 1436e3d75133213f3927179b624b6e806b10638f /wizards | |
parent | fd262525f45b389cd61d881c78d5d270afe298d6 (diff) |
ScriptForge - (SF_FileSystem) new Normalize() method
Normalize a pathname by collapsing redundant
separators and up-level references
so that A//B, A/B/, A/./B and A/foo/../B
all become A/B.
On Windows, it converts forward slashes
to backward slashes.
The Basic Normalize() method invokes the
_SF_FileSystem__Normalize() function located in
the ScriptForgeHelper.py module for execution
with the os.path builtin library
The os.path.normpath() function can easily be
executed directly from python user scripts.
However the FileSystem.Normalize() method is
proposed as well for Python scripts
- for compatibility Basic/Python reasons
- to manage the FileNaming notation
Change-Id: I1e089612432bd2c75b2e76ffa984289ef7f9d75c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136835
Tested-by: Jean-Pierre Ledure <jp@ledure.be>
Reviewed-by: Jean-Pierre Ledure <jp@ledure.be>
Tested-by: Jenkins
Diffstat (limited to 'wizards')
-rw-r--r-- | wizards/source/scriptforge/SF_FileSystem.xba | 44 | ||||
-rw-r--r-- | wizards/source/scriptforge/python/ScriptForgeHelper.py | 17 | ||||
-rw-r--r-- | wizards/source/scriptforge/python/scriptforge.py | 3 | ||||
-rw-r--r-- | wizards/source/sfdocuments/SF_Calc.xba | 2 | ||||
-rw-r--r-- | wizards/source/sfunittests/SF_UnitTest.xba | 4 |
5 files changed, 64 insertions, 6 deletions
diff --git a/wizards/source/scriptforge/SF_FileSystem.xba b/wizards/source/scriptforge/SF_FileSystem.xba index 39ea4888e3d1..8b21ea9a70df 100644 --- a/wizards/source/scriptforge/SF_FileSystem.xba +++ b/wizards/source/scriptforge/SF_FileSystem.xba @@ -1268,6 +1268,7 @@ Public Function Methods() As Variant , "HashFile" _ , "MoveFile" _ , "MoveFolder" _ + , "Normalize" _ , "OpenTextFile" _ , "PickFile" _ , "PickFolder" _ @@ -1384,6 +1385,47 @@ Catch: End Function ' ScriptForge.SF_FileSystem.MoveFolder REM ----------------------------------------------------------------------------- +Public Function Normalize(Optional ByVal FileName As Variant) As String +''' Normalize a pathname by collapsing redundant separators and up-level references +''' so that A//B, A/B/, A/./B and A/foo/../B all become A/B. +''' On Windows, it converts forward slashes to backward slashes. +''' Args: +''' FileName: a string representing a file. The file may not exist. +''' Returns: +''' The normalized filename in filenaming notation +''' Example: +''' Print SF_FileSystem.Normalize("A/foo/../B/C/./D//E") ' A/B/C/D/E + +Dim sNorm As String ' Return value +Const cstPyHelper = "$" & "_SF_FileSystem__Normalize" +Const cstThisSub = "FileSystem.Normalize" +Const cstSubArgs = "FileName" + + If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch + sNorm = "" + +Check: + If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then + If Not SF_Utils._ValidateFile(FileName, "FileName") Then GoTo Finally + End If + +Try: + With ScriptForge.SF_Session + sNorm = .ExecutePythonScript(.SCRIPTISSHARED, _SF_.PythonHelper & cstPyHelper _ + , _ConvertFromUrl(FileName)) + ' The Python os.path expects and returns a file name in os notation + If SF_FileSystem.FileNaming <> "SYS" Then sNorm = ConvertToUrl(sNorm) + End With + +Finally: + Normalize = sNorm + SF_Utils._ExitFunction(cstThisSub) + Exit Function +Catch: + GoTo Finally +End Function ' ScriptForge.SF_FileSystem.Normalize + +REM ----------------------------------------------------------------------------- Public Function OpenTextFile(Optional ByVal FileName As Variant _ , Optional ByVal IOMode As Variant _ , Optional ByVal Create As Variant _ @@ -1759,7 +1801,7 @@ End Function ' ScriptForge.FileSystem._ConvertFromUrl REM ----------------------------------------------------------------------------- Private Function _ConvertToUrl(psFile) As String ''' Execute the builtin ConvertToUrl function only when relevant -''' i.e. when FileNaming (how arguments and return values are provided) = "SYS" +''' i.e. when FileNaming (how arguments and return values are provided) <> "URL" ''' Called at the top of methods receiving file names as arguments ''' Remark: psFile might contain wildcards diff --git a/wizards/source/scriptforge/python/ScriptForgeHelper.py b/wizards/source/scriptforge/python/ScriptForgeHelper.py index 39627323319c..e228095053df 100644 --- a/wizards/source/scriptforge/python/ScriptForgeHelper.py +++ b/wizards/source/scriptforge/python/ScriptForgeHelper.py @@ -24,6 +24,8 @@ """ Collection of Python helper functions called from the ScriptForge Basic libraries to execute specific services that are not or not easily available from Basic directly. +When relevant, the methods present in the ScripForge Python module might call the +functions below for compatibility reasons. """ import getpass @@ -156,6 +158,16 @@ def _SF_FileSystem__HashFile(filename: str, algorithm: str) -> str: # used by S return '' +def _SF_FileSystem__Normalize(systemfilepath: str) -> str: + # used by SF_FileSystem.Normalize() Basic method + """ + Normalize a pathname by collapsing redundant separators and up-level references so that + A//B, A/B/, A/./B and A/foo/../B all become A/B. + On Windows, it converts forward slashes to backward slashes. + """ + return os.path.normpath(systemfilepath) + + # ################################################################# # Platform service # ################################################################# @@ -300,8 +312,9 @@ if __name__ == "__main__": print(_SF_Platform('PythonVersion')) # print(hashlib.algorithms_guaranteed) - print(_SF_FileSystem__HashFile('/opt/libreoffice6.4/program/libbootstraplo.so', 'md5')) - print(_SF_FileSystem__HashFile('/opt/libreoffice6.4/share/Scripts/python/Capitalise.py', 'sha512')) + print(_SF_FileSystem__HashFile('/opt/libreoffice7.3/program/libbootstraplo.so', 'md5')) + print(_SF_FileSystem__HashFile('/opt/libreoffice7.3/share/Scripts/python/Capitalise.py', 'sha512')) + print(_SF_FileSystem__Normalize('A/foo/../B/C/./D//E')) # print(_SF_String__HashStr('œ∑¡™£¢∞§¶•ªº–≠œ∑´®†¥¨ˆøπ“‘åß∂ƒ©˙∆˚¬', 'MD5')) # 616eb9c513ad07cd02924b4d285b9987 # diff --git a/wizards/source/scriptforge/python/scriptforge.py b/wizards/source/scriptforge/python/scriptforge.py index 86c70b26be50..452530a34c26 100644 --- a/wizards/source/scriptforge/python/scriptforge.py +++ b/wizards/source/scriptforge/python/scriptforge.py @@ -1157,6 +1157,9 @@ class SFScriptForge: def MoveFile(self, source, destination): return self.ExecMethod(self.vbMethod, 'MoveFile', source, destination) + def Normalize(self, filename): + return self.ExecMethod(self.vbMethod, 'Normalize', filename) + def MoveFolder(self, source, destination): return self.ExecMethod(self.vbMethod, 'MoveFolder', source, destination) diff --git a/wizards/source/sfdocuments/SF_Calc.xba b/wizards/source/sfdocuments/SF_Calc.xba index fe833f3c838a..8cfbd3419791 100644 --- a/wizards/source/sfdocuments/SF_Calc.xba +++ b/wizards/source/sfdocuments/SF_Calc.xba @@ -4525,4 +4525,4 @@ CatchSheet: End Function ' SFDocuments.SF_Calc._ValidateSheetName REM ============================================ END OF SFDOCUMENTS.SF_CALC -</script:module> +</script:module>
\ No newline at end of file diff --git a/wizards/source/sfunittests/SF_UnitTest.xba b/wizards/source/sfunittests/SF_UnitTest.xba index 5007fb6a7255..baeef90de3b3 100644 --- a/wizards/source/sfunittests/SF_UnitTest.xba +++ b/wizards/source/sfunittests/SF_UnitTest.xba @@ -227,8 +227,8 @@ Private Sub Class_Initialize() Set TestTimer = Nothing Set SuiteTimer = Nothing Set CaseTimer = Nothing - Set Exception = CreateScriptService("ScriptForge.Exception") - Set Session = CreateScriptService("ScriptForge.Session") + Set Exception = ScriptForge.SF_Exception ' Do not use CreateScriptService to allow New SF_UnitTest from other libraries + Set Session = ScriptForge.SF_Session End Sub ' SFUnitTests.SF_UnitTest Constructor REM ----------------------------------------------------------------------------- |