Initial port to python3
[htsworkflow.git] / htsworkflow / util / rdfhelp.py
index fda8772f858601a0f488c61248da3d39d67c7be3..76cde06e97d10593e9eef6d166df59e0329270d6 100644 (file)
@@ -3,11 +3,13 @@
 import collections
 from datetime import datetime
 from glob import glob
-from urlparse import urlparse, urlunparse
-from urllib2 import urlopen
+from urllib.parse import urlparse, urlunparse
+from urllib.request import urlopen
 import logging
 import os
+import sys
 import types
+from pkg_resources import resource_listdir, resource_string
 
 import lxml.html
 import lxml.html.clean
@@ -40,9 +42,9 @@ def display_query_results(results):
     """A very simple display of sparql query results showing name value pairs
     """
     for row in results:
-        for k, v in row.items()[::-1]:
-            print "{0}: {1}".format(k, v)
-        print
+        for k, v in list(row.items())[::-1]:
+            print("{0}: {1}".format(k, v))
+        print()
 
 def html_query_results(result_stream):
     from django.conf import settings
@@ -63,11 +65,11 @@ def html_query_results(result_stream):
     for row in result_stream:
         new_row = collections.OrderedDict()
         row_urls = []
-        for k,v in row.items():
+        for k,v in list(row.items()):
             new_row[k] = Simplified(v)
         results.append(new_row)
     context = Context({'results': results,})
-    print template.render(context)
+    print(template.render(context))
 
 def blankOrUri(value=None):
     """Return a blank node for None or a resource node for strings.
@@ -75,7 +77,7 @@ def blankOrUri(value=None):
     node = None
     if value is None:
         node = RDF.Node()
-    elif type(value) in types.StringTypes:
+    elif type(value) in str:
         node = RDF.Node(uri_string=value)
     elif isinstance(value, RDF.Node):
         node = value
@@ -83,21 +85,21 @@ def blankOrUri(value=None):
     return node
 
 
-def toTypedNode(value):
+def toTypedNode(value, language="en"):
     """Convert a python variable to a RDF Node with its closest xsd type
     """
-    if type(value) == types.BooleanType:
+    if type(value) == bool:
         value_type = xsdNS['boolean'].uri
         if value:
-            value = u'1'
+            value = '1'
         else:
-            value = u'0'
-    elif type(value) in (types.IntType, types.LongType):
+            value = '0'
+    elif type(value) in (int, int):
         value_type = xsdNS['decimal'].uri
-        value = unicode(value)
-    elif type(value) == types.FloatType:
+        value = str(value)
+    elif type(value) == float:
         value_type = xsdNS['float'].uri
-        value = unicode(value)
+        value = str(value)
     elif isinstance(value, datetime):
         value_type = xsdNS['dateTime'].uri
         if value.microsecond == 0:
@@ -106,20 +108,22 @@ def toTypedNode(value):
             value = value.strftime(ISOFORMAT_MS)
     else:
         value_type = None
-        value = unicode(value)
+        value = str(value)
 
     if value_type is not None:
         node = RDF.Node(literal=value, datatype=value_type)
     else:
-        node = RDF.Node(literal=unicode(value).encode('utf-8'))
+        node = RDF.Node(literal=str(value).encode('utf-8'), language=language)
     return node
 
 
 def fromTypedNode(node):
     """Convert a typed RDF Node to its closest python equivalent
     """
-    if node is None:
-        return None
+    if not isinstance(node, RDF.Node):
+        return node
+    if node.is_resource():
+        return node
 
     value_type = get_node_type(node)
     literal = node.literal_value['string']
@@ -143,7 +147,7 @@ def fromTypedNode(node):
     elif value_type in ('dateTime'):
         try:
             return datetime.strptime(literal, ISOFORMAT_MS)
-        except ValueError, _:
+        except ValueError as _:
             return datetime.strptime(literal, ISOFORMAT_SHORT)
     return literal
 
@@ -245,7 +249,7 @@ def get_model(model_name=None, directory=None):
 
 
 def load_into_model(model, parser_name, path, ns=None):
-    if type(ns) in types.StringTypes:
+    if type(ns) in str:
         ns = RDF.Uri(ns)
 
     if isinstance(path, RDF.Node):
@@ -258,22 +262,23 @@ def load_into_model(model, parser_name, path, ns=None):
     if len(url_parts[0]) == 0 or url_parts[0] == 'file':
         url_parts[0] = 'file'
         url_parts[2] = os.path.abspath(url_parts[2])
-        if parser_name is None or parser_name == 'guess':
-            parser_name = guess_parser_by_extension(path)
+    if parser_name is None or parser_name == 'guess':
+        parser_name = guess_parser_by_extension(path)
     url = urlunparse(url_parts)
     logger.info("Opening {0} with parser {1}".format(url, parser_name))
 
     rdf_parser = RDF.Parser(name=parser_name)
 
+    statements = []
     retries = 3
     while retries > 0:
         try:
             retries -= 1
             statements = rdf_parser.parse_as_stream(url, ns)
             retries = 0
-        except RDF.RedlandError, e:
+        except RDF.RedlandError as e:
             errmsg = "RDF.RedlandError: {0} {1} tries remaining"
-            logger.error(errmsg.format(str(e), tries))
+            logger.error(errmsg.format(str(e), retries))
 
     for s in statements:
         conditionally_add_statement(model, s, ns)
@@ -291,7 +296,7 @@ def load_string_into_model(model, parser_name, data, ns=None):
 def fixup_namespace(ns):
     if ns is None:
         ns = RDF.Uri("http://localhost/")
-    elif type(ns) in types.StringTypes:
+    elif type(ns) in str:
         ns = RDF.Uri(ns)
     elif not(isinstance(ns, RDF.Uri)):
         errmsg = "Namespace should be string or uri not {0}"
@@ -318,17 +323,24 @@ def add_default_schemas(model, schema_path=None):
     or in the list of directories provided in schema_path
     """
 
