From 4d4911172d9c7656ba01037649b8f0d2bcfe449f Mon Sep 17 00:00:00 2001 From: Arohan Ajit Date: Tue, 18 Feb 2025 13:23:01 -0500 Subject: [PATCH] style: Fixed context opening error in wxGUI/dbmgr (#5062) --- gui/wxpython/dbmgr/base.py | 224 +++++++++++++++++++------------------ pyproject.toml | 1 - 2 files changed, 115 insertions(+), 110 deletions(-) diff --git a/gui/wxpython/dbmgr/base.py b/gui/wxpython/dbmgr/base.py index b0c76dd92cc..7e8aaed5bea 100644 --- a/gui/wxpython/dbmgr/base.py +++ b/gui/wxpython/dbmgr/base.py @@ -220,133 +220,139 @@ def LoadData(self, layer, columns=None, where=None, sql=None): # values, so while sticking with ASCII we make it something # highly unlikely to exist naturally. fs = "{_sep_}" + with tempfile.NamedTemporaryFile(mode="w+b") as outFile: + cmdParams = {"quiet": True, "parent": self, "flags": "c", "separator": fs} - outFile = tempfile.NamedTemporaryFile(mode="w+b") - - cmdParams = {"quiet": True, "parent": self, "flags": "c", "separator": fs} - - if sql: - cmdParams.update({"sql": sql, "output": outFile.name, "overwrite": True}) - RunCommand("db.select", **cmdParams) - self.sqlFilter = {"sql": sql} - else: - cmdParams.update( - { - "map": self.mapDBInfo.map, - "layer": layer, - "where": where, - "stdout": outFile, - } - ) + if sql: + cmdParams.update( + {"sql": sql, "output": outFile.name, "overwrite": True} + ) + RunCommand("db.select", **cmdParams) + self.sqlFilter = {"sql": sql} + else: + cmdParams.update( + { + "map": self.mapDBInfo.map, + "layer": layer, + "where": where, + "stdout": outFile, + } + ) - self.sqlFilter = {"where": where} + self.sqlFilter = {"where": where} - if columns: - # Enclose column name with SQL standard double quotes - cmdParams.update({"columns": ",".join([f'"{col}"' for col in columns])}) + if columns: + # Enclose column name with SQL standard double quotes + cmdParams.update( + {"columns": ",".join([f'"{col}"' for col in columns])} + ) - RunCommand("v.db.select", **cmdParams) + RunCommand("v.db.select", **cmdParams) - # These two should probably be passed to init more cleanly - # setting the numbers of items = number of elements in the dictionary - self.itemDataMap = {} - self.itemIndexMap = [] - self.itemCatsMap = {} + # These two should probably be passed to init more cleanly + # setting the numbers of items = number of elements in the dictionary + self.itemDataMap = {} + self.itemIndexMap = [] + self.itemCatsMap = {} - self.DeleteAllItems() + self.DeleteAllItems() - # self.ClearAll() - for i in range(self.GetColumnCount()): - self.DeleteColumn(0) + # self.ClearAll() + for i in range(self.GetColumnCount()): + self.DeleteColumn(0) - i = 0 - info = wx.ListItem() - if globalvar.wxPythonPhoenix: - info.Mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT - info.Image = -1 - info.Format = 0 - else: - info.m_mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT - info.m_image = -1 - info.m_format = 0 - for column in columns: + i = 0 + info = wx.ListItem() if globalvar.wxPythonPhoenix: - info.Text = column - self.InsertColumn(i, info) + info.Mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT + info.Image = -1 + info.Format = 0 else: - info.m_text = column - self.InsertColumnInfo(i, info) - i += 1 - if i >= 256: - self.log.write(_("Can display only 256 columns.")) + info.m_mask = ( + wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT + ) + info.m_image = -1 + info.m_format = 0 + for column in columns: + if globalvar.wxPythonPhoenix: + info.Text = column + self.InsertColumn(i, info) + else: + info.m_text = column + self.InsertColumnInfo(i, info) + i += 1 + if i >= 256: + self.log.write(_("Can display only 256 columns.")) - i = 0 - outFile.seek(0) + i = 0 + outFile.seek(0) - enc = GetDbEncoding() - first_wrong_encoding = True - while True: - # os.linesep doesn't work here (MSYS) - # not sure what the replace is for? - # but we need strip to get rid of the ending newline - # which on windows leaves \r in a last empty attribute table cell - # and causes error - try: - record = ( - decode(outFile.readline(), encoding=enc).strip().replace("\n", "") - ) - except UnicodeDecodeError: - record = ( - outFile.readline() - .decode(encoding=enc, errors="replace") - .strip() - .replace("\n", "") - ) - if first_wrong_encoding: - first_wrong_encoding = False - GWarning( - parent=self, - message=_( - "Incorrect encoding {enc} used. Set encoding in GUI " - "Settings or set GRASS_DB_ENCODING variable." - ).format(enc=enc), + enc = GetDbEncoding() + first_wrong_encoding = True + while True: + # os.linesep doesn't work here (MSYS) + # not sure what the replace is for? + # but we need strip to get rid of the ending newline + # which on windows leaves \r in a last empty attribute table cell + # and causes error + try: + record = ( + decode(outFile.readline(), encoding=enc) + .strip() + .replace("\n", "") + ) + except UnicodeDecodeError: + record = ( + outFile.readline() + .decode(encoding=enc, errors="replace") + .strip() + .replace("\n", "") ) + if first_wrong_encoding: + first_wrong_encoding = False + GWarning( + parent=self, + message=_( + "Incorrect encoding {enc} used. Set encoding in GUI " + "Settings or set GRASS_DB_ENCODING variable." + ).format(enc=enc), + ) - if not record: - break + if not record: + break - record = record.split(fs) - if len(columns) != len(record): - # Assuming there will be always at least one. - last = record[-1] - show_max = 3 - if len(record) > show_max: - record = record[:show_max] - # TODO: The real fix here is to use JSON output from v.db.select or - # proper CSV output and real CSV reader here (Python csv and json - # packages). - raise GException( - _( - "Unable to read the table <{table}> from the database due" - " to seemingly inconsistent number of columns in the data" - " transfer." - " Check row: {row}..." - " Likely, a newline character is present in the attribute value" - " starting with: '{value}'" - " Use the v.db.select module to investigate." - ).format(table=tableName, row=" | ".join(record), value=last) - ) - self.columns = {} # because of IsEmpty method - return None + record = record.split(fs) + if len(columns) != len(record): + # Assuming there will be always at least one. + last = record[-1] + show_max = 3 + if len(record) > show_max: + record = record[:show_max] + # TODO: The real fix here is to use JSON output from v.db.select or + # proper CSV output and real CSV reader here (Python csv and json + # packages). + raise GException( + _( + "Unable to read the table <{table}> from the database due" + " to seemingly inconsistent number of columns in the data" + " transfer." + " Check row: {row}..." + " Likely, a newline character is present in the attribute value" + " starting with: '{value}'" + " Use the v.db.select module to investigate." + ).format(table=tableName, row=" | ".join(record), value=last) + ) + self.columns = {} # because of IsEmpty method + return None - self.AddDataRow(i, record, columns, keyId) + self.AddDataRow(i, record, columns, keyId) - i += 1 - if i >= 100000: - self.log.write(_("Viewing limit: 100000 records.")) - break + i += 1 + if i >= 100000: + self.log.write(_("Viewing limit: 100000 records.")) + break - self.SetItemCount(i) + self.SetItemCount(i) if where: item = -1 diff --git a/pyproject.toml b/pyproject.toml index 1265b045615..877a7248ec1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -284,7 +284,6 @@ ignore = [ # Other ignores: "**.py" = ["PYI066"] "*/testsuite/**.py" = ["PT009", "PT027"] -"gui/wxpython/dbmgr/base.py" = ["SIM115"] "gui/wxpython/gcp/manager.py" = ["PTH208"] "gui/wxpython/gmodeler/panels.py" = ["SIM115"] "gui/wxpython/gui_core/dialogs.py" = ["PTH208"]