Skip to content

Commit

Permalink
set value types before comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
shred86 committed Jun 18, 2024
1 parent f70ab39 commit 1ccf9e4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 49 deletions.
90 changes: 58 additions & 32 deletions src/O4_Config_Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ def write_global_cfg(self):
UI.vprint(1, "Global tile configuration settings saved.")
except Exception as e:
UI.lvprint(1, "Could not write global config.")
_LOGGER.error("Could not write global config: %s", e)
_LOGGER.exception("Could not write global config: %s", e)
return

def reset_app_cfg(self) -> None:
Expand Down Expand Up @@ -1063,7 +1063,7 @@ def write_app_cfg(self) -> None:
UI.vprint(1, "Application configuration settings saved.")
except Exception as e:
UI.lvprint(1, "Could not write application settings to global config.")
_LOGGER.error(
_LOGGER.exception(
"Could not write application settings to global config: %s", e
)
return
Expand Down Expand Up @@ -1186,7 +1186,7 @@ def check_unsaved_changes(self, select_tile=False) -> str:
try:
(lat, lon) = self.parent.get_lat_lon()
except Exception as e:
_LOGGER.error("Could not get lat/lon coordinates: %s", e)
_LOGGER.exception("Could not get lat/lon coordinates: %s", e)
return

custom_build_dir = self.parent.custom_build_dir_entry.get()
Expand All @@ -1209,14 +1209,16 @@ def check_unsaved_changes(self, select_tile=False) -> str:
# Skip zone_list since we're only checking config tab values and default_website + default_zl
if var == "zone_list":
continue
current_value = self.v_[var].get()
# Compare current_value with value in file_dict
if file_dict[var] != current_value:
tab_value = self.set_value_type(var, self.v_[var].get())
file_value = self.set_value_type(var, file_dict[var])

# Compare tab_value with value in file_dict
if file_value != tab_value:
_LOGGER.debug(
"Unsaved changes for %s - current value: %s, config file value: %s",
"Unsaved changes in tile config for %s - current value: %s, config file value: %s",
var,
current_value,
file_dict[var],
tab_value,
file_value,
)
unsaved_changes["tile"] = True
break
Expand All @@ -1228,27 +1230,26 @@ def check_unsaved_changes(self, select_tile=False) -> str:
line.strip().split("=") for line in f if line.strip()
)
for var in list_global_tile_vars:
# Skip default_website and default_zl since they're not a part of the tab settings
# TODO: Remove this as it's not in list_global_tile_vars
if var == "default_website" or var == "default_zl":
continue
# Config file has global_ prefix so we need to remove it
# Config file doesn't have global_ prefix so we need to remove it
_var = var.replace(global_prefix, "")
current_value = self.v_[_var].get()
if file_dict[_var] != current_value:

tab_value = self.set_value_type(_var, self.v_[_var].get())
file_value = self.set_value_type(_var, file_dict[_var])

if file_value != tab_value:
_LOGGER.debug(
"Unsaved changes for %s - current value: %s, config file value: %s",
"Unsaved changes in global config for %s - current value: %s, config file value: %s",
var,
current_value,
file_dict[_var],
tab_value,
file_value,
)
unsaved_changes["tile"] = True
break
except FileNotFoundError:
pass

except Exception as e:
_LOGGER.error("Error opening tile config file: %s", e)
_LOGGER.exception(e)

if not select_tile:
# Check Global Config tab values against the global config file
Expand All @@ -1260,32 +1261,37 @@ def check_unsaved_changes(self, select_tile=False) -> str:
for var in list_global_tile_vars:
# Config file has global_ prefix so we need to remove it
_var = var.replace(global_prefix, "")
current_value = self.v_[var].get()
if file_dict[_var] != current_value:

tab_value = self.set_value_type(_var, self.v_[var].get())
file_value = self.set_value_type(_var, file_dict[_var])

