Skip to content

add support for schema.org #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions pygeometa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ def import_metadata(schema: str, metadata: str) -> dict:
:returns: MCF object
"""

content = None
error_message = None

if schema == 'autodetect':
schemas = get_supported_schemas()
else:
Expand All @@ -344,11 +347,17 @@ def import_metadata(schema: str, metadata: str) -> dict:
schema_object = load_schema(s)

try:
return schema_object.import_(metadata)
content = schema_object.import_(metadata)
break
except NotImplementedError:
raise RuntimeError(f'Import not supported for {s}')
error_message = f'Import not supported for {s}'
except Exception as err:
raise RuntimeError(f'Import failed: {err}')
error_message = f'Import failed: {err}'

if error_message is not None:
LOGGER.warning(error_message)

return content


def transform_metadata(input_schema: str, output_schema: str,
Expand Down
45 changes: 45 additions & 0 deletions pygeometa/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,48 @@ def json_serial(obj) -> Any:
msg = f'{obj} type {type(obj)} not serializable'
LOGGER.error(msg)
raise TypeError(msg)


def generate_datetime(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'
elif len(date_value) == 19: # YYYY-MM-DDTHH:MM:SS
msg = 'YYYY-MM-DDTHH:MM:SS with no timezone; converting to UTC'
LOGGER.debug(msg)
format_ = '%Y-%m-%dT%H:%M:%S'

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
3 changes: 2 additions & 1 deletion pygeometa/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@
THISDIR = os.path.dirname(os.path.realpath(__file__))

SCHEMAS = {
'dcat': 'pygeometa.schemas.dcat.DCATOutputSchema',
'iso19139': 'pygeometa.schemas.iso19139.ISO19139OutputSchema',
'iso19139-2': 'pygeometa.schemas.iso19139_2.ISO19139_2OutputSchema',
'iso19139-hnap': 'pygeometa.schemas.iso19139_hnap.ISO19139HNAPOutputSchema', # noqa
'oarec-record': 'pygeometa.schemas.ogcapi_records.OGCAPIRecordOutputSchema', # noqa
'schema-org': 'pygeometa.schemas.schema_org.SchemaOrgOutputSchema',
'stac-item': 'pygeometa.schemas.stac.STACItemOutputSchema',
'dcat': 'pygeometa.schemas.dcat.DCATOutputSchema',
'wmo-cmp': 'pygeometa.schemas.wmo_cmp.WMOCMPOutputSchema',
'wmo-wcmp2': 'pygeometa.schemas.wmo_wcmp2.WMOWCMP2OutputSchema',
'wmo-wigos': 'pygeometa.schemas.wmo_wigos.WMOWIGOSOutputSchema'
Expand Down
58 changes: 6 additions & 52 deletions pygeometa/schemas/ogcapi_records/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@
#
# =================================================================

from datetime import date, datetime
import logging
import os
from typing import Union

from pygeometa.core import get_charstring
from pygeometa.helpers import json_dumps
from pygeometa.helpers import generate_datetime, json_dumps
from pygeometa.schemas.base import BaseOutputSchema

THISDIR = os.path.dirname(os.path.realpath(__file__))
Expand Down Expand Up @@ -158,12 +157,11 @@ def write(self, mcf: dict, stringify: str = True) -> Union[dict, str]:

LOGGER.debug('Checking for dates')

if 'dates' in mcf['identification']:
if 'creation' in mcf['identification']['dates']:
record['properties']['created'] = self.generate_date(mcf['identification']['dates']['creation']) # noqa

if 'revision' in mcf['identification']['dates']:
record['properties']['updated'] = self.generate_date(mcf['identification']['dates']['revision']) # noqa
for key, value in mcf['identification']['dates'].items():
if key == 'creation':
record['properties']['created'] = generate_datetime(value)
elif key == 'revision':
record['properties']['updated'] = generate_datetime(value)

rights = get_charstring(mcf['identification'].get('rights'),
self.lang1, self.lang2)
Expand Down Expand Up @@ -417,47 +415,3 @@ 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'
elif len(date_value) == 19: # YYYY-MM-DDTHH:MM:SS
msg = 'YYYY-MM-DDTHH:MM:SS with no timezone; converting to UTC'
LOGGER.debug(msg)
format_ = '%Y-%m-%dT%H:%M:%S'

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
Loading