summaryrefslogtreecommitdiff
path: root/wizards
diff options
context:
space:
mode:
authorJean-Pierre Ledure <jp@ledure.be>2022-07-05 17:47:56 +0200
committerJean-Pierre Ledure <jp@ledure.be>2022-07-06 12:27:09 +0200
commit7c0f0afd58c00211236b138ddd4804099c5aec83 (patch)
tree1436e3d75133213f3927179b624b6e806b10638f /wizards
parentfd262525f45b389cd61d881c78d5d270afe298d6 (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.xba44
-rw-r--r--wizards/source/scriptforge/python/ScriptForgeHelper.py17
-rw-r--r--wizards/source/scriptforge/python/scriptforge.py3
-rw-r--r--wizards/source/sfdocuments/SF_Calc.xba2
-rw-r--r--wizards/source/sfunittests/SF_UnitTest.xba4
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
, &quot;HashFile&quot; _
, &quot;MoveFile&quot; _
, &quot;MoveFolder&quot; _
+ , &quot;Normalize&quot; _
, &quot;OpenTextFile&quot; _
, &quot;PickFile&quot; _
, &quot;PickFolder&quot; _
@@ -1384,6 +1385,47 @@ Catch:
End Function &apos; ScriptForge.SF_FileSystem.MoveFolder
REM -----------------------------------------------------------------------------
+Public Function Normalize(Optional ByVal FileName As Variant) As String
+&apos;&apos;&apos; Normalize a pathname by collapsing redundant separators and up-level references
+&apos;&apos;&apos; so that A//B, A/B/, A/./B and A/foo/../B all become A/B.
+&apos;&apos;&apos; On Windows, it converts forward slashes to backward slashes.
+&apos;&apos;&apos; Args:
+&apos;&apos;&apos; FileName: a string representing a file. The file may not exist.
+&apos;&apos;&apos; Returns:
+&apos;&apos;&apos; The normalized filename in filenaming notation
+&apos;&apos;&apos; Example:
+&apos;&apos;&apos; Print SF_FileSystem.Normalize(&quot;A/foo/../B/C/./D//E&quot;) &apos; A/B/C/D/E
+
+Dim sNorm As String &apos; Return value
+Const cstPyHelper = &quot;$&quot; &amp; &quot;_SF_FileSystem__Normalize&quot;
+Const cstThisSub = &quot;FileSystem.Normalize&quot;
+Const cstSubArgs = &quot;FileName&quot;
+
+ If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
+ sNorm = &quot;&quot;
+
+Check:
+ If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
+ If Not SF_Utils._ValidateFile(FileName, &quot;FileName&quot;) Then GoTo Finally
+ End If
+
+Try:
+ With ScriptForge.SF_Session
+ sNorm = .ExecutePythonScript(.SCRIPTISSHARED, _SF_.PythonHelper &amp; cstPyHelper _
+ , _ConvertFromUrl(FileName))
+ &apos; The Python os.path expects and returns a file name in os notation
+ If SF_FileSystem.FileNaming &lt;&gt; &quot;SYS&quot; Then sNorm = ConvertToUrl(sNorm)
+ End With
+
+Finally:
+ Normalize = sNorm
+ SF_Utils._ExitFunction(cstThisSub)
+ Exit Function
+Catch:
+ GoTo Finally
+End Function &apos; 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 &apos; ScriptForge.FileSystem._ConvertFromUrl
REM -----------------------------------------------------------------------------
Private Function _ConvertToUrl(psFile) As String
&apos;&apos;&apos; Execute the builtin ConvertToUrl function only when relevant
-&apos;&apos;&apos; i.e. when FileNaming (how arguments and return values are provided) = &quot;SYS&quot;
+&apos;&apos;&apos; i.e. when FileNaming (how arguments and return values are provided) &lt;&gt; &quot;URL&quot;
&apos;&apos;&apos; Called at the top of methods receiving file names as arguments
&apos;&apos;&apos; 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 &apos; 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(&quot;ScriptForge.Exception&quot;)
- Set Session = CreateScriptService(&quot;ScriptForge.Session&quot;)
+ Set Exception = ScriptForge.SF_Exception &apos; Do not use CreateScriptService to allow New SF_UnitTest from other libraries
+ Set Session = ScriptForge.SF_Session
End Sub &apos; SFUnitTests.SF_UnitTest Constructor
REM -----------------------------------------------------------------------------