[Python-modules-commits] [python-pyld] 187/276: Support link headers in default document loader.

Wolfgang Borgert debacle at moszumanska.debian.org
Wed Oct 8 23:48:09 UTC 2014


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to branch master
in repository python-pyld.

commit 9c81f3ece493e56af6cad287df2750dd084a9180
Author: Dave Longley <dlongley at digitalbazaar.com>
Date:   Tue Sep 10 13:21:10 2013 -0400

    Support link headers in default document loader.
---
 lib/pyld/jsonld.py | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/lib/pyld/jsonld.py b/lib/pyld/jsonld.py
index 113d96d..3c75c14 100644
--- a/lib/pyld/jsonld.py
+++ b/lib/pyld/jsonld.py
@@ -80,10 +80,12 @@ KEYWORDS = [
     '@value',
     '@vocab']
 
+# JSON-LD link header rel
+LINK_HEADER_REL = 'http://www.w3.org/ns/json-ld#context'
+
 # Restraints
 MAX_CONTEXT_URLS = 10
 
-
 def compact(input_, ctx, options=None):
     """
     Performs JSON-LD compaction.
@@ -293,9 +295,29 @@ def load_document(url):
                 'documentUrl': url,
                 'document': handle.read()
             }
+            doc['documentUrl'] = handle.geturl()
+            headers = dict(handle.info())
+            content_type = headers.get('content-type')
+            link_header = headers.get('link')
+            if link_header and content_type != 'application/ld+json':
+                link_header = parse_link_header(link_header).get(
+                    LINK_HEADER_REL)
+                # only 1 related link header permitted
+                if isinstance(link_header, list):
+                    raise JsonLdError(
+                        'URL could not be dereferenced, it has more than one '
+                        'associated HTTP Link Header.',
+                        'jsonld.LoadDocumentError',
+                        {'url': url},
+                        code='multiple context link headers')
+                if link_header:
+                    doc['contextUrl'] = link_header['target']
         return doc
+    except JsonLdError as e:
+        raise e
     except Exception as cause:
-        raise JsonLdError('Could not retrieve a JSON-LD document from the URL. '
+        raise JsonLdError(
+            'Could not retrieve a JSON-LD document from the URL. ',
             'jsonld.LoadDocumentError', code='loading document failed',
             cause=cause)
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-pyld.git



More information about the Python-modules-commits mailing list