def __call__(self, map_file: types.Mapfile):
     backend_file = mappyfile.loads(self.template)
     backend_file.update(self.map_file_to_dict(map_file))
     errors = mappyfile.validate(backend_file)
     if errors:
         raise Exception(errors)
     return mappyfile.dumps(backend_file)
Beispiel #2
0
def validate(ctx, mapfiles, expand, version):
    """
    Validate Mapfile(s) against the Mapfile schema

    The MAPFILES argument is a list of paths, either to individual Mapfiles, or a folders containing Mapfiles.
    Wildcards are supported (natively on Linux, and up to one level deep on Windows).
    Validation errors are reported to the console. The program returns the error count - this will be 0 if no
    validation errors are encountered.

    Example of validating a single Mapfile:

        mappyfile validate C:/Temp/valid.map

    Example of validating two folders containing Mapfiles, without expanding INCLUDES:

        mappyfile validate C:/Temp/*.map D:/GitHub/mappyfile/tests/mapfiles/*.map --no-expand
    """

    all_mapfiles = get_mapfiles(mapfiles)

    if len(all_mapfiles) == 0:
        click.echo("No Mapfiles found at the following paths: {}".format(
            ",".join(mapfiles)))
        return

    validation_count = 0
    errors = 0

    for fn in all_mapfiles:
        fn = click.format_filename(fn)
        try:
            d, trace_o_incl = mappyfile.open(fn,
                                             output_trace=True,
                                             expand_includes=expand,
                                             include_position=True)
        except Exception as ex:
            logger.exception(ex)
            click.echo("{} failed to parse successfully".format(fn))
            continue

        validation_messages = mappyfile.validate(d, trace_o_incl, version)
        if validation_messages:
            for v in validation_messages:
                v["fn"] = fn
                msg = "{fn} (File: {file} Line: {line} Column: {column}) {message} - {error}".format(
                    **v)
                click.echo(msg)
                errors += 1
        else:
            click.echo("{} validated successfully".format(fn))
            validation_count += 1

    click.echo("{} file(s) validated ({} successfully)".format(
        len(all_mapfiles), validation_count))
    sys.exit(errors)