Esempio n. 1
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)
Esempio n. 2
0
def test_enumeration():
    s = """
    MAP
        EXTENT 0 0 100 100
        DEBUG on
        NAME Test
        shapepath "test/path"
        LAYER
            CLASSITEM "Test"
            CLASS
                EXPRESSION "Field"
                STYLE
                    SIZE [sizefield]
                END
            END
        END
    END
    """
    p = Parser()
    m = MapfileToDict()
    ast = p.parse(s)
    d = m.transform(ast)
    print(d)
    logging.debug(json.dumps(d, indent=4))
    pp = PrettyPrinter(indent=4, quote="'", newlinechar="\n")
    res = pp.pprint(d)
    print(res)
Esempio n. 3
0
def to_dict(s):
    p = Parser()
    m = MapfileToDict()
    ast = p.parse(s)
    d = m.transform(ast)
    print(json.dumps(d, indent=4))
    return d
Esempio n. 4
0
def test_includes_max_recursion():
    p = Parser()

    with pytest.raises(Exception) as excinfo:
        p.parse_file('./tests/samples/include1_recursive.map')

    assert('Maximum nested include exceeded' in str(excinfo.value))
Esempio n. 5
0
def test_non_ascii():
    p = Parser()

    ast = p.parse_file('./tests/samples/non_ascii.map')
    m = MapfileToDict()

    d = (m.transform(ast))  # works
    print(mappyfile.dumps(d))
Esempio n. 6
0
def test_includes_nested_path():
    p = Parser()

    ast = p.parse_file('./tests/samples/include1_nested_path.map')
    m = MapfileToDict()

    d = (m.transform(ast))  # works
    print(mappyfile.dumps(d))
Esempio n. 7
0
def test_includes():
    p = Parser()

    ast = p.parse_file('./tests/samples/include1.map')
    m = MapfileToDict()

    d = (m.transform(ast))  # works
    print(mappyfile.dumps(d))
Esempio n. 8
0
def test_non_ascii():
    p = Parser()

    ast = p.parse_file('./tests/samples/non_ascii.map')
    m = MapfileToDict()

    d = (m.transform(ast))  # works
    print(mappyfile.dumps(d))
Esempio n. 9
0
def main():
    s = """
    MAP
        NAME 'blah'
        ANGLE 100
        DEBUG on
        CONFIG "ON_MISSING_DATA" FAIL # can be quoted or non-quoted
        STATUS on # need to enforce lowercase
        EXTENT -100 -100 100 100
        SIZE 400 400
        PROJECTION
            AUTO
        END
        LAYER
            PROJECTION
                AUTO
            END
            STATUS ON
            NAME "hi"
            TYPE polygon

            FEATURE
              POINTS 1 1 50 50 1 50 1 1 END
            END
            CLASS
                STYLE
                    COLOR 255 0 0
                END
            END

        END
        #LAYER
        #    NAME "hi2"
        #    TYPE point
        #END
    END
    """

    p = Parser()
    m = MapfileToDict()

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

    # test a value passed in from the editor
    d["status"] = "OFF"  # need to convert to uppercase if not already?

    # d["layers"][0]["type"] = "POINTX"

    print(d)
    # s = schema["definitions"]["map"]["properties"]["status"]

    """
Esempio n. 10
0
def test_includes_relative_path():
    """
    File location can be given as a full path to the file, or (in MapServer >= 4.10.1) as a
    path relative to the mapfile.
    http://mapserver.org/mapfile/include.html
    """
    p = Parser()

    ast = p.parse_file('./tests/samples/include4.map')
    m = MapfileToDict()

    d = (m.transform(ast))  # works
    print(mappyfile.dumps(d))
Esempio n. 11
0
def test_includes_relative_path():
    """
    File location can be given as a full path to the file, or (in MapServer >= 4.10.1) as a
    path relative to the mapfile.
    http://mapserver.org/mapfile/include.html
    """
    p = Parser()

    ast = p.parse_file('./tests/samples/include4.map')
    m = MapfileToDict()

    d = (m.transform(ast))  # works
    print(mappyfile.dumps(d))
Esempio n. 12
0
def test_or_expression():

    s = """
    CLASS
        EXPRESSION (([val] = 'A') OR ([val] = 'B'))
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(d)
    print(json.dumps(d, indent=4))
    assert (d["expression"] == "( ( [val] = 'A' ) OR ( [val] = 'B' ) )")
