Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d.mon: Fix SIM115 linter warnings by using context managers for file operations #5015

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 45 additions & 41 deletions display/d.mon/render_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,23 @@ def remove_mapfile(mapfile):
# read environment variables from file
def read_env_file(env_file):
width = height = legfile = None
fd = open(env_file)
if fd is None:
grass.fatal("Unable to open file '{0}'".format(env_file))
lines = fd.readlines()
for line in lines:
if line.startswith("#"):
continue
k, v = line.rstrip("\n").split("#", 1)[0].strip().split("=", 1)
os.environ[k] = v
if width is None and k == "GRASS_RENDER_WIDTH":
width = int(v)
if height is None and k == "GRASS_RENDER_HEIGHT":
height = int(v)
if legfile is None and k == "GRASS_LEGEND_FILE":
legfile = v
fd.close()
try:
with open(env_file) as fd:
for line in fd:
line = line.strip()
if line.startswith("#") or not line:
continue
# Split on comment and parse key=value
k, v = line.split("#", 1)[0].strip().split("=", 1)
os.environ[k] = v
if width is None and k == "GRASS_RENDER_WIDTH":
width = int(v)
if height is None and k == "GRASS_RENDER_HEIGHT":
height = int(v)
if legfile is None and k == "GRASS_LEGEND_FILE":
legfile = v
except OSError:
grass.fatal(f"Unable to open file '{env_file}'")

if width is None or height is None:
grass.fatal("Unknown monitor size")
Expand Down Expand Up @@ -86,20 +87,20 @@ def update_cmd_file(cmd_file, cmd, mapfile):

mode = "w" if cmd[0] == "d.erase" else "a"
# update cmd file
fd = open(cmd_file, mode)
if fd is None:
grass.fatal("Unable to open file '{0}'".format(cmd_file))
if mode == "a":
frame = os.getenv("GRASS_RENDER_FRAME", None)
if frame:
fd.write("# GRASS_RENDER_FRAME={0}\n".format(frame))
if mapfile:
fd.write("# GRASS_RENDER_FILE={0}\n".format(mapfile))
fd.write(" ".join(gtask.cmdtuple_to_list(cmd)))
fd.write("\n")
else:
fd.write("")
fd.close()
try:
with open(cmd_file, mode) as fd:
if mode == "a":
frame = os.getenv("GRASS_RENDER_FRAME", None)
if frame:
fd.write("# GRASS_RENDER_FRAME={0}\n".format(frame))
if mapfile:
fd.write("# GRASS_RENDER_FILE={0}\n".format(mapfile))
fd.write(" ".join(gtask.cmdtuple_to_list(cmd)))
fd.write("\n")
else:
fd.write("")
except OSError:
grass.fatal(f"Unable to open file '{cmd_file}'")


# adjust region
Expand Down Expand Up @@ -152,14 +153,15 @@ def read_stdin(cmd):
opt = "input"

if opt:
tmpfile = tempfile.NamedTemporaryFile(dir=path).name + ".txt"
fd = open(tmpfile, "w")
while 1:
line = sys.stdin.readline()
if not line:
break
fd.write(line)
fd.close()
with tempfile.NamedTemporaryFile(
dir=path, suffix=".txt", mode="w", delete=False
) as fd:
while True:
line = sys.stdin.readline()
if not line:
break
fd.write(line)
tmpfile = fd.name
cmd[1][opt] = tmpfile


Expand All @@ -172,11 +174,13 @@ def read_stdin(cmd):

width, height, legfile = read_env_file(os.path.join(path, "env"))
if mon.startswith("wx"):
mapfile = tempfile.NamedTemporaryFile(dir=path).name
if cmd[0] in {"d.barscale", "d.legend", "d.northarrow", "d.legend.vect"}:
mapfile += ".png"
suffix = ".png"
else:
mapfile += ".ppm"
suffix = ".ppm"

with tempfile.NamedTemporaryFile(dir=path, suffix=suffix) as tmpfile:
mapfile = tmpfile.name
# to force rendering by wx monitors, but don't create a map file for
# non-rendering modules
if cmd[0] not in non_rendering_modules:
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ ignore = [
# Other ignores:
"**.py" = ["PYI066"]
"*/testsuite/**.py" = ["PT009", "PT027"]
"display/d.mon/render_cmd.py" = ["SIM115"]
"gui/wxpython/core/globalvar.py" = ["PTH208"]
"gui/wxpython/core/render.py" = ["SIM115"]
"gui/wxpython/core/settings.py" = ["PTH208", "SIM115"]
Expand Down
Loading