Skip to content

Commit

Permalink
style: Fix redundant-open-modes (UP015) (#4410)
Browse files Browse the repository at this point in the history
  • Loading branch information
echoix authored Sep 30, 2024
1 parent 33d0d15 commit 480e050
Show file tree
Hide file tree
Showing 52 changed files with 78 additions and 83 deletions.
2 changes: 1 addition & 1 deletion display/d.mon/render_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def remove_mapfile(mapfile):
# read environment variables from file
def read_env_file(env_file):
width = height = legfile = None
fd = open(env_file, "r")
fd = open(env_file)
if fd is None:
grass.fatal("Unable to open file '{0}'".format(env_file))
lines = fd.readlines()
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/core/gconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ def load_source(modname, filename):
skipInterface = True
if os.path.splitext(command[0])[1] in {".py", ".sh"}:
try:
with open(command[0], "r") as sfile:
with open(command[0]) as sfile:
for line in sfile:
if len(line) < 3:
continue
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ def GetWindow(self):
env["GISDBASE"], env["LOCATION_NAME"], env["MAPSET"], "WIND"
)
try:
windfile = open(filename, "r")
windfile = open(filename)
except OSError as e:
sys.exit(
_("Error: Unable to open '%(file)s'. Reason: %(ret)s. wxGUI exited.\n")
Expand Down
4 changes: 2 additions & 2 deletions gui/wxpython/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ def update_nested_dict_by_dict(dictionary, update):
return dictionary

try:
with open(self.filePath, "r") as f:
with open(self.filePath) as f:
update = json.load(f, object_hook=settings_JSON_decode_hook)
update_nested_dict_by_dict(settings, update)
except json.JSONDecodeError as e:
Expand All @@ -942,7 +942,7 @@ def _readLegacyFile(self, settings=None):
settings = self.userSettings

try:
fd = open(self.legacyFilePath, "r")
fd = open(self.legacyFilePath)
except OSError:
sys.stderr.write(
_("Unable to read settings file <%s>\n") % self.legacyFilePath
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/core/watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def on_modified(self, event):
self.modified_time = timestamp
# wait to make sure file writing is done
time.sleep(0.1)
with open(event.src_path, "r") as f:
with open(event.src_path) as f:
gisrc = {}
for line in f:
key, val = line.split(":")
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/core/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,7 @@ def read(self, parent):
:return: list of map layers
"""
try:
file = open(self.filename, "r")
file = open(self.filename)
except OSError:
wx.MessageBox(
parent=parent,
Expand Down
4 changes: 2 additions & 2 deletions gui/wxpython/gcp/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def __init__(self, parent, giface):
self.target_gisrc = os.environ["GISRC"]
self.gisrc_dict = {}
try:
f = open(self.target_gisrc, "r")
f = open(self.target_gisrc)
for line in f:
line = line.replace("\n", "").strip()
if len(line) < 1:
Expand Down Expand Up @@ -1603,7 +1603,7 @@ def ReadGCPs(self):
GError(parent=self, message=_("target mapwin not defined"))

try:
f = open(self.file["points"], "r")
f = open(self.file["points"])
GCPcnt = 0

for line in f:
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/gmodeler/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def _substituteFile(self, item, params=None, checkOnly=False):

for finput in self.fileInput:
# read lines
fd = open(finput, "r")
fd = open(finput)
try:
data = self.fileInput[finput] = fd.read()
finally:
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/gui_core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2588,7 +2588,7 @@ def OnFileLoad(self, event):

data = ""
try:
f = open(path, "r")
f = open(path)
except OSError as e:
gcmd.GError(
parent=self,
Expand Down
4 changes: 2 additions & 2 deletions gui/wxpython/gui_core/ghelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def _pageCopyright(self):
"""Copyright information"""
copyfile = os.path.join(os.getenv("GISBASE"), "COPYING")
if os.path.exists(copyfile):
copyrightFile = open(copyfile, "r")
copyrightFile = open(copyfile)
copytext = copyrightFile.read()
copyrightFile.close()
else:
Expand Down Expand Up @@ -303,7 +303,7 @@ def _pageLicense(self):
"""Licence about"""
licfile = os.path.join(os.getenv("GISBASE"), "GPL.TXT")
if os.path.exists(licfile):
licenceFile = open(licfile, "r")
licenceFile = open(licfile)
license = "".join(licenceFile.readlines())
licenceFile.close()
else:
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/gui_core/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ def _loadSettings(self):
return data

try:
fd = open(self.settingsFile, "r")
fd = open(self.settingsFile)
except OSError:
return data

Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/image2target/ii2t_gis_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ def _readGisRC(self):

if gisrc and os.path.isfile(gisrc):
try:
rc = open(gisrc, "r")
rc = open(gisrc)
for line in rc:
try:
key, val = line.split(":", 1)
Expand Down
4 changes: 2 additions & 2 deletions gui/wxpython/image2target/ii2t_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def __init__(self, parent, giface):
self.target_gisrc = os.environ["GISRC"]
self.gisrc_dict = {}
try:
f = open(self.target_gisrc, "r")
f = open(self.target_gisrc)
for line in f:
line = line.replace("\n", "").strip()
if len(line) < 1:
Expand Down Expand Up @@ -1627,7 +1627,7 @@ def ReadGCPs(self):
GError(parent=self, message=_("target mapwin not defined"))

try:
f = open(self.file["control_points"], "r")
f = open(self.file["control_points"])
GCPcnt = 0

for line in f:
Expand Down
12 changes: 5 additions & 7 deletions gui/wxpython/location_wizard/wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2543,7 +2543,7 @@ def __readData(self):
"""Get georeferencing information from tables in $GISBASE/etc/proj"""

# read projection and parameters
f = open(os.path.join(globalvar.ETCDIR, "proj", "parms.table"), "r")
f = open(os.path.join(globalvar.ETCDIR, "proj", "parms.table"))
self.projections = {}
self.projdesc = {}
for line in f:
Expand All @@ -2566,7 +2566,7 @@ def __readData(self):
f.close()

# read datum definitions
f = open(os.path.join(globalvar.ETCDIR, "proj", "datum.table"), "r")
f = open(os.path.join(globalvar.ETCDIR, "proj", "datum.table"))
self.datums = {}
paramslist = []
for line in f:
Expand All @@ -2584,7 +2584,7 @@ def __readData(self):
f.close()

# read Earth-based ellipsiod definitions
f = open(os.path.join(globalvar.ETCDIR, "proj", "ellipse.table"), "r")
f = open(os.path.join(globalvar.ETCDIR, "proj", "ellipse.table"))
self.ellipsoids = {}
for line in f:
line = line.expandtabs(1)
Expand All @@ -2600,9 +2600,7 @@ def __readData(self):
f.close()

# read Planetary ellipsiod definitions
f = open(
os.path.join(globalvar.ETCDIR, "proj", "ellipse.table.solar.system"), "r"
)
f = open(os.path.join(globalvar.ETCDIR, "proj", "ellipse.table.solar.system"))
self.planetary_ellipsoids = {}
for line in f:
line = line.expandtabs(1)
Expand All @@ -2618,7 +2616,7 @@ def __readData(self):
f.close()

# read projection parameter description and parsing table
f = open(os.path.join(globalvar.ETCDIR, "proj", "desc.table"), "r")
f = open(os.path.join(globalvar.ETCDIR, "proj", "desc.table"))
self.paramdesc = {}
for line in f:
line = line.strip()
Expand Down
4 changes: 2 additions & 2 deletions gui/wxpython/mapdisp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def __init__(self, giface, cmdfile=None, mapfile=None):
self.renderMgr = RenderMapMgr(self)

# update legend file variable with the one d.mon uses
with open(monFile["env"], "r") as f:
with open(monFile["env"]) as f:
lines = f.readlines()
for line in lines:
if "GRASS_LEGEND_FILE" in line:
Expand All @@ -123,7 +123,7 @@ def GetLayersFromCmdFile(self):

nlayers = 0
try:
fd = open(self.cmdfile, "r")
fd = open(self.cmdfile)
lines = fd.readlines()
fd.close()
# detect d.out.file, delete the line from the cmd file and export
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/modules/colorrules.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ def OnLoadRulesFile(self, event):

self.rulesPanel.Clear()

fd = open(path, "r")
fd = open(path)
self.ReadColorTable(ctable=fd.read())
fd.close()

Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/modules/mcalc_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ def OnLoadExpression(self, event):
return

try:
fobj = open(path, "r")
fobj = open(path)
mctxt = fobj.read()
finally:
fobj.close()
Expand Down
8 changes: 4 additions & 4 deletions gui/wxpython/photo2image/ip2i_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def __init__(
self.source_gisrc = os.environ["GISRC"]
self.gisrc_dict = {}
try:
f = open(self.target_gisrc, "r")
f = open(self.target_gisrc)
for line in f:
line = line.replace("\n", "").strip()
if len(line) < 1:
Expand Down Expand Up @@ -429,7 +429,7 @@ def __init__(
import re

try:
fc = open(self.file["camera"], mode="r")
fc = open(self.file["camera"])
fc_count = 0
for line in fc:
fc_count += 1
Expand All @@ -438,7 +438,7 @@ def __init__(
numberOfFiducial = int(line.split()[-1])
dataFiducialX = []
dataFiducialY = []
fc = open(self.file["camera"], mode="r")
fc = open(self.file["camera"])
fc_count = 0
for line in fc:
fc_count += 1
Expand Down Expand Up @@ -955,7 +955,7 @@ def ReadGCPs(self):
GError(parent=self, message=_("target mapwin not defined"))

try:
f = open(self.file["points"], "r")
f = open(self.file["points"])
GCPcnt = 0

for line in f:
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/psmap/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ def GetImageOrigSize(self, imagePath):
# if eps, read info from header
if os.path.splitext(fileName)[1].lower() == ".eps":
bbInfo = "%%BoundingBox"
file = open(imagePath, "r")
file = open(imagePath)
w = h = 0
while file:
line = file.readline()
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/vnet/vnet_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ def GetLastModified(self):
"head",
)
try:
head = open(headPath, "r")
head = open(headPath)
for line in head:
i = line.find(
"MAP DATE:",
Expand Down
2 changes: 1 addition & 1 deletion imagery/i.atcorr/create_iwave.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def read_input(csvfile):
first column is wavelength
values are those of the discrete band filter functions
"""
infile = open(csvfile, "r")
infile = open(csvfile)

# get number of bands and band names
bands = infile.readline().split(",")
Expand Down
8 changes: 4 additions & 4 deletions lib/init/grass.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def create_gisrc(tmpdir, gisrcrc):
def read_gisrc(filename):
kv = {}
try:
f = open(filename, "r")
f = open(filename)
except OSError:
return kv

Expand All @@ -522,7 +522,7 @@ def write_gisrcrc(gisrcrc, gisrc, skip_variable=None):
"""Reads gisrc file and write to gisrcrc"""
debug("Reading %s" % gisrc)
number = 0
with open(gisrc, "r") as f:
with open(gisrc) as f:
lines = f.readlines()
for line in lines:
if skip_variable in line:
Expand All @@ -535,7 +535,7 @@ def write_gisrcrc(gisrcrc, gisrc, skip_variable=None):

def read_env_file(path):
kv = {}
f = open(path, "r")
f = open(path)
for line in f:
k, v = line.split(":", 1)
kv[k.strip()] = v.strip()
Expand Down Expand Up @@ -1098,7 +1098,7 @@ def set_language(grass_config_dir):

# Override value is stored in wxGUI preferences file.
try:
with open(os.path.join(grass_config_dir, "wx.json"), "r") as json_file:
with open(os.path.join(grass_config_dir, "wx.json")) as json_file:
try:
language = json.load(json_file)["language"]["locale"]["lc_all"]
except KeyError:
Expand Down
4 changes: 2 additions & 2 deletions man/build_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def check_for_desc_override(basename):


def read_file(name):
f = open(name, "r")
f = open(name)
s = f.read()
f.close()
return s
Expand Down Expand Up @@ -476,7 +476,7 @@ def write_html_footer(f, index_url, year=None):


def get_desc(cmd):
f = open(cmd, "r")
f = open(cmd)
while True:
line = f.readline()
if not line:
Expand Down
4 changes: 2 additions & 2 deletions man/build_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def check_for_desc_override(basename):


def read_file(name):
f = open(name, "r")
f = open(name)
s = f.read()
f.close()
return s
Expand Down Expand Up @@ -337,7 +337,7 @@ def write_rest_footer(f, index_url):


def get_desc(cmd):
f = open(cmd, "r")
f = open(cmd)
while True:
line = f.readline()
if not line:
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ ignore = [
"TRY201", # verbose-raise
"TRY300", # try-consider-else
"TRY301", # raise-within-try
"UP015", # redundant-open-modes
"UP030", # format-literals
"UP031", # printf-string-formatting
"UP032", # f-string
Expand Down
6 changes: 2 additions & 4 deletions python/grass/grassdb/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ def _read_from_plain_text(history_path):
stores only executed commands."""
content_list = []
try:
with open(
history_path, encoding="utf-8", mode="r", errors="replace"
) as file_history:
with open(history_path, encoding="utf-8", errors="replace") as file_history:
content_list = [
{"command": line.strip(), "command_info": None} for line in file_history
]
Expand Down Expand Up @@ -287,7 +285,7 @@ def _add_entry_to_JSON(history_path, entry):
:param dict entry: entry consisting of 'command' and 'command_info' keys
"""
try:
with open(history_path, encoding="utf-8", mode="r") as file_history:
with open(history_path, encoding="utf-8") as file_history:
existing_data = json.load(file_history)
except (OSError, ValueError):
existing_data = []
Expand Down
Loading

0 comments on commit 480e050

Please sign in to comment.