if file_value != tab_value:
_LOGGER.debug(
"Unsaved changes for %s - current value: %s, config file value: %s",
"Unsaved changes in global config for %s - current value: %s, config file value: %s",
var,
current_value,
file_dict[_var],
tab_value,
file_value,
)
unsaved_changes["global"] = True
break
# Check App Config tab values against the global config file
for var in list_app_vars:
current_value = self.v_[var].get()
if file_dict[var] != current_value:
tab_value = self.set_value_type(var, self.v_[var].get())
file_value = self.set_value_type(var, file_dict[var])

if file_value != tab_value:
_LOGGER.debug(
"Unsaved changes for %s - current value: %s, config file value: %s",
"Unsaved changes in global config for %s - current value: %s, config file value: %s",
var,
current_value,
file_dict[var],
tab_value,
file_value,
)
unsaved_changes["application"] = True
break
except FileNotFoundError:
_LOGGER.error("Global configuration file (Ortho4XP.cfg) not found.")
except Exception as e:
_LOGGER.error("Error opening global config file: %s", e)
_LOGGER.exception(e)

if any(unsaved_changes.values()):
message = ""
Expand All @@ -1300,7 +1306,7 @@ def check_unsaved_changes(self, select_tile=False) -> str:
message = f"{', '.join(keys[:-1])} and {keys[-1]} Config tabs have unsaved changes.\n"
elif count == 3:
message = (
f"Tile, Global and Application Config tabs have unsaved changes.\n"
f"Tile, Global, and Application Config tabs have unsaved changes.\n"
)
# Appears to be an issue with macOS and using "Cancel" as sometimes it will present
# the messagebox twice. Does not happenw with "Yes/No" options.
Expand All @@ -1317,6 +1323,26 @@ def check_unsaved_changes(self, select_tile=False) -> str:
if not select_tile:
self.destroy()

def set_value_type(self, var: str, value) -> float | bool | str | list:
"""
Return value based on type in cfg_vars.
:param str value: value to be converted.
:return: value in type based on cfg_vars
"""
# Using floats for both int and float since we're going to compare them
if cfg_vars[var]["type"] == int or cfg_vars[var]["type"] == float:
return float(value)
if cfg_vars[var]["type"] == bool:
if value == "True":
return True
if value == "False":
return False
if cfg_vars[var]["type"] == str:
return str(value)
if cfg_vars[var]["type"] == list:
return list(value)