Esempio n. 13
0
def test_boolean():

    s = """
    LAYER
        TRANSFORM TRUE
        TYPE POINT
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert (d["transform"])
Esempio n. 14
0
def test_boolean():

    s = """
    LAYER
        TRANSFORM TRUE
        TYPE POINT
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert(d["transform"])
Esempio n. 15
0
def test_all_maps():

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

    p = Parser(expand_includes=False)

    for fn in os.listdir(sample_dir):
        print(fn)
        try:
            ast = p.parse_file(os.path.join(sample_dir, fn))
            #ast.to_png_with_pydot(r'C:\Temp\Trees\%s.png' % os.path.basename(fn))
        except:
            logging.warning("Cannot process %s ", fn)
            raise
Esempio n. 16
0
def test_or_expression():

    s = """
    CLASS
        EXPRESSION (([val] = 'A') OR ([val] = 'B'))
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(d)
    print(json.dumps(d, indent=4))
    assert(d["expression"] == "( ( [val] = 'A' ) OR ( [val] = 'B' ) )")
Esempio n. 17
0
def output(s):
    """
    Parse, transform, and pretty print
    the result
    """
    p = Parser()
    t = ExpressionsTransformer()

    ast = p.parse(s)
    logging.debug(ast.pretty())
    print(ast.pretty())
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    return d
Esempio n. 18
0
def test_lowercase():

    s = """
    MAP
        NAME 'blah'
        ANGLE 100
        DEBUG on
        CONFIG "ON_MISSING_DATA" FAIL # can be quoted or non-quoted
        STATUS on # need to enforce lowercase
        EXTENT -100 -100 100 100
        SIZE 400 400
        PROJECTION
            AUTO
        END
        LAYER
            PROJECTION
                AUTO
            END
            STATUS ON
            NAME "hi"
            TYPE polygon

            FEATURE
              POINTS 1 1 50 50 1 50 1 1 END
            END
            CLASS
                STYLE
                    COLOR 255 0 0
                END
            END

        END
        #LAYER
        #    NAME "hi2"
        #    TYPE point
        #END
    END
    """

    p = Parser()
    m = MapfileToDict()

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

    print(json.dumps(d, indent=4))
    errors = validate(s)
    print(errors)
    assert(len(errors) == 0)
Esempio n. 19
0
def test_lowercase():

    s = """
    MAP
        NAME 'blah'
        ANGLE 100
        DEBUG on
        CONFIG "ON_MISSING_DATA" FAIL # can be quoted or non-quoted
        STATUS on # need to enforce lowercase
        EXTENT -100 -100 100 100
        SIZE 400 400
        PROJECTION
            AUTO
        END
        LAYER
            PROJECTION
                AUTO
            END
            STATUS ON
            NAME "hi"
            TYPE polygon

            FEATURE
              POINTS 1 1 50 50 1 50 1 1 END
            END
            CLASS
                STYLE
                    COLOR 255 0 0
                END
            END

        END
        #LAYER
        #    NAME "hi2"
        #    TYPE point
        #END
    END
    """

    p = Parser()
    m = MapfileToDict()

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

    print(json.dumps(d, indent=4))
    errors = validate(s)
    print(errors)
    assert (len(errors) == 0)
Esempio n. 20
0
def test_pattern():

    s = """
    STYLE
        PATTERN 10 1 50 50 1 50 1 1 END
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert (len(d["pattern"]) == 4)
    assert (len(d["pattern"][0]) == 2)
    assert (d["pattern"][0][0] == 10)
Esempio n. 21
0
def get_dict(s):
    """
    Parse, transform, and pretty print
    the result
    """
    p = Parser()
    m = MapfileToDict()
    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))

    return d
