コード例 #1
0
def test_object_versioning():
    """
    Exclude whole objects if they were added in a
    later version of MapServer
    """

    s = """MAP
    NAME "sample"
    LAYER
        TYPE POLYGON
        COMPOSITE
            COMPOP "lighten"
            OPACITY 50
            COMPFILTER "blur(10)"
        END
    END
END"""

    d = mappyfile.loads(s, include_position=False)
    v = Validator()
    errors = v.validate(d, add_comments=True, version=6.0)
    assert len(errors) == 1

    d = mappyfile.loads(s, include_position=False)
    errors = v.validate(d, add_comments=True, version=7.0)
    assert len(errors) == 0
コード例 #2
0
def main(msautotest_fld, create_new_copy=True):

    msautotest_copy = os.path.join(os.path.dirname(msautotest_fld),
                                   "msautotest_mappyfile")

    if create_new_copy:
        create_copy(msautotest_fld, msautotest_copy)

    parser = Parser()
    transformer = MapfileToDict()
    pp = PrettyPrinter()

    # these two maps aren't in utf8
    # see https://github.com/mapserver/mapserver/pull/5460
    # ignore_list = ["wms_inspire_scenario1.map","wms_inspire_scenario2.map"]

    # transparent_layer.map has an extra END, see https://github.com/mapserver/mapserver/pull/5468
    # polyline_no_clip.map needs symbol names in quotes, and SYMBOL is ambiguous

    ignore_list = [
        "polyline_no_clip.map", "poly-label-multiline-pos-auto.map",
        "poly-label-pos-auto.map", "embed_sb_rgba.map",
        "embed_sb_rgba_offset.map"
    ]  # has attributes all on the same line

    mapfiles = glob.glob(msautotest_fld + '/**/*.map')
    mapfiles = [f for f in mapfiles if os.path.basename(f) not in ignore_list]

    # target_map = "polyline_no_clip.map"
    # mapfiles = [f for f in mapfiles if os.path.basename(f) in (target_map)]

    v = Validator()

    for fn in mapfiles:

        d = parse_mapfile(parser, transformer, pp, fn)
        errors = v.validate(d, add_messages=True)
        if errors:
            logging.warning("{} failed validation".format(fn))

        output_file = fn.replace(msautotest_fld, msautotest_copy)
        try:
            mappyfile.utils.write(d, output_file)
        except Exception:
            logging.warning(json.dumps(d, indent=4))
            logging.warning("%s could not be successfully re-written", fn)
            raise

        # now try reading it again
        print(json.dumps(d, indent=4))
        d = parse_mapfile(parser, transformer, pp, output_file)

        errors = v.validate(d, add_messages=True)
        if errors:
            logging.warning("{} failed validation".format(fn))
コード例 #3
0
def main(msautotest_fld, create_new_copy=True):

    msautotest_copy = os.path.join(os.path.dirname(msautotest_fld), "msautotest_mappyfile")

    if create_new_copy:
        create_copy(msautotest_fld, msautotest_copy)

    parser = Parser()
    transformer = MapfileToDict()
    pp = PrettyPrinter()

    # these two maps aren't in utf8
    # see https://github.com/mapserver/mapserver/pull/5460
    # ignore_list = ["wms_inspire_scenario1.map","wms_inspire_scenario2.map"]

    # transparent_layer.map has an extra END, see https://github.com/mapserver/mapserver/pull/5468
    # polyline_no_clip.map needs symbol names in quotes, and SYMBOL is ambiguous

    ignore_list = ["polyline_no_clip.map",
                   "poly-label-multiline-pos-auto.map", "poly-label-pos-auto.map",
                   "embed_sb_rgba.map", "embed_sb_rgba_offset.map"]  # has attributes all on the same line

    mapfiles = glob.glob(msautotest_fld + '/**/*.map')
    mapfiles = [f for f in mapfiles if os.path.basename(f) not in ignore_list]

    # target_map = "polyline_no_clip.map"
    # mapfiles = [f for f in mapfiles if os.path.basename(f) in (target_map)]

    v = Validator()

    for fn in mapfiles:

        d = parse_mapfile(parser, transformer, pp, fn)
        errors = v.validate(d, add_comments=True)
        if errors:
            logging.warning("{} failed validation".format(fn))

        output_file = fn.replace(msautotest_fld, msautotest_copy)
        try:
            mappyfile.utils.write(d, output_file)
        except Exception:
            logging.warning(json.dumps(d, indent=4))
            logging.warning("%s could not be successfully re-written", fn)
            raise

        # now try reading it again
        print(json.dumps(d, indent=4))
        d = parse_mapfile(parser, transformer, pp, output_file)

        errors = v.validate(d, add_comments=True)
        if errors:
            logging.warning("{} failed validation".format(fn))
