Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow partial Mapfiles classes to be tested #155

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
17 changes: 15 additions & 2 deletions mappyfile/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ def format(ctx, input_mapfile, output_mapfile, indent, spacer, quote, newlinecha
@click.argument('mapfiles', nargs=-1, type=click.Path())
@click.option('--expand/--no-expand', default=True, show_default=True, help="Expand any INCLUDE directives found in the Mapfile") # noqa
@click.option('--version', default=7.6, show_default=True, help="The MapServer version number used to validate the Mapfile") # noqa
@click.option('--partials', default=False, show_default=True, help="Allow validation of partial Mapfiles, for example a file containing a LAYER definition") # noqa
@click.pass_context
def validate(ctx, mapfiles, expand, version):
def validate(ctx, mapfiles, expand, version, partials):
"""
Validate Mapfile(s) against the Mapfile schema

Expand Down Expand Up @@ -157,7 +158,19 @@ def validate(ctx, mapfiles, expand, version):
click.echo("{} failed to parse successfully".format(fn))
continue

validation_messages = mappyfile.validate(d, version)
if partials is True:
# handle files which contain individual Mapfile classes such as LAYER, CLASS etc.
if isinstance(d, dict):
schema_name = d.get("__type__", "map")
validation_messages = mappyfile.validate(d, schema_name=schema_name, version=version)
elif isinstance(d, list):
validation_messages = []
for v in d:
schema_name = v.get("__type__", "map")
validation_messages += mappyfile.validate(v, schema_name=schema_name, version=version)
else:
validation_messages = mappyfile.validate(d, schema_name="map", version=version)

if validation_messages:
for v in validation_messages:
v["fn"] = fn
Expand Down
8 changes: 6 additions & 2 deletions mappyfile/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,11 +613,13 @@ def update(d1, d2):
return d1


def validate(d, version=None):
def validate(d, schema_name="map", 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.
A schema name can also be set so individual layers, classes, and
styles can be validated indepentally from a map.

Parameters
----------
Expand All @@ -626,6 +628,8 @@ def validate(d, version=None):
A Python dictionary based on the the mappyfile schema
version: float
The MapServer version number used to validate the Mapfile
schema_name: string
The MapServer schema to use for validating the dict e.g. "map", "layer", "class"

Returns
-------
Expand All @@ -635,7 +639,7 @@ def validate(d, version=None):

"""
v = Validator()
return v.validate(d, version=version)
return v.validate(d, schema_name=schema_name, version=version)


def _save(output_file, string):
Expand Down