Esempio n. 22
0
def test_pattern():

    s = """
    STYLE
        PATTERN 10 1 50 50 1 50 1 1 END
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert(len(d["pattern"]) == 4)
    assert(len(d["pattern"][0]) == 2)
    assert(d["pattern"][0][0] == 10)
Esempio n. 23
0
def test_auto_projection():

    s = """
    MAP
        PROJECTION
            AUTO
        END
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert(len(d["projection"]) == 1)
Esempio n. 24
0
def get_dict(s):
    """
    Parse, transform, and pretty print
    the result
    """
    p = Parser()
    m = MapfileToDict()
    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))

    return d
Esempio n. 25
0
def test_auto_projection():

    s = """
    MAP
        PROJECTION
            AUTO
        END
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert (len(d["projection"]) == 1)
Esempio n. 26
0
def load(fp,
         expand_includes=True,
         include_position=False,
         include_comments=False,
         **kwargs):
    """
    Load a Mapfile from an open file or file-like object.

    Parameters
    ----------

    fp: file
        A file-like object - as with all Mapfiles this should be encoded in "utf-8"
    expand_includes: boolean
        Load any ``INCLUDE`` files in the MapFile
    include_comments: boolean
         Include or discard comment strings from the Mapfile - *experimental*
    include_position: boolean
         Include the position of the Mapfile tokens in the output

    Returns
    -------

    dict
        A Python dictionary representing the Mapfile in the mappyfile format

    Example
    -------

    To open a Mapfile from a file and return it as a dictionary object::

        with open('mymap.map') as fp:
            d = mappyfile.load(fp)

    Notes
    -----

    Partial Mapfiles can also be opened, for example a file containing a ``LAYER`` object.
    """
    p = Parser(expand_includes=expand_includes,
               include_comments=include_comments,
               **kwargs)
    ast = p.load(fp)
    m = MapfileToDict(include_position=include_position,
                      include_comments=include_comments,
                      **kwargs)
    d = m.transform(ast)
    return d
Esempio n. 27
0
def open(fn,
         expand_includes=True,
         include_comments=False,
         include_position=False,
         **kwargs):
    """
    Load a Mapfile from the supplied filename into a Python dictionary.

    Parameters
    ----------

    fn: string
        The path to the Mapfile, or partial Mapfile
    expand_includes: boolean
        Load any ``INCLUDE`` files in the MapFile
    include_comments: boolean
         Include or discard comment strings from the Mapfile - *experimental*
    include_position: boolean
         Include the position of the Mapfile tokens in the output

    Returns
    -------

    dict
        A Python dictionary representing the Mapfile in the mappyfile format

    Example
    -------

    To open a Mapfile from a filename and return it as a dictionary object::

        d = mappyfile.open('mymap.map')

    Notes
    -----

    Partial Mapfiles can also be opened, for example a file containing a ``LAYER`` object.

    """
    p = Parser(expand_includes=expand_includes,
               include_comments=include_comments,
               **kwargs)
    ast = p.parse_file(fn)
    m = MapfileToDict(include_position=include_position,
                      include_comments=include_comments,
                      **kwargs)
    d = m.transform(ast)
    return d
Esempio n. 28
0
def test_oneline_label():

    s = """
    label
      type truetype size 8 font "default"
    end
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert (d["type"] == "truetype")
    assert (d["size"] == 8)
    assert (d["font"] == "default")
    assert (d["__type__"] == "label")
Esempio n. 29
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"] 
    ignore_list = []

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

        d = parse_mapfile(parser, transformer, pp, fn)
        output_file = fn.replace(msautotest_fld, msautotest_copy)
        mf = mappyfile.utils.write(d, output_file)

        # now try reading it again
        d = parse_mapfile(parser, transformer, pp, output_file)
