Skip to content

Commit

Permalink
gui: Early exit from functions or loops (#4916)
Browse files Browse the repository at this point in the history
* gui: Early exit loops when possible

Flattens nested conditionals and moves error messages or returns near the top

* gui: Early exit functions

Flattens nested conditionals and moves error messages near the top

* Adjust CheckGCPcount() and return false even when msg is False

* format with ruff format
  • Loading branch information
echoix authored Feb 4, 2025
1 parent 5293516 commit 2619f91
Show file tree
Hide file tree
Showing 54 changed files with 1,465 additions and 1,501 deletions.
5 changes: 2 additions & 3 deletions gui/wxpython/animation/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,10 +727,9 @@ def _update(self):
if isStart:
self.animationData.startRegion = isStart
else:
if isStart:
self.animationData.startRegion = isStart
else:
if not isStart:
raise GException(_("Region information is not complete"))
self.animationData.startRegion = isStart
if isEnd:
self.animationData.endRegion = self.endRegion.GetValue()
self.animationData.zoomRegionValue = None
Expand Down
6 changes: 2 additions & 4 deletions gui/wxpython/animation/nviztask.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,10 @@ def GetCommandSeries(self, layerList, paramName):

if len(layerList) > 1:
raise GException(_("Please add only one layer in the list."))
return
layer = layerList[0]
if hasattr(layer, "maps"):
series = layer.maps
else:
if not hasattr(layer, "maps"):
raise GException(_("No map series nor space-time dataset is added."))
series = layer.maps

for value in series:
self.task.set_param(paramName, value)
Expand Down
17 changes: 9 additions & 8 deletions gui/wxpython/animation/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,14 +758,15 @@ def Clear(self):
Debug.msg(4, "MapFilesPool.Clear")

for key in list(self.dictionary.keys()):
if self.referenceCount[key] <= 0:
name, ext = os.path.splitext(self.dictionary[key])
os.remove(self.dictionary[key])
if ext == ".ppm":
os.remove(name + ".pgm")
del self.dictionary[key]
del self.referenceCount[key]
del self.size[key]
if self.referenceCount[key] > 0:
continue
name, ext = os.path.splitext(self.dictionary[key])
os.remove(self.dictionary[key])
if ext == ".ppm":
os.remove(name + ".pgm")
del self.dictionary[key]
del self.referenceCount[key]
del self.size[key]


class BitmapPool(DictRefCounter):
Expand Down
53 changes: 27 additions & 26 deletions gui/wxpython/animation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@ def validateMapNames(names, etype):
for name in names:
if name.find("@") >= 0:
nameShort, mapset = name.split("@", 1)
if nameShort in mapDict[mapset]:
newNames.append(name)
else:
raise GException(_("Map <%s> not found.") % name)
else:
found = False
for mapset, mapNames in mapDict.items():
if name in mapNames:
found = True
newNames.append(name + "@" + mapset)
if not found:
if nameShort not in mapDict[mapset]:
raise GException(_("Map <%s> not found.") % name)
newNames.append(name)

continue
found = False
for mapset, mapNames in mapDict.items():
if name in mapNames:
found = True
newNames.append(name + "@" + mapset)
if not found:
raise GException(_("Map <%s> not found.") % name)
return newNames


Expand Down Expand Up @@ -331,21 +331,22 @@ def layerListToCmdsMatrix(layerList):
continue
if hasattr(layer, "maps"):
for i, part in enumerate(layer.cmd):
if part.startswith("map="):
cmd = layer.cmd[:]
cmds = []
for map_ in layer.maps:
# check if dataset uses layers instead of maps
mapName, mapLayer = getNameAndLayer(map_)
cmd[i] = "map={name}".format(name=mapName)
if mapLayer:
try:
idx = cmd.index("layer")
cmd[idx] = "layer={layer}".format(layer=mapLayer)
except ValueError:
cmd.append("layer={layer}".format(layer=mapLayer))
cmds.append(cmd[:])
cmdsForComposition.append(cmds)
if not part.startswith("map="):
continue
cmd = layer.cmd[:]
cmds = []
for map_ in layer.maps:
# check if dataset uses layers instead of maps
mapName, mapLayer = getNameAndLayer(map_)
cmd[i] = "map={name}".format(name=mapName)
if mapLayer:
try:
idx = cmd.index("layer")
cmd[idx] = "layer={layer}".format(layer=mapLayer)
except ValueError:
cmd.append("layer={layer}".format(layer=mapLayer))
cmds.append(cmd[:])
cmdsForComposition.append(cmds)
else:
cmdsForComposition.append([layer.cmd] * count)

Expand Down
38 changes: 19 additions & 19 deletions gui/wxpython/core/gcmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,25 +180,25 @@ def __init__(self, args, **kwargs):
# "^" must be the first character in the list to avoid double
# escaping.
for c in ("^", "|", "&", "<", ">"):
if c in args[i]:
if "=" in args[i]:
a = args[i].split("=")
k = a[0] + "="
v = "=".join(a[1 : len(a)])
else:
k = ""
v = args[i]

# If there are spaces, the argument was already
# esscaped with double quotes, so don't escape it
# again.
if c in v and " " not in v:
# Here, we escape each ^ in ^^^ with ^^ and a
# <special character> with ^ + <special character>,
# so we need 7 carets.

v = v.replace(c, "^^^^^^^" + c)
args[i] = k + v
if c not in args[i]:
continue
if "=" in args[i]:
a = args[i].split("=")
k = a[0] + "="
v = "=".join(a[1 : len(a)])
else:
k = ""
v = args[i]

# If there are spaces, the argument was already
# escaped with double quotes, so don't escape it
# again.
if c in v and " " not in v:
# Here, we escape each ^ in ^^^ with ^^ and a
# <special character> with ^ + <special character>,
# so we need 7 carets.
v = v.replace(c, "^^^^^^^" + c)
args[i] = k + v

subprocess.Popen.__init__(self, args, **kwargs)

Expand Down
69 changes: 32 additions & 37 deletions gui/wxpython/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,12 @@ def GetCmd(self, string=False):
:return: command list/string
"""
if string:
if self.type == "command":
scmd = []
for c in self.cmd:
scmd.append(utils.GetCmdString(c))

return ";".join(scmd)
return utils.GetCmdString(self.cmd)
return self.cmd
if not string:
return self.cmd
if self.type == "command":
scmd = [utils.GetCmdString(c) for c in self.cmd]
return ";".join(scmd)
return utils.GetCmdString(self.cmd)

def GetType(self):
"""Get map layer type"""
Expand Down Expand Up @@ -1405,29 +1402,28 @@ def DeleteLayer(self, layer, overlay=False):

list_ = self.overlays if overlay else self.layers

if layer in list_:
if layer.mapfile:
base, mapfile = os.path.split(layer.mapfile)
tempbase = mapfile.split(".")[0]
if base == "" or tempbase == "":
return None
basefile = os.path.join(base, tempbase) + r".*"
# this comes all the way from r28605, so leaving
# it as it is, although it does not really fit with the
# new system (but probably works well enough)
for f in glob.glob(basefile):
os.remove(f)

if layer.GetType() in {"vector", "thememap"}:
if os.path.isfile(layer._legrow):
os.remove(layer._legrow)

list_.remove(layer)

self.layerRemoved.emit(layer=layer)
return layer
if layer not in list_:
return None
if layer.mapfile:
base, mapfile = os.path.split(layer.mapfile)
tempbase = mapfile.split(".")[0]
if base == "" or tempbase == "":
return None
basefile = os.path.join(base, tempbase) + r".*"
# this comes all the way from r28605, so leaving
# it as it is, although it does not really fit with the
# new system (but probably works well enough)
for f in glob.glob(basefile):
os.remove(f)

return None
if layer.GetType() in {"vector", "thememap"}:
if os.path.isfile(layer._legrow):
os.remove(layer._legrow)

list_.remove(layer)

self.layerRemoved.emit(layer=layer)
return layer

def SetLayers(self, layers):
self.layers = layers
Expand Down Expand Up @@ -1652,12 +1648,11 @@ def GetOverlay(self, id, list=False):
"""
ovl = [overlay for overlay in self.overlays if overlay.id == id]

if not list:
if len(ovl) != 1:
return None
return ovl[0]

return ovl
if list:
return ovl
if len(ovl) != 1:
return None
return ovl[0]

def DeleteOverlay(self, overlay):
"""Delete overlay
Expand Down
Loading

0 comments on commit 2619f91

Please sign in to comment.