mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-21 14:23:53 +00:00
--json option for validate command line usage
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
import functools
|
||||||
|
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
import ifcopenshell
|
import ifcopenshell
|
||||||
|
|
||||||
named_type = ifcopenshell.ifcopenshell_wrapper.named_type
|
named_type = ifcopenshell.ifcopenshell_wrapper.named_type
|
||||||
@@ -13,6 +19,24 @@ attribute = ifcopenshell.ifcopenshell_wrapper.attribute
|
|||||||
|
|
||||||
class ValidationError(Exception): pass
|
class ValidationError(Exception): pass
|
||||||
|
|
||||||
|
log_entry_type = namedtuple('log_entry_type', ("level", "message", "product"))
|
||||||
|
|
||||||
|
class json_logger:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.statements = []
|
||||||
|
self.product = None
|
||||||
|
|
||||||
|
def set_product(self, product):
|
||||||
|
self.product = product
|
||||||
|
|
||||||
|
def log(self, level, message, product):
|
||||||
|
self.statements.append(log_entry_type(level, message, product)._asdict())
|
||||||
|
|
||||||
|
def __getattr__(self, level):
|
||||||
|
return functools.partial(self.log, level, product=self.product)
|
||||||
|
|
||||||
|
|
||||||
simple_type_python_mapping = {
|
simple_type_python_mapping = {
|
||||||
# @todo should include unicode for Python2
|
# @todo should include unicode for Python2
|
||||||
"string": str,
|
"string": str,
|
||||||
@@ -76,6 +100,9 @@ def try_valid(attr, val):
|
|||||||
def validate(f, logger):
|
def validate(f, logger):
|
||||||
schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name(f.schema)
|
schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name(f.schema)
|
||||||
for inst in f:
|
for inst in f:
|
||||||
|
if hasattr(logger, 'set_product'):
|
||||||
|
logger.set_product(inst)
|
||||||
|
|
||||||
entity = schema.declaration_by_name(inst.is_a())
|
entity = schema.declaration_by_name(inst.is_a())
|
||||||
|
|
||||||
for attr, val, is_derived in zip(entity.all_attributes(), inst, entity.derived()):
|
for attr, val, is_derived in zip(entity.all_attributes(), inst, entity.derived()):
|
||||||
@@ -88,22 +115,40 @@ def validate(f, logger):
|
|||||||
try:
|
try:
|
||||||
assert_valid(attr, val)
|
assert_valid(attr, val)
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
logger.error('In {}\n{}'.format(inst, e))
|
if hasattr(logger, 'set_product'):
|
||||||
|
logger.error(str(e))
|
||||||
|
else:
|
||||||
|
logger.error('In {}\n{}'.format(inst, e))
|
||||||
|
|
||||||
for attr in entity.all_inverse_attributes():
|
for attr in entity.all_inverse_attributes():
|
||||||
val = getattr(inst, attr.name())
|
val = getattr(inst, attr.name())
|
||||||
try:
|
try:
|
||||||
assert_valid_inverse(attr, val)
|
assert_valid_inverse(attr, val)
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
logger.error('In {}\n{}'.format(inst, e))
|
if hasattr(logger, 'set_product'):
|
||||||
|
logger.error(str(e))
|
||||||
|
else:
|
||||||
|
logger.error('In {}\n{}'.format(inst, e))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
for fn in sys.argv[1:]:
|
filenames = [x for x in sys.argv[1:] if not x.startswith('--')]
|
||||||
logger = logging.getLogger('validate')
|
flags = set(x for x in sys.argv[1:] if x.startswith('--'))
|
||||||
logger.setLevel(logging.DEBUG)
|
|
||||||
print("Validating", fn)
|
for fn in filenames:
|
||||||
validate(ifcopenshell.open(fn), logger)
|
if '--json' in flags:
|
||||||
|
logger = json_logger()
|
||||||
|
else:
|
||||||
|
logger = logging.getLogger('validate')
|
||||||
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
f = ifcopenshell.open(fn)
|
||||||
|
|
||||||
|
print("Validating", fn, file=sys.stderr)
|
||||||
|
validate(f, logger)
|
||||||
|
|
||||||
|
if '--json' in flags:
|
||||||
|
print("\n".join(json.dumps(x, default=str) for x in logger.statements))
|
||||||
|
|||||||
Reference in New Issue
Block a user