Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
flake8 .
- name: Check types with mypy
run: |
mypy mappyfile tests docs/examples
mypy mappyfile tests docs/examples --check-untyped-defs
- name: Command line tests
run: |
mappyfile schema mapfile-schema.json
Expand Down
7 changes: 5 additions & 2 deletions docs/examples/animation/animated_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
def create_frame(mapfile, line, dist):
# get the polygon layer
pl = mappyfile.find(mapfile["layers"], "name", "polygon")
assert pl is not None
# buffer the line
dilated = line.buffer(dist, cap_style=3)
# now set the FEATURES in the Mapfile to be the WKT from Shapely
pl["features"][0]["wkt"] = "'%s'" % dilated.wkt
# create an image from this Mapfile
return create_image("animation_%s" % str(dist), mapfile, format="gif")
return create_image(
"animation_%s" % str(dist), mapfile, output_folder="", format="gif"
)


def create_frames(mapfile):
Expand Down Expand Up @@ -74,7 +77,7 @@ def create_animation(img_files):

def main():
mf = "./docs/examples/animation/animated_buffer.map"
mapfile = mappyfile.load(mf)
mapfile = mappyfile.open(mf)
img_files = create_frames(mapfile)
create_animation(img_files)

Expand Down
1 change: 1 addition & 0 deletions docs/examples/api/adding_values_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_class():
# START OF ADD CLASS EXAMPLE
# find a layer using its name
layer = mappyfile.find(mapfile["layers"], "name", "sea")
assert layer is not None

new_class_string = """
CLASS
Expand Down
2 changes: 2 additions & 0 deletions docs/examples/api/search_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ def test():

# search for a layer by name
layer = mappyfile.find(mapfile["layers"], "name", "sea")
assert layer is not None
print(layer["name"]) # "sea"

# search for all layers in a group
for layer in mappyfile.findall(mapfile["layers"], "group", "my_group"):
assert layer is not None
print(layer["name"])

# END OF API EXAMPLE
Expand Down
4 changes: 4 additions & 0 deletions docs/examples/geometry/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
def dilation(mapfile):
line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)])
ll = mappyfile.find(mapfile["layers"], "name", "line")
assert ll is not None
ll["features"][0]["wkt"] = line.wkt

dilated = line.buffer(0.5, cap_style=3)
pl = mappyfile.find(mapfile["layers"], "name", "polygon")
assert pl is not None
pl["features"][0]["wkt"] = dilated.wkt

mapfile["extent"] = " ".join(map(str, dilated.buffer(0.8).bounds))
Expand All @@ -28,9 +30,11 @@ def erosion(mapfile, dilated):
If we wanted to start from scratch we could simply reread it
"""
ll = mappyfile.find(mapfile["layers"], "name", "line")
assert ll is not None
ll["status"] = "OFF"

pl = mappyfile.find(mapfile["layers"], "name", "polygon")
assert pl is not None

# make a deep copy of the polygon layer in the Map
# so any modification are made to this layer only
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def _create_image_from_map(map_file, out_img, format):
os.environ["PATH"] = DLL_LOCATION + ";" + os.environ["PATH"]

p = Popen(params, stdout=PIPE, bufsize=1)
assert p.stdout is not None
with p.stdout:
for line in iter(p.stdout.readline, b""):
print(line)

p.wait() # wait for the subprocess to exit

# os.startfile(out_img)
return out_img
15 changes: 14 additions & 1 deletion docs/examples/image_from_mapfile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import subprocess
from subprocess import Popen, PIPE
import tempfile
import logging
import sys
import os


def open_file(path: str):
if sys.platform == "win32":
os.startfile(path) # subprocess not needed here
elif sys.platform == "darwin":
subprocess.run(["open", path], check=True)
else:
subprocess.run(["xdg-open", path], check=True)


def create_image_from_map(map_file, dll_location):
of = tempfile.NamedTemporaryFile(delete=False, suffix=".png", prefix="tmp_")
of.close()
Expand All @@ -18,14 +29,16 @@ def create_image_from_map(map_file, dll_location):
logging.debug(" ".join(params))

p = Popen(params, stdout=PIPE, bufsize=1)
assert p.stdout is not None

with p.stdout:
print(map_file)
for line in iter(p.stdout.readline, b""):
print(line)

p.wait() # wait for the subprocess to exit

os.startfile(of.name)
open_file(of.name)


