Start to move all the RDF namespace definitions into a single module.
[htsworkflow.git] / htsworkflow / util / rdfhelp.py
1 """Helper features for working with librdf
2 """
3 import RDF
4 import types
5
6 # standard ontology namespaces
7 dublinCoreNS = RDF.NS("http://purl.org/dc/elements/1.1/")
8 rdfNS = RDF.NS("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
9 rdfsNS= RDF.NS("http://www.w3.org/2000/01/rdf-schema#")
10 xsdNS = RDF.NS("http://www.w3.org/2001/XMLSchema#")
11
12 # internal ontologies
13 submitOntology = RDF.NS("http://jumpgate.caltech.edu/wiki/UCSCSubmissionOntology#")
14 libraryOntology = RDF.NS("http://jumpgate.caltech.edu/wiki/LibraryOntology#")
15
16 def blankOrUri(value=None):
17     node = None
18     if value is None:
19         node = RDF.Node()
20     elif type(value) in types.StringTypes:
21         node = RDF.Node(uri_string=value)
22     elif isinstance(value, RDF.Node):
23         node = value
24
25     return node
26
27
28 def toTypedNode(value):
29     if type(value) == types.BooleanType:
30         value_type = xsdNS['boolean'].uri
31         if value:
32             value = u'1'
33         else:
34             value = u'0'
35     elif type(value) in types.StringTypes:
36         value_type = xsdNS['string'].uri
37     else:
38         value_type = None
39         value = unicode(value)
40
41     return RDF.Node(literal=value, datatype=value_type)
42