Skip to content

Commit b1331f7

Browse files
authored
Merge branch 'IfcOpenShell:master' into enforce-header-entity-length-in-grammar
2 parents e428a06 + 4a56a2b commit b1331f7

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,14 @@ def parse(
333333
with_progress=False,
334334
with_tree=True,
335335
with_header=False,
336-
only_header=False
336+
only_header=False,
337337
):
338338
if filename:
339339
assert not filecontent
340340
filecontent = builtins.open(filename, encoding=None).read()
341341

342342
if only_header:
343-
assert with_header, "'only_header=True' requires 'with_header=True'"
343+
with_header = True
344344

345345
# Match and remove the comments
346346
p = r"/\*[\s\S]*?\*/"

__main__.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
import sys
2-
import time
2+
import json
3+
import argparse
34
from . import parse, ValidationError
45

5-
if __name__ == "__main__":
6-
args = [x for x in sys.argv[1:] if not x.startswith("-")]
7-
flags = [x for x in sys.argv[1:] if x.startswith("-")]
8-
9-
fn = args[0]
10-
start_time = time.time()
11-
6+
def main():
7+
parser = argparse.ArgumentParser(description="Parse and validate STEP file.")
8+
parser.add_argument("filename", help="The STEP file to validate.")
9+
parser.add_argument("--progress", action="store_true", help="Show progress during validation.")
10+
parser.add_argument("--json", action="store_true", help="Output errors in JSON format.")
11+
parser.add_argument("--only-header", action="store_true", help="Validate only the header section.")
12+
13+
args = parser.parse_args()
14+
1215
try:
13-
parse(filename=fn, with_progress="--progress" in flags, with_tree=False)
14-
if "--json" not in flags:
16+
parse(
17+
filename=args.filename,
18+
with_progress = args.progress,
19+
with_tree = False,
20+
only_header=args.only_header,
21+
)
22+
if not args.json:
1523
print("Valid", file=sys.stderr)
1624
exit(0)
1725
except ValidationError as exc:
18-
if "--json" not in flags:
26+
if not args.json:
1927
print(exc, file=sys.stderr)
2028
else:
21-
import sys
22-
import json
23-
2429
json.dump(exc.asdict(), sys.stdout)
2530
exit(1)
31+
32+
if __name__ == '__main__':
33+
main()

test_parser.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,24 @@ def test_file_mvd_attr():
105105
assert f.mvd.Remark['SomeKey'] == 'SomeValue'
106106
assert len(f.mvd.comments) == 2
107107
assert all(v in vars(f.header).keys() for v in ['file_description', 'file_name', 'file_schema'])
108-
assert len(f.header.file_name) == 7
108+
assert len(f.header.file_name) == 7
109+
110+
111+
@pytest.mark.parametrize("filename", [
112+
'fixtures/fail_invalid_header_entity.ifc',
113+
'fixtures/fail_no_header.ifc',
114+
])
115+
def test_invalid_headers_(filename):
116+
# error in header; with_header should raise an error
117+
with pytest.raises(ValidationError):
118+
parse(filename=filename, with_tree=False, only_header=True, with_header=True)
119+
120+
@pytest.mark.parametrize("filename", [
121+
'fixtures/fail_duplicate_id.ifc',
122+
'fixtures/fail_double_comma.ifc',
123+
'fixtures/fail_double_semi.ifc'
124+
])
125+
def test_valid_headers(filename):
126+
# error in body; with_header should not raise an error
127+
with nullcontext():
128+
parse(filename=filename, with_tree=False, only_header=True, with_header=True)

0 commit comments

Comments
 (0)