فهرست منبع

init: Solve most of Flake8 issues in grass.py (#1536)

This solves most of issues reported by Flake8 in grass.py and enables these checks
when possible. The rest is ignored using per-file ignores.

Configuration moved to the main .flake8 file in source code root directory
(CI now checks lib/init together with the other files from root dir).
Vaclav Petras 4 سال پیش
والد
کامیت
2312a5da4b
4فایلهای تغییر یافته به همراه42 افزوده شده و 63 حذف شده
  1. 6 2
      .flake8
  2. 0 1
      .github/workflows/flake8.yml
  3. 0 28
      lib/init/.flake8
  4. 36 32
      lib/init/grass.py

+ 6 - 2
.flake8

@@ -5,8 +5,13 @@ ignore =
 
 
 per-file-ignores =
 per-file-ignores =
     # Quick reference for some of the most common errors
     # Quick reference for some of the most common errors
-    # F401 imported but unused
     # E501 line too long
     # E501 line too long
+    # E722 do not use bare 'except'
+    # W605 invalid escape sequence
+    # F401 imported but unused
+    # F821 undefined name 'unicode'
+    # F841 local variable assigned to but never used
+    lib/init/grass.py: E501, E722, F821, F841, W605
     utils/mkrest.py: E501, W605
     utils/mkrest.py: E501, W605
     utils/gitlog2changelog.py: E722, E712, W605
     utils/gitlog2changelog.py: E722, E712, W605
     utils/g.html2man/ggroff.py: W605
     utils/g.html2man/ggroff.py: W605
@@ -51,7 +56,6 @@ exclude =
     # Test output directory
     # Test output directory
     testreport,
     testreport,
     # Have their own configs for now
     # Have their own configs for now
-    lib/init,
     python/grass,
     python/grass,
     gui/wxpython,
     gui/wxpython,
     scripts/*/*.py,
     scripts/*/*.py,

+ 0 - 1
.github/workflows/flake8.yml

@@ -13,7 +13,6 @@ jobs:
         directory:
         directory:
           - .
           - .
           - gui/wxpython
           - gui/wxpython
-          - lib/init
           - python/grass
           - python/grass
           - scripts
           - scripts
           - temporal
           - temporal

+ 0 - 28
lib/init/.flake8

@@ -1,28 +0,0 @@
-[flake8]
-ignore =
-    E203,  # whitespace before ':' (Black)
-    W503,  # line break before binary operator (Black)
-    E501, # line too long
-    E722, # do not use bare 'except'
-    E741, # ambiguous variable name 'l'
-    F401, # 'types' imported but unused
-    F522, # '...'.format(...) has unused named argument(s): version
-    F821, # undefined name
-    F841, # local variable 'e' is assigned to but never used
-    W605, # invalid escape sequence '\.'
-
-max-line-length = 88
-
-exclude =
-    .git,
-    __pycache__,
-    .env,
-    .venv,
-    env,
-    venv,
-    ENV,
-    env.bak,
-    venv.bak,
-
-builtins =
-    _

+ 36 - 32
lib/init/grass.py

@@ -47,7 +47,6 @@ import shutil
 import signal
 import signal
 import string
 import string
 import subprocess
 import subprocess
-import types
 import re
 import re
 import six
 import six
 import platform
 import platform
@@ -79,8 +78,9 @@ if ENCODING is None:
 # possibly same for GRASS_PROJSHARE and others but maybe not
 # possibly same for GRASS_PROJSHARE and others but maybe not
 #
 #
 # We need to simultaneously make sure that:
 # We need to simultaneously make sure that:
-# - we get GISBASE from os.environ if it is defined (doesn't this mean that we are already
-#   inside a GRASS session? If we are, why do we need to run this script again???).
+# - we get GISBASE from os.environ if it is defined (doesn't this mean that we are
+#   already inside a GRASS session? If we are, why do we need to run this script
+#   again???).
 # - GISBASE exists as an ENV variable
 # - GISBASE exists as an ENV variable
 #
 #
 # pmav99: Ugly as hell, but that's what the code before the refactoring was doing.
 # pmav99: Ugly as hell, but that's what the code before the refactoring was doing.
@@ -158,7 +158,7 @@ def to_text_string(obj, encoding=ENCODING):
 def try_remove(path):
 def try_remove(path):
     try:
     try:
         os.remove(path)
         os.remove(path)
-    except:
+    except:  # noqa: E722
         pass
         pass
 
 
 
 
@@ -376,7 +376,8 @@ def help_message(default_gui):
                 "exit after creation of location or mapset. Only with -c flag"
                 "exit after creation of location or mapset. Only with -c flag"
             ),
             ),
             force_removal=_(
             force_removal=_(
-                "force removal of .gislock if exists (use with care!). Only with --text flag"
+                "force removal of .gislock if exists (use with care!)."
+                " Only with --text flag"
             ),
             ),
             text=_("use text based interface (skip graphical welcome screen)"),
             text=_("use text based interface (skip graphical welcome screen)"),
             text_detail=_("and set as default"),
             text_detail=_("and set as default"),
