Beispiel #1
0
    def to_yaml(self, path=None):
        """Write content to a YAML file."""
        from renku.core.management import LocalClient

        data = DatasetSchemaV7().dump(self)
        path = path or self.__reference__ or os.path.join(self.path, LocalClient.METADATA)
        jsonld.write_yaml(path=path, data=data)
Beispiel #2
0
    def to_yaml(self):
        """Write an instance to the referenced YAML file."""
        from renku import __version__

        self.agent_version = __version__

        data = ProjectSchema().dump(self)
        jsonld.write_yaml(path=self.__reference__, data=data)
Beispiel #3
0
def _apply_on_the_fly_jsonld_migrations(path,
                                        jsonld_context,
                                        fields,
                                        client=None,
                                        jsonld_migrations=None,
                                        jsonld_translate=None):
    data = read_yaml(path)

    if jsonld_translate:
        # perform the translation
        data = pyld.jsonld.compact(data, jsonld_translate)
        # compact using the class json-ld context
        data.pop('@context', None)
        data = pyld.jsonld.compact(data, jsonld_context)

    data.setdefault('@context', jsonld_context)

    if jsonld_migrations:
        schema_type = data.get('@type')
        migrations = []

        if isinstance(schema_type, list):
            for schema in schema_type:
                migrations += jsonld_migrations.get(schema, [])
        elif isinstance(schema_type, str):
            migrations += jsonld_migrations.get(schema_type, [])

        for migration in set(migrations):
            data = migration(data, client)

    if data['@context'] != jsonld_context:
        # merge new context into old context to prevent properties
        # getting lost in jsonld expansion
        if isinstance(data['@context'], str):
            data['@context'] = {'@base': data['@context']}
        data['@context'].update(jsonld_context)
        try:
            compacted = pyld.jsonld.compact(data, jsonld_context)
        except Exception:
            compacted = data
    else:
        compacted = data

    data = {}

    for k, v in compacted.items():
        if k in fields:
            no_value_context = isinstance(v, dict) and '@context' not in v
            has_nested_context = (k in compacted['@context']
                                  and '@context' in compacted['@context'][k])
            if no_value_context and has_nested_context:
                # Propagate down context
                v['@context'] = compacted['@context'][k]['@context']

            data[k] = v

    write_yaml(path, data)
Beispiel #4
0
 def to_yaml(self, path):
     """Write content to a YAML file."""
     data = ProjectSchemaV3().dump(self)
     jsonld.write_yaml(path=path, data=data)
Beispiel #5
0
 def to_yaml(self):
     """Write an instance to the referenced YAML file."""
     data = DatasetSchema(flattened=True).dump(self)
     jsonld.write_yaml(path=self.__reference__, data=data)
Beispiel #6
0
 def to_yaml(self):
     """Write an instance to the referenced YAML file."""
     data = ProjectSchema().dump(self)
     jsonld.write_yaml(path=self.__reference__, data=data)
def _apply_on_the_fly_jsonld_migrations(path,
                                        jsonld_context,
                                        fields,
                                        client=None,
                                        jsonld_migrations=None,
                                        jsonld_translate=None):
    data = read_yaml(path)

    if jsonld_translate:
        # perform the translation
        data = pyld.jsonld.expand(data)
        data_str = json.dumps(data)
        for k, v in jsonld_translate.items():
            data_str = data_str.replace(v, k)
        data = json.loads(data_str)
        data = pyld.jsonld.compact(data, jsonld_context)

    data.setdefault("@context", jsonld_context)

    _migrate_types(data)

    if jsonld_migrations:
        schema_type = data.get("@type")
        migrations = []

        if isinstance(schema_type, list):
            for schema in schema_type:
                migrations += jsonld_migrations.get(schema, [])
        elif isinstance(schema_type, str):
            migrations += jsonld_migrations.get(schema_type, [])

        for migration in set(migrations):
            data = migration(data, client)

    if data["@context"] != jsonld_context:
        # merge new context into old context to prevent properties
        # getting lost in jsonld expansion
        if isinstance(data["@context"], str):
            data["@context"] = {"@base": data["@context"]}
        data["@context"].update(jsonld_context)
        try:
            compacted = pyld.jsonld.compact(data, jsonld_context)
        except Exception:
            compacted = data
    else:
        compacted = data

    data = {}

    for k, v in compacted.items():
        if k in fields:
            no_value_context = isinstance(v, dict) and "@context" not in v
            has_nested_context = k in compacted[
                "@context"] and "@context" in compacted["@context"][k]
            if no_value_context and has_nested_context:
                # Propagate down context
                v["@context"] = compacted["@context"][k]["@context"]

            data[k] = v

    data["@context"] = jsonld_context

    _migrate_types(data)

    write_yaml(path, data)