コード例 #1
0
def create_credential(filename):
    """Reads a credential document from filename and returns the compact
  JSON-LD representation of the credential."""
    with open(filename, 'r') as f:
        doc = json.load(f)

    context = doc.pop('@context', {})
    jsonld.set_document_loader(jsonld.requests_document_loader(timeout=5))
    credential = jsonld.compact(doc, context)
    return credential
コード例 #2
0
    def loader(url, prev_url, options=None):
        """
        Retrieves JSON-LD from a URL, a file location or as text

        :param url: the URL to retrieve.
        :param prev_url: Dictionary to carry the previous URL referenced
        :param options: Additional options

        :return: the RemoteDocument.
        """
        if options is None:
            options = {}

        # Process text being passed in as the document
        if url.strip()[0] in '[{' or '\n' in url:
            return {
                'contentType': 'text/plain',
                'contextUrl': None,
                'documentUrl': None,
                'document': json.loads(url)
            }

        # process relative URL
        pieces = urlparse(url)
        if not any([pieces.scheme, pieces.netloc]):
            if prev_url['url']:
                url = urljoin(prev_url['url'], url)
                pieces = urlparse(url)
        else:
            prev_url['url'] = url

        # check for file access
        if pieces.scheme == 'file':
            try:
                with open(pieces.path) as f:
                    doc = f.read()
                return {
                    'contentType': 'text/plain',
                    'contextUrl': None,
                    'documentUrl': url,
                    'document': json.loads(doc)
                }
            except Exception as cause:
                raise JsonLdError(
                    f'Could not retrieve a JSON-LD document from {url}.',
                    'jsonld.LoadDocumentError',
                    code='loading document failed',
                    cause=cause)
        else:
            return requests_document_loader(**kwargs)(url, options)
コード例 #3
0
        for k in KEYS_TO_EXPAND:
            if k in newObj.keys():
                if isinstance(newObj.get(k), list):
                    v = [expand(lv.get('@id')) for lv in newObj.get(k)]
                    v = v if v != [None] else None
                else:
                    v = expand(newObj[k])
                if bool(v):
                    newObj[k] = v
        return (newObj if bool(newObj) else None)
    else:
        expanded = [expand(n, keepUndefined) for n in newObj]
        return (expanded if bool(expanded) else None)


jsonld.set_document_loader(jsonld.requests_document_loader(timeout=10))

# compact a document according to a particular context
# see: http://json-ld.org/spec/latest/json-ld/#compacted-document-form
expanded = expand(
    'https://raw.githubusercontent.com/ReproNim/schema-standardization/master/activities/BeckAnxietyInventory/BeckAnxietyInventory_schema'
)

print(json.dumps(expanded, indent=2))

# Output:
# {
#   "@context": {...},
#   "image": "http://manu.sporny.org/images/manu.png",
#   "homepage": "http://manu.sporny.org/",
#   "name": "Manu Sporny"
コード例 #4
0
from pyld import jsonld
import hashlib
from datetime import datetime

from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
import base64

from typing import Any, Dict


# cache the downloaded "schemas", otherwise the library is super slow
# (https://github.com/digitalbazaar/pyld/issues/70)
_CACHE: Dict[str, Any] = {}
LOADER = jsonld.requests_document_loader()

def _caching_document_loader(url: str) -> Any:
    if url in _CACHE:
        return _CACHE[url]
    resp = LOADER(url)
    _CACHE[url] = resp
    return resp

jsonld.set_document_loader(_caching_document_loader)


def options_hash(doc):
    doc = dict(doc['signature'])
    for k in ['type', 'id', 'signatureValue']:
        if k in doc:
            del doc[k]
