Exemplo n.º 1
0
def _test(mod):
    """Performs standard document validation tests."""
    # Set test document/error factories.
    error_setters = (_set_doc_errors_01, _set_doc_errors_02)
    if hasattr(mod, "get_document_error_setters"):
        error_setters += mod.get_document_error_setters()

    # Perform validation test.
    for error_setter in error_setters:
        # ... set valid test document.
        doc = tu.get_doc(mod)

        # ... set expected error count.
        expected_error_count = error_setter(doc)

        # ... validate document.
        errors = pyesdoc.validate(doc)

        # ... print errors if mismatch between expected & actual.
        # if len(errors) == expected_error_count:
        #     for error in errors:
        #         rt.log(error, level=rt.LOG_LEVEL_WARNING)

        # ... assert errors.
        tu.assert_iter(errors, expected_error_count)
Exemplo n.º 2
0
def _get_content(i, s, settings):
    """Generates a CIM document for a CMIP6 institute | source combination.

    """
    # Set JSON content accessors.
    accessors = _get_data_accessors(i, s, settings)

    # Map JSON -> CIM.
    doc = _map_model(i, s, accessors)
    if doc is None:
        return

    # Destructure injected properties.
    _destructure(doc)

    # Emit validation report.
    errors = pyesdoc.validate(doc)
    errors = [
        e for e in errors if e.endswith('values --> is an empty list') == False
    ]
    if errors:
        print("INVALID CIM DOCUMENT: {}".format(s))
        for err in errors:
            print(err)

    # Return JSON string.
    return pyesdoc.encode(doc)
Exemplo n.º 3
0
def _test(mod):
    """Performs standard document validation tests."""
    # Set test document/error factories.
    error_setters = (_set_doc_errors_01, _set_doc_errors_02)
    if hasattr(mod, "get_document_error_setters"):
        error_setters += mod.get_document_error_setters()

    # Perform validation test.
    for error_setter in error_setters:
        # ... set valid test document.
        doc = tu.get_doc(mod)

        # ... set expected error count.
        expected_error_count = error_setter(doc)

        # ... validate document.
        errors = pyesdoc.validate(doc)

        # ... print errors if mismatch between expected & actual.
        # if len(errors) == expected_error_count:
        #     for error in errors:
        #         rt.log(error, level=rt.LOG_LEVEL_WARNING)

        # ... assert errors.
        tu.assert_iter(errors, expected_error_count, length_compare=tu.COMPARE_LTE)
Exemplo n.º 4
0
def _get_document(institution, domain, model, topics):
    """Generates one CIM document per institute | domain | model.

    """
    # Set JSON data file accessors.
    accessors = [
        ModelTopicOutput.create(institution, domain, model, t) for t in topics
    ]

    # Map JSON -> CIM.
    doc = _map_model(institution, domain, model, accessors)
    if doc is None:
        return

    # Destructure injected properties.
    _destructure(doc)

    # Emit validation report.
    errors = pyesdoc.validate(doc)
    errors = [
        e for e in errors if e.endswith('values --> is an empty list') == False
    ]
    if errors:
        print("INVALID CIM DOCUMENT: {}".format(s))
        for err in errors:
            print(err)

    return doc
Exemplo n.º 5
0
def _main():
    """Main entry point.

    """
    # Open document & validate.
    doc = pyesdoc.read(options.file)
    report = pyesdoc.validate(doc)

    # Inform user of validation result.
    if report:
        if options.outfile:
            _emit_to_file_system(report)
        else:
            _emit_to_stdout(report)
    else:
        pyesdoc.rt.log("Documemt is valid.")
Exemplo n.º 6
0
def _main():
    """Main entry point.

    """
    # Open document & validate.
    doc = pyesdoc.read(options.file)
    report = pyesdoc.validate(doc)

    # Inform user of validation result.
    if report:
        if options.outfile:
            _emit_to_file_system(report)
        else:
            _emit_to_stdout(report)
    else:
        pyesdoc.rt.log("Documemt is valid.")
Exemplo n.º 7
0
def _get_content(i, s, settings):
    """Generates a CIM document for a CMIP6 institute | source combination.

    """
    doc = _map_model(i, s, _get_data_accessors(i, s, settings))
    if doc is None:
        return

    # Destructure injected properties.
    _destructure(doc)

    # Emit validation report.
    errors = pyesdoc.validate(doc)
    errors = [e for e in errors if
              e.endswith('values --> is an empty list') == False]
    if errors:
        print "INVALID CIM DOCUMENT:", s
        for err in errors:
            print err

    # Return JSON string.
    return pyesdoc.encode(doc)
Exemplo n.º 8
0
    # Iterate over all machine tabs to get all sets of outputs
    for index, input_dict in enumerate(inputs):
        # Return machine doc with applicable models and experiments:
        cim_out, apply_models_out, appl_exp_out = generate_outputs(
            input_dict,
            two_c_pools=two_c_pools[index],
            two_s_pools=two_s_pools[index],
            docs_given=has_docs[index])

        # Validate the CIM document - there should not be any errors
        if pyesdoc.is_valid(cim_out):
            print("Complete: machine CIM document generated and is valid.")
        else:
            print("Machine CIM document generated is not valid:")
            for err in pyesdoc.validate(cim_out):
                print(err)

        # Test serialisation of the machine doc...
        j = pyesdoc.encode(cim_out, pyesdoc.constants.ENCODING_JSON)
        assert json.loads(j)
        assert isinstance(pyesdoc.decode(j, pyesdoc.constants.ENCODING_JSON),
                          cim.Machine)

        x = pyesdoc.encode(cim_out, pyesdoc.constants.ENCODING_XML)
        assert isinstance(pyesdoc.decode(x, pyesdoc.constants.ENCODING_XML),
                          cim.Machine)

        # CIM document is valid and can be encoded correctly, so ready to
        # store it in the specified location as JSON:
        pyesdoc.write(cim_out, CIM_OUT_PATH, encoding=encoding)