Skip to content

Utils

Namespaces

Class representing the namespaces available to an RDF graph.

Source code in ckanext/nhm/dcat/utils.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class Namespaces:
    """
    Class representing the namespaces available to an RDF graph.
    """

    def __init__(self, graph):
        """
        :param graph: the graph object to bind the used namespaces to
        """
        self.graph = graph

        self.bound = set()
        self.known_namespaces = {
            'adms': namespace.Namespace('http://www.w3.org/ns/adms#'),
            'aiiso': namespace.Namespace('http://purl.org/vocab/aiiso/schema#'),
            'cc': namespace.Namespace('http://creativecommons.org/ns#'),
            'dc': namespace.DCTERMS,
            'dcat': namespace.Namespace('http://www.w3.org/ns/dcat#'),
            'dqv': namespace.Namespace('http://www.w3.org/ns/dqv#'),
            'dwc': namespace.Namespace('http://rs.tdwg.org/dwc/terms/'),
            'foaf': namespace.FOAF,
            'locn': namespace.Namespace('http://www.w3.org/ns/locn#'),
            'gsp': namespace.Namespace('http://www.opengis.net/ont/geosparql#'),
            'mads': namespace.Namespace('http://www.loc.gov/mads/rdf/v1#'),
            'org': namespace.Namespace('http://www.w3.org/TR/vocab-org/#'),
            'owl': namespace.OWL,
            'rdf': namespace.RDF,
            'rdfs': namespace.RDFS,
            'schema': namespace.Namespace('http://schema.org/'),
            'sdmx_code': namespace.Namespace(
                'http://purl.org/linked-data/sdmx/2009/code#'
            ),
            'sdwc': namespace.Namespace('http://rs.tdwg.org/dwc/xsd/simpledarwincore#'),
            'skos': namespace.SKOS,
            'tdwgi': namespace.Namespace(
                'http://rs.tdwg.org/ontology/voc/Institution#'
            ),
            'time': namespace.Namespace('http://www.w3.org/2006/time'),
            'vcard': namespace.Namespace('http://www.w3.org/2006/vcard/ns#'),
            'void': namespace.VOID,
        }

    def __getattr__(self, prefix):
        """
        Returns the namespace associated with the given prefix and ensures it is bound
        to the graph if it hasn't been already.

        :param prefix: the namespace prefix as defined in the known_namespaces dict
            attribute
        :returns: the namespace object
        """
        if prefix in self.known_namespaces:
            ns = self.known_namespaces[prefix]
            if (prefix, ns) not in self.bound:
                self.graph.bind(prefix, ns)
                self.bound.add((prefix, ns))
            return ns
        else:
            # if you get this error, just add it above!
            raise ValueError(f'No known namespace with prefix {prefix}')

__getattr__(prefix)

Returns the namespace associated with the given prefix and ensures it is bound to the graph if it hasn't been already.

Parameters:

Name Type Description Default
prefix

the namespace prefix as defined in the known_namespaces dict attribute

required

Returns:

Type Description

the namespace object

Source code in ckanext/nhm/dcat/utils.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def __getattr__(self, prefix):
    """
    Returns the namespace associated with the given prefix and ensures it is bound
    to the graph if it hasn't been already.

    :param prefix: the namespace prefix as defined in the known_namespaces dict
        attribute
    :returns: the namespace object
    """
    if prefix in self.known_namespaces:
        ns = self.known_namespaces[prefix]
        if (prefix, ns) not in self.bound:
            self.graph.bind(prefix, ns)
            self.bound.add((prefix, ns))
        return ns
    else:
        # if you get this error, just add it above!
        raise ValueError(f'No known namespace with prefix {prefix}')

__init__(graph)

Parameters:

Name Type Description Default
graph

the graph object to bind the used namespaces to

required
Source code in ckanext/nhm/dcat/utils.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def __init__(self, graph):
    """
    :param graph: the graph object to bind the used namespaces to
    """
    self.graph = graph

    self.bound = set()
    self.known_namespaces = {
        'adms': namespace.Namespace('http://www.w3.org/ns/adms#'),
        'aiiso': namespace.Namespace('http://purl.org/vocab/aiiso/schema#'),
        'cc': namespace.Namespace('http://creativecommons.org/ns#'),
        'dc': namespace.DCTERMS,
        'dcat': namespace.Namespace('http://www.w3.org/ns/dcat#'),
        'dqv': namespace.Namespace('http://www.w3.org/ns/dqv#'),
        'dwc': namespace.Namespace('http://rs.tdwg.org/dwc/terms/'),
        'foaf': namespace.FOAF,
        'locn': namespace.Namespace('http://www.w3.org/ns/locn#'),
        'gsp': namespace.Namespace('http://www.opengis.net/ont/geosparql#'),
        'mads': namespace.Namespace('http://www.loc.gov/mads/rdf/v1#'),
        'org': namespace.Namespace('http://www.w3.org/TR/vocab-org/#'),
        'owl': namespace.OWL,
        'rdf': namespace.RDF,
        'rdfs': namespace.RDFS,
        'schema': namespace.Namespace('http://schema.org/'),
        'sdmx_code': namespace.Namespace(
            'http://purl.org/linked-data/sdmx/2009/code#'
        ),
        'sdwc': namespace.Namespace('http://rs.tdwg.org/dwc/xsd/simpledarwincore#'),
        'skos': namespace.SKOS,
        'tdwgi': namespace.Namespace(
            'http://rs.tdwg.org/ontology/voc/Institution#'
        ),
        'time': namespace.Namespace('http://www.w3.org/2006/time'),
        'vcard': namespace.Namespace('http://www.w3.org/2006/vcard/ns#'),
        'void': namespace.VOID,
    }

as_dwc_list(objects)

Returns the given objects as a list using the DwC standard | style separator.

Parameters:

Name Type Description Default
objects

the objects

required

Returns:

Type Description

a | separated string

Source code in ckanext/nhm/dcat/utils.py
28
29
30
31
32
33
34
35
def as_dwc_list(objects):
    """
    Returns the given objects as a list using the DwC standard | style separator.

    :param objects: the objects
    :returns: a | separated string
    """
    return ' | '.join(objects)

object_uri(uuid, version=None)

Returns an URI for an object.

Source code in ckanext/nhm/dcat/utils.py
13
14
15
16
17
def object_uri(uuid, version=None):
    """
    Returns an URI for an object.
    """
    return toolkit.url_for('object.view', uuid=uuid, version=version, qualified=True)

rdf_resources()

Return list of resource IDs with RDF records.

Source code in ckanext/nhm/dcat/utils.py
20
21
22
23
24
25
def rdf_resources():
    """
    Return list of resource IDs with RDF records.
    """
    # FIXME - Need to add in indexlots and artefacts
    return [get_specimen_resource_id()]