コード例 #5
0
def create_app(**kwargs):
    app = Flask(__name__)
    with app.app_context():
        app.cfg = Cfg()
        startup_msg = 'starting'
        if kwargs:
            app.testing = True
            app.cfg.set_debug_config(**kwargs)
            startup_msg += ' in testing mode with kwargs:'
            for k, v in kwargs.items():
                startup_msg += ' {}={}'.format(k, v)
        log(startup_msg)

        class RegexConverter(BaseConverter):
            """ Make it possible to distinguish routes by <regex("[exampl]"):>.

                https://stackoverflow.com/a/5872904
            """
            def __init__(self, url_map, *items):
                super(RegexConverter, self).__init__(url_map)
                self.regex = items[0]

        app.url_map.converters['regex'] = RegexConverter

        if app.cfg.use_frbs():
            log('using Firebase')
            cred = firebase_admin.credentials.Certificate(app.cfg.frbs_conf())
            firebase_admin.initialize_app(cred)
        else:
            log('NOT using Firebase')
        app.config['SQLALCHEMY_DATABASE_URI'] = app.cfg.db_uri()
        app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

        from jsonkeeper.models import db
        db.init_app(app)
        db.create_all()

        jsonld.set_document_loader(jsonld.requests_document_loader(timeout=7))

        for code in default_exceptions.keys():
            """ Make app return exceptions in JSON form. Also add CORS headers.

                Based on http://flask.pocoo.org/snippets/83/ but updated to use
                register_error_handler method.
                Note: this doesn't seem to work for 405 Method Not Allowed in
                      an Apache + gnunicorn setup.
            """
            @app.errorhandler(code)
            def make_json_error(error):
                resp = jsonify(message=str(error))
                resp.status_code = (error.code if isinstance(
                    error, HTTPException) else 500)
                return add_CORS_headers(resp)

        @app.after_request
        def set_response_headers(response):
            response.headers['Cache-Control'] = ('private, no-store, no-cache,'
                                                 ' must-revalidate')
            # response.headers['Pragma'] = 'no-cache'
            # response.headers['Expires'] = '0'
            return response

        from jsonkeeper.views import jk
        app.register_blueprint(jk)

        if app.cfg.garbage_collection_interval() > 0:
            log('initializing garbage collection')
            scheduler = BackgroundScheduler()
            scheduler.start()
            scheduler.add_job(
                func=collect_garbage,
                trigger=IntervalTrigger(
                    seconds=app.cfg.garbage_collection_interval()),
                id='garbage_collection_job',
                name='collect garbage according to interval set in config',
                replace_existing=True)
            atexit.register(lambda: scheduler.shutdown())

        return app
コード例 #6
0
 def __init__(self, logger):
     self._logger = logger
     requests = jsonld.requests_document_loader(timeout=10)
     jsonld.set_document_loader(requests)
コード例 #7
0
ファイル: runtests.py プロジェクト: digitalbazaar/pyld
    def main(self):
        print('PyLD Tests')
        print('Use -h or --help to view options.\n')

        # add program options
        self.parser.add_option('-m', '--manifest', dest='manifest',
            help='The single test manifest to run', metavar='FILE')
        self.parser.add_option('-d', '--directory', dest='directory',
            help='The directory with the root manifest to run', metavar='DIR')
        self.parser.add_option('-e', '--earl', dest='earl',
            help='The filename to write an EARL report to')
        self.parser.add_option('-b', '--bail', dest='bail',
            action='store_true', default=False,
            help='Bail out as soon as any test fails')
        self.parser.add_option('-l', '--loader', dest='loader',
            default='requests',
            help='The remote URL document loader: requests, aiohttp '
                 '[default: %default]')
        self.parser.add_option('-v', '--verbose', dest='verbose',
            action='store_true', default=False,
            help='Print verbose test data')

        # parse command line options
        (self.options, args) = self.parser.parse_args()

        # ensure a manifest or a directory was specified
        if self.options.manifest is None and self.options.directory is None:
            raise Exception('No test manifest or directory specified.')

        # Set a default JSON-LD document loader
        if self.options.loader == 'requests':
            jsonld._default_document_loader = jsonld.requests_document_loader()
        elif self.options.loader == 'aiohttp':
            jsonld._default_document_loader = jsonld.aiohttp_document_loader()

        # config runner
        self.failfast = self.options.bail

        # get root manifest filename
        if self.options.manifest:
            global HTTP_BASE
            global HTTPS_BASE
            HTTP_BASE = 'http://json-ld.org/test-suite/tests'
            HTTPS_BASE = 'https://json-ld.org/test-suite/tests'
            filename = os.path.abspath(self.options.manifest)
        else:
            filename = os.path.abspath(
                os.path.join(self.options.directory, 'manifest.jsonld'))

        # load root manifest
        global ROOT_MANIFEST_DIR
        ROOT_MANIFEST_DIR = os.path.dirname(filename)
        root_manifest = read_json(filename)
        suite = Manifest(root_manifest, filename).load()

        # run tests
        result = self.run(suite)

        # output earl report if specified
        if self.options.earl:
            filename = os.path.abspath(self.options.earl)
            print('Writing EARL report to: %s' % filename)
            result.writeReport(filename)

        if not result.wasSuccessful():
            exit(1)
