From bb69bf9e43cead6f56cdd8f73208d34e3b6f686b Mon Sep 17 00:00:00 2001 From: Arohan Ajit Date: Sat, 21 Sep 2024 10:41:46 +0000 Subject: [PATCH 1/8] updated precommit --- gui/wxpython/core/gcmd.py | 12 ++++++------ gui/wxpython/core/gconsole.py | 3 ++- gui/wxpython/core/render.py | 6 +++++- gui/wxpython/core/settings.py | 9 +++++---- gui/wxpython/core/toolboxes.py | 16 +++++++++++++--- gui/wxpython/core/utils.py | 24 ++++++++++++++++++------ gui/wxpython/core/watchdog.py | 9 ++++----- gui/wxpython/core/workspace.py | 17 +++++++++++++---- 8 files changed, 66 insertions(+), 30 deletions(-) diff --git a/gui/wxpython/core/gcmd.py b/gui/wxpython/core/gcmd.py index 7d4ed678d79..b93789f22f7 100644 --- a/gui/wxpython/core/gcmd.py +++ b/gui/wxpython/core/gcmd.py @@ -36,6 +36,12 @@ from threading import Thread import wx +from core.debug import Debug +from core.globalvar import SCT_EXT + +from grass.script import core as grass +from grass.script.utils import decode, encode + is_mswindows = sys.platform == "win32" if is_mswindows: from win32file import ReadFile, WriteFile @@ -45,12 +51,6 @@ import select import fcntl -from core.debug import Debug -from core.globalvar import SCT_EXT - -from grass.script import core as grass -from grass.script.utils import decode, encode - def DecodeString(string): """Decode string using system encoding diff --git a/gui/wxpython/core/gconsole.py b/gui/wxpython/core/gconsole.py index bad4aac29cd..b8934837eaf 100644 --- a/gui/wxpython/core/gconsole.py +++ b/gui/wxpython/core/gconsole.py @@ -691,7 +691,8 @@ def load_source(modname, filename): if len(command) == 1 and not skipInterface: try: task = gtask.parse_interface(command[0]) - except: + except Exception as e: + print(f"Exception occured: {e}") task = None else: task = None diff --git a/gui/wxpython/core/render.py b/gui/wxpython/core/render.py index b98e4323578..3fe4b4c0e03 100644 --- a/gui/wxpython/core/render.py +++ b/gui/wxpython/core/render.py @@ -1237,7 +1237,11 @@ def SetRegion(self, windres=False, windres3=False): return grass_region - except: + except KeyError as e: + Debug.msg(1, f"Map.SetRegion(): KeyError - {e!s}") + return None + except Exception as e: + Debug.msg(1, f"Map.SetRegion(): Unexpected error - {e!s}") return None def GetListOfLayers( diff --git a/gui/wxpython/core/settings.py b/gui/wxpython/core/settings.py index 45620220aed..9c9e15236c9 100644 --- a/gui/wxpython/core/settings.py +++ b/gui/wxpython/core/settings.py @@ -110,8 +110,9 @@ def _generateLocale(self): self.locs.sort() # Add a default choice to not override system locale self.locs.insert(0, "system") - except: - # No NLS + except Exception as e: + # Catch any other unexpected exceptions + print(f"Unexpected error generating locales: {e}") self.locs = ["system"] return "system" @@ -992,8 +993,8 @@ def SaveToFile(self, settings=None): if not os.path.exists(dirPath): try: os.mkdir(dirPath) - except: - GError(_("Unable to create settings directory")) + except OSError as e: + GError(_("Unable to create settings directory: {}").format(str(e))) return try: with open(self.filePath, "w") as f: diff --git a/gui/wxpython/core/toolboxes.py b/gui/wxpython/core/toolboxes.py index ae6b1b6a0a6..3706470b758 100644 --- a/gui/wxpython/core/toolboxes.py +++ b/gui/wxpython/core/toolboxes.py @@ -209,12 +209,22 @@ def getMenudataFile(userRootFile, newFile, fallback): fh.write(xml) fh.close() return menudataFile - except: + except OSError as e: _debug( 2, ( - "toolboxes.getMenudataFile: writing menudata failed, " - "returning fallback file" + "toolboxes.getMenudataFile: writing menudata failed." + f"Error: {e!s}" + "Returning fallback file." + ), + ) + return fallback + except Exception as e: + _debug( + 2, + ( + f"toolboxes.getMenudataFile: Unexpected error: {e!s}" + "Returning fallback file." ), ) return fallback diff --git a/gui/wxpython/core/utils.py b/gui/wxpython/core/utils.py index 10d6dca2759..ceb64f2eb4d 100644 --- a/gui/wxpython/core/utils.py +++ b/gui/wxpython/core/utils.py @@ -77,7 +77,11 @@ def GetTempfile(pref=None): return os.path.join(pref, file) else: return tempfile - except: + except (AttributeError, TypeError) as e: + print(f"Error splitting tempfile path: {e}") + return None + except Exception as e: + print(f"Unexpected error in GetTempfile: {e}") return None @@ -255,7 +259,11 @@ def ListOfCatsToRange(cats): try: cats = list(map(int, cats)) - except: + except ValueError as e: + print(f"Error converting cats to integers: {e}") + return catstr + except TypeError as e: + print(f"Invalid type for cats: {e}") return catstr i = 0 @@ -579,8 +587,10 @@ def GetListOfLocations(dbase): os.path.join(location, "*") ): listOfLocations.append(os.path.basename(location)) - except: - pass + except OSError as e: + print(f"Error accessing database directory: {e}") + except Exception as e: + print(f"Unexpected error in GetListOfLocations: {e}") ListSortLower(listOfLocations) @@ -632,7 +642,8 @@ def _getGDALFormats(): """Get dictionary of available GDAL drivers""" try: ret = grass.read_command("r.in.gdal", quiet=True, flags="f") - except: + except Exception as e: + print(f"Unexpected error in _getGDALFormats: {e}") ret = None return _parseFormats(ret), _parseFormats(ret, writableOnly=True) @@ -642,7 +653,8 @@ def _getOGRFormats(): """Get dictionary of available OGR drivers""" try: ret = grass.read_command("v.in.ogr", quiet=True, flags="f") - except: + except Exception as e: + print(f"Unexpected error in _getOGRFormats: {e}") ret = None return _parseFormats(ret), _parseFormats(ret, writableOnly=True) diff --git a/gui/wxpython/core/watchdog.py b/gui/wxpython/core/watchdog.py index 9315a1010d6..652bc1d755d 100644 --- a/gui/wxpython/core/watchdog.py +++ b/gui/wxpython/core/watchdog.py @@ -19,6 +19,10 @@ import os import time +import wx +from wx.lib.newevent import NewEvent + +from grass.script import core as grass watchdog_used = True try: @@ -32,11 +36,6 @@ PatternMatchingEventHandler = object FileSystemEventHandler = object -import wx -from wx.lib.newevent import NewEvent - -from grass.script import core as grass - updateMapset, EVT_UPDATE_MAPSET = NewEvent() currentMapsetChanged, EVT_CURRENT_MAPSET_CHANGED = NewEvent() diff --git a/gui/wxpython/core/workspace.py b/gui/wxpython/core/workspace.py index 63f8b91cb89..4520674e1a2 100644 --- a/gui/wxpython/core/workspace.py +++ b/gui/wxpython/core/workspace.py @@ -24,6 +24,7 @@ from core.utils import normalize_whitespace from core.settings import UserSettings from core.gcmd import EncodeString, GetDefaultEncoding +from core.debug import Debug from nviz.main import NvizSettings from grass.script import core as gcore @@ -123,8 +124,8 @@ def __processFile(self): try: self.layerManager["pos"] = (posVal[0], posVal[1]) self.layerManager["size"] = (posVal[2], posVal[3]) - except: - pass + except Exception as e: + Debug.msg(1, f"Error setting layer manager position/size: {e}") # current working directory cwdPath = self.__getNodeText(node_lm, "cwd") if cwdPath: @@ -155,7 +156,8 @@ def __processFile(self): try: pos = (posVal[0], posVal[1]) size = (posVal[2], posVal[3]) - except: + except Exception as e: + Debug.msg(1, f"Error setting layer manager position/size: {e}") pos = None size = None # this happens on Windows when mapwindow is minimized when @@ -2019,7 +2021,14 @@ def _get_value(self, line): """Get value of element""" try: return line.strip(" ").split(" ")[1].strip(" ") - except: + except IndexError: + print("Error: Line does not contain at least two words") + return "" + except AttributeError: + print("Error: Input is not a string") + return "" + except Exception as e: + print(f"Unexpected error: {e}") return "" def _get_element(self, line): From 0fab9131126b70040dc36cf7a5c20b919418219b Mon Sep 17 00:00:00 2001 From: Arohan Ajit Date: Sat, 21 Sep 2024 10:42:13 +0000 Subject: [PATCH 2/8] update --- .flake8 | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.flake8 b/.flake8 index bd33e493bfc..ad6c6227778 100644 --- a/.flake8 +++ b/.flake8 @@ -26,14 +26,6 @@ per-file-ignores = doc/gui/wxpython/example/dialogs.py: F401 locale/grass_po_stats.py: E122, E128, E231, E401, E722 gui/scripts/d.wms.py: E501 - gui/wxpython/core/gcmd.py: E402 - gui/wxpython/core/gconsole.py: E722 - gui/wxpython/core/toolboxes.py: E722 - gui/wxpython/core/utils.py: E722 - gui/wxpython/core/workspace.py: E722 - gui/wxpython/core/render.py: E722 - gui/wxpython/core/settings.py: E722 - gui/wxpython/core/watchdog.py: E402 gui/wxpython/datacatalog/tree.py: E731, E402 gui/wxpython/dbmgr/base.py: E722 gui/wxpython/dbmgr/dialogs.py: E722 From 297c2de664b3e38285a2ca6350037e7066b1e6b3 Mon Sep 17 00:00:00 2001 From: Arohan Ajit Date: Sat, 21 Sep 2024 16:03:54 +0000 Subject: [PATCH 3/8] revert e722 v0 --- .flake8 | 2 ++ gui/wxpython/core/gconsole.py | 3 +-- gui/wxpython/core/render.py | 7 +------ 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/.flake8 b/.flake8 index ad6c6227778..40c55ce1c12 100644 --- a/.flake8 +++ b/.flake8 @@ -26,6 +26,8 @@ per-file-ignores = doc/gui/wxpython/example/dialogs.py: F401 locale/grass_po_stats.py: E122, E128, E231, E401, E722 gui/scripts/d.wms.py: E501 + gui/wxpython/core/gconsole.py: E722 + gui/wxpython/core/render.py: E722 gui/wxpython/datacatalog/tree.py: E731, E402 gui/wxpython/dbmgr/base.py: E722 gui/wxpython/dbmgr/dialogs.py: E722 diff --git a/gui/wxpython/core/gconsole.py b/gui/wxpython/core/gconsole.py index b8934837eaf..bad4aac29cd 100644 --- a/gui/wxpython/core/gconsole.py +++ b/gui/wxpython/core/gconsole.py @@ -691,8 +691,7 @@ def load_source(modname, filename): if len(command) == 1 and not skipInterface: try: task = gtask.parse_interface(command[0]) - except Exception as e: - print(f"Exception occured: {e}") + except: task = None else: task = None diff --git a/gui/wxpython/core/render.py b/gui/wxpython/core/render.py index 3fe4b4c0e03..52cb2b201e2 100644 --- a/gui/wxpython/core/render.py +++ b/gui/wxpython/core/render.py @@ -1236,12 +1236,7 @@ def SetRegion(self, windres=False, windres3=False): Debug.msg(3, "Map.SetRegion(): %s" % grass_region) return grass_region - - except KeyError as e: - Debug.msg(1, f"Map.SetRegion(): KeyError - {e!s}") - return None - except Exception as e: - Debug.msg(1, f"Map.SetRegion(): Unexpected error - {e!s}") + except: return None def GetListOfLayers( From d4121d7f38d6420526647225a9c0e5af5a8800d3 Mon Sep 17 00:00:00 2001 From: Arohan Ajit Date: Sat, 21 Sep 2024 16:12:46 +0000 Subject: [PATCH 4/8] revert v2 --- .flake8 | 1 + gui/wxpython/core/render.py | 1 + gui/wxpython/core/settings.py | 9 ++++----- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.flake8 b/.flake8 index 40c55ce1c12..103b46f50c1 100644 --- a/.flake8 +++ b/.flake8 @@ -28,6 +28,7 @@ per-file-ignores = gui/scripts/d.wms.py: E501 gui/wxpython/core/gconsole.py: E722 gui/wxpython/core/render.py: E722 + gui/wxpython/core/settings.py: E722 gui/wxpython/datacatalog/tree.py: E731, E402 gui/wxpython/dbmgr/base.py: E722 gui/wxpython/dbmgr/dialogs.py: E722 diff --git a/gui/wxpython/core/render.py b/gui/wxpython/core/render.py index 52cb2b201e2..b98e4323578 100644 --- a/gui/wxpython/core/render.py +++ b/gui/wxpython/core/render.py @@ -1236,6 +1236,7 @@ def SetRegion(self, windres=False, windres3=False): Debug.msg(3, "Map.SetRegion(): %s" % grass_region) return grass_region + except: return None diff --git a/gui/wxpython/core/settings.py b/gui/wxpython/core/settings.py index 9c9e15236c9..45620220aed 100644 --- a/gui/wxpython/core/settings.py +++ b/gui/wxpython/core/settings.py @@ -110,9 +110,8 @@ def _generateLocale(self): self.locs.sort() # Add a default choice to not override system locale self.locs.insert(0, "system") - except Exception as e: - # Catch any other unexpected exceptions - print(f"Unexpected error generating locales: {e}") + except: + # No NLS self.locs = ["system"] return "system" @@ -993,8 +992,8 @@ def SaveToFile(self, settings=None): if not os.path.exists(dirPath): try: os.mkdir(dirPath) - except OSError as e: - GError(_("Unable to create settings directory: {}").format(str(e))) + except: + GError(_("Unable to create settings directory")) return try: with open(self.filePath, "w") as f: From f8a18d0ca0b2f25b68624ed85a82d2198605c958 Mon Sep 17 00:00:00 2001 From: Arohan Ajit Date: Sat, 21 Sep 2024 19:32:18 +0000 Subject: [PATCH 5/8] revert toolbox --- .flake8 | 1 + gui/wxpython/core/toolboxes.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.flake8 b/.flake8 index 103b46f50c1..f107f29a5c4 100644 --- a/.flake8 +++ b/.flake8 @@ -29,6 +29,7 @@ per-file-ignores = gui/wxpython/core/gconsole.py: E722 gui/wxpython/core/render.py: E722 gui/wxpython/core/settings.py: E722 + gui/wxpython/core/toolboxes.py: E722 gui/wxpython/datacatalog/tree.py: E731, E402 gui/wxpython/dbmgr/base.py: E722 gui/wxpython/dbmgr/dialogs.py: E722 diff --git a/gui/wxpython/core/toolboxes.py b/gui/wxpython/core/toolboxes.py index 3706470b758..fcbf25b686e 100644 --- a/gui/wxpython/core/toolboxes.py +++ b/gui/wxpython/core/toolboxes.py @@ -219,11 +219,11 @@ def getMenudataFile(userRootFile, newFile, fallback): ), ) return fallback - except Exception as e: + except: _debug( 2, ( - f"toolboxes.getMenudataFile: Unexpected error: {e!s}" + "toolboxes.getMenudataFile: Unexpected error" "Returning fallback file." ), ) From aaf15c97e6e0e7b7130a1f917c02a3b0717073ca Mon Sep 17 00:00:00 2001 From: Arohan Ajit Date: Sat, 21 Sep 2024 19:33:27 +0000 Subject: [PATCH 6/8] revert toolbox --- gui/wxpython/core/toolboxes.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/gui/wxpython/core/toolboxes.py b/gui/wxpython/core/toolboxes.py index fcbf25b686e..ae6b1b6a0a6 100644 --- a/gui/wxpython/core/toolboxes.py +++ b/gui/wxpython/core/toolboxes.py @@ -209,22 +209,12 @@ def getMenudataFile(userRootFile, newFile, fallback): fh.write(xml) fh.close() return menudataFile - except OSError as e: - _debug( - 2, - ( - "toolboxes.getMenudataFile: writing menudata failed." - f"Error: {e!s}" - "Returning fallback file." - ), - ) - return fallback except: _debug( 2, ( - "toolboxes.getMenudataFile: Unexpected error" - "Returning fallback file." + "toolboxes.getMenudataFile: writing menudata failed, " + "returning fallback file" ), ) return fallback From f3aef24079b25764fa6948d9089177a55773c8b4 Mon Sep 17 00:00:00 2001 From: Arohan Ajit Date: Sat, 21 Sep 2024 19:40:58 +0000 Subject: [PATCH 7/8] updated utils --- .flake8 | 1 + gui/wxpython/core/utils.py | 24 ++++++------------------ 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/.flake8 b/.flake8 index f107f29a5c4..eb7e09f0ba5 100644 --- a/.flake8 +++ b/.flake8 @@ -30,6 +30,7 @@ per-file-ignores = gui/wxpython/core/render.py: E722 gui/wxpython/core/settings.py: E722 gui/wxpython/core/toolboxes.py: E722 + gui/wxpython/core/utils.py: E722 gui/wxpython/datacatalog/tree.py: E731, E402 gui/wxpython/dbmgr/base.py: E722 gui/wxpython/dbmgr/dialogs.py: E722 diff --git a/gui/wxpython/core/utils.py b/gui/wxpython/core/utils.py index ceb64f2eb4d..10d6dca2759 100644 --- a/gui/wxpython/core/utils.py +++ b/gui/wxpython/core/utils.py @@ -77,11 +77,7 @@ def GetTempfile(pref=None): return os.path.join(pref, file) else: return tempfile - except (AttributeError, TypeError) as e: - print(f"Error splitting tempfile path: {e}") - return None - except Exception as e: - print(f"Unexpected error in GetTempfile: {e}") + except: return None @@ -259,11 +255,7 @@ def ListOfCatsToRange(cats): try: cats = list(map(int, cats)) - except ValueError as e: - print(f"Error converting cats to integers: {e}") - return catstr - except TypeError as e: - print(f"Invalid type for cats: {e}") + except: return catstr i = 0 @@ -587,10 +579,8 @@ def GetListOfLocations(dbase): os.path.join(location, "*") ): listOfLocations.append(os.path.basename(location)) - except OSError as e: - print(f"Error accessing database directory: {e}") - except Exception as e: - print(f"Unexpected error in GetListOfLocations: {e}") + except: + pass ListSortLower(listOfLocations) @@ -642,8 +632,7 @@ def _getGDALFormats(): """Get dictionary of available GDAL drivers""" try: ret = grass.read_command("r.in.gdal", quiet=True, flags="f") - except Exception as e: - print(f"Unexpected error in _getGDALFormats: {e}") + except: ret = None return _parseFormats(ret), _parseFormats(ret, writableOnly=True) @@ -653,8 +642,7 @@ def _getOGRFormats(): """Get dictionary of available OGR drivers""" try: ret = grass.read_command("v.in.ogr", quiet=True, flags="f") - except Exception as e: - print(f"Unexpected error in _getOGRFormats: {e}") + except: ret = None return _parseFormats(ret), _parseFormats(ret, writableOnly=True) From 912acceb5943431c8423aa6341e28f9b66906a3d Mon Sep 17 00:00:00 2001 From: Arohan Ajit Date: Sat, 21 Sep 2024 19:45:37 +0000 Subject: [PATCH 8/8] revert workspace --- .flake8 | 1 + gui/wxpython/core/workspace.py | 17 ++++------------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/.flake8 b/.flake8 index eb7e09f0ba5..ecea7b82a0b 100644 --- a/.flake8 +++ b/.flake8 @@ -31,6 +31,7 @@ per-file-ignores = gui/wxpython/core/settings.py: E722 gui/wxpython/core/toolboxes.py: E722 gui/wxpython/core/utils.py: E722 + gui/wxpython/core/workspace.py: E722 gui/wxpython/datacatalog/tree.py: E731, E402 gui/wxpython/dbmgr/base.py: E722 gui/wxpython/dbmgr/dialogs.py: E722 diff --git a/gui/wxpython/core/workspace.py b/gui/wxpython/core/workspace.py index 4520674e1a2..63f8b91cb89 100644 --- a/gui/wxpython/core/workspace.py +++ b/gui/wxpython/core/workspace.py @@ -24,7 +24,6 @@ from core.utils import normalize_whitespace from core.settings import UserSettings from core.gcmd import EncodeString, GetDefaultEncoding -from core.debug import Debug from nviz.main import NvizSettings from grass.script import core as gcore @@ -124,8 +123,8 @@ def __processFile(self): try: self.layerManager["pos"] = (posVal[0], posVal[1]) self.layerManager["size"] = (posVal[2], posVal[3]) - except Exception as e: - Debug.msg(1, f"Error setting layer manager position/size: {e}") + except: + pass # current working directory cwdPath = self.__getNodeText(node_lm, "cwd") if cwdPath: @@ -156,8 +155,7 @@ def __processFile(self): try: pos = (posVal[0], posVal[1]) size = (posVal[2], posVal[3]) - except Exception as e: - Debug.msg(1, f"Error setting layer manager position/size: {e}") + except: pos = None size = None # this happens on Windows when mapwindow is minimized when @@ -2021,14 +2019,7 @@ def _get_value(self, line): """Get value of element""" try: return line.strip(" ").split(" ")[1].strip(" ") - except IndexError: - print("Error: Line does not contain at least two words") - return "" - except AttributeError: - print("Error: Input is not a string") - return "" - except Exception as e: - print(f"Unexpected error: {e}") + except: return "" def _get_element(self, line):