Beispiel #1
0
    def from_yaml(cls, path, client=None):
        """Return an instance from a YAML file."""
        data = jsonld.read_yaml(path)
        self = cls.from_jsonld(data=data, client=client)
        self.__reference__ = path

        return self
Beispiel #2
0
def read_jsonld_yaml(path):
    """Read YAML file and return normalized expanded JSON-LD."""
    data = read_yaml(path)
    jsonld = pyld.jsonld.expand(data)[0]
    v = normalize(jsonld)

    return v
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 from_yaml(cls, path, client):
        """Read content from YAML file."""
        data = jsonld.read_yaml(path)
        self = ProjectSchemaV3().load(data)

        if not self.creator:
            self.creator = Person.from_git(client.repo)

        if not self._id or "NULL/NULL" in self._id:
            self._id = generate_project_id(client=client,
                                           name=self.name,
                                           creator=self.creator)

        return self
Beispiel #5
0
 def from_yaml(cls, path, client=None, commit=None):
     """Read content from YAML file."""
     data = jsonld.read_yaml(path)
     self = DatasetSchemaV3(client=client, commit=commit).load(data)
     self.__reference__ = path
     return self
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)