Skip to content

Commit 4d49111

Browse files
authored
style: Fixed context opening error in wxGUI/dbmgr (#5062)
1 parent 938b9ad commit 4d49111

File tree

2 files changed

+115
-110
lines changed

2 files changed

+115
-110
lines changed

gui/wxpython/dbmgr/base.py

Lines changed: 115 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -220,133 +220,139 @@ def LoadData(self, layer, columns=None, where=None, sql=None):
220220
# values, so while sticking with ASCII we make it something
221221
# highly unlikely to exist naturally.
222222
fs = "{_sep_}"
223+
with tempfile.NamedTemporaryFile(mode="w+b") as outFile:
224+
cmdParams = {"quiet": True, "parent": self, "flags": "c", "separator": fs}
223225

224-
outFile = tempfile.NamedTemporaryFile(mode="w+b")
225-
226-
cmdParams = {"quiet": True, "parent": self, "flags": "c", "separator": fs}
227-
228-
if sql:
229-
cmdParams.update({"sql": sql, "output": outFile.name, "overwrite": True})
230-
RunCommand("db.select", **cmdParams)
231-
self.sqlFilter = {"sql": sql}
232-
else:
233-
cmdParams.update(
234-
{
235-
"map": self.mapDBInfo.map,
236-
"layer": layer,
237-
"where": where,
238-
"stdout": outFile,
239-
}
240-
)
226+
if sql:
227+
cmdParams.update(
228+
{"sql": sql, "output": outFile.name, "overwrite": True}
229+
)
230+
RunCommand("db.select", **cmdParams)
231+
self.sqlFilter = {"sql": sql}
232+
else:
233+
cmdParams.update(
234+
{
235+
"map": self.mapDBInfo.map,
236+
"layer": layer,
237+
"where": where,
238+
"stdout": outFile,
239+
}
240+
)
241241

242-
self.sqlFilter = {"where": where}
242+
self.sqlFilter = {"where": where}
243243

244-
if columns:
245-
# Enclose column name with SQL standard double quotes
246-
cmdParams.update({"columns": ",".join([f'"{col}"' for col in columns])})
244+
if columns:
245+
# Enclose column name with SQL standard double quotes
246+
cmdParams.update(
247+
{"columns": ",".join([f'"{col}"' for col in columns])}
248+
)
247249

248-
RunCommand("v.db.select", **cmdParams)
250+
RunCommand("v.db.select", **cmdParams)
249251

250-
# These two should probably be passed to init more cleanly
251-
# setting the numbers of items = number of elements in the dictionary
252-
self.itemDataMap = {}
253-
self.itemIndexMap = []
254-
self.itemCatsMap = {}
252+
# These two should probably be passed to init more cleanly
253+
# setting the numbers of items = number of elements in the dictionary
254+
self.itemDataMap = {}
255+
self.itemIndexMap = []
256+
self.itemCatsMap = {}
255257

256-
self.DeleteAllItems()
258+
self.DeleteAllItems()
257259

258-
# self.ClearAll()
259-
for i in range(self.GetColumnCount()):
260-
self.DeleteColumn(0)
260+
# self.ClearAll()
261+
for i in range(self.GetColumnCount()):
262+
self.DeleteColumn(0)
261263

262-
i = 0
263-
info = wx.ListItem()
264-
if globalvar.wxPythonPhoenix:
265-
info.Mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT
266-
info.Image = -1
267-
info.Format = 0
268-
else:
269-
info.m_mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT
270-
info.m_image = -1
271-
info.m_format = 0
272-
for column in columns:
264+
i = 0
265+
info = wx.ListItem()
273266
if globalvar.wxPythonPhoenix:
274-
info.Text = column
275-
self.InsertColumn(i, info)
267+
info.Mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT
268+
info.Image = -1
269+
info.Format = 0
276270
else:
277-
info.m_text = column
278-
self.InsertColumnInfo(i, info)
279-
i += 1
280-
if i >= 256:
281-
self.log.write(_("Can display only 256 columns."))
271+
info.m_mask = (
272+
wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT
273+
)
274+
info.m_image = -1
275+
info.m_format = 0
276+
for column in columns:
277+
if globalvar.wxPythonPhoenix:
278+
info.Text = column
279+
self.InsertColumn(i, info)
280+
else:
281+
info.m_text = column
282+
self.InsertColumnInfo(i, info)
283+
i += 1
284+
if i >= 256:
285+
self.log.write(_("Can display only 256 columns."))
282286

283-
i = 0
284-
outFile.seek(0)
287+
i = 0
288+
outFile.seek(0)
285289

286-
enc = GetDbEncoding()
287-
first_wrong_encoding = True
288-
while True:
289-
# os.linesep doesn't work here (MSYS)
290-
# not sure what the replace is for?
291-
# but we need strip to get rid of the ending newline
292-
# which on windows leaves \r in a last empty attribute table cell
293-
# and causes error
294-
try:
295-
record = (
296-
decode(outFile.readline(), encoding=enc).strip().replace("\n", "")
297-
)
298-
except UnicodeDecodeError:
299-
record = (
300-
outFile.readline()
301-
.decode(encoding=enc, errors="replace")
302-
.strip()
303-
.replace("\n", "")
304-
)
305-
if first_wrong_encoding:
306-
first_wrong_encoding = False
307-
GWarning(
308-
parent=self,
309-
message=_(
310-
"Incorrect encoding {enc} used. Set encoding in GUI "
311-
"Settings or set GRASS_DB_ENCODING variable."
312-
).format(enc=enc),
290+
enc = GetDbEncoding()
291+
first_wrong_encoding = True
292+
while True:
293+
# os.linesep doesn't work here (MSYS)
294+
# not sure what the replace is for?
295+
# but we need strip to get rid of the ending newline
296+
# which on windows leaves \r in a last empty attribute table cell
297+
# and causes error
298+
try:
299+
record = (
300+
decode(outFile.readline(), encoding=enc)
301+
.strip()
302+
.replace("\n", "")
303+
)
304+
except UnicodeDecodeError:
305+
record = (
306+
outFile.readline()
307+
.decode(encoding=enc, errors="replace")
308+
.strip()
309+
.replace("\n", "")
313310
)
311+
if first_wrong_encoding:
312+
first_wrong_encoding = False
313+
GWarning(
314+
parent=self,
315+
message=_(
316+
"Incorrect encoding {enc} used. Set encoding in GUI "
317+
"Settings or set GRASS_DB_ENCODING variable."
318+
).format(enc=enc),
319+
)
314320

315-
if not record:
316-
break
321+
if not record:
322+
break
317323

318-
record = record.split(fs)
319-
if len(columns) != len(record):
320-
# Assuming there will be always at least one.
321-
last = record[-1]
322-
show_max = 3
323-
if len(record) > show_max:
324-
record = record[:show_max]
325-
# TODO: The real fix here is to use JSON output from v.db.select or
326-
# proper CSV output and real CSV reader here (Python csv and json
327-
# packages).
328-
raise GException(
329-
_(
330-
"Unable to read the table <{table}> from the database due"
331-
" to seemingly inconsistent number of columns in the data"
332-
" transfer."
333-
" Check row: {row}..."
334-
" Likely, a newline character is present in the attribute value"
335-
" starting with: '{value}'"
336-
" Use the v.db.select module to investigate."
337-
).format(table=tableName, row=" | ".join(record), value=last)
338-
)
339-
self.columns = {} # because of IsEmpty method
340-
return None
324+
record = record.split(fs)
325+
if len(columns) != len(record):
326+
# Assuming there will be always at least one.
327+
last = record[-1]
328+
show_max = 3
329+
if len(record) > show_max:
330+
record = record[:show_max]
331+
# TODO: The real fix here is to use JSON output from v.db.select or
332+
# proper CSV output and real CSV reader here (Python csv and json
333+
# packages).
334+
raise GException(
335+
_(
336+
"Unable to read the table <{table}> from the database due"
337+
" to seemingly inconsistent number of columns in the data"
338+
" transfer."
339+
" Check row: {row}..."
340+
" Likely, a newline character is present in the attribute value"
341+
" starting with: '{value}'"
342+
" Use the v.db.select module to investigate."
343+
).format(table=tableName, row=" | ".join(record), value=last)
344+
)
345+
self.columns = {} # because of IsEmpty method
346+
return None
341347

342-
self.AddDataRow(i, record, columns, keyId)
348+
self.AddDataRow(i, record, columns, keyId)
343349

344-
i += 1
345-
if i >= 100000:
346-
self.log.write(_("Viewing limit: 100000 records."))
347-
break
350+
i += 1
351+
if i >= 100000:
352+
self.log.write(_("Viewing limit: 100000 records."))
353+
break
348354

349-
self.SetItemCount(i)
355+
self.SetItemCount(i)
350356

351357
if where:
352358
item = -1

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ ignore = [
284284
# Other ignores:
285285
"**.py" = ["PYI066"]
286286
"*/testsuite/**.py" = ["PT009", "PT027"]
287-
"gui/wxpython/dbmgr/base.py" = ["SIM115"]
288287
"gui/wxpython/gcp/manager.py" = ["PTH208"]
289288
"gui/wxpython/gmodeler/panels.py" = ["SIM115"]
290289
"gui/wxpython/gui_core/dialogs.py" = ["PTH208"]

0 commit comments

Comments
 (0)