Skip to content

Commit

Permalink
gui: Use context manager for opening files and temporary files (#4917)
Browse files Browse the repository at this point in the history
* gui: Use context manager for opening files and temporary files

* gui: Use context manager for opening files and temporary files

* style: Fix RET504 [*] Unnecessary assignment to `img` before `return` statement

* style: Fix new write-whole-file (FURB103) errors

* style: Fix new read-whole-file (FURB101) errors

* checks: Remove fixed Ruff SIM115 exclusions

* Use subprocess.DEVNULL to let Popen handle os.devnull
  • Loading branch information
echoix authored Jan 30, 2025
1 parent 2b36598 commit 02861c2
Show file tree
Hide file tree
Showing 29 changed files with 467 additions and 637 deletions.
27 changes: 9 additions & 18 deletions gui/wxpython/animation/temporal_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import datetime
from operator import itemgetter
from pathlib import Path

import grass.script as gs
import grass.temporal as tgis
Expand Down Expand Up @@ -411,28 +412,24 @@ def createAbsoluteInterval():
gs.mapcalc(exp="temp_6 = rand(0, 650)", overwrite=True)

n1 = gs.read_command("g.tempfile", pid=1, flags="d").strip()
fd = open(n1, "w")
fd.write(
Path(n1).write_text(
"prec_1|2001-01-01|2001-02-01\n"
"prec_2|2001-04-01|2001-05-01\n"
"prec_3|2001-05-01|2001-09-01\n"
"prec_4|2001-09-01|2002-01-01\n"
"prec_5|2002-01-01|2002-05-01\n"
"prec_6|2002-05-01|2002-07-01\n"
)
fd.close()

n2 = gs.read_command("g.tempfile", pid=2, flags="d").strip()
fd = open(n2, "w")
fd.write(
Path(n2).write_text(
"temp_1|2000-10-01|2001-01-01\n"
"temp_2|2001-04-01|2001-05-01\n"
"temp_3|2001-05-01|2001-09-01\n"
"temp_4|2001-09-01|2002-01-01\n"
"temp_5|2002-01-01|2002-05-01\n"
"temp_6|2002-05-01|2002-07-01\n"
)
fd.close()
name1 = "absinterval1"
name2 = "absinterval2"
gs.run_command(
Expand Down Expand Up @@ -486,28 +483,24 @@ def createRelativeInterval():
gs.mapcalc(exp="temp_6 = rand(0, 650)", overwrite=True)

n1 = gs.read_command("g.tempfile", pid=1, flags="d").strip()
fd = open(n1, "w")
fd.write(
Path(n1).write_text(
"prec_1|1|4\n"
"prec_2|6|7\n"
"prec_3|7|10\n"
"prec_4|10|11\n"
"prec_5|11|14\n"
"prec_6|14|17\n"
)
fd.close()

n2 = gs.read_command("g.tempfile", pid=2, flags="d").strip()
fd = open(n2, "w")
fd.write(
Path(n2).write_text(
"temp_1|5|6\n"
"temp_2|6|7\n"
"temp_3|7|10\n"
"temp_4|10|11\n"
"temp_5|11|18\n"
"temp_6|19|22\n"
)
fd.close()
name1 = "relinterval1"
name2 = "relinterval2"
gs.run_command(
Expand Down Expand Up @@ -560,16 +553,14 @@ def createAbsolutePoint():
gs.mapcalc(exp="prec_6 = rand(0, 650)", overwrite=True)

n1 = gs.read_command("g.tempfile", pid=1, flags="d").strip()
fd = open(n1, "w")
fd.write(
Path(n1).write_text(
"prec_1|2001-01-01\n"
"prec_2|2001-03-01\n"
"prec_3|2001-04-01\n"
"prec_4|2001-05-01\n"
"prec_5|2001-08-01\n"
"prec_6|2001-09-01\n"
)
fd.close()
name = "abspoint"
gs.run_command(
"t.create",
Expand Down Expand Up @@ -608,9 +599,9 @@ def createRelativePoint():
gs.mapcalc(exp="prec_6 = rand(0, 650)", overwrite=True)

n1 = gs.read_command("g.tempfile", pid=1, flags="d").strip()
fd = open(n1, "w")
fd.write("prec_1|1\nprec_2|3\nprec_3|5\nprec_4|7\nprec_5|11\nprec_6|13\n")
fd.close()
Path(n1).write_text(
"prec_1|1\nprec_2|3\nprec_3|5\nprec_4|7\nprec_5|11\nprec_6|13\n"
)
name = "relpoint"
gs.run_command(
"t.create",
Expand Down
8 changes: 4 additions & 4 deletions gui/wxpython/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ def get_tempfile_name(suffix, create=False):
# which may mitigate problems (like not cleaning files) in case we
# go little beyond what is in the documentation in terms of opening
# closing and removing the tmp file
tmp = tempfile.NamedTemporaryFile(suffix=suffix, delete=False)
# we don't want it open, we just need the name
name = tmp.name
tmp.close()
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
# we don't want it open, we just need the name
name = tmp.name

if not create:
# remove empty file to have a clean state later
os.remove(name)
Expand Down
4 changes: 1 addition & 3 deletions gui/wxpython/core/toolboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,7 @@ def getMenudataFile(userRootFile, newFile, fallback):

try:
xml = _getXMLString(tree.getroot())
fh = open(menudataFile, "w")
fh.write(xml)
fh.close()
Path(menudataFile).write_text(xml)
return menudataFile
except Exception:
_debug(
Expand Down
21 changes: 11 additions & 10 deletions gui/wxpython/dbmgr/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4008,17 +4008,18 @@ def Update(self, driver, database, table, column):
return

fd, sqlFilePath = tempfile.mkstemp(text=True)
sqlFile = open(sqlFilePath, "w")
stats = ["count", "min", "max", "avg", "sum", "null"]
for fn in stats:
if fn == "null":
sqlFile.write(
"select count(*) from %s where %s is null;%s"
% (table, column, "\n")
)
else:
sqlFile.write("select %s(%s) from %s;%s" % (fn, column, table, "\n"))
sqlFile.close()
with open(sqlFilePath, "w") as sqlFile:
for fn in stats:
if fn == "null":
sqlFile.write(
"select count(*) from %s where %s is null;%s"
% (table, column, "\n")
)
else:
sqlFile.write(
"select %s(%s) from %s;%s" % (fn, column, table, "\n")
)

dataStr = RunCommand(
"db.select",
Expand Down
Loading

0 comments on commit 02861c2

Please sign in to comment.