Modernize the code by using os.scandir() instead of os.listdir(). eric7

Sat, 24 Dec 2022 17:31:46 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 24 Dec 2022 17:31:46 +0100
branch
eric7
changeset 9646
ab5678db972f
parent 9645
31aaa11672d3
child 9647
e4c2ae118781

Modernize the code by using os.scandir() instead of os.listdir().

src/eric7/Graphics/ApplicationDiagramBuilder.py file | annotate | diff | comparison | revisions
src/eric7/Graphics/PackageDiagramBuilder.py file | annotate | diff | comparison | revisions
src/eric7/PluginManager/PluginRepositoryDialog.py file | annotate | diff | comparison | revisions
src/eric7/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckerDialog.py file | annotate | diff | comparison | revisions
src/eric7/Project/Project.py file | annotate | diff | comparison | revisions
src/eric7/SystemUtilities/FileSystemUtilities.py file | annotate | diff | comparison | revisions
src/eric7/eric7_doc.py file | annotate | diff | comparison | revisions
--- a/src/eric7/Graphics/ApplicationDiagramBuilder.py	Fri Dec 23 11:37:49 2022 +0100
+++ b/src/eric7/Graphics/ApplicationDiagramBuilder.py	Sat Dec 24 17:31:46 2022 +0100
@@ -74,7 +74,7 @@
             0,
             tot,
             self.tr("%v/%m Modules"),
-            self.parent(),
+            None,
         )
         progress.setWindowTitle(self.tr("Application Diagram"))
         try:
@@ -117,25 +117,27 @@
             # project is a package
             return path
         else:
-            # TODO: replace os.listdir() with os.scandir()
             # check, if any of the top directories is a package
-            for entry in [e for e in os.listdir(path) if not e.startswith(".")]:
-                fullpath = os.path.join(path, entry)
-                if os.path.isdir(fullpath):
-                    init = os.path.join(fullpath, "__init__.py")
-                    if os.path.exists(init):
-                        candidates.append(fullpath)
+            with os.scandir(path) as dirEntriesIterator:
+                for entry in [
+                    e for e in dirEntriesIterator if not e.name.startswith(".")
+                ]:
+                    if entry.is_dir() and os.path.exists(
+                        os.path.join(entry.path, "__init__.py")
+                    ):
+                        candidates.append(entry.path)
 
-            # TODO: replace os.listdir() with os.scandir()
             # check, if project uses the 'src' layout
-            if os.path.exists(os.path.join(path, "src")):
-                srcPath = os.path.join(path, "src")
-                for entry in [e for e in os.listdir(srcPath) if not e.startswith(".")]:
-                    fullpath = os.path.join(srcPath, entry)
-                    if os.path.isdir(fullpath):
-                        init = os.path.join(fullpath, "__init__.py")
-                        if os.path.exists(init):
-                            candidates.append(fullpath)
+            srcPath = os.path.join(path, "src")
+            if os.path.exists(srcPath):
+                with os.scandir(srcPath) as dirEntriesIterator:
+                    for entry in [
+                        e for e in dirEntriesIterator if not e.name.startswith(".")
+                    ]:
+                        if entry.is_dir() and os.path.exists(
+                            os.path.join(entry.path, "__init__.py")
+                        ):
+                            candidates.append(entry.path)
 
             if len(candidates) == 1:
                 return candidates[0]
--- a/src/eric7/Graphics/PackageDiagramBuilder.py	Fri Dec 23 11:37:49 2022 +0100
+++ b/src/eric7/Graphics/PackageDiagramBuilder.py	Sat Dec 24 17:31:46 2022 +0100
@@ -154,15 +154,14 @@
         subpackagesDict = {}
         subpackagesList = []
 
-        # TODO: replace os.listdir() with os.scandir()
-        for subpackage in os.listdir(self.package):
-            subpackagePath = os.path.join(self.package, subpackage)
-            if (
-                os.path.isdir(subpackagePath)
-                and subpackage != "__pycache__"
-                and len(glob.glob(os.path.join(subpackagePath, "__init__.*"))) != 0
-            ):
-                subpackagesList.append(subpackagePath)
+        with os.scandir(self.package) as dirEntriesIterator:
+            for subpackage in dirEntriesIterator:
+                if (
+                    subpackage.is_dir()
+                    and subpackage.name != "__pycache__"
+                    and len(glob.glob(os.path.join(subpackage.path, "__init__.*"))) != 0
+                ):
+                    subpackagesList.append(subpackage.path)
 
         tot = 0
         for ext in supportedExt:
@@ -178,6 +177,7 @@
         )
         progress.setWindowTitle(self.tr("Package Diagram"))
         try:
+            start = 0
             progress.show()
             QApplication.processEvents()
 
@@ -190,7 +190,7 @@
                     modules.extend(
                         glob.glob(FileSystemUtilities.normjoinpath(subpackage, ext))
                     )
-                for prog, module in enumerate(modules):
+                for prog, module in enumerate(modules, start=start):
                     progress.setValue(prog)
                     if time.monotonic() - now > 0.01:
                         QApplication.processEvents()
