Skip to content

Commit

Permalink
[#56] Scheming compatibility profile, serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed May 21, 2024
1 parent e0f15f5 commit 0b6a8dd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
6 changes: 4 additions & 2 deletions ckanext/dcat/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ def set_titles(object_dict):

def before_dataset_index(self, dataset_dict):
schema = None
schema_show = p.toolkit.get_action("scheming_dataset_schema_show")
if schema_show:
try:
schema_show = p.toolkit.get_action("scheming_dataset_schema_show")
try:
schema = schema_show({}, {"type": dataset_dict["type"]})
except p.toolkit.ObjectNotFound:
pass
except KeyError:
pass

if schema:
for field in schema['dataset_fields']:
Expand Down
73 changes: 42 additions & 31 deletions ckanext/dcat/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,18 @@ def __init__(self, graph, dataset_schema='dataset', compatibility_mode=False):
# _license().
self._licenceregister_cache = None

schema_show = toolkit.get_action("scheming_dataset_schema_show")
if schema_show:
try:
schema_show = toolkit.get_action("scheming_dataset_schema_show")
try:
schema = schema_show({}, {"type": dataset_schema})
except toolkit.ObjectNotFound:
raise toolkit.ObjectNotFound(f"Unknown dataset schema: {dataset_schema}")

self._dataset_schema = schema

except KeyError:
pass

def _datasets(self):
'''
Generator that returns all DCAT datasets on the graph
Expand Down Expand Up @@ -1132,7 +1135,7 @@ def parse_dataset(self, dataset_dict, dataset_ref):
contact = self._contact_details(dataset_ref, ADMS.contactPoint)

if contact:
for key in ('uri', 'name', 'email'):
for key in ('uri', 'name', 'email'):
if contact.get(key):
dataset_dict['extras'].append(
{'key': 'contact_{0}'.format(key),
Expand Down Expand Up @@ -1358,32 +1361,6 @@ def graph_from_dataset(self, dataset_dict, dataset_ref):
_type=URIRef, value_modifier=self._add_mailto
)

# TODO: this will go into a separate profile
contact = dataset_dict.get("contact")
if isinstance(contact, list) and len(contact):
for item in contact:
contact_uri = item.get('uri')
if contact_uri:
contact_details = CleanedURIRef(contact_uri)
else:
contact_details = BNode()

g.add((contact_details, RDF.type, VCARD.Organization))
g.add((dataset_ref, DCAT.contactPoint, contact_details))

self._add_triple_from_dict(
item, contact_details,
VCARD.fn, 'name'
)
# Add mail address as URIRef, and ensure it has a mailto: prefix
self._add_triple_from_dict(
item, contact_details,
VCARD.hasEmail, 'email',
_type=URIRef, value_modifier=self._add_mailto
)



# Publisher
if any([
self._get_dataset_value(dataset_dict, 'publisher_uri'),
Expand Down Expand Up @@ -1774,14 +1751,14 @@ def graph_from_dataset(self, dataset_dict, dataset_ref):
]
self._add_list_triples_from_dict(resource_dict, distribution, items)

# Access services
access_service_list = resource_dict.get('access_services', [])
if isinstance(access_service_list, str):
try:
access_service_list = json.loads(access_service_list)
except ValueError:
access_service_list = []

# Access service
for access_service_dict in access_service_list:

access_service_uri = access_service_dict.get('uri')
Expand Down Expand Up @@ -1817,7 +1794,7 @@ def graph_from_dataset(self, dataset_dict, dataset_ref):
self._add_list_triples_from_dict(access_service_dict, access_service_node, items)

if access_service_list:
resource_dict['access_services'] = json.dumps(access_service_list)
resource_dict['access_services'] = json.dumps(access_service_list)

def graph_from_catalog(self, catalog_dict, catalog_ref):

Expand Down Expand Up @@ -2201,3 +2178,37 @@ def _parse_list_value(data_dict, field_name):
pass

return dataset_dict

def graph_from_dataset(self, dataset_dict, dataset_ref):

contact = dataset_dict.get("contact")
if isinstance(contact, list) and len(contact):
for item in contact:
contact_uri = item.get('uri')
if contact_uri:
contact_details = CleanedURIRef(contact_uri)
else:
contact_details = BNode()

self.g.add((contact_details, RDF.type, VCARD.Organization))
self.g.add((dataset_ref, DCAT.contactPoint, contact_details))

self._add_triple_from_dict(
item, contact_details,
VCARD.fn, 'name'
)
# Add mail address as URIRef, and ensure it has a mailto: prefix
self._add_triple_from_dict(
item, contact_details,
VCARD.hasEmail, 'email',
_type=URIRef, value_modifier=self._add_mailto
)

resources = dataset_dict.get('resources', [])
for resource in resources:
if resource.get('access_services'):
if isinstance(resource['access_services'], str):
try:
resource['access_services'] = json.loads(resource['access_services'])
except ValueError:
pass

0 comments on commit 0b6a8dd

Please sign in to comment.