Introduction

Installation

To be able to use this library you need to have a modern version of Python installed. Currently we’re supporting versions 2.7, 3.3 and 3.4 of Python.

This easiest way to install this library is through pip or easy install:

$ pip install skosprovider_rdf

This will download and install skosprovider_rdf and a few libraries it depends on.

Usage

This library offers an implementation of the skosprovider.providers.VocabularyProvider interface that uses an rdflib.graph.Graph as input. This provider can be used to add a SKOS vocabulary contained in an RDF file to your application. The provider itself does not read the SKOS file, but expects to be passed a Graph. So any type of RDF serialisation that can be read by rdflib, can be used with this provider.

# -*- coding: utf-8 -*-

import os

from rdflib import Graph

from skosprovider_rdf.providers import RDFProvider

graph = Graph()

file = os.path.join(os.path.dirname(__file__), '..', 'tests', 'data', 'simple_turtle_products')
graph.parse(file, format="turtle")

provider = RDFProvider(
    {'id': 'PRODUCTS'}, 
    graph
)

print "provider.get_all()"
print "------------------"
print provider.get_all()
print ""

print "provider.find({'label': 'jewelry'})"
print "-----------------------------------"
print provider.find({'label': 'jewelry'})
print ""


print "provider.get_by_id('http://wwww.products.com/Jewellery')"
print "--------------------------------------------------------"
print provider.get_by_id('http://www.products.com/Jewellery')
print ""

print "provider.get_by_uri('http://wwww.products.com/Jewellery')"
print "---------------------------------------------------------"
print provider.get_by_uri('http://www.products.com/Jewellery')
print ""

It also provides a utility function to dump any implementation of skosprovider.providers.VocabularyProvider to a rdflib.graph.Graph. Again, since the provider only deals with the Graph object, it’s possible to serialise a VocabularyProvider to whatever RDF serialisations rdflib allows.

# -*- coding: utf-8 -*-
'''
This script demonstrates dumping a 
:class:`skosprovider.providers.SimpleCsvProvider` as a RDF Graph. In this
case, `n3` serialisation is used, other serialisations are available through
:mod:`rdflib`.
'''

import os
import csv

from skosprovider.providers import SimpleCsvProvider

from skosprovider.uri import UriPatternGenerator

from skosprovider.skos import ConceptScheme, Label, Note, Source

from skosprovider_rdf.utils import rdf_dumper

ifile = open(
    os.path.join(os.path.dirname(__file__), 'data', 'menu.csv'),
    "r"
)

reader = csv.reader(ifile)

csvprovider = SimpleCsvProvider(
    {'id': 'MENU'},
    reader,
    uri_generator=UriPatternGenerator('http://id.python.org/menu/%s'),
    concept_scheme=ConceptScheme(
        uri='http://id.python.org/menu',
        labels=[
            Label(type='prefLabel', language='en', label='A pythonesque menu.')
        ],
        notes=[
            Note(
                type='changeNote',
                language='en',
                note="<strong>We didn't need no change notes when I was younger.</strong>",
                markup='HTML'
            )
        ],
        sources=[
            Source("Monthy Python's Flying Circus, 1970. Spam.")
        ]
    )
)

graph = rdf_dumper(csvprovider)

print graph.serialize(format='n3')