Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Arohan Ajit committed Jan 31, 2025
1 parent 0ec1690 commit 88b0e36
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 75 deletions.
25 changes: 12 additions & 13 deletions gui/wxpython/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,25 +883,24 @@ def GetWindow(self):
env["GISDBASE"], env["LOCATION_NAME"], env["MAPSET"], "WIND"
)
try:
windfile = open(filename)
with open(filename) as windfile:
for line in windfile:
line = line.strip()
try:
key, value = line.split(":", 1)
except ValueError as e:
sys.stderr.write(
_("\nERROR: Unable to read WIND file: %s\n") % e
)
return None

self.wind[key.strip()] = value.strip()
except OSError as e:
sys.exit(
_("Error: Unable to open '%(file)s'. Reason: %(ret)s. wxGUI exited.\n")
% {"file": filename, "ret": e}
)

for line in windfile:
line = line.strip()
try:
key, value = line.split(":", 1)
except ValueError as e:
sys.stderr.write(_("\nERROR: Unable to read WIND file: %s\n") % e)
return None

self.wind[key.strip()] = value.strip()

windfile.close()

return self.wind

def AdjustRegion(self):
Expand Down
43 changes: 20 additions & 23 deletions gui/wxpython/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from core import globalvar
from core.gcmd import GException, GError
from core.utils import GetSettingsPath, PathJoin, rgb2str
from pathlib import Path


class SettingsJSONEncoder(json.JSONEncoder):
Expand Down Expand Up @@ -104,7 +105,8 @@ def __init__(self):
def _generateLocale(self):
"""Generate locales"""
try:
self.locs = os.listdir(os.path.join(os.environ["GISBASE"], "locale"))
locale_path = Path(os.environ["GISBASE"]) / "locale"
self.locs = [p.name for p in locale_path.iterdir() if p.is_dir()]
self.locs.append("en") # GRASS doesn't ship EN po files
self.locs.sort()
# Add a default choice to not override system locale
Expand Down Expand Up @@ -941,30 +943,28 @@ def _readLegacyFile(self, settings=None):
settings = self.userSettings

try:
fd = open(self.legacyFilePath)
with open(self.legacyFilePath) as fd:
line = ""
for line in fd:
line = line.rstrip("%s" % os.linesep)
group, key = line.split(self.sep)[0:2]
kv = line.split(self.sep)[2:]
subkeyMaster = None
if len(kv) % 2 != 0: # multiple (e.g. nviz)
subkeyMaster = kv[0]
del kv[0]
idx = 0
while idx < len(kv):
subkey = [subkeyMaster, kv[idx]] if subkeyMaster else kv[idx]
value = kv[idx + 1]
value = self._parseValue(value, read=True)
self.Append(settings, group, key, subkey, value)
idx += 2
except OSError:
sys.stderr.write(
_("Unable to read settings file <%s>\n") % self.legacyFilePath
)
return

try:
line = ""
for line in fd:
line = line.rstrip("%s" % os.linesep)
group, key = line.split(self.sep)[0:2]
kv = line.split(self.sep)[2:]
subkeyMaster = None
if len(kv) % 2 != 0: # multiple (e.g. nviz)
subkeyMaster = kv[0]
del kv[0]
idx = 0
while idx < len(kv):
subkey = [subkeyMaster, kv[idx]] if subkeyMaster else kv[idx]
value = kv[idx + 1]
value = self._parseValue(value, read=True)
self.Append(settings, group, key, subkey, value)
idx += 2
except ValueError as e:
print(
_(
Expand All @@ -975,9 +975,6 @@ def _readLegacyFile(self, settings=None):
% {"file": self.legacyFilePath, "detail": e, "line": line},
file=sys.stderr,
)
fd.close()

fd.close()

def SaveToFile(self, settings=None):
"""Save settings to the file"""
Expand Down
50 changes: 24 additions & 26 deletions gui/wxpython/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,30 +828,28 @@ def StoreEnvVariable(key, value=None, envFile=None):
lineSkipped = []
if os.path.exists(envFile):
try:
fd = open(envFile)
with open(envFile) as fd:
for line in fd:
line = line.rstrip(os.linesep)
try:
k, v = (x.strip() for x in line.split(" ", 1)[1].split("=", 1))
except Exception as e:
sys.stderr.write(
_("%s: line skipped - unable to parse '%s'\nReason: %s\n")
% (envFile, line, e)
)
lineSkipped.append(line)
continue
if k in environ:
sys.stderr.write(_("Duplicated key: %s\n") % k)
environ[k] = v
except OSError as error:
sys.stderr.write(
_("Unable to open file '{name}': {error}\n").format(
name=envFile, error=error
)
)
return
for line in fd:
line = line.rstrip(os.linesep)
try:
k, v = (x.strip() for x in line.split(" ", 1)[1].split("=", 1))
except Exception as e:
sys.stderr.write(
_("%s: line skipped - unable to parse '%s'\nReason: %s\n")
% (envFile, line, e)
)
lineSkipped.append(line)
continue
if k in environ:
sys.stderr.write(_("Duplicated key: %s\n") % k)
environ[k] = v

fd.close()

# update environmental variables
if value is None:
Expand All @@ -861,22 +859,22 @@ def StoreEnvVariable(key, value=None, envFile=None):

# write update env file
try:
fd = open(envFile, "w")
with open(envFile, "w") as fd:
expCmd = "set" if windows else "export"

fd.writelines(
"%s %s=%s\n" % (expCmd, key, value) for key, value in environ.items()
)

# write also skipped lines
fd.writelines(line + os.linesep for line in lineSkipped)
except OSError as error:
sys.stderr.write(
_("Unable to create file '{name}': {error}\n").format(
name=envFile, error=error
)
)
return
expCmd = "set" if windows else "export"

fd.writelines("%s %s=%s\n" % (expCmd, key, value) for key, value in environ.items())

# write also skipped lines
fd.writelines(line + os.linesep for line in lineSkipped)

fd.close()


def SetAddOnPath(addonPath=None, key="PATH"):
Expand Down
13 changes: 5 additions & 8 deletions gui/wxpython/core/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,11 @@ def read(self, parent):
:return: list of map layers
"""
try:
file = open(self.filename)
with open(self.filename) as file: # Changed to context manager
line_id = 1
for line in file:
self.process_line(line.rstrip("\n"), line_id)
line_id += 1
except OSError:
wx.MessageBox(
parent=parent,
Expand All @@ -1740,13 +1744,6 @@ def read(self, parent):
)
return []

line_id = 1
for line in file:
self.process_line(line.rstrip("\n"), line_id)
line_id += 1

file.close()

if self.num_error > 0:
wx.MessageBox(
parent=parent,
Expand Down
5 changes: 0 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,6 @@ ignore = [
# Other ignores:
"**.py" = ["PYI066"]
"*/testsuite/**.py" = ["PT009", "PT027"]
"gui/wxpython/core/globalvar.py" = ["PTH208"]
"gui/wxpython/core/render.py" = ["SIM115"]
"gui/wxpython/core/settings.py" = ["PTH208", "SIM115"]
"gui/wxpython/core/utils.py" = ["SIM115"]
"gui/wxpython/core/workspace.py" = ["SIM115"]
"gui/wxpython/datacatalog/catalog.py" = ["PTH208"]
"gui/wxpython/dbmgr/base.py" = ["SIM115"]
"gui/wxpython/gcp/manager.py" = ["PTH208"]
Expand Down

0 comments on commit 88b0e36

Please sign in to comment.