if __name__ == "__main__":
Expand Down
2 changes: 0 additions & 2 deletions docs/scripts/class_diagrams.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
python class_diagrams.py
"""

import os
import pydot

FONT = "Lucida Sans"
Expand Down Expand Up @@ -59,7 +58,6 @@ def save_file(graph, fn):
filename = "%s.png" % fn
graph.write_png(filename)
graph.write("%s.dot" % fn)
os.startfile(filename)


def layer_children():
Expand Down
2 changes: 1 addition & 1 deletion mappyfile/dictutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def findunique(lst, key):
)


def findkey(d: dict, *keys: list[Any]) -> dict:
def findkey(d: dict, *keys: Any) -> dict:
"""
Get a value from a dictionary based on a list of keys and/or list indexes.

Expand Down
10 changes: 6 additions & 4 deletions mappyfile/ordereddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
# =================================================================

from collections import OrderedDict
from typing import Any
import copy
from mappyfile.tokens import OBJECT_LIST_KEYS
import json
Expand Down Expand Up @@ -138,10 +139,11 @@ def setdefault(self, key, *args, **kwargs):
# pylint: disable=protected-access
return super().setdefault(self.__class__._k(key), *args, **kwargs)

def update(self, e=None, **f):
if e is not None:
super().update(self.__class__(CaseInsensitiveOrderedDict, e))
super().update(self.__class__(CaseInsensitiveOrderedDict, **f))
def update(self, *args: Any, **kwargs: Any) -> None:
for arg in args:
super().update(self.__class__(CaseInsensitiveOrderedDict, arg))
if kwargs:
super().update(self.__class__(CaseInsensitiveOrderedDict, **kwargs))

def _convert_keys(self):
for k in list(self.keys()):
Expand Down
3 changes: 2 additions & 1 deletion mappyfile/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def get_schema_file(self, schema_name: str) -> str:
def get_json_from_file(self, schema_name: str):
if schema_name not in self.schemas:
schema_file = self.get_schema_file(schema_name)

with open(schema_file, encoding="utf-8") as f:
try:
jsn_schema = json.load(f)
Expand Down Expand Up @@ -197,7 +198,7 @@ def convert_lowercase(self, x: Any) -> Any:
return x

def create_message(
self, rootdict: dict, path: str, error, add_comments: bool
self, rootdict: dict, path: list[Any], error, add_comments: bool
) -> dict:
"""
Add a validation comment to the dictionary
Expand Down
2 changes: 1 addition & 1 deletion misc/docs_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
https://dzone.com/articles/a-brief-tutorial-on-parsing-restructuredtext-rest
https://github.com/eliben/code-for-blog/blob/master/2017/parsing-rst/rst-link-check.py

The root cause of the problem is indeed the fact that registering new roles/directives affects docutils
The root cause of the problem is indeed the fact that registering new roles/directives affects docutils
globally. I don't think this can be easily solved.

https://github.com/sphinx-doc/sphinx/issues/2799
Expand Down
2 changes: 1 addition & 1 deletion run_local.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ flake8 .
# mypy --install-types
# to check within functions missing types
# mypy mappyfile tests --check-untyped-defs
mypy mappyfile tests
mypy mappyfile tests docs/examples --check-untyped-defs

pytest --doctest-modules
# pytest ./tests
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ def test_parser_validation():
END
END
"""
with pytest.raises(UnexpectedToken) as e:
with pytest.raises(UnexpectedToken):
p.parse(config_text_bad2)
6 changes: 5 additions & 1 deletion tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def _create_image_from_map(map_file, out_img, format):
return None
else:
logging.info("Created %s", out_img)
os.startfile(out_img)
# os.startfile is only available on Windows
# os.startfile(out_img)

return out_img

Expand Down Expand Up @@ -325,6 +326,7 @@ def test_deref():
schema_name = "cluster"
validator = v.get_schema_validator(schema_name)
jsn_schema = validator.schema
assert isinstance(jsn_schema, dict)

print(json.dumps(jsn_schema, indent=4))
print(jsn_schema["properties"]["filter"])
Expand All @@ -345,11 +347,13 @@ def test_cached_schema():
schema_name = "cluster"
validator = v.get_schema_validator(schema_name)
jsn_schema = validator.schema
assert isinstance(jsn_schema, dict)
assert list(jsn_schema["properties"]["filter"].keys())[0] == "$ref"

# get the schame again
validator = v.get_schema_validator(schema_name)
jsn_schema = validator.schema
assert isinstance(jsn_schema, dict)
assert list(jsn_schema["properties"]["filter"].keys())[0] == "$ref"


Expand Down