Skip to content

Commit

Permalink
safeguard OARec dates (#248)
Browse files Browse the repository at this point in the history
* OARec: safeguard RFC3339 date output

* update dates

* update Python versions in CI

* update copyright
  • Loading branch information
tomkralidis authored Aug 27, 2024
1 parent 305e497 commit cd38e68
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
name: Build and Deploy Documentation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@master
- uses: actions/setup-python@v5
with:
python-version: '3.x'
python-version: '3.11'
- name: Install requirements 📦
run: |
python -m pip install --upgrade pip
Expand Down
10 changes: 3 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.10", "3.11"]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@master
- uses: actions/setup-python@v5
name: Setup Python ${{ matrix.python-version }}
with:
python-version: ${{ matrix.python-version }}
Expand All @@ -28,15 +28,11 @@ jobs:
run: |
coverage run --source pygeometa setup.py test
coverage report -m
# with:
# fail_ci_if_error: false
- name: build docs 🏗️
run: mkdocs build -f docs/mkdocs.yml
- name: run flake8 ⚙️
run: flake8
- name: build Python package 🏗️
run: python3 setup.py sdist bdist_wheel --universal
- name: build Debian package 🏗️
# with:
# fail_ci_if_error: false
run: sudo debuild -b -uc -us
60 changes: 46 additions & 14 deletions pygeometa/schemas/ogcapi_records/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# those files. Users are asked to read the 3rd Party Licenses
# referenced with those assets.
#
# Copyright (c) 2023 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand All @@ -43,7 +43,7 @@
#
# =================================================================

from datetime import datetime
from datetime import date, datetime
import json
import logging
import os
Expand Down Expand Up @@ -153,21 +153,13 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:
record['time'] = None

LOGGER.debug('Checking for dates')

if 'dates' in mcf['identification']:
if 'creation' in mcf['identification']['dates']:
record['properties']['created'] = str(mcf['identification']['dates']['creation']) # noqa
if 'revision' in mcf['identification']['dates']:
record['properties']['updated'] = str(mcf['identification']['dates']['revision']) # noqa

if record['properties'].get('created') is None:
record['properties']['created'] = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') # noqa
record['properties']['created'] = self.generate_date(mcf['identification']['dates']['creation']) # noqa

for date_type in ['created', 'updated']:
ds = record['properties'].get(date_type)
if ds is not None and len(ds) == 10:
LOGGER.debug('Date type found; expanding to date-time')
dt = datetime.strptime(ds, '%Y-%m-%d').strftime('%Y-%m-%dT%H:%M:%SZ') # noqa
record['properties'][date_type] = dt
if 'revision' in mcf['identification']['dates']:
record['properties']['updated'] = self.generate_date(mcf['identification']['dates']['revision']) # noqa

rights = get_charstring(mcf['identification'].get('rights'),
self.lang1, self.lang2)
Expand Down Expand Up @@ -416,3 +408,43 @@ def generate_link(self, distribution: dict) -> dict:
link['channel'] = distribution['channel']

return link

def generate_date(self, date_value: str) -> str:
"""
Helper function to derive RFC3339 date from MCF date type
:param date_value: `str` of date value
:returns: `str` of date-time value
"""

value = None

if isinstance(date_value, str) and date_value != 'None':
if len(date_value) == 10: # YYYY-MM-DD
format_ = '%Y-%m-%d'
elif len(date_value) == 7: # YYYY-MM
format_ = '%Y-%m'
elif len(date_value) == 4: # YYYY
format_ = '%Y'

LOGGER.debug('date type found; expanding to date-time')
value = datetime.strptime(date_value, format_).strftime('%Y-%m-%dT%H:%M:%SZ') # noqa

elif isinstance(date_value, int) and len(str(date_value)) == 4:
date_value2 = str(date_value)
LOGGER.debug('date type found; expanding to date-time')
format_ = '%Y'
value = datetime.strptime(date_value2, format_).strftime('%Y-%m-%dT%H:%M:%SZ') # noqa

elif isinstance(date_value, (date, datetime)):
value = date_value.strftime('%Y-%m-%dT%H:%M:%SZ')

elif date_value in [None, 'None']:
value = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')

else:
msg = f'Unknown date string: {date_value}'
raise RuntimeError(msg)

return value
4 changes: 2 additions & 2 deletions pygeometa/schemas/wmo_wcmp2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# those files. Users are asked to read the 3rd Party Licenses
# referenced with those assets.
#
# Copyright (c) 2023 Tom Kralidis
# Copyright (c) 2024 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -106,7 +106,7 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:
except KeyError:
LOGGER.warning('Missing wmo:dataPolicy')

if 'created' not in record['properties']:
if record['properties'].get('created') is None:
record['properties']['created'] = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') # noqa

if stringify:
Expand Down

0 comments on commit cd38e68

Please sign in to comment.