@@ -393,7 +394,8 @@ def help_message(default_gui):
             gisdbase_detail=_("directory containing Locations"),
             gisdbase_detail=_("directory containing Locations"),
             location=_("initial GRASS Location"),
             location=_("initial GRASS Location"),
             location_detail=_(
             location_detail=_(
-                "directory containing Mapsets with one common coordinate system (projection)"
+                "directory containing Mapsets with one common"
+                " coordinate system (projection)"
             ),
             ),
             mapset=_("initial GRASS Mapset"),
             mapset=_("initial GRASS Mapset"),
             full_mapset=_("fully qualified initial Mapset directory"),
             full_mapset=_("fully qualified initial Mapset directory"),
@@ -493,7 +495,7 @@ def create_tmp(user, gis_lock):
         )
         )
         try:
         try:
             os.mkdir(tmpdir, 0o700)
             os.mkdir(tmpdir, 0o700)
-        except:
+        except:  # noqa: E722
             tmp = None
             tmp = None
 
 
     if not tmp:
     if not tmp:
@@ -504,7 +506,7 @@ def create_tmp(user, gis_lock):
             )
             )
             try:
             try:
                 os.mkdir(tmpdir, 0o700)
                 os.mkdir(tmpdir, 0o700)
-            except:
+            except:  # noqa: E722
                 tmp = None
                 tmp = None
             if tmp:
             if tmp:
                 break
                 break
@@ -1041,19 +1043,18 @@ def set_mapset(
 
 
     # TODO: arg param seems to be always the mapset parameter (or a dash
     # TODO: arg param seems to be always the mapset parameter (or a dash
     # in a distant past), refactor
     # in a distant past), refactor
-    l = arg
-    if l:
+    if arg:
         # TODO: the block below could be just one line: os.path.abspath(l)
         # TODO: the block below could be just one line: os.path.abspath(l)
         # abspath both resolves relative paths and normalizes the path
         # abspath both resolves relative paths and normalizes the path
         # so that trailing / is stripped away and split then always returns
         # so that trailing / is stripped away and split then always returns
         # non-empty element as the last element (which is good for both mapset
         # non-empty element as the last element (which is good for both mapset
         # and location split)
         # and location split)
-        if l == ".":
-            l = os.getcwd()
-        elif not os.path.isabs(l):
-            l = os.path.abspath(l)
-        if l.endswith(os.path.sep):
-            l = l.rstrip(os.path.sep)
+        if arg == ".":
+            arg = os.getcwd()
+        elif not os.path.isabs(arg):
+            arg = os.path.abspath(arg)
+        if arg.endswith(os.path.sep):
+            arg = arg.rstrip(os.path.sep)
             # now we can get the last element by split on the first go
             # now we can get the last element by split on the first go
             # and it works for the last element being mapset or location
             # and it works for the last element being mapset or location
 
 
@@ -1062,9 +1063,9 @@ def set_mapset(
             mapset = "tmp_" + uuid.uuid4().hex
             mapset = "tmp_" + uuid.uuid4().hex
             create_new = True
             create_new = True
         else:
         else:
-            l, mapset = os.path.split(l)
-        l, location_name = os.path.split(l)
-        gisdbase = l
+            arg, mapset = os.path.split(arg)
+        arg, location_name = os.path.split(arg)
+        gisdbase = arg
 
 
     # all was None for tmp loc so that case goes here quickly
     # all was None for tmp loc so that case goes here quickly
     # TODO: but the above code needs review anyway
     # TODO: but the above code needs review anyway
@@ -1366,7 +1367,8 @@ def set_language(grass_config_dir):
     # Unfortunately currently a working solution for Windows is lacking
     # Unfortunately currently a working solution for Windows is lacking
     # thus it always on Vista and XP will print an error.
     # thus it always on Vista and XP will print an error.
     # See discussion for Windows not following its own documentation and
     # See discussion for Windows not following its own documentation and
-    # not accepting ISO codes as valid locale identifiers http://bugs.python.org/issue10466
+    # not accepting ISO codes as valid locale identifiers
+    # http://bugs.python.org/issue10466
     language = "None"  # Such string sometimes is present in wx file
     language = "None"  # Such string sometimes is present in wx file
     encoding = None
     encoding = None
 
 
@@ -1426,7 +1428,7 @@ def set_language(grass_config_dir):
         )
         )
         try:
         try:
             locale.setlocale(locale.LC_ALL, language)
             locale.setlocale(locale.LC_ALL, language)
