Skip to content

Commit

Permalink
Merge pull request #121 from ajnelson-nist/add_allow_info
Browse files Browse the repository at this point in the history
Add way to report non-conformance on sh:Warning but not sh:Info
  • Loading branch information
ashleysommer committed Feb 15, 2022
2 parents 0bd9883 + 9029dfc commit 72abfe4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
9 changes: 9 additions & 0 deletions pyshacl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ def __call__(self, parser, namespace, values, option_string=None):
help="Run Shape's SHACL Rules iteratively until the data_graph reaches a steady state.",
)
parser.add_argument('--abort', dest='abort', action='store_true', default=False, help='Abort on first invalid data.')
parser.add_argument(
'--allow-infos',
dest='allow_infos',
action='store_true',
default=False,
help='Shapes marked with severity of Info will not cause result to be invalid.',
)
parser.add_argument(
'-w',
'--allow-warnings',
Expand Down Expand Up @@ -177,6 +184,8 @@ def main():
validator_kwargs['iterate_rules'] = True
if args.abort:
validator_kwargs['abort_on_first'] = True
if args.allow_infos:
validator_kwargs['allow_infos'] = True
if args.allow_warnings:
validator_kwargs['allow_warnings'] = True
if args.shacl_file_format:
Expand Down
6 changes: 6 additions & 0 deletions pyshacl/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ def validate(
]
] = None,
abort_on_first: Optional[bool] = False,
allow_infos: Optional[bool] = False,
allow_warnings: Optional[bool] = False,
_evaluation_path: Optional[List] = None,
):
Expand Down Expand Up @@ -476,6 +477,11 @@ def validate(
focus_value_nodes = self.value_nodes(target_graph, focus)
filter_reports: bool = False
allow_conform: bool = False
if allow_infos:
if self.severity == SH_Info:
allow_conform = True
else:
filter_reports = True
if allow_warnings:
if self.severity in (SH_Warning, SH_Info):
allow_conform = True
Expand Down
10 changes: 9 additions & 1 deletion pyshacl/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def _load_default_options(cls, options_dict: dict):
options_dict.setdefault('use_js', False)
options_dict.setdefault('iterate_rules', False)
options_dict.setdefault('abort_on_first', False)
options_dict.setdefault('allow_infos', False)
options_dict.setdefault('allow_warnings', False)
if 'logger' not in options_dict:
options_dict['logger'] = logging.getLogger(__name__)
Expand Down Expand Up @@ -243,6 +244,7 @@ def run(self):
named_graphs = [the_target_graph]
reports = []
abort_on_first: bool = bool(self.options.get("abort_on_first", False))
allow_infos: bool = bool(self.options.get("allow_infos", False))
allow_warnings: bool = bool(self.options.get("allow_warnings", False))
non_conformant = False
aborted = False
Expand All @@ -252,7 +254,9 @@ def run(self):
apply_rules(advanced['rules'], g, iterate=iterate_rules)
try:
for s in shapes:
_is_conform, _reports = s.validate(g, abort_on_first=abort_on_first, allow_warnings=allow_warnings)
_is_conform, _reports = s.validate(
g, abort_on_first=abort_on_first, allow_infos=allow_infos, allow_warnings=allow_warnings
)
non_conformant = non_conformant or (not _is_conform)
reports.extend(_reports)
if abort_on_first and non_conformant:
Expand Down Expand Up @@ -332,6 +336,7 @@ def validate(
inference: Optional[str] = None,
inplace: Optional[bool] = False,
abort_on_first: Optional[bool] = False,
allow_infos: Optional[bool] = False,
allow_warnings: Optional[bool] = False,
**kwargs,
):
Expand All @@ -353,6 +358,8 @@ def validate(
:type inplace: bool
:param abort_on_first: Stop evaluating constraints after first violation is found
:type abort_on_first: bool | None
:param allow_infos: Shapes marked with severity of sh:Info will not cause result to be invalid.
:type allow_infos: bool | None
:param allow_warnings: Shapes marked with severity of sh:Warning or sh:Info will not cause result to be invalid.
:type allow_warnings: bool | None
:param kwargs:
Expand Down Expand Up @@ -409,6 +416,7 @@ def validate(
'inference': inference,
'inplace': inplace,
'abort_on_first': abort_on_first,
'allow_infos': allow_infos,
'allow_warnings': allow_warnings,
'advanced': advanced,
'iterate_rules': iterate_rules,
Expand Down

0 comments on commit 72abfe4

Please sign in to comment.