Skip to content

Commit

Permalink
grass.script.core: Use a context manager for opening files (SIM115) t…
Browse files Browse the repository at this point in the history
…o solve some ResourceWarnings (#4559)

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

* Use os.mkdir for string variable
  • Loading branch information
echoix authored Dec 27, 2024
1 parent 9555845 commit fcfc5fa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ 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" = ["PTH208", "SIM115"]
"python/grass/script/core.py" = ["PTH208"]
"python/grass/script/db.py" = ["SIM115"]
"python/grass/script/raster.py" = ["SIM115"]
"python/grass/script/utils.py" = ["FURB189", "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 @@ -1680,14 +1681,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 @@ -1871,16 +1871,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 @@ -1896,8 +1895,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 @@ -1921,16 +1923,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

0 comments on commit fcfc5fa

Please sign in to comment.