def build(self, tree): self.diagram = Diagram() self.instantiate(self.diagram.racks[0], tree) for rack in self.diagram.racks[:]: if len(rack.nodes) == 0: self.diagram.racks.remove(rack) return self.diagram
class DiagramTreeBuilder: def build(self, tree): self.diagram = Diagram() self.instantiate(self.diagram.racks[0], tree) for rack in self.diagram.racks[:]: if len(rack.nodes) == 0: self.diagram.racks.remove(rack) return self.diagram def instantiate(self, rack, tree): for stmt in tree.stmts: if isinstance(stmt, parser.Attr): try: rack.set_attribute(stmt) except AttributeError: self.diagram.set_attribute(stmt) elif isinstance(stmt, parser.RackItem): if stmt.number is None: if len(rack.nodes) > 0: item = rack.nodes[-1] number = item.number + item.colheight else: number = 1 else: number = stmt.number item = RackItem(number, unquote(stmt.label)) item.set_attributes(stmt.attrs) rack.nodes.append(item) elif isinstance(stmt, parser.Rack): _rack = Rack() self.diagram.racks.append(_rack) self.instantiate(_rack, stmt) elif isinstance(stmt, parser.AttrPlugin): self.diagram.set_plugin(stmt.name, stmt.attrs)
def build(cls, tree): Rack.clear() RackItem.clear() Diagram.clear() return cls(tree).run()