import argparse from itertools import groupby from pathlib import Path from ee.part import Part, load_db, common_fact_types class Messages(object): INFO = 1 WARNING = 2 ERROR = 3 def __init__(self, part): self.part = part self.messages = [] def error(self, msg): self.messages.append((self.part, Messages.ERROR, msg)) return self def warning(self, msg): self.messages.append((self.part, Messages.WARNING, msg)) return self def info(self, msg): self.messages.append((self.part, Messages.INFO, msg)) return self @property def errors(self): return [m[2] for m in self.messages if m[1] == Messages.ERROR] @property def warnings(self): return [m[2] for m in self.messages if m[1] == Messages.WARNING] @property def infos(self): return [m[2] for m in self.messages if m[1] == Messages.INFO] def __len__(self): return self.messages.__len__() def append(self, messages: "Messages"): self.messages.extend(messages.messages) def check_has_footprint(part: Part): fp = part.facts.get_value(common_fact_types.footprint) if fp is not None: return return Messages(part).warning("No footprint") def validate(f, part: Part): validators = [ check_has_footprint ] messages = Messages(part) for validator in validators: m = validator(part) if m: messages.append(m) print("{}".format(part.printable_reference), file=f) print("{}".format("=" * len(part.printable_reference)), file=f) print("", file=f) for msg in messages.errors: print("* ERROR: {}".format(msg), file=f) for msg in messages.warnings: print("* WARNING: {}".format(msg), file=f) for msg in messages.infos: print("* INFO: {}".format(msg), file=f) if len(messages) == 0: print("No issues found", file=f) print("", file=f) return messages def work(in_path: Path, out_path: Path): in_parts = load_db(in_path) with out_path.open("w") as f: messages = Messages(None) for xml in in_parts.iterparts(): part = Part(xml) ms = validate(f, part) messages.append(ms) print("", file=f) print("Message index", file=f) print("=============", file=f) print("", file=f) for message, messages_ in groupby(messages.messages, lambda m: m[2]): messages = list(messages_) print(message, file=f) print("{}".format("-" * len(message)), file=f) for m in messages: print(" * {}".format(m[0].printable_reference), file=f) parser = argparse.ArgumentParser() parser.add_argument("--in", dest="in_path", required=True, metavar="PART DB") parser.add_argument("--out", required=True, metavar="REQUIREMENTS") args = parser.parse_args() work(Path(args.in_path), Path(args.out))