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)
def test_single_string_projection(): d = {"projection": "init=epsg:4326", "__type__": "map"} pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert (s == "MAP PROJECTION 'init=epsg:4326' END END")
def test_format_list(): s = """ CLASS STYLE COLOR 173 216 230 END STYLE OUTLINECOLOR 2 2 2 WIDTH 1 LINECAP BUTT PATTERN 5 5 10 10 END END END """ ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0) # expected k = "pattern" lst = [[5, 5, 10, 10]] assert (pp.is_paired_list(k)) r = pp.process_list(k, lst, 0) exp = [u'PATTERN', '5 5\n10 10', u'END'] assert (r == exp)
def test_comment_parsing(): s = """ # Map comment 1 # Map comment 2 MAP NAME 'Test' # Name comment # Layer comment LAYER TYPE POLYGON # This is a polygon! END END""" p = Parser(include_comments=True) m = MapfileToDict(include_position=False, include_comments=True) ast = p.parse(s) print(p._comments) assert len(p._comments) == 5 print(ast.pretty()) d = m.transform(ast) # transform the rest print(json.dumps(d, indent=4)) pp = PrettyPrinter(indent=0, quote="'", newlinechar="\n") s = pp.pprint(d) print(s)
def test_points(): d = { "points": [[[1, 1], [50, 50], [1, 50], [1, 1]], [[100, 100], [50, 50], [100, 50], [100, 100]]], "__type__": "feature" } pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert(s == "FEATURE POINTS 1 1 50 50 1 50 1 1 END POINTS 100 100 50 50 100 50 100 100 END END")
def test_style_pattern(): d = {"pattern": [[10, 1], [50, 50], [1, 50], [1, 1]], "__type__": "style"} pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) assert (s == "STYLE PATTERN 10 1 50 50 1 50 1 1 END END")
def test_auto_projection(): d = {"projection": ["auto"], "__type__": "map"} pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert (s == "MAP PROJECTION AUTO END END")
def test_print_boolean(): d = {"transform": True, "__type__": "layer"} pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert (s == "LAYER TRANSFORM TRUE END")
def _pprint(d, indent, spacer, quote, newlinechar, end_comment, **kwargs): pp = PrettyPrinter(indent=indent, spacer=spacer, quote=quote, newlinechar=newlinechar, end_comment=end_comment, **kwargs) return pp.pprint(d)
def test_print_boolean(): d = { "transform": True, "__type__": "layer" } pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert(s == "LAYER TRANSFORM TRUE END")
def test_auto_projection(): d = { "projection": ["auto"], "__type__": "map" } pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert(s == "MAP PROJECTION AUTO END END")
def _pprint(d, indent, spacer, quote, newlinechar, end_comment, align_values, separate_complex_types, **kwargs): pp = PrettyPrinter(indent=indent, spacer=spacer, quote=quote, newlinechar=newlinechar, end_comment=end_comment, align_values=align_values, separate_complex_types=separate_complex_types, **kwargs) return pp.pprint(d)
def test_comment(): d = collections.OrderedDict() d["name"] = "Test" d["__type__"] = "layer" d["__comments__"] = {"name": "Test comment"} print(json.dumps(d, indent=4)) pp = PrettyPrinter(indent=0, quote="'", newlinechar="\n") s = pp.pprint(d) exp = """LAYER NAME 'Test' # Test comment END""" assert(s == exp)
def test_comment(): d = collections.OrderedDict() d["name"] = "Test" d["__type__"] = "layer" d["__comments__"] = {"name": "# Test comment"} print(json.dumps(d, indent=4)) pp = PrettyPrinter(indent=0, quote="'", newlinechar="\n") s = pp.pprint(d) exp = """LAYER NAME 'Test' # Test comment END""" print(s) assert(s == exp)
def test_end_comment(): s = "MAP LAYER TYPE POINT NAME 'Test' END END" ast = mappyfile.loads(s) pp = PrettyPrinter(indent=4, quote='"', newlinechar="\n", end_comment=True) res = pp.pprint(ast) print(res) exp = """MAP LAYER TYPE POINT NAME "Test" END # LAYER END # MAP""" assert res == exp
def test_config(): s = """ MAP config "MS_ERRORFILE" "my.log" END """ ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") res = pp.pprint(ast) #print(res) exp = u"MAP CONFIG 'MS_ERRORFILE' 'my.log' END" assert (res == exp)
def test_connectionoptions(): values = {"FLATTEN_NESTED_ATTRIBUTES": "YES"} d = {"connectionoptions": values, "__type__": "layer"} d = collections.OrderedDict(sorted(d.items())) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert ( s == "LAYER CONNECTIONOPTIONS 'FLATTEN_NESTED_ATTRIBUTES' 'YES' END END")
def test_join(): d = { "__type__": "layer", "name": "Joined", "joins": [{ "__type__": "join", "name": "table_join" }] } pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") d = collections.OrderedDict(sorted(d.items())) s = pp.pprint(d) assert (s == "LAYER JOIN NAME 'table_join' END NAME 'Joined' END")
def test_unicode(): s = u""" MAP METADATA "ows_title" "éúáí" END END """ ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") res = pp.pprint(ast) exp = u"MAP METADATA 'ows_title' 'éúáí' END END" assert (res == exp)
def test_unicode(): s = u""" MAP METADATA "ows_title" "éúáí" END END """ ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") res = pp.pprint(ast) exp = u"MAP METADATA 'ows_title' 'éúáí' END END" assert(res == exp)
def test_processing(): d = { "name": "ProcessingLayer", "processing": ["BANDS=1", "CONTOUR_ITEM=elevation", "CONTOUR_INTERVAL=20"], "__type__": "layer" } d = collections.OrderedDict(sorted(d.items())) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert(s == "LAYER NAME 'ProcessingLayer' PROCESSING 'BANDS=1' PROCESSING 'CONTOUR_ITEM=elevation' PROCESSING 'CONTOUR_INTERVAL=20' END")
def test_two_includes(): s = """ MAP INCLUDE "include1.txt" INCLUDE "include2.txt" END """ d = mappyfile.loads(s, expand_includes=False) logging.debug(json.dumps(d, indent=4)) pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'") output = pp.pprint(d) print(output) expected = "MAP INCLUDE 'include1.txt' INCLUDE 'include2.txt' END" assert(output == expected)
def test_two_includes(): s = """ MAP INCLUDE "include1.txt" INCLUDE "include2.txt" END """ d = mappyfile.loads(s, expand_includes=False) logging.debug(json.dumps(d, indent=4)) pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'") output = pp.pprint(d) print(output) expected = "MAP INCLUDE 'include1.txt' INCLUDE 'include2.txt' END" assert (output == expected)
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)
def test_metadata(): md = {"MS_ENABLE_MODES": "!*", "WMS_ENABLE_REQUEST": "*"} md = collections.OrderedDict(sorted(md.items())) d = {"metadata": md, "__type__": "map"} d = collections.OrderedDict(sorted(d.items())) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert ( s == "MAP METADATA 'MS_ENABLE_MODES' '!*' 'WMS_ENABLE_REQUEST' '*' END END")
def test_projection(): d = { "projection": ["proj=utm", "ellps=GRS80", "datum=NAD83", "zone=15", "units=m", "north", "no_defs"], "__type__": "map" } pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert(s == "MAP PROJECTION 'proj=utm' 'ellps=GRS80' 'datum=NAD83' 'zone=15' 'units=m' 'north' 'no_defs' END END")
def test_scaletoken(): sd = {"0": "ON", "255000000": "OFF"} sd = collections.OrderedDict(sorted(sd.items())) d = {"name": "%border%", "values": sd, "__type__": "scaletoken"} d = collections.OrderedDict(sorted(d.items())) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert ( s == "SCALETOKEN NAME '%border%' VALUES '0' 'ON' '255000000' 'OFF' END END")
def test_includes_no_expand(): """ https://github.com/geographika/mappyfile/issues/39 """ s = """ MAP INCLUDE "includes/mymapfile.map" END """ d = mappyfile.loads(s, expand_includes=False) pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'") output = pp.pprint(d) expected = "MAP INCLUDE 'includes/mymapfile.map' END" assert (output == expected)
def test_includes_no_expand(): """ https://github.com/geographika/mappyfile/issues/39 """ s = """ MAP INCLUDE "includes/mymapfile.map" END """ d = mappyfile.loads(s, expand_includes=False) pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'") output = pp.pprint(d) expected = "MAP INCLUDE 'includes/mymapfile.map' END" assert(output == expected)
def test_config(): cd = {"ms_nonsquare": "YES", "on_missing_data": "FAIL"} cd = collections.OrderedDict(sorted(cd.items())) d = {"config": cd, "__type__": "map"} d = collections.OrderedDict(sorted(d.items())) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert ( s == "MAP CONFIG 'MS_NONSQUARE' 'YES' CONFIG 'ON_MISSING_DATA' 'FAIL' END")
def test_multiple_layers(): d = { "layers": [{ "name": "Layer1", "__type__": "layer" }, { "name": "Layer2", "__type__": "layer" }], "__type__": "map" } pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert (s == "MAP LAYER NAME 'Layer1' END LAYER NAME 'Layer2' END END")
def test_multiple_layers(): d = { "layers": [{ "name": "Layer1", "__type__": "layer" }, { "name": "Layer2", "__type__": "layer" }], "__type__": "map" } pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert(s == "MAP LAYER NAME 'Layer1' END LAYER NAME 'Layer2' END END")
def test_include_from_string(): """ Check that a file is correctly included when parsing text and that the current working directory is used as the root path for includes - see https://github.com/geographika/mappyfile/issues/55 """ s = """ MAP INCLUDE "tests/samples/include3.map" END """ d = mappyfile.loads(s, expand_includes=True) pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'") output = pp.pprint(d) expected = "MAP NAME 'test' END" assert(output == expected)
def test_header_list_comments(): """ __type__ is used as the key for any object-level comments """ d = collections.OrderedDict() d["name"] = "Test" d["__type__"] = "layer" d["__comments__"] = {"__type__": ["# Layer comment 1", "# Layer comment 2"]} print(json.dumps(d, indent=4)) pp = PrettyPrinter(indent=0, quote="'", newlinechar="\n") s = pp.pprint(d) exp = """# Layer comment 1 # Layer comment 2 LAYER NAME 'Test' END""" assert(s == exp)
def test_style_pattern(): d = { "pattern": [[10, 1], [50, 50], [1, 50], [1, 1]], "__type__": "style" } pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) assert(s == "STYLE PATTERN 10 1 50 50 1 50 1 1 END END")
def test_header_list_comments(): """ __type__ is used as the key for any object-level comments """ d = collections.OrderedDict() d["name"] = "Test" d["__type__"] = "layer" d["__comments__"] = {"__type__": ["Layer comment 1", "Layer comment 2"]} print(json.dumps(d, indent=4)) pp = PrettyPrinter(indent=0, quote="'", newlinechar="\n") s = pp.pprint(d) exp = """# Layer comment 1 # Layer comment 2 LAYER NAME 'Test' END""" assert(s == exp)
def test_already_escaped(): """ Don't escape an already escaped quote """ s = r'CLASS EXPRESSION "\"Tignish" END' ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote='"', newlinechar=" ") res = pp.pprint(ast) exp = r'CLASS EXPRESSION "\"Tignish" END' assert(res == exp) s = r"CLASS EXPRESSION '\'Tignish' END" ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") res = pp.pprint(ast) exp = r"CLASS EXPRESSION '\'Tignish' END" assert(res == exp)
def test_double_comment(): d = collections.OrderedDict() d["name"] = "Test" d["__type__"] = "layer" d["__comments__"] = {"name": "Name comment", "type": "Type comment"} d["type"] = "polygon" print(json.dumps(d, indent=4)) pp = PrettyPrinter(indent=0, quote="'", newlinechar="\n") s = pp.pprint(d) exp = """LAYER NAME 'Test' # Name comment TYPE POLYGON # Type comment END""" assert(s == exp)
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)
def test_double_comment(): d = collections.OrderedDict() d["name"] = "Test" d["__type__"] = "layer" d["__comments__"] = {"name": "# Name comment", "type": "# Type comment"} d["type"] = "polygon" print(json.dumps(d, indent=4)) pp = PrettyPrinter(indent=0, quote="'", newlinechar="\n") s = pp.pprint(d) exp = """LAYER NAME 'Test' # Name comment TYPE POLYGON # Type comment END""" print(s) assert(s == exp)
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
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
def test_nested_quotes(): """ If values contain quotes then make sure they are escaped shp2img -m C:\Temp\msautotest\misc\ogr_vrtconnect.tmp.map """ s = """ LAYER NAME shppoly TYPE polygon CONNECTIONTYPE OGR CONNECTION '<OGRVRTDataSource><OGRVRTLayer name="poly"><SrcDataSource relativeToVRT="0">data/shppoly</SrcDataSource><SrcLayer>poly</SrcLayer></OGRVRTLayer></OGRVRTDataSource>' END""" ast = mappyfile.loads(s) pp = PrettyPrinter(indent=0, quote='"', newlinechar=" ") # expected res = pp.pprint(ast) print(res) exp = 'LAYER NAME "shppoly" TYPE POLYGON CONNECTIONTYPE OGR CONNECTION "<OGRVRTDataSource><OGRVRTLayer name="poly">' \ '<SrcDataSource relativeToVRT="0">data/shppoly</SrcDataSource><SrcLayer>poly</SrcLayer></OGRVRTLayer></OGRVRTDataSource>" END' assert(res == exp)
def test_metadata(): md = { "MS_ENABLE_MODES": "!*", "WMS_ENABLE_REQUEST": "*" } md = collections.OrderedDict(sorted(md.items())) d = { "metadata": md, "__type__": "map" } d = collections.OrderedDict(sorted(d.items())) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert(s == "MAP METADATA 'MS_ENABLE_MODES' '!*' 'WMS_ENABLE_REQUEST' '*' END END")
def test_config(): cd = { "ms_nonsquare": "YES", "on_missing_data": "FAIL" } cd = collections.OrderedDict(sorted(cd.items())) d = { "config": cd, "__type__": "map" } d = collections.OrderedDict(sorted(d.items())) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert(s == "MAP CONFIG 'MS_NONSQUARE' 'YES' CONFIG 'ON_MISSING_DATA' 'FAIL' END")
def test_scaletoken(): sd = { "0": "ON", "255000000": "OFF" } sd = collections.OrderedDict(sorted(sd.items())) d = { "name": "%border%", "values": sd, "__type__": "scaletoken" } d = collections.OrderedDict(sorted(d.items())) pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(d) print(s) assert(s == "SCALETOKEN NAME '%border%' VALUES '0' 'ON' '255000000' 'OFF' END END")
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)
def test_example_comment_dict(): d = { "__type__": "map", "__comments__": { "__type__": ["# Map comment 1", "# Map comment 2"] }, "name": "Test", "layers": [{ "__type__": "layer", "__comments__": { "__type__": "# Layer comment", "type": ["# This is a polygon!", "# Another comment"] }, "type": "POLYGON" }] } pp = PrettyPrinter(indent=4, quote="'", newlinechar="\n") s = pp.pprint(d) print(s)
def test_class_list(): d1 = { "text": "([area])", "expression": "([area])", "__type__": "class" } d2 = { "text": "(\"[area]\")", "expression": "(\"[area]\")", "__type__": "class" } d1 = collections.OrderedDict(sorted(d1.items())) d2 = collections.OrderedDict(sorted(d2.items())) classes = [d1, d2] pp = PrettyPrinter(indent=0, quote="'", newlinechar=" ") s = pp.pprint(classes) print(s) assert(s == 'CLASS EXPRESSION ([area]) TEXT ([area]) END CLASS EXPRESSION ("[area]") TEXT ("[area]") END')
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
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)
def test_comment_parsing(): s = """ # Map comment 1 # Map comment 2 MAP NAME 'Test' # Name comment # Layer comment LAYER TYPE POLYGON # This is a polygon! END END""" p = Parser(include_comments=True) m = MapfileToDict(include_position=False, include_comments=True) ast = p.parse(s) print(p._comments) print(ast.pretty()) d = m.transform(ast) # transform the rest print(json.dumps(d, indent=4)) pp = PrettyPrinter(indent=0, quote="'", newlinechar="\n") s = pp.pprint(d) print(s)
def _pprint(d, indent, spacer, quote, newlinechar): pp = PrettyPrinter(indent=indent, spacer=spacer, quote=quote, newlinechar=newlinechar) return pp.pprint(d)
def output(d): pp = PrettyPrinter(indent=0, newlinechar=" ", quote="'") s = pp.pprint(d) logging.debug(s)