def test_inheritance(): """Test type and context inheritance.""" types = {'prov:Activity', 'wfprov:ProcessRun'} context_keys = {'prov', 'wfprov', '@version'} @jsonld.s(type='prov:Activity', context={'prov': 'A'}) class Activity: """Define an activity.""" @jsonld.s(type='wfprov:ProcessRun', context={'wfprov': 'B'}) class ProcessRun(Activity): """Define a process execution based on an activity.""" data = jsonld.asjsonld(ProcessRun()) assert set(data['@type']) == types assert set(data['@context'].keys()) == context_keys types = {'prov:Activity', 'wfprov:ProcessRun', 'wfprov:WorkflowRun'} @jsonld.s(type='wfprov:WorkflowRun') class WorkflowRun(ProcessRun): """Define a workflow run.""" data = jsonld.asjsonld(WorkflowRun()) assert set(data['@type']) == types assert set(data['@context'].keys()) == context_keys
def jsonld(client, datasets, **kwargs): """Format datasets as JSON-LD.""" data = [ asjsonld(dataset, basedir=os.path.relpath('.', start=str( dataset.__reference__.parent))) for dataset in datasets ] return dumps(data, indent=2)
def _jsonld(graph, format, *args, **kwargs): """Return formatted graph in JSON-LD ``format`` function.""" import json import pyld from renku.core.models.jsonld import asjsonld output = getattr(pyld.jsonld, format)( [asjsonld(action) for action in graph.activities.values()]) return json.dumps(output, indent=2)
def jsonld(client, tags): """Format dataset tags as JSON-LD. :param client: LocalClient instance. :param tags: Dataset tags. """ from renku.core.models.json import dumps from renku.core.models.jsonld import asjsonld data = [asjsonld(tag) for tag in tags] return dumps(data, indent=2)
def jsonld(client, records, **kwargs): """Format dataset files as JSON-LD. :param client: LocalClient instance. :param records: Filtered collection. """ from renku.core.models.json import dumps from renku.core.models.jsonld import asjsonld data = [asjsonld(record) for record in records] return dumps(data, indent=2)
def convert_cwl_metadata(rv, v, meta): """Convert custom metadata to cwl.""" if not v: return if isinstance(v, (list, tuple)): result = [asjsonld(vv) for vv in v] else: result = asjsonld(v) if 'reverse' in meta and meta['reverse']: if '@reverse' not in rv: rv['@reverse'] = {} rv['@reverse'][meta['property']] = result else: rv[meta['property']] = result if '$namespaces' not in rv: rv['$namespaces'] = {} rv['$namespaces'][meta['prefix']] = meta['namespace']
def test_project_serialization(): """Test project serialization with JSON-LD context.""" with freeze_time('2017-03-01T08:00:00.000000+00:00') as frozen_time: project = Project(name='demo') assert project.name == 'demo' assert project.created == frozen_time().replace(tzinfo=timezone.utc) assert project.updated == frozen_time().replace(tzinfo=timezone.utc) data = asjsonld(project) assert 'schema:Project' in data['@type'] assert 'prov:Location' in data['@type'] context = data['@context'] assert 'schema:name' == context['name'] assert Person._jsonld_context == context['creator']['@context'] assert 'schema:dateUpdated' == context['updated'] assert 'schema:dateCreated' == context['created'] assert 'schema:schemaVersion' == context['version']