-        except locale.Error as e:
+        except locale.Error:
             try:
             try:
                 # Locale lang.encoding might be missing. Let's try
                 # Locale lang.encoding might be missing. Let's try
                 # UTF-8 encoding before giving up as on Linux systems
                 # UTF-8 encoding before giving up as on Linux systems
@@ -1489,14 +1491,16 @@ def set_language(grass_config_dir):
                         normalized = locale.normalize("%s.%s" % (language, encoding))
                         normalized = locale.normalize("%s.%s" % (language, encoding))
                         locale.setlocale(locale.LC_ALL, normalized)
                         locale.setlocale(locale.LC_ALL, normalized)
                     except locale.Error as e:
                     except locale.Error as e:
-                        # If we got so far, attempts to set up language and locale have failed
-                        # on this system
+                        # If we got so far, attempts to set up language and locale have
+                        # failed on this system.
                         sys.stderr.write(
                         sys.stderr.write(
                             "Failed to enforce user specified language '%s' with error: '%s'\n"
                             "Failed to enforce user specified language '%s' with error: '%s'\n"
                             % (language, e)
                             % (language, e)
                         )
                         )
                         sys.stderr.write(
                         sys.stderr.write(
-                            "A LANGUAGE environmental variable has been set.\nPart of messages will be displayed in the requested language.\n"
+                            "A LANGUAGE environmental variable has been set.\n"
+                            "Part of messages will be displayed in"
+                            " the requested language.\n"
                         )
                         )
                         # Even if setting locale will fail, let's set LANG in a hope,
                         # Even if setting locale will fail, let's set LANG in a hope,
                         # that UI will use it GRASS texts will be in selected language,
                         # that UI will use it GRASS texts will be in selected language,
@@ -1563,8 +1567,6 @@ def lock_mapset(mapset_path, force_gislock_removal, user):
     Behavior on error must be changed somehow; now it fatals but GUI case is
     Behavior on error must be changed somehow; now it fatals but GUI case is
     unresolved.
     unresolved.
     """
     """
-    from grass.grassdb.checks import is_mapset_valid
-
     if not os.path.exists(mapset_path):
     if not os.path.exists(mapset_path):
         fatal(_("Path '%s' doesn't exist") % mapset_path)
         fatal(_("Path '%s' doesn't exist") % mapset_path)
     if not os.access(mapset_path, os.W_OK):
     if not os.access(mapset_path, os.W_OK):
@@ -1902,9 +1904,9 @@ def csh_startup(location, grass_env_file):
         if os.access(path, os.R_OK):
         if os.access(path, os.R_OK):
             s = readfile(path)
             s = readfile(path)
             lines = s.splitlines()
             lines = s.splitlines()
-            for l in lines:
-                if mail_re.match(l):
-                    f.write(l)
+            for line in lines:
+                if mail_re.match(line):
+                    f.write(line)
 
 
     path = os.getenv("PATH").split(":")
     path = os.getenv("PATH").split(":")
     f.write("set path = ( %s ) \n" % " ".join(path))
     f.write("set path = ( %s ) \n" % " ".join(path))
@@ -1974,7 +1976,7 @@ def sh_like_startup(location, location_name, grass_env_file, sh):
     else:
     else:
         f.write(
         f.write(
             "PS1='{name} {db_place}:\\W > '\n".format(
             "PS1='{name} {db_place}:\\W > '\n".format(
-                name=grass_name, version=GRASS_VERSION, db_place="$_GRASS_DB_PLACE"
+                name=grass_name, db_place="$_GRASS_DB_PLACE"
             )
             )
         )
         )
 
 
@@ -2548,13 +2550,15 @@ def main():
                 if not default_gisdbase:
                 if not default_gisdbase:
                     fatal(
                     fatal(
                         _(
                         _(
-                            "Failed to start GRASS GIS, grassdata directory could not be found or created."
+                            "Failed to start GRASS GIS, grassdata directory cannot"
+                            " be found or created."
                         )
                         )
                     )
                     )
                 elif not default_location:
                 elif not default_location:
                     fatal(
                     fatal(
                         _(
                         _(
-                            "Failed to start GRASS GIS, no default location to copy in the installation or copying failed."
+                            "Failed to start GRASS GIS, no default location to copy in"
+                            " the installation or copying failed."
                         )
                         )
                     )
                     )
                 if can_start_in_mapset(
                 if can_start_in_mapset(