@@ -206,6 +206,7 @@
                         if "." in name:
                             name = name.rsplit(".", 1)[1]
                         subpackagesDict[packageName].append(name)
+                start = prog
                 subpackagesDict[packageName].sort()
                 # move __init__ to the front
                 if "__init__" in subpackagesDict[packageName]:
--- a/src/eric7/PluginManager/PluginRepositoryDialog.py	Fri Dec 23 11:37:49 2022 +0100
+++ b/src/eric7/PluginManager/PluginRepositoryDialog.py	Sat Dec 24 17:31:46 2022 +0100
@@ -1082,33 +1082,35 @@
             pluginsRegister.append(pluginName)
 
     downloadPath = Preferences.getPluginManager("DownloadPath")
-    downloads = {}  # plug-in name as key, file name as value
+    downloads = {}  # plug-in name as key, file name and version as value
 
-    # TODO: replace os.listdir() with os.scandir()
     # step 1: extract plug-ins and downloaded files
-    for pluginFile in os.listdir(downloadPath):
-        if not os.path.isfile(os.path.join(downloadPath, pluginFile)):
-            continue
+    with os.scandir(downloadPath) as dirEntriesIterator:
+        for pluginFile in dirEntriesIterator:
+            if not pluginFile.is_file():
+                continue
 
-        try:
-            pluginName, pluginVersion = pluginFile.replace(".zip", "").rsplit("-", 1)
-            pluginVersionList = re.split("[._-]", pluginVersion)
-            for index in range(len(pluginVersionList)):
-                try:
-                    pluginVersionList[index] = int(pluginVersionList[index])
-                except ValueError:
-                    # use default of 0
-                    pluginVersionList[index] = 0
-        except ValueError:
-            # rsplit() returned just one entry, i.e. file name doesn't contain
-            # version info separated by '-'
-            # => assume version 0.0.0
-            pluginName = pluginFile.replace(".zip", "")
-            pluginVersionList = [0, 0, 0]
+            try:
+                pluginName, pluginVersion = pluginFile.name.replace(".zip", "").rsplit(
+                    "-", 1
+                )
+                pluginVersionList = re.split("[._-]", pluginVersion)
+                for index in range(len(pluginVersionList)):
+                    try:
+                        pluginVersionList[index] = int(pluginVersionList[index])
+                    except ValueError:
+                        # use default of 0
+                        pluginVersionList[index] = 0
+            except ValueError:
+                # rsplit() returned just one entry, i.e. file name doesn't contain
+                # version info separated by '-'
+                # => assume version 0.0.0
+                pluginName = pluginFile.replace(".zip", "")
+                pluginVersionList = [0, 0, 0]
 
-        if pluginName not in downloads:
-            downloads[pluginName] = []
-        downloads[pluginName].append((pluginFile, tuple(pluginVersionList)))
+            if pluginName not in downloads:
+                downloads[pluginName] = []
+            downloads[pluginName].append((pluginFile, tuple(pluginVersionList)))
 
     # step 2: delete old entries
     hiddenPlugins = Preferences.getPluginManager("HiddenPlugins")
--- a/src/eric7/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckerDialog.py	Fri Dec 23 11:37:49 2022 +0100
+++ b/src/eric7/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckerDialog.py	Sat Dec 24 17:31:46 2022 +0100
@@ -191,11 +191,15 @@
         if isinstance(fn, list):
             files = fn
         elif os.path.isdir(fn):
-            files = []
-            for ext in self.syntaxCheckService.getExtensions():
-                files.extend(
-                    FileSystemUtilities.direntries(fn, True, "*{0}".format(ext), 0)
-                )
+            files = FileSystemUtilities.direntries(
+                fn,
+                filesonly=True,
+                pattern=[
+                    "*{0}".format(ext)
+                    for ext in self.syntaxCheckService.getExtensions()
+                ],
+                followsymlinks=False,
+            )
         else:
             files = [fn]
 
--- a/src/eric7/Project/Project.py	Fri Dec 23 11:37:49 2022 +0100
+++ b/src/eric7/Project/Project.py	Sat Dec 24 17:31:46 2022 +0100
@@ -2030,26 +2030,29 @@
         self.__addSingleDirectory(filetype, source, target, True)
 
         ignore_patterns = [
+            ".svn",
+            ".hg",
+            ".git",
+            ".ropeproject",
+            ".eric7project",
+            ".jedi",
+            "__pycache__",
+        ] + [
             pattern
             for pattern, filetype in self.__pdata["FILETYPES"].items()
             if filetype == "__IGNORE__"
         ]
 
-        # TODO: replace os.listdir() with os.scandir()
         # now recurse into subdirectories
