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

grass.script.core: Use a context manager for opening files (SIM115) to solve some ResourceWarnings #4559

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ ignore = [
"python/grass/pygrass/vector/sql.py" = ["FLY002"]
"python/grass/pygrass/vector/testsuite/test_table.py" = ["PLW0108"]
"python/grass/script/array.py" = ["A005"]
"python/grass/script/core.py" = ["SIM115"]
"python/grass/script/db.py" = ["SIM115"]
"python/grass/script/raster.py" = ["SIM115"]
"python/grass/script/utils.py" = ["SIM115"]
Expand Down
48 changes: 21 additions & 27 deletions python/grass/script/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,8 @@ def _text_to_key_value_dict(
{'a': ['Hello'], 'c': [1, 2, 3, 4, 5], 'b': [1.0], 'd': ['hello', 8, 0.1]}

"""
text = open(filename).readlines()
with Path(filename).open() as f:
text = f.readlines()
kvdict = KeyValue()

for line in text:
Expand Down Expand Up @@ -1676,14 +1677,13 @@ def find_program(pgm, *args):
or non-zero return code
:return: True otherwise
"""
nuldev = open(os.devnull, "w+")
try:
# TODO: the doc or impl is not correct, any return code is accepted
call([pgm] + list(args), stdin=nuldev, stdout=nuldev, stderr=nuldev)
found = True
except Exception:
found = False
nuldev.close()
with open(os.devnull, "w+") as nuldev:
try:
# TODO: the doc or impl is not correct, any return code is accepted
call([pgm] + list(args), stdin=nuldev, stdout=nuldev, stderr=nuldev)
found = True
except Exception:
found = False

return found

Expand Down Expand Up @@ -1866,16 +1866,15 @@ def create_project(
def _set_location_description(path, location, text):
"""Set description (aka title aka MYNAME) for a location"""
try:
fd = codecs.open(
with codecs.open(
os.path.join(path, location, "PERMANENT", "MYNAME"),
encoding="utf-8",
mode="w",
)
if text:
fd.write(text + os.linesep)
else:
fd.write(os.linesep)
fd.close()
) as fd:
if text:
fd.write(text + os.linesep)
else:
fd.write(os.linesep)
except OSError as e:
raise ScriptError(repr(e))

Expand All @@ -1891,8 +1890,11 @@ def _create_location_xy(database, location):
cur_dir = Path.cwd()
try:
os.chdir(database)
permanent_dir = Path(location, "PERMANENT")
default_wind_path = permanent_dir / "DEFAULT_WIND"
wind_path = permanent_dir / "WIND"
os.mkdir(location)
os.mkdir(os.path.join(location, "PERMANENT"))
permanent_dir.mkdir()

# create DEFAULT_WIND and WIND files
regioninfo = [
Expand All @@ -1916,16 +1918,8 @@ def _create_location_xy(database, location):
"t-b resol: 1",
]

defwind = open(os.path.join(location, "PERMANENT", "DEFAULT_WIND"), "w")
for param in regioninfo:
defwind.write(param + "%s" % os.linesep)
defwind.close()

shutil.copy(
os.path.join(location, "PERMANENT", "DEFAULT_WIND"),
os.path.join(location, "PERMANENT", "WIND"),
)

default_wind_path.write_text("\n".join(regioninfo))
shutil.copy(default_wind_path, wind_path)
os.chdir(cur_dir)
except OSError as e:
raise ScriptError(repr(e))
Expand Down
Loading