コード例 #8
0
"""Contsructor to take a Python dict containing an API Documentation and
create a HydraDoc object for it
"""
import re
import json
from pyld import jsonld
import requests
from hydra_python_core.doc_writer import (HydraDoc, HydraClass, HydraClassProp,
                                          HydraClassOp, HydraStatus, HydraLink,
                                          HydraCollection, DocUrl)
from typing import Any, Dict, Match, Optional, Tuple, Union, List
from hydra_python_core.namespace import hydra, rdfs
from urllib.parse import urlparse

jsonld.set_document_loader(jsonld.requests_document_loader())


def create_doc(doc: Dict[str, Any],
               HYDRUS_SERVER_URL: str = None,
               API_NAME: str = None) -> HydraDoc:
    """
    Create the HydraDoc object from the API Documentation.

    :param doc: dictionary of hydra api doc
    :param HYDRUS_SERVER_URL: url of the hydrus server
    :param API_NAME: name of the api
    :return: instance of HydraDoc which server and agent can understand
    :raise SyntaxError: If the `doc` doesn't have an entry for `@id` , `@context`, `@type` key.
    """

    # These keys must be there in the APIDOC: @context, @id, @type
コード例 #9
0
ファイル: runtests.py プロジェクト: yarikoptic/pyld
    def main(self):
        print('PyLD Tests')
        print('Use -h or --help to view options.\n')

        # add program options
        self.parser.add_option('-m',
                               '--manifest',
                               dest='manifest',
                               help='The single test manifest to run',
                               metavar='FILE')
        self.parser.add_option(
            '-d',
            '--directory',
            dest='directory',
            help='The directory with the root manifest to run',
            metavar='DIR')
        self.parser.add_option('-e',
                               '--earl',
                               dest='earl',
                               help='The filename to write an EARL report to')
        self.parser.add_option('-b',
                               '--bail',
                               dest='bail',
                               action='store_true',
                               default=False,
                               help='Bail out as soon as any test fails')
        self.parser.add_option(
            '-l',
            '--loader',
            dest='loader',
            default='requests',
            help='The remote URL document loader: requests, aiohttp '
            '[default: %default]')
        self.parser.add_option('-v',
                               '--verbose',
                               dest='verbose',
                               action='store_true',
                               default=False,
                               help='Print verbose test data')

        # parse command line options
        (self.options, args) = self.parser.parse_args()

        # ensure a manifest or a directory was specified
        if self.options.manifest is None and self.options.directory is None:
            raise Exception('No test manifest or directory specified.')

        # Set a default JSON-LD document loader
        if self.options.loader == 'requests':
            jsonld._default_document_loader = jsonld.requests_document_loader()
        elif self.options.loader == 'aiohttp':
            jsonld._default_document_loader = jsonld.aiohttp_document_loader()

        # config runner
        self.failfast = self.options.bail

        # get root manifest filename
        if self.options.manifest:
            global HTTP_BASE
            global HTTPS_BASE
            HTTP_BASE = 'http://json-ld.org/test-suite/tests'
            HTTPS_BASE = 'https://json-ld.org/test-suite/tests'
            filename = os.path.abspath(self.options.manifest)
        else:
            filename = os.path.abspath(
                os.path.join(self.options.directory, 'manifest.jsonld'))

        # load root manifest
        global ROOT_MANIFEST_DIR
        ROOT_MANIFEST_DIR = os.path.dirname(filename)
        root_manifest = read_json(filename)
        suite = Manifest(root_manifest, filename).load()

        # run tests
        result = self.run(suite)

        # output earl report if specified
        if self.options.earl:
            filename = os.path.abspath(self.options.earl)
            print('Writing EARL report to: %s' % filename)
            result.writeReport(filename)

        if not result.wasSuccessful():
            exit(1)
