diff --git a/__init__.py b/__init__.py index 133dca6..02a5d1e 100644 --- a/__init__.py +++ b/__init__.py @@ -333,14 +333,14 @@ def parse( with_progress=False, with_tree=True, with_header=False, - only_header=False + only_header=False, ): if filename: assert not filecontent filecontent = builtins.open(filename, encoding=None).read() if only_header: - assert with_header, "'only_header=True' requires 'with_header=True'" + with_header = True # Match and remove the comments p = r"/\*[\s\S]*?\*/" diff --git a/__main__.py b/__main__.py index 15c98cb..7230888 100644 --- a/__main__.py +++ b/__main__.py @@ -1,25 +1,33 @@ import sys -import time +import json +import argparse from . import parse, ValidationError -if __name__ == "__main__": - args = [x for x in sys.argv[1:] if not x.startswith("-")] - flags = [x for x in sys.argv[1:] if x.startswith("-")] - - fn = args[0] - start_time = time.time() - +def main(): + parser = argparse.ArgumentParser(description="Parse and validate STEP file.") + parser.add_argument("filename", help="The STEP file to validate.") + parser.add_argument("--progress", action="store_true", help="Show progress during validation.") + parser.add_argument("--json", action="store_true", help="Output errors in JSON format.") + parser.add_argument("--only-header", action="store_true", help="Validate only the header section.") + + args = parser.parse_args() + try: - parse(filename=fn, with_progress="--progress" in flags, with_tree=False) - if "--json" not in flags: + parse( + filename=args.filename, + with_progress = args.progress, + with_tree = False, + only_header=args.only_header, + ) + if not args.json: print("Valid", file=sys.stderr) exit(0) except ValidationError as exc: - if "--json" not in flags: + if not args.json: print(exc, file=sys.stderr) else: - import sys - import json - json.dump(exc.asdict(), sys.stdout) exit(1) + +if __name__ == '__main__': + main() diff --git a/test_parser.py b/test_parser.py index fb2565a..48f3185 100644 --- a/test_parser.py +++ b/test_parser.py @@ -105,4 +105,24 @@ def test_file_mvd_attr(): assert f.mvd.Remark['SomeKey'] == 'SomeValue' assert len(f.mvd.comments) == 2 assert all(v in vars(f.header).keys() for v in ['file_description', 'file_name', 'file_schema']) - assert len(f.header.file_name) == 7 \ No newline at end of file + assert len(f.header.file_name) == 7 + + +@pytest.mark.parametrize("filename", [ + 'fixtures/fail_invalid_header_entity.ifc', + 'fixtures/fail_no_header.ifc', +]) +def test_invalid_headers_(filename): + # error in header; with_header should raise an error + with pytest.raises(ValidationError): + parse(filename=filename, with_tree=False, only_header=True, with_header=True) + +@pytest.mark.parametrize("filename", [ + 'fixtures/fail_duplicate_id.ifc', + 'fixtures/fail_double_comma.ifc', + 'fixtures/fail_double_semi.ifc' +]) +def test_valid_headers(filename): + # error in body; with_header should not raise an error + with nullcontext(): + parse(filename=filename, with_tree=False, only_header=True, with_header=True)