eric6/Utilities/__init__.py

branch
maintenance
changeset 8142
43248bafe9b2
parent 8043
0acf98cd089a
parent 8100
a4c231453cbc
child 8176
31965986ecd1
diff -r 874fdd14d3a2 -r 43248bafe9b2 eric6/Utilities/__init__.py
--- a/eric6/Utilities/__init__.py	Mon Feb 01 10:38:43 2021 +0100
+++ b/eric6/Utilities/__init__.py	Tue Mar 02 17:12:08 2021 +0100
@@ -16,6 +16,7 @@
 import getpass
 import ctypes
 import subprocess           # secok
+import shlex
 
 
 def __showwarning(message, category, filename, lineno, file=None, line=""):
@@ -1238,15 +1239,20 @@
     return dirs
 
 
-def findVolume(volumeName):
+def findVolume(volumeName, findAll=False):
     """
     Function to find the directory belonging to a given volume name.
     
     @param volumeName name of the volume to search for
     @type str
-    @return directory path of the given volume name
-    @rtype str
+    @param findAll flag indicating to get the directories for all volumes
+        starting with the given name (defaults to False)
+    @type bool (optional)
+    @return directory path or list of directory paths for the given volume
+        name
+    @rtype str or list of str
     """
+    volumeDirectories = []
     volumeDirectory = None
     
     if isWindowsPlatform():
@@ -1277,10 +1283,14 @@
         try:
             for disk in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
                 dirpath = "{0}:\\".format(disk)
-                if (os.path.exists(dirpath) and
-                        getVolumeName(dirpath) == volumeName):
-                    volumeDirectory = dirpath
-                    break
+                if os.path.exists(dirpath):
+                    if findAll:
+                        if getVolumeName(dirpath).startswith(volumeName):
+                            volumeDirectories.append(dirpath)
+                    else:
+                        if getVolumeName(dirpath) == volumeName:
+                            volumeDirectory = dirpath
+                            break
         finally:
             ctypes.windll.kernel32.SetErrorMode(oldMode)
     else:
@@ -1290,17 +1300,30 @@
                 mountOutput = (
                     subprocess.check_output(mountCommand).splitlines()  # secok
                 )
-                mountedVolumes = [x.split()[2] for x in mountOutput]
-                for volume in mountedVolumes:
-                    if volume.decode("utf-8").endswith(volumeName):
-                        volumeDirectory = volume.decode("utf-8")
+                mountedVolumes = [
+                    x.decode("utf-8").split(" type")[0].split(maxsplit=2)[2]
+                    for x in mountOutput
+                ]
+                if findAll:
+                    for volume in mountedVolumes:
+                        if volumeName in volume:
+                            volumeDirectories.append(volume)
+                    if volumeDirectories:
                         break
-                if volumeDirectory:
-                    break
+                else:
+                    for volume in mountedVolumes:
+                        if volume.endswith(volumeName):
+                            volumeDirectory = volume
+                            break
+                    if volumeDirectory:
+                        break
             except FileNotFoundError:
                 pass
     
-    return volumeDirectory
+    if findAll:
+        return volumeDirectories
+    else:
+        return volumeDirectory
 
 
 def getTestFileName(fn):
@@ -1321,58 +1344,23 @@
     """
     Function used to convert an option string into a list of options.
     
-    @param s option string (string or string)
-    @return list of options (list of strings)
-    """
-    rx = re.compile(r"""\s([\w=/-]*"[^"]+"|[\w=/-]*'[^']+'|[^\s]+)""")
-    s = re.sub(r"%[A-Z%]", _percentReplacementFunc, s)
-    return parseString(s, rx)
-    
-
-def parseEnvironmentString(s):
-    """
-    Function used to convert an environment string into a list of environment
-    settings.
-    
-    @param s environment string (string)
-    @return list of environment settings (list of strings)
-    """
-    rx = re.compile(r"""\s(\w+\+?=[^\s]+|\w+="[^"]+"|\w+='[^']+')""")
-    return parseString(s, rx)
-
-
-def parseString(s, rx):
-    """
-    Function used to convert a string into a list.
-    
-    @param s string to be parsed
+    @param s option string
     @type str
-    @param rx regular expression object defining the parse pattern
-    @type re.Pattern
-    @return list of parsed data
+    @return list of options
     @rtype list of str
     """
-    olist = []
-    if s:
-        if not s.startswith(' '):
-            # prepare the  string to fit our pattern
-            s = ' ' + s
-        
-        for match in rx.finditer(s):
-            cs = match.group(1)
-            if cs.startswith('"') or cs.startswith("'"):
-                cs = cs[1:-1]
-            olist.append(cs)
-    
-    return olist
+    s = re.sub(r"%[A-Z%]", _percentReplacementFunc, s)
+    return shlex.split(s)
 
 
 def _percentReplacementFunc(matchobj):
     """
     Protected function called for replacing % codes.
     
-    @param matchobj matchobject for the code
+    @param matchobj match object for the code
+    @type re.Match
     @return replacement string
+    @rtype str
     """
     return getPercentReplacement(matchobj.group(0))
     
@@ -1381,8 +1369,10 @@
     """
     Function to get the replacement for code.
     
-    @param code code indicator (string)
-    @return replacement string (string)
+    @param code code indicator
+    @type str
+    @return replacement string
+    @rtype str
     """
     if code in ["C", "%C"]:
         # column of the cursor of the current editor

eric ide

mercurial