API Documentation¶
Skos module¶
This module contains a read-only model of the SKOS specification.
To complement the SKOS specification, some elements were borrowed from the SKOS-THES specification (eg. superordinate and subordinate array).
New in version 0.2.0.
-
class
skosprovider.skos.
Collection
(id, uri=None, concept_scheme=None, labels=[], notes=[], members=[], member_of=[], superordinates=[])[source]¶ A SKOS Collection.
-
concept_scheme
= None¶ The
ConceptScheme
this Collection is a part of.
-
id
= None¶ An id for this Collection within a vocabulary
-
label
(language=u'any')[source]¶ Provide a single label for this collection.
This uses the
label()
function to determine which label to return.Parameters: language (string) – The preferred language to receive the label in. This should be a valid IANA language tag. Return type: skosprovider.skos.Label
or False if no labels were found.
-
labels
= []¶ A
lst
ofskosprovider.skos.label
instances.
-
member_of
= []¶ A
lst
of collection ids.
-
members
= []¶ A
lst
of concept or collection ids.
-
notes
= []¶ A
lst
ofskosprovider.skos.Note
instances.
-
superordinates
= []¶ A
lst
of concept ids.
-
type
= u'collection'¶ The type of this concept or collection.
eg. ‘collection’
-
uri
= None¶ A proper uri for this Collection
-
-
class
skosprovider.skos.
Concept
(id, uri=None, concept_scheme=None, labels=[], notes=[], broader=[], narrower=[], related=[], member_of=[], subordinate_arrays=[], matches={})[source]¶ A SKOS Concept.
-
broader
= []¶ A
lst
of concept ids.
-
concept_scheme
= None¶ The
ConceptScheme
this Concept is a part of.
-
id
= None¶ An id for this Concept within a vocabulary
eg. 12345
-
label
(language=u'any')[source]¶ Provide a single label for this concept.
This uses the
label()
function to determine which label to return.Parameters: language (string) – The preferred language to receive the label in. This should be a valid IANA language tag. Return type: skosprovider.skos.Label
or False if no labels were found.
-
matchtypes
= [u'close', u'exact', u'related', u'broad', u'narrow']¶ Matches with Concepts in other ConceptSchemes.
This dictionary contains a key for each type of Match (close, exact, related, broad, narrow). Attached to each key is a list of URI’s.
-
member_of
= []¶ A
lst
of collection ids.
-
narrower
= []¶ A
lst
of concept ids.
A
lst
of concept ids.
-
type
= u'concept'¶ The type of this concept or collection.
eg. ‘concept’
-
uri
= None¶ A proper uri for this Concept
eg. http://id.example.com/skos/trees/1
-
-
class
skosprovider.skos.
ConceptScheme
(uri, labels=[], notes=[])[source]¶ A SKOS ConceptScheme.
Parameters: - uri (string) – A URI for this conceptscheme.
- labels (list) – A list of
skosprovider.skos.Label
instances. - notes (list) – A list of
skosprovider.skos.Note
instances.
-
label
(language=u'any')[source]¶ Provide a single label for this conceptscheme.
This uses the
label()
function to determine which label to return.Parameters: language (string) – The preferred language to receive the label in. This should be a valid IANA language tag. Return type: skosprovider.skos.Label
or False if no labels were found.
-
labels
= []¶ A
lst
ofskosprovider.skos.label
instances.
-
notes
= []¶ A
lst
ofskosprovider.skos.Note
instances.
-
class
skosprovider.skos.
Label
(label, type=u'prefLabel', language=u'und')[source]¶ A SKOS Label.
-
static
is_valid_type
(type)[source]¶ Check if the argument is a valid SKOS label type.
Parameters: type (string) – The type to be checked.
-
label
= None¶ The label itself (eg. churches, trees, Spitfires, …)
-
language
= u'und'¶ The language the label is in (eg. en, en-US, nl, nl-BE).
-
type
= u'prefLabel'¶ The type of this label ( prefLabel, altLabel, hiddenLabel).
-
valid_types
= [u'prefLabel', u'altLabel', u'hiddenLabel']¶ The valid types for a label
-
static
-
class
skosprovider.skos.
Note
(note, type=u'note', language=u'und')[source]¶ A SKOS Note.
-
static
is_valid_type
(type)[source]¶ Check if the argument is a valid SKOS note type.
Parameters: type (string) – The type to be checked.
-
language
= u'und'¶ The language the label is in (eg. en, en-US, nl, nl-BE).
-
note
= None¶ The note itself
-
type
= u'note'¶ The type of this note ( note, definition, scopeNote, …).
-
valid_types
= [u'note', u'changeNote', u'definition', u'editorialNote', u'example', u'historyNote', u'scopeNote']¶ The valid types for a note
-
static
-
skosprovider.skos.
dict_to_label
(dict)[source]¶ Transform a dict with keys label, type and language into a
Label
.If the argument passed is already a
Label
, this method just returns the argument.
-
skosprovider.skos.
dict_to_note
(dict)[source]¶ Transform a dict with keys note, type and language into a
Note
.If the argument passed is already a
Note
, this method just returns the argument.
-
skosprovider.skos.
label
(labels=[], language=u'any')[source]¶ Provide a label for a list of labels.
The items in the list of labels are assumed to be either instances of
Label
, or dicts with at least the key label in them. These will be passed to thedict_to_label()
function.This method tries to find a label by looking if there’s a pref label for the specified language. If there’s no pref label, it looks for an alt label. It disregards hidden labels.
While matching languages, preference will be given to exact matches. But, if no exact match is present, an inexact match will be attempted. This might be because a label in language nl-BE is being requested, but only nl or even nl-NL is present. Similarly, when requesting nl, a label with language nl-NL or even nl-Latn-NL will also be considered, providing no label is present that has an exact match with the requested language.
If language ‘any’ was specified, all labels will be considered, regardless of language.
To find a label without a specified language, pass None as language.
If a language or None was specified, and no label could be found, this method will automatically try to find a label in some other language.
Finally, if no label could be found, None is returned.
Parameters: language (string) – The preferred language to receive the label in. This should be a valid IANA language tag.
Providers module¶
This module provides an abstraction of controlled vocabularies.
This abstraction allows our application to work with both local and remote vocabs (be they SOAP, REST, XML-RPC or something else).
The basic idea is that we have skos providers. Each provider is an instance
of a VocabularyProvider
. The same class can thus be reused with
different configurations to handle different vocabs. Generally speaking, every
instance of a certain VocabularyProvider
will deal with concepts and
collections from a single conceptscheme.
-
class
skosprovider.providers.
VocabularyProvider
(metadata, **kwargs)[source]¶ An interface that all vocabulary providers must follow.
-
expand
(id)[source]¶ Expand a concept or collection to all it’s narrower concepts.
This method should recurse and also return narrower concepts of narrower concepts.
If the id passed belongs to a
skosprovider.skos.Concept
, the id of the concept itself should be include in the return value.If the id passed belongs to a
skosprovider.skos.Collection
, the id of the collection itself must not be present in the return value In this case the return value includes all the member concepts and their narrower concepts.Parameters: id – A concept or collection id. Return type: A list of id’s or False if the concept or collection doesn’t exist.
-
expand_concept
(id)[source]¶ Expand a concept to the concept itself and all it’s narrower concepts.
Deprecated since version 0.2.0: This method has been deprecated, please use
expand()
.Parameters: id – A concept id. Return type: A list of id’s or False if the concept doesn’t exist.
-
find
(query, **kwargs)[source]¶ Find concepts that match a certain query.
Currently query is expected to be a dict, so that complex queries can be passed. You can use this dict to search for concepts or collections with a certain label, with a certain type and for concepts that belong to a certain collection.
# Find anything that has a label of church. provider.find({'label': 'church'} # Find all concepts that are a part of collection 5. provider.find({'type': 'concept', 'collection': {'id': 5}) # Find all concepts, collections or children of these # that belong to collection 5. provider.find({'collection': {'id': 5, 'depth': 'all'})
Parameters: query – A dict that can be used to express a query. The following keys are permitted:
- label: Search for something with this label value. An empty label is equal to searching for all concepts.
- type: Limit the search to certain SKOS elements. If not present all is assumed:
- concept: Only return
skosprovider.skos.Concept
instances. - collection: Only return
skosprovider.skos.Collection
instances. - all: Return both
skosprovider.skos.Concept
andskosprovider.skos.Collection
instances.
- concept: Only return
- collection: Search only for concepts belonging to a certain collection. This argument should be a dict with two keys:
- id: The id of a collection. Required.
- depth: Can be members or all. Optional. If not present, members is assumed, meaning only concepts or collections that are a direct member of the collection should be considered. When set to all, this method should return concepts and collections that are a member of the collection or are a narrower concept of a member of the collection.
Returns: A lst
of concepts and collections. Each of these is a dict with the following keys:- id: id within the conceptscheme
- uri: uri of the concept or collection
- type: concept or collection
- label: A label to represent the concept or collection. It is determined by looking at the **kwargs parameter, the default
- language of the provider and finally falls back to en.
-
get_all
(**kwargs)[source]¶ Returns all concepts and collections in this provider.
Returns: A lst
of concepts and collections. Each of these is a dict with the following keys:- id: id within the conceptscheme
- uri: uri of the concept or collection
- type: concept or collection
- label: A label to represent the concept or collection. It is determined by looking at the **kwargs parameter, the default
- language of the provider and finally falls back to en.
-
get_by_id
(id)[source]¶ Get all information on a concept or collection, based on id.
Providers should assume that all id’s passed are strings. If a provider knows that internally it uses numeric identifiers, it’s up to the provider to do the typecasting. Generally, this should not be done by changing the id’s themselves (eg. from int to str), but by doing the id comparisons in a type agnostic way.
Since this method could be used to find both concepts and collections, it’s assumed that there are no id collisions between concepts and collections.
Return type: skosprovider.skos.Concept
orskosprovider.skos.Collection
or False if the concept or collection is unknown to the provider.
-
get_by_uri
(uri)[source]¶ Get all information on a concept or collection, based on a URI.
Return type: skosprovider.skos.Concept
orskosprovider.skos.Collection
or False if the concept or collection is unknown to the provider.
-
get_children_display
(id, **kwargs)[source]¶ Return a list of concepts or collections that should be displayed under this concept or collection.
Parameters: id (str) – A concept or collection id. Returns: A lst
of concepts and collections. Each of these is a dict with the following keys:- id: id within the conceptscheme
- uri: uri of the concept or collection
- type: concept or collection
- label: A label to represent the concept or collection. It is determined by looking at the **kwargs parameter, the default
- language of the provider and finally falls back to en.
-
get_metadata
()[source]¶ Get some metadata on the provider or the vocab it represents.
Return type: Dict.
-
get_top_concepts
(**kwargs)[source]¶ Returns all top-level concepts in this provider.
Top-level concepts are concepts that have no broader concepts themselves. They might have narrower concepts, but this is not mandatory.
Returns: A lst
of concepts, NOTcollections. Each of these is a dict with the following keys:- id: id within the conceptscheme
- uri: uri of the concept or collection
- type: concept or collection
- label: A label to represent the concept or collection. It is determined by looking at the **kwargs parameter, the default
- language of the provider and finally falls back to en.
-
get_top_display
(**kwargs)[source]¶ Returns all concepts or collections that form the top-level of a display hierarchy.
As opposed to the
get_top_concepts()
, this method can possibly return both concepts and collections.Returns: A lst
of concepts and collections. Each of these is a dict with the following keys:- id: id within the conceptscheme
- uri: uri of the concept or collection
- type: concept or collection
- label: A label to represent the concept or collection. It is determined by looking at the **kwargs parameter, the default
- language of the provider and finally falls back to en.
-
-
class
skosprovider.providers.
MemoryProvider
(metadata, list, **kwargs)[source]¶ A provider that keeps everything in memory.
The data is passed in the constructor of this provider as a
lst
ofskosprovider.skos.Concept
andskosprovider.skos.Collection
instances.-
case_insensitive
= True¶ Is searching for labels case insensitive?
By default a search for a label is done case insensitive. Older versions of this provider were case sensitive. If this behaviour is desired, this can be triggered by providing a case_insensitive keyword to the constructor.
-
-
class
skosprovider.providers.
DictionaryProvider
(metadata, list, **kwargs)[source]¶ A simple vocab provider that use a python list of dicts.
The provider expects a list with elements that are dicts that represent the concepts.
-
class
skosprovider.providers.
SimpleCsvProvider
(metadata, reader, **kwargs)[source]¶ A provider that reads a simple csv format into memory.
The supported csv format looks like this: <id>,<preflabel>,<note>
This provider essentialy provides a flat list of concepts. This is commonly associated with short lookup-lists.
New in version 0.2.0.
Registry module¶
This module provides a registry for skos providers.
This registry helps us find providers during runtime. We can also apply some operations to all or several providers at the same time.
-
class
skosprovider.registry.
Registry
[source]¶ This registry collects all skos providers.
-
concept_scheme_uri_map
= {}¶ Dictionary mapping concept scheme uri’s to vocabulary id’s.
-
find
(query, **kwargs)[source]¶ Launch a query across all or a selection of providers.
# Find anything that has a label of church in any provider. registry.find({'label': 'church'}) # Find anything that has a label of church with the BUILDINGS provider. # Attention, this syntax was deprecated in version 0.3.0 registry.find({'label': 'church'}, providers=['BUILDINGS']) # Find anything that has a label of church with the BUILDINGS provider. registry.find({'label': 'church'}, providers={'ids': ['BUILDINGS']}) # Find anything that has a label of church with a provider # marked with the subject 'architecture'. registry.find({'label': 'church'}, providers={'subject': 'architecture'})
Parameters: - providers (dict) – Optional. If present, it should be a dictionary.
This dictionary can contain any of the keyword arguments available
to the
get_providers()
method. The query will then only be passed to the providers confirming to these arguments. - query (dict) – The query parameters that will be passed on to each
find()
method of the selected.providers
.
Returns: a list of
dict
. Each dict has two keys: id and concepts.- providers (dict) – Optional. If present, it should be a dictionary.
This dictionary can contain any of the keyword arguments available
to the
-
get_all
()[source]¶ Get all concepts from all providers.
Returns: a list of dict
. Each dict has two keys: id and concepts.
-
get_by_uri
(uri)[source]¶ Get a concept or collection by its uri.
Returns a single concept or collection if one exists with this uri. Returns False otherwise.
Parameters: uri (string) – The uri to find a concept or collection for. Return type: skosprovider.skos.Concept
orskosprovider.skos.Collection
-
get_provider
(id)[source]¶ Get a provider by id.
Parameters: id (str) – The identifier for the provider. Returns: A skosprovider.providers.VocabularyProvider
or False if the id is unknown.
-
get_providers
(**kwargs)[source]¶ Get all providers registered.
If keyword ids is present, get only the providers with these ids.
If keys subject is present, get only the providers that have this subject.
# Get all providers with subject 'biology' registry.get_providers(subject='biology') # Get all providers with id 1 or 2 registry.get_providers(ids=[1,2]) # Get all providers with id 1 or 2 and subject 'biology' registry.get_providers(ids=[1,2], subject='biology']
-
providers
= {}¶ Dictionary containing all providers, keyed by id.
-
register_provider
(provider)[source]¶ Register a
skosprovider.providers.VocabularyProvider
.Parameters: provider (skosprovider.providers.VocabularyProvider) – The provider to register.
-
remove_provider
(id)[source]¶ Remove the provider with the given id or URI.
Parameters: id (str) – The identifier for the provider. Returns: A skosprovider.providers.VocabularyProvider
or False if the id is unknown.
-
Uri module¶
This module provides utilities for working with URIS.
New in version 0.3.0.
-
class
skosprovider.uri.
DefaultConceptSchemeUrnGenerator
[source]¶ Generate a URN for a conceptscheme specific to skosprovider.
Used for generating default URI for providers that do not have an explicit conceptscheme.
-
class
skosprovider.uri.
DefaultUrnGenerator
(vocabulary_id)[source]¶ Generate a URN specific to skosprovider.
Used for providers that do not implement a specific
UriGenerator
.Parameters: vocabulary_id – An identifier for the vocabulary we’re generating URIs for.
-
class
skosprovider.uri.
TypedUrnGenerator
(vocabulary_id)[source]¶ Generate a URN specific to skosprovider that contains a type.
Parameters: vocabulary_id – An identifier for the vocabulary we’re generating URIs for.
Exceptions module¶
This module provides custom exceptions for skos providers.
New in version 0.5.0.
- This exception can be raised by a provider if it’s unable to provide the thesaurus. This can occur
- when an underlying resource is unavailable (database connection, webservice, …). The message should contain some more information about the problem.
Utils module¶
This module contains utility functions for dealing with skos providers.
-
skosprovider.utils.
dict_dumper
(provider)[source]¶ Dump a provider to a format that can be passed to a
skosprovider.providers.DictionaryProvider
.Parameters: provider (skosprovider.providers.VocabularyProvider) – The provider that wil be turned into a dict. Return type: A list of dicts. New in version 0.2.0.