Esempio n. 30
0
def test_oneline_label():

    s = """
    label
      type truetype size 8 font "default"
    end
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert(d["type"] == "truetype")
    assert(d["size"] == 8)
    assert(d["font"] == "default")
    assert(d["__type__"] == "label")
Esempio n. 31
0
def test_points():

    s = """
    FEATURE
        POINTS 1 1 50 50 1 50 1 1 END
        POINTS 100 100 50 50 100 50 100 100 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["points"]) == 2)
    assert(len(d["points"][0]) == 4)
    assert(len(d["points"][0][0]) == 2)
    assert(d["points"][0][0][0] == 1)
Esempio n. 32
0
def test_config_directive():

    s = """
    MAP
        NAME 'ConfigMap'
        CONFIG MS_ERRORFILE "stderr"
        CONFIG "PROJ_DEBUG" "OFF"
        CONFIG "ON_MISSING_DATA" "IGNORE"
    END
    """

    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert (len(d["config"]) == 3)
Esempio n. 33
0
def create_layer_diagram():
    s = """
      CLASS
        text "country name"
        NAME "test1"
        STYLE
            COLOR 180 180 180
        END
      END
    """

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

    of = './docs/images/class_parsed.png'

    ast.to_png_with_pydot(of)
Esempio n. 34
0
def test_points():

    s = """
    FEATURE
        POINTS 1 1 50 50 1 50 1 1 END
        POINTS 100 100 50 50 100 50 100 100 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["points"]) == 2)
    assert (len(d["points"][0]) == 4)
    assert (len(d["points"][0][0]) == 2)
    assert (d["points"][0][0][0] == 1)
Esempio n. 35
0
def open(fn, expand_includes=True, include_comments=False, include_position=False):
    """
    Load a Mapfile from the supplied filename into a Python dictionary

    :param string fn: The path to the Mapfile, or partial Mapfile
    :param boolean expand_includes: Load any ``INCLUDE`` files in the MapFile
    :param boolean include_comments: Include or discard comment strings from
                                     the Mapfile - *experimental*
    :param boolean include_position: Include the position of the Mapfile tokens in the output
    """
    p = Parser(expand_includes=expand_includes,
               include_comments=include_comments)
    ast = p.parse_file(fn)
    m = MapfileToDict(include_position=include_position,
                      include_comments=include_comments)
    d = m.transform(ast)
    return d
Esempio n. 36
0
def test_config_directive():

    s = """
    MAP
        NAME 'ConfigMap'
        CONFIG MS_ERRORFILE "stderr"
        CONFIG "PROJ_DEBUG" "OFF"
        CONFIG "ON_MISSING_DATA" "IGNORE"
    END
    """

    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert(len(d["config"]) == 3)
Esempio n. 37
0
def loads(s,
          expand_includes=True,
          include_position=False,
          include_comments=False,
          **kwargs):
    """
    Load a Mapfile from a string

    Parameters
    ----------

    s: string
        The Mapfile, or partial Mapfile, text
    expand_includes: boolean
        Load any ``INCLUDE`` files in the MapFile
    include_comments: boolean
         Include or discard comment strings from the Mapfile - *experimental*
    include_position: boolean
         Include the position of the Mapfile tokens in the output

    Returns
    -------

    dict
        A Python dictionary representing the Mapfile in the mappyfile format

    Example
    -------

    To open a Mapfile from a string and return it as a dictionary object::

        s = '''MAP NAME "TEST" END'''

        d = mappyfile.loads(s)
        assert d["name"] == "TEST"

    """
    p = Parser(expand_includes=expand_includes,
               include_comments=include_comments,
               **kwargs)
    ast = p.parse(s)
    m = MapfileToDict(include_position=include_position,
                      include_comments=include_comments,
                      **kwargs)
    d = m.transform(ast)
    return d
Esempio n. 38
0
def loads(s, expand_includes=True, include_position=False, include_comments=False):
    """
    Load a Mapfile from a string

    :param string s: The Mapfile, or partial Mapfile, text
    :param boolean expand_includes: Load any ``INCLUDE`` files in the MapFile
    :param boolean include_comments: Include or discard comment strings from
                                     the Mapfile - *experimental*
    :param boolean include_position: Include the position of the Mapfile tokens in the output
    """
    p = Parser(expand_includes=expand_includes,
               include_comments=include_comments)
    ast = p.parse(s)
    m = MapfileToDict(include_position=include_position,
                      include_comments=include_comments)
    d = m.transform(ast)
    return d
Esempio n. 39
0
def test_extra_end():
    """
    Check an extra end keyword throws an error
    """
    s = """MAP