コード例 #4
0
ファイル: utils.py プロジェクト: hugbe8/mappyfile
def validate(d, trace_o_incl=None, version=None):
    """
    Validate a mappyfile dictionary by using the Mapfile schema.
    An optional version number can be used to specify a specific
    a Mapfile is valid for a specific MapServer version.

    Parameters
    ----------

    d: dict
        A Python dictionary based on the the mappyfile schema
    trace_o_incl: list
        A trace of the origin of lines for include files, use to find the original location of an error
    version: float
        The MapServer version number used to validate the Mapfile

    Returns
    -------

    list
          A list containing validation errors

    """
    v = Validator()
    return v.validate(d, trace_o_incl, version=version)
コード例 #5
0
ファイル: test_sample_maps.py プロジェクト: mhiley/mappyfile
def test_all_maps():

    sample_dir = os.path.join(os.path.dirname(__file__), "sample_maps")

    p = Parser(expand_includes=False)
    m = MapfileToDict(include_position=True)
    v = Validator()

    failing_maps = []

    for fn in os.listdir(sample_dir):
        print(fn)
        try:
            ast = p.parse_file(os.path.join(sample_dir, fn))
            d = m.transform(ast)
            errors = v.validate(d)
            try:
                assert (len(errors) == 0)
            except AssertionError as ex:
                logging.warning("Validation errors in %s ", fn)
                logging.warning(errors)
        except (BaseException, UnexpectedToken) as ex:
            logging.warning("Cannot process %s ", fn)
            logging.error(ex)
            failing_maps.append(fn)

    logging.warning(failing_maps)
コード例 #6
0
def test_double_error():

    s = """MAP
    NAME "sample"
    STATUS ON
    SIZE 600 400
    SYMBOLSET "../etc/symbols.txt"
    EXTENT -180 -90 180
    UNITS DD
    SHAPEPATH "../data"
    IMAGECOLOR 255 255 256
    FONTSET "../etc/fonts.txt"
    WEB
        IMAGEPATH "/ms4w/tmp/ms_tmp/"
        IMAGEURL "/ms_tmp/"
    END
    LAYER
        NAME "global-raster"
        TYPE RASTER
        STATUS DEFAULT
        DATA "bluemarble.gif"
    END
END"""

    d = mappyfile.loads(s, include_position=True)
    # print(json.dumps(d, indent=4))
    v = Validator()
    errors = v.validate(d, add_comments=True)
    # print(json.dumps(d, indent=4))
    # print(errors)
    for e in errors:
        print(e)
    assert(len(errors) == 2)
    print(mappyfile.dumps(d))
コード例 #7
0
def test_line_position_mutlilines():

    s = """MAP
    NAME "sample"
    LAYER
        NAME "test"
        STATUS DEFAULT
        DATA "SELECT GEOM
        FROM
        TABLE"
        TYPE LINEX
    END
END"""

    p = Parser()
    ast = p.parse(s)
    print(ast)

    d = mappyfile.loads(s, include_position=True)
    v = Validator()
    errors = v.validate(d, add_comments=True)
    print(json.dumps(d, indent=4))
    # print(errors)
    for e in errors:
        print(e)
    assert (len(errors) == 1)
    err = errors[0]
    assert (err["line"] == 9)
    assert (err["column"] == 9)
    print(mappyfile.dumps(d))
