eric6/Utilities/__init__.py

changeset 8221
0572a215bd2f
parent 8217
385f60c94548
child 8240
93b8a353c4bf
--- a/eric6/Utilities/__init__.py	Sun Apr 11 12:38:16 2021 +0200
+++ b/eric6/Utilities/__init__.py	Sun Apr 11 16:53:48 2021 +0200
@@ -267,14 +267,17 @@
     except (UnicodeError, LookupError):
         pass
     
-    if Preferences.getEditor("AdvancedEncodingDetection"):
-        # Use the guessed one even if confifence level is low
-        if guess and guess['encoding'] is not None:
-            try:
-                codec = guess['encoding'].lower()
-                return str(text, codec), '{0}-guessed'.format(codec)
-            except (UnicodeError, LookupError):
-                pass
+    if (
+        Preferences.getEditor("AdvancedEncodingDetection") and
+        guess and
+        guess['encoding'] is not None
+    ):
+        # Use the guessed one even if confidence level is low
+        try:
+            codec = guess['encoding'].lower()
+            return str(text, codec), '{0}-guessed'.format(codec)
+        except (UnicodeError, LookupError):
+            pass
     
     # Assume UTF-8 loosing information
     return str(text, "utf-8", "ignore"), 'utf-8-ignore'
@@ -629,10 +632,7 @@
     @return dictionary of string, boolean, complex, float and int
     """
     flags = {}
-    if isinstance(text, str):
-        lines = text.rstrip().splitlines()
-    else:
-        lines = text
+    lines = text.rstrip().splitlines() if isinstance(text, str) else text
     for line in reversed(lines):
         try:
             index = line.index("eflag:")
@@ -823,11 +823,8 @@
         return False
     
     dirs = path.split(os.pathsep)
-    for directory in dirs:
-        if os.access(os.path.join(directory, file), os.X_OK):
-            return True
-    
-    return False
+    return any(os.access(os.path.join(directory, file), os.X_OK)
+               for directory in dirs)
 
 
 def startswithPath(path, start):
@@ -908,9 +905,8 @@
             return ""
         
     cur_path = os.path.join(os.curdir, file)
-    if os.path.exists(cur_path):
-        if os.access(cur_path, os.X_OK):
-            return cur_path
+    if os.path.exists(cur_path) and os.access(cur_path, os.X_OK):
+        return cur_path
 
     path = os.getenv('PATH')
     
@@ -945,9 +941,8 @@
             return []
         
     cur_path = os.path.join(os.curdir, file)
-    if os.path.exists(cur_path):
-        if os.access(cur_path, os.X_OK):
-            paths.append(cur_path)
+    if os.path.exists(cur_path) and os.access(cur_path, os.X_OK):
+        paths.append(cur_path)
 
     path = os.getenv('PATH')
     
@@ -986,9 +981,8 @@
     
     for filename in filenames:
         cur_path = os.path.join(os.curdir, filename)
-        if os.path.exists(cur_path):
-            if os.access(cur_path, os.X_OK):
-                return os.path.abspath(cur_path)
+        if os.path.exists(cur_path) and os.access(cur_path, os.X_OK):
+            return os.path.abspath(cur_path)
 
     path = os.getenv('PATH')
     
@@ -1166,10 +1160,7 @@
     @return list of all files and directories in the tree rooted
         at path. The names are expanded to start with path.
     """
-    if filesonly:
-        files = []
-    else:
-        files = [path]
+    files = [] if filesonly else [path]
     try:
         entries = os.listdir(path)
         for entry in entries:
@@ -1479,9 +1470,8 @@
     """
     user = getpass.getuser()
     
-    if isWindowsPlatform():
-        if not user:
-            return win32_GetUserName()
+    if isWindowsPlatform() and not user:
+        return win32_GetUserName()
     
     return user
 
@@ -1601,11 +1591,11 @@
                 line0 = source.splitlines()[0]
             else:
                 line0 = source[0]
-            if line0.startswith("#!"):
-                if "python3" in line0:
-                    pyVer = 3
-                elif "python" in line0:
-                    pyVer = 3
+            if (
+                line0.startswith("#!") and
+                (("python3" in line0) or ("python" in line0))
+            ):
+                pyVer = 3
         
         if pyVer == 0 and ext in py3Ext:
             pyVer = 3
@@ -1858,11 +1848,7 @@
     proc.setProcessChannelMode(QProcess.ProcessChannelMode.MergedChannels)
     proc.start(interpreter, args)
     finished = proc.waitForFinished(30000)
-    if finished:
-        if proc.exitCode() == 0:
-            return True
-    
-    return False
+    return finished and proc.exitCode() == 0
 
 ###############################################################################
 # Other utility functions below
@@ -1887,10 +1873,7 @@
     except (ImportError, AttributeError):
         sip_version_str = "sip version not available"
     
-    if sys.maxsize > 2**32:
-        sizeStr = "64-Bit"
-    else:
-        sizeStr = "32-Bit"
+    sizeStr = "64-Bit" if sys.maxsize > 2**32 else "32-Bit"
     
     info = ["Version Numbers:"]
     
@@ -2022,17 +2005,16 @@
     proc.setProcessChannelMode(QProcess.ProcessChannelMode.MergedChannels)
     proc.start(interpreter, args)
     finished = proc.waitForFinished(30000)
-    if finished:
-        if proc.exitCode() == 0:
-            text = proc.readAllStandardOutput()
-            sysPathResult = str(text, "utf-8", "replace").strip()
-            try:
-                sysPath = json.loads(sysPathResult)
-                if "" in sysPath:
-                    sysPath.remove("")
-            except (TypeError, ValueError):
-                # ignore faulty results and return empty list
-                pass
+    if finished and proc.exitCode() == 0:
+        text = proc.readAllStandardOutput()
+        sysPathResult = str(text, "utf-8", "replace").strip()
+        try:
+            sysPath = json.loads(sysPathResult)
+            if "" in sysPath:
+                sysPath.remove("")
+        except (TypeError, ValueError):
+            # ignore faulty results and return empty list
+            pass
     
     return sysPath
 

eric ide

mercurial