-        for name in os.listdir(source):
-            ns = os.path.join(source, name)
-            if os.path.isdir(ns):
-                skip = False
-                for ignore_pattern in ignore_patterns:
-                    if fnmatch.fnmatch(name, ignore_pattern):
-                        skip = True
-                        break
-                if skip:
-                    continue
-
-                nt = os.path.join(target, name)
-                self.__addRecursiveDirectory(filetype, ns, nt)
+        with os.scandir(source) as dirEntriesIterator:
+            for dirEntry in dirEntriesIterator:
+                if dirEntry.is_dir() and not any(
+                    fnmatch.fnmatch(dirEntry.name, ignore_pattern)
+                    for ignore_pattern in ignore_patterns
+                ):
+                    self.__addRecursiveDirectory(
+                        filetype, dirEntry.path, os.path.join(target, dirEntry.name)
+                    )
 
     @pyqtSlot()
     def addDirectory(self, fileTypeFilter=None, startdir=None):
@@ -6925,10 +6928,11 @@
         """
         with os.scandir(self.getProjectPath()) as ppathDirEntriesIterator:
             for dirEntry in ppathDirEntriesIterator:
-                if dirEntry.is_dir():
-                    # potential venv directory; check for 'pyvenv.cfg'
-                    if os.path.exists(os.path.join(dirEntry.path, "pyvenv.cfg")):
-                        return dirEntry.path
+                # potential venv directory; check for 'pyvenv.cfg'
+                if dirEntry.is_dir() and os.path.exists(
+                    os.path.join(dirEntry.path, "pyvenv.cfg")
+                ):
+                    return dirEntry.path
 
         # check for some common names in case 'pyvenv.cfg' is missing
         for venvPathName in (".venv", "venv", ".env", "env"):
--- a/src/eric7/SystemUtilities/FileSystemUtilities.py	Fri Dec 23 11:37:49 2022 +0100
+++ b/src/eric7/SystemUtilities/FileSystemUtilities.py	Sat Dec 24 17:31:46 2022 +0100
@@ -462,41 +462,42 @@
         ".ropeproject",
         ".eric7project",
         ".jedi",
+        "__pycache__",
     ]
     if ignore is not None:
         ignoreList.extend(ignore)
 
-    # TODO: replace os.listdir() with os.scandir()
-    try:
-        entries = os.listdir(path)
-        for entry in entries:
+    with contextlib.suppress(OSError, UnicodeDecodeError), os.scandir(
+        path
+    ) as dirEntriesIterator:
+        for dirEntry in dirEntriesIterator:
             if checkStop and checkStop():
                 break
 
-            if entry in ignoreList:
+            if dirEntry.name in ignoreList:
                 continue
 
-            fentry = os.path.join(path, entry)
             if (
                 pattern
-                and not os.path.isdir(fentry)
-                and not any(fnmatch.fnmatch(entry, p) for p in patterns)
+                and not dirEntry.is_dir()
+                and not any(fnmatch.fnmatch(dirEntry.name, p) for p in patterns)
             ):
                 # entry doesn't fit the given pattern
                 continue
 
-            if os.path.isdir(fentry):
-                if os.path.islink(fentry) and not followsymlinks:
+            if dirEntry.is_dir():
+                if dirEntry.is_symlink() and not followsymlinks:
                     continue
                 files += direntries(
-                    fentry, filesonly, pattern, followsymlinks, checkStop
+                    dirEntry.path,
+                    filesonly=filesonly,
+                    pattern=pattern,
+                    followsymlinks=followsymlinks,
+                    checkStop=checkStop,
+                    ignore=ignore,
                 )
             else:
-                files.append(fentry)
-    except OSError:
-        pass
-    except UnicodeDecodeError:
-        pass
+                files.append(dirEntry.path)
     return files
 
 
@@ -508,31 +509,21 @@
     @param excludeDirs basename of directories to ignore
     @return list of all directories found
     """
-    # TODO: replace os.listdir() with os.scandir()
     try:
-        names = os.listdir(path)
+        dirs = []
+        with os.scandir(path) as dirEntriesIterator:
+            for dirEntry in dirEntriesIterator:
+                if (
+                    dirEntry.is_dir()
+                    and not dirEntry.is_symlink()
+                    and dirEntry.name not in excludeDirs
+                ):
+                    dirs.append(dirEntry.path)
+                    dirs.extend(getDirs(dirEntry.path, excludeDirs))
+        return dirs
     except OSError:
         return []
 
-    dirs = []
-    for name in names:
-        if os.path.isdir(os.path.join(path, name)) and not os.path.islink(
-            os.path.join(path, name)
-        ):
-            exclude = 0
-            for e in excludeDirs:
-                if name.split(os.sep, 1)[0] == e:
-                    exclude = 1
-                    break
-            if not exclude:
-                dirs.append(os.path.join(path, name))
-
-    for name in dirs[:]:
-        if not os.path.islink(name):
-            dirs += getDirs(name, excludeDirs)
-
-    return dirs
-
 
 def findVolume(volumeName, findAll=False):
     """
--- a/src/eric7/eric7_doc.py	Fri Dec 23 11:37:49 2022 +0100
+++ b/src/eric7/eric7_doc.py	Sat Dec 24 17:31:46 2022 +0100
@@ -195,6 +195,7 @@
         "build",
         "doc",
         "docs",
+        "__pycache__",
     ]
     excludePatterns = []
     startDir = ""

eric ide

mercurial