コード例 #8
0
ファイル: test_validation.py プロジェクト: mhiley/mappyfile
def test_double_error():

    s = """MAP
    NAME "sample"
    STATUS ON
    SIZE 600 400
    SYMBOLSET "../etc/symbols.txt"
    EXTENT -180 -90 180
    UNITS DD
    SHAPEPATH "../data"
    IMAGECOLOR 255 255 256
    FONTSET "../etc/fonts.txt"
    WEB
        IMAGEPATH "/ms4w/tmp/ms_tmp/"
        IMAGEURL "/ms_tmp/"
    END
    LAYER
        NAME "global-raster"
        TYPE RASTER
        STATUS DEFAULT
        DATA "bluemarble.gif"
    END
END"""

    d = mappyfile.loads(s, include_position=True)
    # print(json.dumps(d, indent=4))
    v = Validator()
    errors = v.validate(d, add_comments=True)
    # print(json.dumps(d, indent=4))
    # print(errors)
    for e in errors:
        print(e)
    assert (len(errors) == 2)
    print(mappyfile.dumps(d))
コード例 #9
0
def test_all_maps():

    sample_dir = os.path.join(os.path.dirname(__file__), "sample_maps")

    p = Parser(expand_includes=False)
    m = MapfileToDict(include_position=True)
    v = Validator()

    failing_maps = []

    for fn in os.listdir(sample_dir):
        print(fn)
        try:
            ast = p.parse_file(os.path.join(sample_dir, fn))
            d = m.transform(ast)
            errors = v.validate(d)
            try:
                assert(len(errors) == 0)
            except AssertionError as ex:
                logging.warning("Validation errors in %s ", fn)
                logging.warning(errors)
        except (BaseException, UnexpectedToken) as ex:
            logging.warning("Cannot process %s ", fn)
            logging.error(ex)
            failing_maps.append(fn)

    logging.warning(failing_maps)
コード例 #10
0
def validate(d):
    """
    Validate a mappyfile dictionary by using the Mapfile schema

    d: dict
        A Python dictionary based on the the mappyfile schema
    """
    v = Validator()
    return v.validate(d)
コード例 #11
0
def output(fn):
    """
    Parse, transform, and pretty print
    the result
    """
    p = Parser(expand_includes=False)
    m = MapfileToDict()
    v = Validator()

    try:
        ast = p.parse_file(fn)
        # print(ast)
        d = m.transform(ast)
        logging.debug("Number of layers: {}".format(len(d["layers"])))

        errors = v.validate(d)
        assert (len(errors) == 0)

    except Exception as ex:
        logging.exception(ex)
        logging.warning("%s could not be successfully parsed", fn)
        d = None
        raise

    if d:
        try:
            s = mappyfile.utils.dumps(d)
        except Exception:
            logging.warning(json.dumps(d, indent=4))
            logging.warning("%s could not be successfully re-written", fn)
            raise

        # now try reading it again
        ast = p.parse(s)
        d = m.transform(ast)

        errors = v.validate(d)
        assert (len(errors) == 0)
コード例 #12
0
def output(fn):
    """
    Parse, transform, and pretty print
    the result
    """
    p = Parser(expand_includes=False, include_comments=True)
    m = MapfileToDict(include_position=True, include_comments=True)
    v = Validator()

    try:
        ast = p.parse_file(fn)
        # print(ast)
        d = m.transform(ast)
        logging.debug("Number of layers: {}".format(len(d["layers"])))

        errors = v.validate(d, add_comments=True)
        assert(len(errors) == 0)

    except Exception as ex:
        logging.exception(ex)
        logging.warning("%s could not be successfully parsed", fn)
        d = None
        raise

    if d:
        try:
            s = mappyfile.utils.dumps(d)
        except Exception:
            logging.warning(json.dumps(d, indent=4))
            logging.warning("%s could not be successfully re-written", fn)
            raise

        # now try reading it again
        ast = p.parse(s)
        d = m.transform(ast)

        errors = v.validate(d)
        assert(len(errors) == 0)
コード例 #13
0
def test_cluster_validation_fail():

    s = u"""
    MAP
        LAYER
            CLUSTER
                MAXDISTANCE 50
                REGION "ELLIPSEZ"
            END
        END
    END
    """

    d = mappyfile.loads(s, include_position=True)
    v = Validator()
    errors = v.validate(d, add_comments=True)
    print(mappyfile.dumps(d))
    assert len(errors) == 1
