pyRdfa.transform.prototype

Encoding of the RDFa prototype vocabulary behavior. This means processing the graph by adding and removing triples based on triples using the rdfa:Prototype and rdfa:ref class and property, respectively. For details, see the HTML5+RDFa document.

@author: U{Ivan Herman} @license: This software is available for use under the U{W3C® SOFTWARE NOTICE AND LICENSE} @contact: Ivan Herman, ivan@w3.org @version: $Id: prototype.py,v 1.1 2013-01-18 09:41:49 ivan Exp $ $Date: 2013-01-18 09:41:49 $

 1# -*- coding: utf-8 -*-
 2"""
 3Encoding of the RDFa prototype vocabulary behavior. This means processing the graph by adding and removing triples
 4based on triples using the rdfa:Prototype and rdfa:ref class and property, respectively. For details, see the HTML5+RDFa document.
 5
 6
 7@author: U{Ivan Herman<a href="http://www.w3.org/People/Ivan/">}
 8@license: This software is available for use under the
 9U{W3C® SOFTWARE NOTICE AND LICENSE<href="http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231">}
10@contact: Ivan Herman, ivan@w3.org
11@version: $Id: prototype.py,v 1.1 2013-01-18 09:41:49 ivan Exp $
12$Date: 2013-01-18 09:41:49 $
13"""
14
15from rdflib import RDF as ns_rdf
16
17from .. import ns_rdfa
18
19Prototype = ns_rdfa["Pattern"]
20pref = ns_rdfa["copy"]
21
22def handle_prototypes(graph):
23    to_remove = set()
24    for x, ref, PR in graph.triples((None, pref, None)):
25        if (PR, ns_rdf["type"], Prototype) in graph:
26            to_remove.add((PR, ns_rdf["type"], Prototype))
27            to_remove.add((x, ref, PR))
28            # there is a reference to a prototype here
29            for (PR, p, y) in graph.triples((PR, None, None)):
30                if not (p == ns_rdf["type"] and y == Prototype):
31                    graph.add((x, p, y))
32                    to_remove.add((PR, p, y))
33    for t in to_remove: graph.remove(t)
Prototype = rdflib.term.URIRef('http://www.w3.org/ns/rdfa#Pattern')
pref = rdflib.term.URIRef('http://www.w3.org/ns/rdfa#copy')
def handle_prototypes(graph):
23def handle_prototypes(graph):
24    to_remove = set()
25    for x, ref, PR in graph.triples((None, pref, None)):
26        if (PR, ns_rdf["type"], Prototype) in graph:
27            to_remove.add((PR, ns_rdf["type"], Prototype))
28            to_remove.add((x, ref, PR))
29            # there is a reference to a prototype here
30            for (PR, p, y) in graph.triples((PR, None, None)):
31                if not (p == ns_rdf["type"] and y == Prototype):
32                    graph.add((x, p, y))
33                    to_remove.add((PR, p, y))
34    for t in to_remove: graph.remove(t)