Skip to content

Commit 77dfc4a

Browse files
committed
remove with_header
1 parent 083f962 commit 77dfc4a

File tree

3 files changed

+26
-46
lines changed

3 files changed

+26
-46
lines changed

__init__.py

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,6 @@ def __str__(self):
211211
"file_schema": namedtuple('file_schema', ['schema_identifiers']),
212212
}
213213

214-
215-
class Ref:
216-
def __init__(self, id):
217-
self.id = id
218-
219-
def __str__(self):
220-
return "#" + str(self.id)
221-
222-
__repr__ = __str__
223-
224-
225214
class IfcType:
226215
def __init__(self, ifctype, value):
227216
self.ifctype = ifctype
@@ -323,19 +312,20 @@ def make_header_ent(ast):
323312
params = T(visit_tokens=True).transform(ast.children[0])
324313
return rule.upper(), params
325314

315+
def validate_header_fields(header):
316+
for field in HEADER_FIELDS.keys():
317+
observed = header.get(field.upper(), [])
318+
expected = HEADER_FIELDS.get(field)._fields
319+
if len(header.get(field.upper(), [])) != len(expected):
320+
raise HeaderFieldError(field.upper(), len(observed), len(expected))
326321

327322

328-
def process_tree(filecontent, file_tree, with_progress, with_header=False):
323+
def process_tree(filecontent, file_tree, with_progress):
329324
ents = defaultdict(list)
330325
header, data = file_tree.children
331326

332-
if with_header:
333-
header = dict(map(make_header_ent, header.children[0].children))
334-
for field in HEADER_FIELDS.keys():
335-
observed = header.get(field.upper(), [])
336-
expected = HEADER_FIELDS.get(field)._fields
337-
if len(header.get(field.upper(), [])) != len(expected):
338-
raise HeaderFieldError(field.upper(), len(observed), len(expected))
327+
header = dict(map(make_header_ent, header.children[0].children))
328+
validate_header_fields(header)
339329

340330
n = len(data.children)
341331
if n:
@@ -352,10 +342,7 @@ def process_tree(filecontent, file_tree, with_progress, with_header=False):
352342
raise DuplicateNameError(filecontent, ent["id"], ent["lines"])
353343
ents[id_].append(ent)
354344

355-
if with_header:
356-
return header, ents
357-
else:
358-
return ents
345+
return header, ents
359346

360347

361348
def parse(
@@ -364,15 +351,11 @@ def parse(
364351
filecontent=None,
365352
with_progress=False,
366353
with_tree=True,
367-
with_header=False,
368354
only_header=False,
369355
):
370356
if filename:
371357
assert not filecontent
372358
filecontent = builtins.open(filename, encoding=None).read()
373-
374-
if only_header:
375-
with_header = True
376359

377360
# Match and remove the comments
378361
p = r"/\*[\s\S]*?\*/"
@@ -381,8 +364,7 @@ def replace_fn(match):
381364
return re.sub(r"[^\n]", " ", match.group(), flags=re.M)
382365

383366
filecontent_wo_comments = re.sub(p, replace_fn, filecontent)
384-
385-
367+
386368
if only_header:
387369
# Extract just the HEADER section using regex
388370
header_match = re.search(
@@ -405,11 +387,7 @@ def replace_fn(match):
405387
header_tree = ast.children[0] # HEADER section
406388

407389
header = dict(map(make_header_ent, header_tree.children[0].children))
408-
for field in HEADER_FIELDS.keys():
409-
observed = header.get(field.upper(), [])
410-
expected = HEADER_FIELDS.get(field)._fields
411-
if len(header.get(field.upper(), [])) != len(expected):
412-
raise HeaderFieldError(field.upper(), len(observed), len(expected))
390+
validate_header_fields(header)
413391
return header
414392

415393

@@ -451,9 +429,9 @@ def replace_fn(match):
451429
ast = parser.parse(filecontent_wo_comments)
452430
except (UnexpectedToken, UnexpectedCharacters) as e:
453431
raise SyntaxError(filecontent, e)
454-
432+
455433
if with_tree:
456-
return process_tree(filecontent, ast, with_progress, with_header)
434+
return process_tree(filecontent, ast, with_progress)
457435
else:
458436
# process_tree() would take care of duplicate identifiers,
459437
# but we need to do it ourselves now using our rudimentary
@@ -562,15 +540,13 @@ def open(fn, only_header: bool = False) -> file:
562540
parse_outcomes = parse(
563541
filename=fn,
564542
with_tree=True,
565-
with_header=True, # must be True to return the header
566543
only_header=True,
567544
)
568545
return file((parse_outcomes, defaultdict(list))) # data section is empty
569546
else:
570547
parse_outcomes = parse(
571548
filename=fn,
572549
with_tree=True,
573-
with_header=True,
574550
only_header=False,
575551
)
576552
return file(parse_outcomes)
File renamed without changes.

test_parser.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def test_file_with_tree(file):
2020

2121
@pytest.mark.parametrize("file", glob.glob("fixtures/*.ifc"))
2222
def test_file_without_tree(file):
23+
if "fail_too_many_header_entity_fields.ifc" in file:
24+
pytest.skip("This file relies on header field validation using the parsed AST, "
25+
"but with_tree=False uses a NullTransformer that discards the AST, "
26+
"so validating the header field names is not possible in this mode.")
2327
with create_context(file):
2428
parse(filename=file, with_tree=False)
2529

@@ -113,25 +117,25 @@ def test_file_mvd_attr():
113117
'fixtures/fail_no_header.ifc',
114118
])
115119
def test_invalid_headers_(filename):
116-
# error in header; with_header should raise an error
120+
# error in header
117121
with pytest.raises(ValidationError):
118-
parse(filename=filename, with_tree=False, only_header=True, with_header=True)
122+
parse(filename=filename, with_tree=False, only_header=True)
119123

120124
@pytest.mark.parametrize("filename", [
121125
'fixtures/fail_duplicate_id.ifc',
122126
'fixtures/fail_double_comma.ifc',
123127
'fixtures/fail_double_semi.ifc'
124128
])
125129
def test_valid_headers(filename):
126-
# error in body; with_header should not raise an error
130+
# error in body
127131
with nullcontext():
128-
parse(filename=filename, with_tree=False, only_header=True, with_header=True)
132+
parse(filename=filename, with_tree=False, only_header=True)
129133

130-
def test_too_many_header_entity_fields():
134+
def test_header_entity_fields():
131135
with pytest.raises(ValidationError):
132-
parse(filename='fixtures/too_many_header_entity_fields.ifc', only_header=True)
136+
parse(filename='fixtures/fail_too_many_header_entity_fields.ifc', only_header=True)
133137

134-
def test_too_many_header_entity_fields_whole_file():
138+
def test_header_entity_fields_whole_file():
135139
with pytest.raises(ValidationError):
136-
parse(filename='fixtures/too_many_header_entity_fields.ifc', with_header=True)
140+
parse(filename='fixtures/fail_too_many_header_entity_fields.ifc')
137141

0 commit comments

Comments
 (0)