NAME "test"
END
END"""

    p = Parser()
    try:
        p.parse(s)
    except UnexpectedToken as ex:
        print(ex.__dict__)
        assert (ex.line == 4)
        assert (ex.column == 0)
        assert (str(ex.token) == 'END')
Esempio n. 40
0
def load(fp, expand_includes=True, include_position=False, include_comments=False):
    """
    Load a Mapfile from a file-like object

    :param fp: A file-like object
    :param boolean expand_includes: Load any ``INCLUDE`` files in the MapFile
    :param boolean include_comments: Include or discard comment strings from
                                     the Mapfile - *experimental*
    :param boolean include_position: Include the position of the Mapfile tokens in the output
    """
    p = Parser(expand_includes=expand_includes,
               include_comments=include_comments)
    ast = p.load(fp)
    m = MapfileToDict(include_position=include_position,
                      include_comments=include_comments)
    d = m.transform(ast)
    return d
Esempio n. 41
0
def test_processing_directive():

    s = """
    LAYER
        NAME 'ProcessingLayer'
        PROCESSING 'BANDS=1'
        PROCESSING 'CONTOUR_ITEM=elevation'
        PROCESSING 'CONTOUR_INTERVAL=20'
    END
    """

    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert(len(d["processing"]) == 3)
Esempio n. 42
0
def create_layer_diagram():
    s = """
      CLASS
        text "country name"
        NAME "test1"
        STYLE
            COLOR 180 180 180
        END
      END
    """

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

    of = './docs/images/class_parsed.png'

    ast.to_png_with_pydot(of)
Esempio n. 43
0
def test_processing_directive():

    s = """
    LAYER
        NAME 'ProcessingLayer'
        PROCESSING 'BANDS=1'
        PROCESSING 'CONTOUR_ITEM=elevation'
        PROCESSING 'CONTOUR_INTERVAL=20'
    END
    """

    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert (len(d["processing"]) == 3)
Esempio n. 44
0
def output():
    """
    Parse, transform, and pretty print 
    the result
    """
    p = Parser()
    m = MapfileToDict()

    fn = r"D:\Temp\large.map"
    with open(fn) as f:
        s = f.read()

    ast = p.parse(s)
    ##print(ast)
    d = m.transform(ast)
    ##print(d)
    pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'")
    pp.pprint(d)
Esempio n. 45
0
def test_all_maps():

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

    p = Parser(expand_includes=False)

    failing_maps = []

    for fn in os.listdir(sample_dir):
        print(fn)
        try:
            p.parse_file(os.path.join(sample_dir, fn))
        except (BaseException, UnexpectedToken) as ex:
            logging.warning("Cannot process %s ", fn)
            logging.error(ex)
            failing_maps.append(fn)

    logging.warning(failing_maps)
Esempio n. 46
0
def output(s):
    """
    Parse, transform, and pretty print
    the result
    """
    p = Parser()
    m = MapfileToDict(include_position=True)

    # 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))
    pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'")
    s = pp.pprint(d)
    logging.debug(s)
    return s
Esempio n. 47
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
Esempio n. 48
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
Esempio n. 49
0
def test_metadata():

    s = """
    MAP
        METADATA
            "wms_enable_request" "*"
            "MS_ENABLE_MODES" "!*"
        END
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert (d["metadata"]["wms_enable_request"] == "*")
    assert (d["metadata"]["MS_ENABLE_MODES"] == "!*")
    assert (d["metadata"]["wms_ENABLE_request"] == "*")
    assert (d["metadata"]["MS_enable_MODES"] == "!*")