-    if schema_path is None:
-        path, _ = os.path.split(__file__)
-        schema_path = [os.path.join(path, 'schemas')]
-    elif type(schema_path) in types.StringTypes:
-        schema_path = [schema_path]
+    schemas = resource_listdir(__name__, 'schemas')
+    for s in schemas:
+        schema = resource_string(__name__,  'schemas/' + s)
+        namespace = 'file://localhost/htsworkflow/schemas/'+s
+        add_schema(model, schema, namespace)
 
-    for p in schema_path:
-        for f in glob(os.path.join(p, '*.turtle')):
-            add_schema(model, f)
+    if schema_path:
+        if type(schema_path) in str:
+            schema_path = [schema_path]
 
-def add_schema(model, filename):
+        for path in schema_path:
+            for pathname in glob(os.path.join(path, '*.turtle')):
+                url = 'file://' + os.path.splitext(pathname)[0]
+                stream = open(pathname, 'r')
+                add_schema(model, stream, url)
+                stream.close()
+
+def add_schema(model, schema, url):
     """Add a schema to a model.
 
     Main difference from 'load_into_model' is it tags it with
@@ -336,8 +348,7 @@ def add_schema(model, filename):
     """
     parser = RDF.Parser(name='turtle')
     context = RDF.Node(RDF.Uri(SCHEMAS_URL))
-    url = 'file://' + filename
-    for s in parser.parse_as_stream(url):
+    for s in parser.parse_string_as_stream(schema, url):
         try:
             model.append(s, context)
         except RDF.RedlandError as e:
@@ -384,16 +395,16 @@ def guess_parser(content_type, pathname):
         return 'turtle'
     elif content_type in ('text/html',):
         return 'rdfa'
-    elif content_type is None:
+    elif content_type is None or content_type in ('text/plain',):
         return guess_parser_by_extension(pathname)
 
 def guess_parser_by_extension(pathname):
     _, ext = os.path.splitext(pathname)
     if ext in ('.xml', '.rdf'):
         return 'rdfxml'
-    elif ext in ('.html'):
+    elif ext in ('.html',):
         return 'rdfa'
-    elif ext in ('.turtle'):
+    elif ext in ('.turtle',):
         return 'turtle'
     return 'guess'
 
@@ -412,12 +423,23 @@ def get_serializer(name='turtle'):
     writer.set_namespace('wot', wotNS._prefix)
 
     # should these be here, kind of specific to an application
-    writer.set_namespace('libraryOntology', libraryOntology._prefix)
+    writer.set_namespace('htswlib', libraryOntology._prefix)
     writer.set_namespace('ucscSubmission', submissionOntology._prefix)
     writer.set_namespace('ucscDaf', dafTermOntology._prefix)
+    writer.set_namespace('geoSoft', geoSoftNS._prefix)
+    writer.set_namespace('encode3', encode3NS._prefix)
     return writer
 
+def get_turtle_header():
+    """Return a turtle header with our typical namespaces
+    """
+    serializer = get_serializer()
+    empty = get_model()
+    return serializer.serialize_model_to_string(empty)
 
-def dump_model(model):
+def dump_model(model, destination=None):
+    if destination is None:
+        destination = sys.stdout
     serializer = get_serializer()
-    print serializer.serialize_model_to_string(model)
+    destination.write(serializer.serialize_model_to_string(model))
+    destination.write(os.linesep)