Skip to content

Commit 83d2548

Browse files
committed
Validate instance attribute values by catching exceptions
1 parent 199bc86 commit 83d2548

1 file changed

Lines changed: 47 additions & 18 deletions

File tree

src/ifcopenshell-python/ifcopenshell/validate.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def set_instance(self, instance):
3434
self.instance = instance
3535

3636
def log(self, level, message, *args, **kwargs):
37-
self.statements.append(log_entry_type(level, message % args, kwargs.get("instance"))._asdict())
37+
self.statements.append(
38+
log_entry_type(level, message % args, kwargs.get("instance"))._asdict()
39+
)
3840

3941
def __getattr__(self, level):
4042
return functools.partial(self.log, level, instance=self.instance)
@@ -81,7 +83,9 @@ def assert_valid(attr, val, schema):
8183
if isinstance(attr_type, simple_type):
8284
invalid = type(val) != simple_type_python_mapping[attr_type.declared_type()]
8385
elif isinstance(attr_type, (entity_type, type_declaration)):
84-
invalid = not isinstance(val, ifcopenshell.entity_instance) or not val.is_a(attr_type.name())
86+
invalid = not isinstance(val, ifcopenshell.entity_instance) or not val.is_a(
87+
attr_type.name()
88+
)
8589
elif isinstance(attr_type, select_type):
8690
val_to_use = val
8791
if isinstance(schema.declaration_by_name(val.is_a()), enumeration_type):
@@ -90,13 +94,19 @@ def assert_valid(attr, val, schema):
9094
else:
9195
invalid = True
9296
if not invalid:
93-
invalid = not any(try_valid(x, val_to_use, schema) for x in attr_type.select_list())
97+
invalid = not any(
98+
try_valid(x, val_to_use, schema) for x in attr_type.select_list()
99+
)
94100
elif isinstance(attr_type, enumeration_type):
95101
invalid = val not in attr_type.enumeration_items()
96102
elif isinstance(attr_type, aggregation_type):
97103
b1, b2 = attr_type.bound1(), attr_type.bound2()
98104
ty = attr_type.type_of_element()
99-
invalid = len(val) < b1 or (b2 != -1 and len(val) > b2) or not all(assert_valid(ty, v, schema) for v in val)
105+
invalid = (
106+
len(val) < b1
107+
or (b2 != -1 and len(val) > b2)
108+
or not all(assert_valid(ty, v, schema) for v in val)
109+
)
100110
else:
101111
raise NotImplementedError("Not impl %s %s" % (type(attr_type), attr_type))
102112

@@ -135,6 +145,7 @@ def validate(f, logger):
135145
logger.set_instance(inst)
136146

137147
entity = schema.declaration_by_name(inst.is_a())
148+
attrs = entity.all_attributes()
138149

139150
if entity.is_abstract():
140151
e = "Entity %s is abstract" % entity.name()
@@ -143,20 +154,38 @@ def validate(f, logger):
143154
else:
144155
logger.error("In %s\n%s", inst, e)
145156

146-
for attr, val, is_derived in zip(entity.all_attributes(), inst, entity.derived()):
147-
148-
if val is None and not (is_derived or attr.optional()):
149-
logger.error("Attribute %s.%s not optional", entity, attr)
150-
151-
if val is not None:
152-
attr_type = attr.type_of_attribute()
153-
try:
154-
assert_valid(attr, val, schema)
155-
except ValidationError as e:
156-
if hasattr(logger, "set_instance"):
157-
logger.error(str(e))
158-
else:
159-
logger.error("In %s\n%s", inst, e)
157+
has_invalid_value = False
158+
for i in range(len(attrs)):
159+
try:
160+
inst[i]
161+
pass
162+
except:
163+
if hasattr(logger, "set_instance"):
164+
logger.error("Invalid attribute value for %s.%s", entity, attrs[i])
165+
else:
166+
logger.error(
167+
"In %s\nInvalid attribute value for %s.%s",
168+
inst,
169+
entity,
170+
attrs[i],
171+
)
172+
has_invalid_value = True
173+
174+
if not has_invalid_value:
175+
for attr, val, is_derived in zip(attrs, inst, entity.derived()):
176+
177+
if val is None and not (is_derived or attr.optional()):
178+
logger.error("Attribute %s.%s not optional", entity, attr)
179+
180+
if val is not None:
181+
attr_type = attr.type_of_attribute()
182+
try:
183+
assert_valid(attr, val, schema)
184+
except ValidationError as e:
185+
if hasattr(logger, "set_instance"):
186+
logger.error(str(e))
187+
else:
188+
logger.error("In %s\n%s", inst, e)
160189

161190
for attr in entity.all_inverse_attributes():
162191
val = getattr(inst, attr.name())

0 commit comments

Comments
 (0)