コード例 #10
0
    def main(self):
        print('PyLD Tests')
        print('Use -h or --help to view options.\n')

        # add program options
        self.parser.add_argument('tests',
                                 metavar='TEST',
                                 nargs='*',
                                 help='A manifest or directory to test')
        self.parser.add_argument(
            '-e',
            '--earl',
            dest='earl',
            help='The filename to write an EARL report to')
        self.parser.add_argument('-b',
                                 '--bail',
                                 dest='bail',
                                 action='store_true',
                                 default=False,
                                 help='Bail out as soon as any test fails')
        self.parser.add_argument(
            '-l',
            '--loader',
            dest='loader',
            default='requests',
            help='The remote URL document loader: requests, aiohttp '
            '[default: %(default)s]')
        self.parser.add_argument(
            '-n',
            '--number',
            dest='number',
            help='Limit tests to those containing the specified test identifier'
        )
        self.parser.add_argument('-v',
                                 '--verbose',
                                 dest='verbose',
                                 action='store_true',
                                 default=False,
                                 help='Print verbose test data')

        # parse command line args
        self.options = self.parser.parse_args()

        # Set a default JSON-LD document loader
        if self.options.loader == 'requests':
            jsonld._default_document_loader = jsonld.requests_document_loader()
        elif self.options.loader == 'aiohttp':
            jsonld._default_document_loader = jsonld.aiohttp_document_loader()

        # config runner
        self.failfast = self.options.bail

        # Global for saving test numbers to focus on
        global ONLY_IDENTIFIER
        if self.options.number:
            ONLY_IDENTIFIER = self.options.number

        if len(self.options.tests):
            # tests given on command line
            test_targets = self.options.tests
        else:
            # default to find known sibling test dirs
            test_targets = []
            sibling_dirs = [
                '../json-ld-api/tests/',
                '../json-ld-framing/tests/',
                '../normalization/tests/',
            ]
            for dir in sibling_dirs:
                if os.path.exists(dir):
                    print('Test dir found', dir)
                    test_targets.append(dir)
                else:
                    print('Test dir not found', dir)

        # ensure a manifest or a directory was specified
        if len(test_targets) == 0:
            raise Exception('No test manifest or directory specified.')

        # make root manifest with target files and dirs
        root_manifest = {
            '@context': 'https://w3c.github.io/tests/context.jsonld',
            '@id': '',
            '@type': 'mf:Manifest',
            'description': 'Top level PyLD test manifest',
            'name': 'PyLD',
            'sequence': [],
            'filename': '/'
        }
        for test in test_targets:
            if os.path.isfile(test):
                root, ext = os.path.splitext(test)
                if ext in ['.json', '.jsonld']:
                    root_manifest['sequence'].append(os.path.abspath(test))
                    #root_manifest['sequence'].append(test)
                else:
                    raise Exception('Unknown test file ext', root, ext)
            elif os.path.isdir(test):
                filename = os.path.join(test, 'manifest.jsonld')
                if os.path.exists(filename):
                    root_manifest['sequence'].append(os.path.abspath(filename))
                else:
                    raise Exception('Manifest not found', filename)
            else:
                raise Exception('Unknown test target.', test)

        # load root manifest
        global ROOT_MANIFEST_DIR
        #ROOT_MANIFEST_DIR = os.path.dirname(root_manifest['filename'])
        ROOT_MANIFEST_DIR = root_manifest['filename']
        suite = Manifest(root_manifest, root_manifest['filename']).load()

        # run tests
        result = self.run(suite)

        # output earl report if specified
        if self.options.earl:
            filename = os.path.abspath(self.options.earl)
            print('Writing EARL report to: %s' % filename)
            result.writeReport(filename)

        if not result.wasSuccessful():
            exit(1)