Esempio n. 1
0
def get_versioned_release_schema(schema):
    """
    Returns the versioned release schema.
    """
    # Update schema metadata.
    release_with_underscores = release.replace('.', '__')
    schema['id'] = f'https://standard.open-contracting.org/schema/{release_with_underscores}/versioned-release-validation-schema.json'  # noqa
    schema['title'] = 'Schema for a compiled, versioned Open Contracting Release.'

    # Release IDs, dates and tags appear alongside values in the versioned release schema.
    remove_omit_when_merged(schema)

    # Create unversioned copies of all definitions.
    unversioned_definitions = {k + 'Unversioned': deepcopy(v) for k, v in schema['definitions'].items()}
    update_refs_to_unversioned_definitions(unversioned_definitions)

    # Determine which `id` fields occur on objects in arrays.
    unversioned_pointers = set()
    get_unversioned_pointers(JsonRef.replace_refs(schema), unversioned_pointers)

    # Omit `ocid` from versioning.
    ocid = schema['properties'].pop('ocid')
    add_versioned(schema, unversioned_pointers)
    schema['properties']['ocid'] = ocid

    # Add the common versioned definitions.
    for name, keywords in common_versioned_definitions.items():
        versioned = deepcopy(versioned_template)
        for keyword, value in keywords.items():
            if value:
                versioned['items']['properties']['value'][keyword] = value
        schema['definitions'][name] = versioned

    # Add missing definitions.
    while True:
        ref = JsonRef.replace_refs(schema)
        try:
            repr(ref)
            break
        except JsonRefError as e:
            name = e.cause.args[0]

            if name.endswith('VersionedId'):
                # Add a copy of an definition with a versioned `id` field, using the same logic as before.
                definition = deepcopy(schema['definitions'][name[:-11]])
                pointer = f'/definitions/{name[:-11]}/properties/id'
                pointers = unversioned_pointers - {pointer}
                _add_versioned(definition, pointers, pointer, 'id', definition['properties']['id'])
            else:
                # Add a copy of an definition with no versioned fields.
                definition = unversioned_definitions[name]

            schema['definitions'][name] = definition

    # Remove all metadata and extended keywords.
    remove_metadata_and_extended_keywords(schema)

    return schema
Esempio n. 2
0
def get_versioned_validation_schema(versioned_release):
    release_with_underscores = release.replace('.', '__')
    versioned_release[
        "id"] = "http://standard.open-contracting.org/schema/{}/versioned-release-validation-schema.json".format(
            release_with_underscores)  # noqa nopep8
    versioned_release[
        "$schema"] = "http://json-schema.org/draft-04/schema#"  # nopep8
    versioned_release[
        "title"] = "Schema for a compiled, versioned Open Contracting Release."  # nopep8

    definitions = versioned_release['definitions']

    new_definitions = OrderedDict()
    for key, value in copy.deepcopy(versioned_release['definitions']).items():
        new_definitions[key + 'Unversioned'] = value

    unversion_refs(new_definitions)

    ocid = versioned_release['properties'].pop("ocid")
    versioned_release['properties'].pop("date")
    versioned_release['properties'].pop("id")
    versioned_release['properties'].pop("tag")

    versioned_release['required'] = ["ocid", "initiationType"]

    add_versions(versioned_release)

    versioned_release['properties']["ocid"] = ocid

    definitions.update(new_definitions)
    add_string_definitions(versioned_release)

    for key, value in definitions.items():
        value.pop("title", None)
        value.pop("description", None)
        if 'properties' not in value:
            continue
        for prop_value in value['properties'].values():
            prop_value.pop("title", None)
            prop_value.pop("description", None)
            prop_value.pop("omitWhenMerged", None)
            prop_value.pop("wholeListMerge", None)
            prop_value.pop("versionId", None)

    return versioned_release
Esempio n. 3
0
import os.path
import re
import sys

docs_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..',
                         '..', 'docs', 'en')
sys.path.append(docs_path)

from conf import release  # noqa

if __name__ == '__main__':
    schema_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    replacement = '/{}/'.format(release.replace('.', '__'))

    for name in os.listdir(schema_dir):
        if name.endswith('.json'):
            with open(os.path.join(schema_dir, name)) as f:
                schema = f.read()

            pattern = r'/\d+__\d+__\d+/'
            if re.search(pattern, schema):
                schema = re.sub(pattern, replacement, schema)

                with open(os.path.join(schema_dir, name), 'w') as f:
                    f.write(schema)