Skip to content

Commit bfe6ba0

Browse files
authored
r.in.srtm: Fixed SIM115, FURB103, FLY002 by adding context manager, f-strings (#5138)
1 parent 41c4b1d commit bfe6ba0

File tree

2 files changed

+12
-32
lines changed

2 files changed

+12
-32
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,6 @@ ignore = [
373373
"scripts/i.spectral/i.spectral.py" = ["FLY002", "SIM115"]
374374
"scripts/m.proj/m.proj.py" = ["SIM115"]
375375
"scripts/r.fillnulls/r.fillnulls.py" = ["SIM115"]
376-
"scripts/r.in.srtm/r.in.srtm.py" = ["FLY002", "SIM115"]
377376
"scripts/r.in.wms/wms_*.py" = ["SIM115"]
378377
"scripts/r.tileset/r.tileset.py" = ["SIM115"]
379378
"scripts/v.*/v.*.py" = ["SIM115"]

scripts/r.in.srtm/r.in.srtm.py

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import atexit
7777
import grass.script as gs
7878
import zipfile as zfile
79+
from pathlib import Path
7980
from grass.exceptions import CalledModuleError
8081

8182

@@ -130,19 +131,7 @@
130131
YDIM 0.000833333333333
131132
"""
132133

133-
proj = "".join(
134-
[
135-
"GEOGCS[",
136-
'"wgs84",',
137-
(
138-
'DATUM["WGS_1984",SPHEROID["wgs84",6378137,298.257223563],TOWGS84[0.000000,'
139-
"0.000000,0.000000]],"
140-
),
141-
'PRIMEM["Greenwich",0],',
142-
'UNIT["degree",0.0174532925199433]',
143-
"]",
144-
]
145-
)
134+
proj = 'GEOGCS["wgs84",DATUM["WGS_1984",SPHEROID["wgs84",6378137,298.257223563],TOWGS84[0.000000,0.000000,0.000000]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]'
146135

147136

148137
def cleanup():
@@ -186,8 +175,8 @@ def main():
186175
suff = ".raw"
187176
swbd = True
188177

189-
zipfile = "{im}{su}.zip".format(im=infile, su=suff)
190-
hgtfile = "{im}{su}".format(im=infile, su=suff)
178+
zipfile = f"{infile}{suff}.zip"
179+
hgtfile = f"{infile}{suff}"
191180

192181
if os.path.isfile(zipfile):
193182
# really a ZIP file?
@@ -206,28 +195,24 @@ def main():
206195
gs.try_remove(tmpdir)
207196
os.mkdir(tmpdir)
208197
if is_zip:
209-
shutil.copyfile(
210-
zipfile, os.path.join(tmpdir, "{im}{su}.zip".format(im=tile, su=suff))
211-
)
198+
shutil.copyfile(zipfile, os.path.join(tmpdir, f"{tile}{suff}.zip"))
212199
else:
213-
shutil.copyfile(
214-
hgtfile, os.path.join(tmpdir, "{im}{su}".format(im=tile[:7], su=suff))
215-
)
200+
shutil.copyfile(hgtfile, os.path.join(tmpdir, f"{tile[:7]}{suff}"))
216201
# change to temporary directory
217202
os.chdir(tmpdir)
218203
in_temp = True
219204

220-
zipfile = "{im}{su}.zip".format(im=tile, su=suff)
221-
hgtfile = "{im}{su}".format(im=tile[:7], su=suff)
205+
zipfile = f"{tile}{suff}.zip"
206+
hgtfile = f"{tile[:7]}{suff}"
222207

223208
bilfile = tile + ".bil"
224209

225210
if is_zip:
226211
# unzip & rename data file:
227212
gs.message(_("Extracting '%s'...") % infile)
228213
try:
229-
zf = zfile.ZipFile(zipfile)
230-
zf.extractall()
214+
with zfile.ZipFile(zipfile) as zf:
215+
zf.extractall()
231216
except (zfile.BadZipfile, zfile.LargeZipFile, PermissionError):
232217
gs.fatal(_("Unable to unzip file."))
233218

@@ -263,15 +248,11 @@ def main():
263248

264249
header = tmpl % (ulxmap, ulymap)
265250
hdrfile = tile + ".hdr"
266-
outf = open(hdrfile, "w")
267-
outf.write(header)
268-
outf.close()
251+
Path(hdrfile).write_text(header)
269252

270253
# create prj file: To be precise, we would need EGS96! But who really cares...
271254
prjfile = tile + ".prj"
272-
outf = open(prjfile, "w")
273-
outf.write(proj)
274-
outf.close()
255+
Path(prjfile).write_text(proj)
275256

276257
try:
277258
gs.run_command("r.in.gdal", input=bilfile, out=tileout)

0 commit comments

Comments
 (0)