コード例 #14
0
def test_root_position():
    """
    Check the root objects position is found correctly
    """

    s = """
    MAP
        METADATA
            "wms_title"    "Toronto Landsat 5 TM"
        END
    END
    """

    d = mappyfile.loads(s, include_position=True)
    v = Validator()
    assert d["__position__"]["line"] == 2
    errors = v.validate(d, add_comments=True)
    assert len(errors) == 1
コード例 #15
0
def test_extra_property_validation():
    """
    Check root errors are handled correctly
    """
    s = """
    MAP
        LAYER
            TYPE POLYGON
        END
    END
    """

    d = to_dict(s)
    d["unwanted"] = "error"
    v = Validator()
    errors = v.validate(d, add_comments=True)
    print(errors)
    assert(len(errors) == 1)
コード例 #16
0
ファイル: test_validation.py プロジェクト: mhiley/mappyfile
def test_extra_property_validation():
    """
    Check root errors are handled correctly
    """
    s = """
    MAP
        LAYER
            TYPE POLYGON
        END
    END
    """

    d = to_dict(s)
    d["__unwanted__"] = "error"
    v = Validator()
    errors = v.validate(d, add_comments=True)
    print(errors)
    assert (len(errors) == 1)
コード例 #17
0
def test_version_warnings():

    s = """MAP
    NAME "sample"
    LAYER
        NAME "test"
        TYPE LINE
        CLASS
            #MADEUP True
            COLOR 0 0 0
        END
    END
END"""

    d = mappyfile.loads(s, include_position=False)
    v = Validator()
    errors = v.validate(d, add_comments=True, version=8.0)
    print(errors)
    assert len(errors) == 1
コード例 #18
0
def output(s, include_position=True, schema_name="map"):
    """
    Parse, transform, validate, and pretty print
    the result
    """
    p = Parser()
    m = MapfileToDict(include_position=include_position)
    ast = p.parse(s)
    logging.debug(ast.pretty())
    d = m.transform(ast)
    logging.debug(json.dumps(d, indent=4))
    v = Validator()
    errors = v.validate(d, schema_name=schema_name)
    logging.error(errors)
    pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'")
    s = pp.pprint(d)
    logging.debug(s)
    assert(len(errors) == 0)
    return s
コード例 #19
0
ファイル: utils.py プロジェクト: tkardi/mappyfile
def validate(d):
    """
    Validate a mappyfile dictionary by using the Mapfile schema

    Parameters
    ----------

    d: dict
        A Python dictionary based on the the mappyfile schema

    Returns
    -------

    list
          A list containing validation errors

    """
    v = Validator()
    return v.validate(d)
コード例 #20
0
def output(s, include_position=True, schema_name="map"):
    """
    Parse, transform, validate, and pretty print
    the result
    """
    p = Parser()
    m = MapfileToDict(include_position=include_position)
    ast = p.parse(s)
    logging.debug(ast.pretty())
    d = m.transform(ast)
    logging.debug(json.dumps(d, indent=4))
    v = Validator()
    errors = v.validate(d, schema_name=schema_name)
    logging.error(errors)
    pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'")
    s = pp.pprint(d)
    logging.debug(s)
    assert (len(errors) == 0)
    return s
コード例 #21
0
def test_cluster_validation():

    s = u"""
    MAP
        LAYER
            TYPE POINT
            CLUSTER
                MAXDISTANCE 50
                REGION "ELLIPSE"
            END
        END
    END
    """

    d = mappyfile.loads(s, include_position=True)
    v = Validator()
    assert d["__position__"]["line"] == 2
    errors = v.validate(d, add_comments=True)
    print(mappyfile.dumps(d))
    assert len(errors) == 0
コード例 #22
0
def output(s, include_position=True, schema_name="map"):
    """
    Parse, transform, validate, and pretty print
    the result
    """
    p = Parser()
    m = MapfileToDict(include_position=include_position)

    # https://stackoverflow.com/questions/900392/getting-the-caller-function-name-inside-another-function-in-python
    logging.info(inspect.stack()[1][3])

    ast = p.parse(s)
    logging.debug(ast.pretty())
    d = m.transform(ast)
    logging.debug(json.dumps(d, indent=4))
    v = Validator()
    errors = v.validate(d, schema_name=schema_name)
    logging.error(errors)
    pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'")
    s = pp.pprint(d)
    logging.debug(s)
    assert(len(errors) == 0)
    return s