Esempio n. 50
0
def test_metadata():

    s = """
    MAP
        METADATA
            "wms_enable_request" "*"
            "MS_ENABLE_MODES" "!*"
        END
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert(d["metadata"]["wms_enable_request"] == "*")
    assert(d["metadata"]["MS_ENABLE_MODES"] == "!*")
    assert(d["metadata"]["wms_ENABLE_request"] == "*")
    assert(d["metadata"]["MS_enable_MODES"] == "!*")
Esempio n. 51
0
def output(s):
    """
    Parse, transform, and pretty print
    the result
    """
    p = Parser()
    m = MapfileToDict(include_position=True)

    # 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))
    pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'")
    s = pp.pprint(d)
    logging.debug(s)
    return s
Esempio n. 52
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))
Esempio n. 53
0
def test_missing_end():
    """
    Check an invalid keyword throws a schema validation
    error
    """
    s = """MAP
LAYER
NAME "Test"
LAYER
NAME "Test2"
END
END"""

    p = Parser()
    try:
        p.parse(s)
    except UnexpectedToken as ex:
        assert (ex.line == 7)
        assert (ex.column == 1)
        assert (ex.token.type == "$END")
Esempio n. 54
0
def test_empty_config_directive():
    """
    Check that a config dict can be added directly without
    needing to create a new dict separately
    """

    s = """
    MAP
        NAME 'ConfigMap'
    END
    """

    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    d["config"]["ms_errorfile"] = "stderr"
    print(json.dumps(d, indent=4))
    assert (d["config"]["ms_errorfile"] == "stderr")
    assert (d["config"]["MS_ERRORFILE"] == "stderr")
Esempio n. 55
0
def test_empty_config_directive():
    """
    Check that a config dict can be added directly without
    needing to create a new dict separately
    """

    s = """
    MAP
        NAME 'ConfigMap'
    END
    """

    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    d["config"]["ms_errorfile"] = "stderr"
    print(json.dumps(d, indent=4))
    assert(d["config"]["ms_errorfile"] == "stderr")
    assert(d["config"]["MS_ERRORFILE"] == "stderr")
Esempio n. 56
0
def test_header_comment2():
    s = """
    # Map comment 1
    # Map comment 2
    MAP
        # comment 2
        NAME "Test" # name comment
        # post name comment
    END"""

    p = Parser(include_comments=True)
    ast = p.parse(s)
    print(p._comments)
    print(ast.pretty())
    m = MapfileToDict(include_position=True, include_comments=True)
    d = m.transform(ast)
    print(json.dumps(d, indent=4))

    pp = PrettyPrinter(indent=4, quote="'", newlinechar="\n")
    s = pp.pprint(d)
    print(s)
Esempio n. 57
0
def test_projection():

    s = """
    MAP
        PROJECTION
            "proj=utm"
            "ellps=GRS80"
            "datum=NAD83"
            "zone=15"
            "units=m"
            "north"
            "no_defs"
        END
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(json.dumps(d, indent=4))
    assert(len(d["projection"]) == 7)
Esempio n. 58
0
def test_expression():

    s = """
    CLASS
        TEXT ([area])
        EXPRESSION ([area])
    END
    CLASS
        TEXT ("[area]")
        EXPRESSION ("[area]")
    END
    """
    p = Parser()
    ast = p.parse(s)
    t = MapfileToDict()
    d = t.transform(ast)
    print(d)
    print(json.dumps(d, indent=4))
    assert(d[0]["text"] == "([area])")
    assert(d[0]["expression"] == "([area])")
    assert(d[1]["text"] == "(\"[area]\")")
    assert(d[1]["expression"] == "(\"[area]\")")
Esempio n. 59
0
def test_scaletoken():

    s = """
    SCALETOKEN
        NAME "%border%"
        VALUES
            "0" "ON"
            "255000000" "OFF"
        END
    END
    """

    p = Parser()
    ast = p.parse(s)
    print(ast.pretty())
    t = MapfileToDict()
    d = t.transform(ast)
    print(d)
    print(json.dumps(d, indent=4))
    # print(dict(d["metadata"]))
    assert(d["__type__"] == "scaletoken")
    assert(d["values"]["0"] == "ON")