Skip to content

Commit 0797389

Browse files
bring back yaml representer removing string quotes from datetime objects
1 parent 7c7ffc8 commit 0797389

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

pygeoapi_config_dialog.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
"""
2424

2525
from copy import deepcopy
26-
from datetime import date, datetime, timezone
26+
from datetime import datetime
2727
import os
2828
from wsgiref import headers
2929
import requests
3030
import yaml
3131

32+
from .utils.helper_functions import datetime_to_string
3233
from .utils.data_diff import diff_yaml_dict
3334

3435
from .ui_widgets.utils import get_url_status
@@ -149,6 +150,15 @@ class CustomDumper(yaml.SafeDumper):
149150
),
150151
)
151152

153+
# make sure datetime items are not saved as strings with quotes
154+
def represent_datetime_as_timestamp(dumper, data: datetime):
155+
value = datetime_to_string(data)
156+
157+
# emit as YAML timestamp → plain scalar, no quotes
158+
return dumper.represent_scalar("tag:yaml.org,2002:timestamp", value)
159+
160+
self.dumper.add_representer(datetime, represent_datetime_as_timestamp)
161+
152162
# custom assignments
153163
self.model = QStringListModel()
154164
self.proxy = QSortFilterProxyModel()
@@ -179,7 +189,7 @@ def on_button_clicked(self, button):
179189
else:
180190
# check #1: show diff with "Procced" and "Cancel" options
181191
diff_approved, processed_config_data = (
182-
self._diff_original_and_current_data()
192+
self._diff_original_and_current_data(get_yaml_output=True)
183193
)
184194
if not diff_approved:
185195
return
@@ -396,7 +406,9 @@ def _set_validate_ui_data(self) -> tuple[bool, list]:
396406
QMessageBox.warning(f"Error deserializing: {e}")
397407
return
398408

399-
def _diff_original_and_current_data(self) -> tuple[bool, dict]:
409+
def _diff_original_and_current_data(
410+
self, get_yaml_output=False
411+
) -> tuple[bool, dict]:
400412
"""Before saving the file, show the diff and give an option to proceed or cancel."""
401413

402414
new_config_data = self.config_data.asdict_enum_safe(
@@ -412,6 +424,14 @@ def _diff_original_and_current_data(self) -> tuple[bool, dict]:
412424
new_config_data,
413425
)
414426

427+
# if get_yaml_output, preserve datetime objects without string conversion.
428+
# This is needed so the yaml dumper is using representer removing quotes from datetime strings
429+
if get_yaml_output:
430+
new_config_data = self.config_data.asdict_enum_safe(
431+
self.config_data, datetime_to_str=False
432+
)
433+
434+
# if no diff detected, directly accept the changes
415435
if (
416436
len(diff_data["added"])
417437
+ len(diff_data["removed"])
@@ -420,7 +440,7 @@ def _diff_original_and_current_data(self) -> tuple[bool, dict]:
420440
):
421441
return True, new_config_data
422442

423-
# add a window with the choice
443+
# if diff detected, show a window with the choice to approve the diff
424444
QgsMessageLog.logMessage(f"{diff_data}")
425445
dialog = ReadOnlyTextDialog(self, "Warning", diff_data, True)
426446
result = dialog.exec_() # returns QDialog.Accepted (1) or QDialog.Rejected (0)

tests/test_yaml_save.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_json_schema_on_open_save(qtbot, sample_yaml: str):
4242

4343
# Save YAML
4444
processed_config_data = dialog.config_data.asdict_enum_safe(
45-
dialog.config_data, datetime_to_str=True
45+
dialog.config_data, datetime_to_str=False
4646
)
4747
abs_new_yaml_path = sample_yaml.with_name(f"saved_{sample_yaml.name}")
4848
dialog.save_to_file(processed_config_data, abs_new_yaml_path)
@@ -101,21 +101,21 @@ def test_open_file_validate_ui_data_save_file(qtbot, sample_yaml: str):
101101
sample_yaml
102102
) # now dialog.config_data has the data stored including .all_missing_props
103103
yaml1_data = dialog.config_data.asdict_enum_safe(
104-
deepcopy(dialog.yaml_original_data), True
104+
deepcopy(dialog.yaml_original_data), datetime_to_str=False
105105
)
106106
yaml1_missing_props = deepcopy(dialog.config_data.all_missing_props)
107107

108108
# Save YAML - EVEN THOUGH some mandatory fields might be missing and recorded as empty strings/lists
109109
processed_config_data = dialog.config_data.asdict_enum_safe(
110-
dialog.config_data, datetime_to_str=True
110+
dialog.config_data, datetime_to_str=False
111111
)
112112
abs_new_yaml_path = sample_yaml.with_name(f"saved_updated_{sample_yaml.name}")
113113
dialog.save_to_file(processed_config_data, abs_new_yaml_path)
114114

115115
# open the new file
116116
dialog.open_file(abs_new_yaml_path) # now dialog.config_data has the data stored
117117
yaml2_data = dialog.config_data.asdict_enum_safe(
118-
deepcopy(dialog.yaml_original_data), True
118+
deepcopy(dialog.yaml_original_data), datetime_to_str=False
119119
)
120120

121121
# get diff between old and new data

0 commit comments

Comments
 (0)