From cdd2228a24b8b7ff227bbff794fe6b7124854cc3 Mon Sep 17 00:00:00 2001 From: Kateryna Konieva Date: Fri, 19 Dec 2025 17:21:05 +0000 Subject: [PATCH 1/3] add datetime representer and allow unicode characters --- pygeoapi/util.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pygeoapi/util.py b/pygeoapi/util.py index 30a18de57..8c051b44a 100644 --- a/pygeoapi/util.py +++ b/pygeoapi/util.py @@ -202,13 +202,29 @@ def path_representer(dumper, data): yaml.add_multi_representer(pathlib.PurePath, path_representer) + def datetime_representer(dumper, data: datetime): + if data.tzinfo is None: + data = data.replace(tzinfo=timezone.utc) + else: + data = data.astimezone(timezone.utc) + value = data.strftime("%Y-%m-%dT%H:%M:%SZ") + + # timestamp in a specified format, without string quotes + return dumper.represent_scalar(u'tag:yaml.org,2002:timestamp', value) + + # create a dedicated dumper to write datetime to yaml correctly + class MyDumper(yaml.SafeDumper): + pass + + MyDumper.add_representer(datetime, datetime_representer) + lock = FileLock(f'{destfile}.lock') with lock: LOGGER.debug('Dumping YAML document') with open(destfile, 'wb') as fh: yaml.dump(dict_, fh, sort_keys=False, encoding='utf8', indent=4, - default_flow_style=False) + default_flow_style=False, allow_unicode=True, Dumper=MyDumper) return True From 99b02b496f484b057fa1b96240ff59f9cafa971c Mon Sep 17 00:00:00 2001 From: Kateryna Konieva Date: Mon, 29 Dec 2025 18:05:28 +0000 Subject: [PATCH 2/3] flake8 fix --- pygeoapi/util.py | 7 ++++--- tests/data/mysql_data.sql | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pygeoapi/util.py b/pygeoapi/util.py index 8c051b44a..914412c1b 100644 --- a/pygeoapi/util.py +++ b/pygeoapi/util.py @@ -211,7 +211,7 @@ def datetime_representer(dumper, data: datetime): # timestamp in a specified format, without string quotes return dumper.represent_scalar(u'tag:yaml.org,2002:timestamp', value) - + # create a dedicated dumper to write datetime to yaml correctly class MyDumper(yaml.SafeDumper): pass @@ -223,8 +223,9 @@ class MyDumper(yaml.SafeDumper): with lock: LOGGER.debug('Dumping YAML document') with open(destfile, 'wb') as fh: - yaml.dump(dict_, fh, sort_keys=False, encoding='utf8', indent=4, - default_flow_style=False, allow_unicode=True, Dumper=MyDumper) + yaml.dump(dict_, fh, sort_keys=False, encoding='utf8', + indent=4, default_flow_style=False, + allow_unicode=True, Dumper=MyDumper) return True diff --git a/tests/data/mysql_data.sql b/tests/data/mysql_data.sql index f2174ae55..9bb7c34f8 100644 --- a/tests/data/mysql_data.sql +++ b/tests/data/mysql_data.sql @@ -1,4 +1,4 @@ --- A test database for the mysql provider; a simple geospatial app +-- A test database for the mysql provider; a simple geospatial app -- Create the database DROP DATABASE IF EXISTS test_geo_app; From 0a17186afaebb598dfe3087e9b5389480594e0d3 Mon Sep 17 00:00:00 2001 From: Kateryna Konieva Date: Tue, 30 Dec 2025 23:25:13 +0000 Subject: [PATCH 3/3] add representer to the default dumper --- pygeoapi/util.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pygeoapi/util.py b/pygeoapi/util.py index 914412c1b..108853ee7 100644 --- a/pygeoapi/util.py +++ b/pygeoapi/util.py @@ -212,11 +212,7 @@ def datetime_representer(dumper, data: datetime): # timestamp in a specified format, without string quotes return dumper.represent_scalar(u'tag:yaml.org,2002:timestamp', value) - # create a dedicated dumper to write datetime to yaml correctly - class MyDumper(yaml.SafeDumper): - pass - - MyDumper.add_representer(datetime, datetime_representer) + yaml.add_representer(datetime, datetime_representer) lock = FileLock(f'{destfile}.lock') @@ -225,7 +221,7 @@ class MyDumper(yaml.SafeDumper): with open(destfile, 'wb') as fh: yaml.dump(dict_, fh, sort_keys=False, encoding='utf8', indent=4, default_flow_style=False, - allow_unicode=True, Dumper=MyDumper) + allow_unicode=True) return True