Skip to content

Add functionality to validate data section only #17

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

Merged
merged 5 commits into from
Jun 26, 2025
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
4 changes: 2 additions & 2 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]*?\*/"
Expand Down
36 changes: 22 additions & 14 deletions __main__.py
Original file line number Diff line number Diff line change
@@ -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()
22 changes: 21 additions & 1 deletion test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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)