Skip to content

Commit c829ef1

Browse files
authored
wxGUI: Fix SIM115: Improve temporary file handling in modules component (#5406)
added context manger
1 parent 3f5b5a4 commit c829ef1

File tree

2 files changed

+57
-58
lines changed

2 files changed

+57
-58
lines changed

gui/wxpython/modules/colorrules.py

+57-56
Original file line numberDiff line numberDiff line change
@@ -1548,8 +1548,13 @@ def LoadRulesFromColumn(self):
15481548
columns += "," + self.properties["loadColumn"]
15491549

15501550
sep = ";"
1551-
if self.inmap:
1552-
outFile = tempfile.NamedTemporaryFile(mode="w+")
1551+
if not self.inmap:
1552+
self.preview.EraseMap()
1553+
del busy
1554+
return
1555+
1556+
# Use a context manager for the file
1557+
with tempfile.NamedTemporaryFile(mode="w+") as outFile:
15531558
ret = RunCommand(
15541559
"v.db.select",
15551560
quiet=True,
@@ -1560,65 +1565,61 @@ def LoadRulesFromColumn(self):
15601565
sep=sep,
15611566
stdout=outFile,
15621567
)
1563-
else:
1564-
self.preview.EraseMap()
1565-
del busy
1566-
return
15671568

1568-
outFile.seek(0)
1569-
i = 0
1570-
minim = maxim = 0.0
1571-
limit = 1000
1569+
outFile.seek(0)
1570+
i = 0
1571+
minim = maxim = 0.0
1572+
limit = 1000
15721573

1573-
colvallist = []
1574-
readvals = False
1574+
colvallist = []
1575+
readvals = False
15751576

1576-
while True:
1577-
# os.linesep doesn't work here (MSYS)
1578-
record = outFile.readline().replace("\n", "")
1579-
if not record:
1580-
break
1581-
self.rulesPanel.ruleslines[i] = {}
1577+
while True:
1578+
# os.linesep doesn't work here (MSYS)
1579+
record = outFile.readline().replace("\n", "")
1580+
if not record:
1581+
break
1582+
self.rulesPanel.ruleslines[i] = {}
15821583

1583-
if not self.properties["loadColumn"]:
1584-
col1 = record
1585-
col2 = None
1586-
else:
1587-
col1, col2 = record.split(sep)
1588-
1589-
minim = min(float(col1), minim)
1590-
maxim = max(float(col1), maxim)
1591-
1592-
# color rules list should only have unique values of col1, not all
1593-
# records
1594-
if col1 not in colvallist:
1595-
self.rulesPanel.ruleslines[i]["value"] = col1
1596-
self.rulesPanel.ruleslines[i][self.attributeType] = col2
1597-
1598-
colvallist.append(col1)
1599-
i += 1
1600-
1601-
if i > limit and readvals is False:
1602-
dlg = wx.MessageDialog(
1603-
parent=self,
1604-
message=_(
1605-
"Number of loaded records reached %d, "
1606-
"displaying all the records will be time-consuming "
1607-
"and may lead to computer freezing, "
1608-
"do you still want to continue?"
1609-
)
1610-
% i,
1611-
caption=_("Too many records"),
1612-
style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
1613-
)
1614-
if dlg.ShowModal() == wx.ID_YES:
1615-
readvals = True
1616-
dlg.Destroy()
1584+
if not self.properties["loadColumn"]:
1585+
col1 = record
1586+
col2 = None
16171587
else:
1618-
del busy
1619-
dlg.Destroy()
1620-
self.updateColumn = False
1621-
return
1588+
col1, col2 = record.split(sep)
1589+
1590+
minim = min(float(col1), minim)
1591+
maxim = max(float(col1), maxim)
1592+
1593+
# color rules list should only have unique values of col1, not all
1594+
# records
1595+
if col1 not in colvallist:
1596+
self.rulesPanel.ruleslines[i]["value"] = col1
1597+
self.rulesPanel.ruleslines[i][self.attributeType] = col2
1598+
1599+
colvallist.append(col1)
1600+
i += 1
1601+
1602+
if i > limit and readvals is False:
1603+
dlg = wx.MessageDialog(
1604+
parent=self,
1605+
message=_(
1606+
"Number of loaded records reached %d, "
1607+
"displaying all the records will be time-consuming "
1608+
"and may lead to computer freezing, "
1609+
"do you still want to continue?"
1610+
)
1611+
% i,
1612+
caption=_("Too many records"),
1613+
style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
1614+
)
1615+
if dlg.ShowModal() == wx.ID_YES:
1616+
readvals = True
1617+
dlg.Destroy()
1618+
else:
1619+
del busy
1620+
dlg.Destroy()
1621+
self.updateColumn = False
1622+
return
16221623

16231624
self.rulesPanel.AddRules(i, start=True)
16241625
ret = self.rulesPanel.LoadRules()

pyproject.toml

-2
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,6 @@ ignore = [
286286
"gui/wxpython/iclass/statistics.py" = ["A005"]
287287
"gui/wxpython/icons/grass_icons.py" = ["PTH208"]
288288
"gui/wxpython/image2target/ii2t_manager.py" = ["PTH208", "SIM115"]
289-
"gui/wxpython/modules/colorrules.py" = ["SIM115"]
290-
"gui/wxpython/modules/mcalc_builder.py" = ["SIM115"]
291289
"gui/wxpython/photo2image/ip2i_manager.py" = ["SIM115"]
292290
"gui/wxpython/psmap/dialogs.py" = ["PTH208"]
293291
"gui/wxpython/psmap/instructions.py" = ["SIM115"]

0 commit comments

Comments
 (0)