Skip to content
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

feat: use iana media type vocabulary for format mapping, if eu format vocabulary does not match #82

Merged
merged 8 commits into from
Oct 3, 2023
25 changes: 23 additions & 2 deletions ckanext/dcatapchharvest/dcat_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from ckantoolkit import config
from rdflib import URIRef, Graph
from rdflib.namespace import Namespace, RDF, SKOS

import xml.etree.ElementTree as ET
import logging

log = logging.getLogger(__name__)

DCT = Namespace("http://purl.org/dc/terms/")
Expand All @@ -27,6 +28,10 @@
"rdf": RDF,
}

media_types_namespaces = {
'ns': 'http://www.iana.org/assignments'
}

license_namespaces = {
"skos": SKOS,
"dct": DCT,
Expand All @@ -35,7 +40,6 @@
"rdfs": RDFS,
}


theme_namespaces = {
"euthemes": EUTHEMES,
"skos": SKOS,
Expand Down Expand Up @@ -248,3 +252,20 @@ def get_publisher_dict_from_dataset(publisher):
if not isinstance(publisher, dict):
publisher = json.loads(publisher)
return publisher.get('url'), publisher.get('name')


def get_iana_media_type_values():
file = os.path.join(__location__, 'iana_media_types.xml')
tree = ET.parse(file)
root = tree.getroot()
records = root.findall('.//ns:record', media_types_namespaces)
media_type_values = {}
for record in records:
if record.find('ns:file', media_types_namespaces) is None:
continue
if record.find('ns:name', media_types_namespaces) is None:
continue
name = record.find('ns:name', media_types_namespaces).text
file_value = record.find('ns:file', media_types_namespaces).text
media_type_values[name] = media_types_namespaces['ns']+'/'+file_value
return media_type_values
Loading