Skip to content

Dwc

dwc_terms(fields) ΒΆ

Get DwC terms and groups, parsed from tdwg_dwcterms. Even though we use simple DwC terms, we use this XSD as it allows us to group terms into events etc., on record display.

Parameters:

Name Type Description Default
fields

list of fields for this record

required

Returns:

Type Description

dict, keyed by groups

Source code in ckanext/nhm/lib/dwc.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def dwc_terms(fields):
    """
    Get DwC terms and groups, parsed from tdwg_dwcterms. Even though we use simple DwC
    terms, we use this XSD as it allows us to group terms into events etc., on record
    display.

    :param fields: list of fields for this record
    :returns: dict, keyed by groups
    """
    fields = list(fields)
    dynamic_properties_uri = None
    terms = OrderedDict()
    for group in DWC_XSD.iterfind('xs:group', namespaces=DWC_XSD.nsmap):
        for element in group.iterfind(
            'xs:sequence/xs:element', namespaces=DWC_XSD.nsmap
        ):
            ns, name = element.get('ref').split(':')
            uri = f'{DWC_XSD.nsmap[ns]}{name}'
            if name == 'dynamicProperties':
                # Keep a references to the dynamic properties uri, we
                # will need this later on
                dynamic_properties_uri = uri
            if name in fields:
                # We have a field for this group -
                # so create the group if it doesn't exist
                try:
                    terms[group.get('name')]
                except KeyError:
                    terms[group.get('name')] = OrderedDict()

                terms[group.get('name')][uri] = name
                # Remove field name from the fields list - those remaining will
                # be dynamic properties
                fields.remove(name)

    # Add created - not actually in DwC
    terms['RecordLevelTerms']['http://purl.org/dc/terms/created'] = 'created'

    # dynamic properties are actually part of RecordLevelTerms, but we treat it slightly differently
    # - filter out all hidden fields (starting with _)
    terms['dynamicProperties'] = {
        dynamic_properties_uri: [f for f in fields if not f.startswith('_')]
    }

    return terms