Reformat ids.py

This commit is contained in:
Thomas Krijnen
2021-02-28 09:34:35 +01:00
parent a7d8bffca5
commit 97df037c93
+30 -42
View File
@@ -4,24 +4,26 @@ import ifcopenshell.util.element
from xml.dom.minidom import parse from xml.dom.minidom import parse
class exception(Exception): pass class exception(Exception):
pass
def error(msg): def error(msg):
raise exception(msg) raise exception(msg)
class facet_evaluation: class facet_evaluation:
""" """
The evaluation of a facet with data from IFC. Converts to bool and has a human readable string format. The evaluation of a facet with data from IFC. Converts to bool and has a human readable string format.
""" """
def __init__(self, success, str): def __init__(self, success, str):
self.success = success self.success = success
self.str = str self.str = str
def __bool__(self): def __bool__(self):
return self.success return self.success
def __str__(self): def __str__(self):
return self.str return self.str
@@ -56,11 +58,11 @@ class facet(metaclass=meta_facet):
return restriction(elems[0]) return restriction(elems[0])
else: else:
return v.firstChild.nodeValue.strip() return v.firstChild.nodeValue.strip()
def __iter__(self): def __iter__(self):
for k in self.parameters: for k in self.parameters:
yield k, getattr(self, k) yield k, getattr(self, k)
def __str__(self): def __str__(self):
return self.message % dict(list(self)) return self.message % dict(list(self))
@@ -69,7 +71,7 @@ class entity(facet):
""" """
The IDS entity facet currently *with* inheritance The IDS entity facet currently *with* inheritance
""" """
parameters = ["name"] parameters = ["name"]
message = "an entity name '%(name)s'" message = "an entity name '%(name)s'"
@@ -77,17 +79,14 @@ class entity(facet):
logger.debug("Testing %s == %s", inst.is_a(), self.name) logger.debug("Testing %s == %s", inst.is_a(), self.name)
# @nb with inheritance # @nb with inheritance
# return inst.is_a() == self.name # return inst.is_a() == self.name
return facet_evaluation( return facet_evaluation(inst.is_a(self.name), self.message % {"name": inst.is_a()})
inst.is_a(self.name),
self.message % {'name':inst.is_a()}
)
class classification(facet): class classification(facet):
""" """
The IDS classification facet by traversing the HasAssociations inverse attribute The IDS classification facet by traversing the HasAssociations inverse attribute
""" """
parameters = ["system", "value"] parameters = ["system", "value"]
message = "a classification reference to '%(value)s' from '%(system)s'" message = "a classification reference to '%(value)s' from '%(system)s'"
@@ -101,9 +100,8 @@ class classification(facet):
return facet_evaluation( return facet_evaluation(
(self.system, self.value) in refs, (self.system, self.value) in refs,
# @todo # @todo
'' "",
) )
class property(facet): class property(facet):
@@ -119,13 +117,13 @@ class property(facet):
pset = props.get(self.propertyset) pset = props.get(self.propertyset)
val = pset.get(self.property) if pset else None val = pset.get(self.property) if pset else None
logger.debug("Testing %s == %s", val, self.value) logger.debug("Testing %s == %s", val, self.value)
di = { di = {
'property': self.property, "property": self.property,
'propertyset': self.propertyset, "propertyset": self.propertyset,
'value': val "value": val,
} }
if val is not None: if val is not None:
msg = self.message % di msg = self.message % di
else: else:
@@ -133,11 +131,8 @@ class property(facet):
msg = "a set '%(propertyset)s', but no property '%(property)'" % di msg = "a set '%(propertyset)s', but no property '%(property)'" % di
else: else:
msg = "no set '%(propertyset)s'" % di msg = "no set '%(propertyset)s'" % di
return facet_evaluation( return facet_evaluation(val == self.value, msg)
val == self.value,
msg
)
class boolean_logic: class boolean_logic:
@@ -151,11 +146,8 @@ class boolean_logic:
def __call__(self, *args): def __call__(self, *args):
eval = [t(*args) for t in self.terms] eval = [t(*args) for t in self.terms]
join = [" and ", " or "][self.fold == any] join = [" and ", " or "][self.fold == any]
return facet_evaluation( return facet_evaluation(self.fold(eval), join.join(map(str, eval)))
self.fold(eval),
join.join(map(str, eval))
)
def __str__(self): def __str__(self):
return [" and ", " or "][self.fold == any].join(map(str, self.terms)) return [" and ", " or "][self.fold == any].join(map(str, self.terms))
@@ -200,14 +192,12 @@ class specification:
return [cls(n) for cls, n in zip(classes, children)] return [cls(n) for cls, n in zip(classes, children)]
phrases = [n for n in node.childNodes if n.nodeType == n.ELEMENT_NODE] phrases = [n for n in node.childNodes if n.nodeType == n.ELEMENT_NODE]
len(phrases) == 2 or error("expected two child nodes for <specification>") len(phrases) == 2 or error("expected two child nodes for <specification>")
phrases[0].tagName == "applicability" or error("expected <applicability>") phrases[0].tagName == "applicability" or error("expected <applicability>")
phrases[1].tagName == "requirements" or error("expected <requirements>") phrases[1].tagName == "requirements" or error("expected <requirements>")
self.applicabiliy, self.requirements = ( self.applicabiliy, self.requirements = (boolean_and(parse_rules(phrase)) for phrase in phrases)
boolean_and(parse_rules(phrase)) for phrase in phrases
)
def __call__(self, inst, logger): def __call__(self, inst, logger):
if self.applicabiliy(inst, logger): if self.applicabiliy(inst, logger):
@@ -216,7 +206,7 @@ class specification:
logger.info(str(self) + "\n%s has" % inst + " " + str(valid) + " so is compliant") logger.info(str(self) + "\n%s has" % inst + " " + str(valid) + " so is compliant")
else: else:
logger.error(str(self) + "\n%s has" % inst + " " + str(valid) + " so is not compliant") logger.error(str(self) + "\n%s has" % inst + " " + str(valid) + " so is not compliant")
def __str__(self): def __str__(self):
return "Given an instance with %(applicabiliy)s\nWe expect %(requirements)s" % self.__dict__ return "Given an instance with %(applicabiliy)s\nWe expect %(requirements)s" % self.__dict__
@@ -232,9 +222,7 @@ class ids:
ids.tagName == "ids" or error("expected <ids>") ids.tagName == "ids" or error("expected <ids>")
self.specifications = [ self.specifications = [
specification(n) specification(n) for n in ids.childNodes if n.nodeType == n.ELEMENT_NODE and n.tagName == "specification"
for n in ids.childNodes
if n.nodeType == n.ELEMENT_NODE and n.tagName == "specification"
] ]
def validate(self, ifc_file, logger): def validate(self, ifc_file, logger):
@@ -249,7 +237,7 @@ if __name__ == "__main__":
import ifcopenshell import ifcopenshell
logger = logging.getLogger("IDS") logger = logging.getLogger("IDS")
logging.basicConfig(level=logging.INFO, format='%(message)s') logging.basicConfig(level=logging.INFO, format="%(message)s")
ids_file = ids(sys.argv[1]) ids_file = ids(sys.argv[1])
ifc_file = ifcopenshell.open(sys.argv[2]) ifc_file = ifcopenshell.open(sys.argv[2])