def dict_to_cfg(self, file:str, cfg_dict: dict) -> None:
"""
Convert dictionary to key=value format and write to file.
Expand Down
33 changes: 16 additions & 17 deletions src/O4_GUI_Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,14 @@ def get_lat_lon(self, check=True):
)
except Exception as e:
error_string += "Latitude wrongly encoded. "
_LOGGER.error(e)
_LOGGER.exception(e)
try:
lon = int(self.lon.get())
if lon < -180 or lon > 179:
error_string += "Longitude out of range (-180,179)."
except Exception as e:
error_string += "Longitude wrongly encoded."
_LOGGER.error(e)
_LOGGER.exception(e)
if error_string and check:
UI.vprint(0, "Error: " + error_string)
return None
Expand All @@ -565,7 +565,7 @@ def build_poly_file(self):
tile.make_dirs()
except Exception as e:
UI.vprint(1, "Process aborted.\n")
_LOGGER.error(e)
_LOGGER.exception(e)
return 0
self.working_thread = threading.Thread(
target=VMAP.build_poly_file, args=[tile]
Expand All @@ -578,7 +578,7 @@ def build_mesh(self, event):
tile.make_dirs()
except Exception as e:
UI.vprint(1, "Process aborted.\n")
_LOGGER.error(e)
_LOGGER.exception("Exception on build_mesh")
return 0
self.working_thread = threading.Thread(
target=MESH.build_mesh, args=[tile]
Expand All @@ -591,7 +591,7 @@ def sort_mesh(self, event):
tile.make_dirs()
except Exception as e:
UI.vprint(1, "Process aborted.\n")
_LOGGER.error(e)
_LOGGER.exception("Exception on sort_mesh")
return 0
self.working_thread = threading.Thread(
target=MESH.sort_mesh, args=[tile]
Expand All @@ -604,7 +604,7 @@ def community_mesh(self, event):
tile.make_dirs()
except Exception as e:
UI.vprint(1, "Process aborted.\n")
_LOGGER.error(e)
_LOGGER.exception("Exception on community_mesh")
return 0
self.working_thread = threading.Thread(
target=MESH.community_mesh, args=[tile]
Expand All @@ -618,7 +618,7 @@ def build_masks(self, event):
tile.make_dirs()
except Exception as e:
UI.vprint(1, "Process aborted.\n")
_LOGGER.error(e)
_LOGGER.exception(e)
return 0
self.working_thread = threading.Thread(
target=MASK.build_masks, args=[tile, for_imagery]
Expand All @@ -631,7 +631,7 @@ def build_tile(self):
tile.make_dirs()
except Exception as e:
UI.vprint(1, "Process aborted.\n")
_LOGGER.error(e)
_LOGGER.exception(e)
return 0
self.working_thread = threading.Thread(
target=TILE.build_tile, args=[tile]
Expand All @@ -644,7 +644,7 @@ def build_all(self):
tile.make_dirs()
except Exception as e:
UI.vprint(1, "Process aborted.\n")
_LOGGER.error(e)
_LOGGER.exception(e)
return 0
self.working_thread = threading.Thread(
target=TILE.build_all, args=[tile]
Expand All @@ -665,7 +665,7 @@ def open_config_window(self):
try:
(lat, lon) = self.get_lat_lon()
except Exception as e:
_LOGGER.error(e)
_LOGGER.exception(e)
return 0
self.config_window = CFG.Ortho4XP_Config(self)
return 1
Expand Down Expand Up @@ -1892,7 +1892,7 @@ def delete_osm_data(self, lat: int, lon: int) -> None:
)
except Exception as e:
UI.vprint(3, e)
_LOGGER.error(e)
_LOGGER.exception(e)

def delete_mask_data(self, lat: int, lon: int) -> None:
"""Delete cached mask data."""
Expand All @@ -1910,7 +1910,7 @@ def delete_mask_data(self, lat: int, lon: int) -> None:
)
except Exception as e:
UI.vprint(3, e)
_LOGGER.error(e)
_LOGGER.exception(e)

def delete_jpeg_imagery(self, lat: int, lon: int) -> None:
"""Delete ortho JPEG immagery."""
Expand All @@ -1933,7 +1933,7 @@ def delete_jpeg_imagery(self, lat: int, lon: int) -> None:
)
except Exception as e:
UI.vprint(3, e)
_LOGGER.error(e)
_LOGGER.exception(e)

def delete_tile_whole(self, lat: int, lon: int) -> None:
"""Delete all tile data."""
Expand All @@ -1956,7 +1956,7 @@ def delete_tile_whole(self, lat: int, lon: int) -> None:
)
except Exception as e:
UI.vprint(3, e)
_LOGGER.error(e)
_LOGGER.exception(e)

def delete_tile_textures(self, lat: int, lon: int) -> None:
"""Delete tile textures."""
Expand Down Expand Up @@ -1989,7 +1989,6 @@ def select_tile(self, event):
if self.parent.config_window is not None and self.parent.config_window.winfo_exists():
result = self.parent.config_window.check_unsaved_changes(select_tile=True)
if result == "cancel":
_LOGGER.info("canceled.")
return
x = self.canvas.canvasx(event.x)
y = self.canvas.canvasy(event.y)
Expand Down Expand Up @@ -2052,7 +2051,7 @@ def batch_build(self):
try:
tile = CFG.Tile(lat, lon, self.custom_build_dir)
except Exception as e:
_LOGGER.error(e)
_LOGGER.exception(e)
return 0
args = [
tile,
Expand Down Expand Up @@ -2135,7 +2134,7 @@ def draw_canvas(self, nx0, ny0):
filepreviewNW,
", please update your installation from a fresh copy.",
)
_LOGGER.error(e)
_LOGGER.exception(e)
return
if nx0 < 2 ** (self.earthzl - 3) - 1:
filepreviewNE = fileprefix + str(nx0 + 1) + "_" + str(ny0) + ".jpg"
Expand Down

0 comments on commit 1ccf9e4

Please sign in to comment.