コード例 #23
0
def output(s, include_position=True, schema_name="map"):
    """
    Parse, transform, validate, and pretty print
    the result
    """
    p = Parser()
    m = MapfileToDict(include_position=include_position)

    # https://stackoverflow.com/questions/900392/getting-the-caller-function-name-inside-another-function-in-python
    logging.info(inspect.stack()[1][3])

    ast = p.parse(s)
    logging.debug(ast.pretty())
    d = m.transform(ast)
    logging.debug(json.dumps(d, indent=4))
    v = Validator()
    errors = v.validate(d, schema_name=schema_name)
    logging.error(errors)
    pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'")
    s = pp.pprint(d)
    logging.debug(s)
    assert (len(errors) == 0)
    return s
コード例 #24
0
def validate(d, version=None):
    """
    Validate a mappyfile dictionary by using the Mapfile schema.
    An optional version number can be used to specify a specific
    a Mapfile is valid for a specific MapServer version.

    Parameters
    ----------

    d: dict
        A Python dictionary based on the the mappyfile schema
   version: float
        The MapServer version number used to validate the Mapfile

    Returns
    -------

    list
          A list containing validation errors

    """
    v = Validator()
    return v.validate(d, version=version)
コード例 #25
0
ファイル: test_transformer.py プロジェクト: mhiley/mappyfile
def test_multiple_layer_projection():
    """
    TODO add validation for this case
    """

    s = """
    MAP
    LAYER
        PROJECTION
            "init=epsg:4326"
            "init=epsg:4326"
        END
        PROJECTION
            "init=epsg:4326"
            "init=epsg:4326"
        END
    END
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict(include_position=True)
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert (len(d["projection"]) == 1)

    p = Parser()
    m = MapfileToDict()

    ast = p.parse(s)
    d = m.transform(ast)

    print(json.dumps(d, indent=4))

    from mappyfile.validator import Validator
    v = Validator()
    return v.validate(d)
コード例 #26
0
def test_multiple_layer_projection():
    """
    TODO add validation for this case
    """

    s = """
    MAP
    LAYER
        PROJECTION
            "init=epsg:4326"
            "init=epsg:4326"
        END
        PROJECTION
            "init=epsg:4326"
            "init=epsg:4326"
        END
    END
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict(include_position=True)
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert(len(d["projection"]) == 1)

    p = Parser()
    m = MapfileToDict()

    ast = p.parse(s)
    d = m.transform(ast)

    print(json.dumps(d, indent=4))

    from mappyfile.validator import Validator
    v = Validator()
    return v.validate(d)
コード例 #27
0
ファイル: test_validation.py プロジェクト: mhiley/mappyfile
def test_add_comments():
    s = """
    MAP
        IMAGECOLOR 'FF00FF'
        LAYER
            EXTENT 0 0 0
            TYPE POLYGON
        END
    END
    """
    d = to_dict(s)
    v = Validator()
    errors = v.validate(d, add_comments=True)

    print(len(errors))
    print(json.dumps(d, indent=4))

    for error in errors:
        print(error)

    pp = PrettyPrinter(indent=4, quote='"')  # expected

    res = pp.pprint(d)
    print(res)
コード例 #28
0
def test_add_comments():
    s = """
    MAP
        IMAGECOLOR 'FF00FF'
        LAYER
            EXTENT 0 0 0
            TYPE POLYGON
        END
    END
    """
    d = to_dict(s)
    v = Validator()
    errors = v.validate(d, add_comments=True)

    print(len(errors))
    print(json.dumps(d, indent=4))

    for error in errors:
        print(error)

    pp = PrettyPrinter(indent=4, quote='"')  # expected

    res = pp.pprint(d)
    print(res)
コード例 #29
0
ファイル: utils.py プロジェクト: tomkralidis/mappyfile
def validate(d):
    v = Validator()
    return v.validate(d)
コード例 #30
0
def validate(s):
    d = to_dict(s)
    v = Validator()
    return v.validate(d)
コード例 #31
0
ファイル: test_validation.py プロジェクト: mhiley/mappyfile
def validate(s):
    d = to_dict(s)
    v = Validator()
    return v.validate(d)
コード例 #32
0
def validate(d):
    v = Validator()
    return v.validate(d)