Exemple #1
0
    def build(cls, tree):
        DiagramNode.clear()
        DiagramEdge.clear()
        NodeGroup.clear()
        Network.clear()

        diagram = DiagramTreeBuilder().build(tree)
        DiagramLayoutManager(diagram).run()
        diagram = cls.update_network_status(diagram)

        return diagram
Exemple #2
0
    def instantiate(self, network, group, tree):
        for stmt in tree.stmts:
            if isinstance(stmt, parser.Node):
                node = DiagramNode.get(stmt.id)
                node.set_attributes(network, stmt.attrs)

                if group:
                    if (node.group and node.group != self.diagram and
                       (group != node.group)):
                        msg = "DiagramNode could not belong to two groups"
                        raise RuntimeError(msg)
                    node.group = group
                    group.nodes.append(node)
                elif node.group is None:
                    node.group = self.diagram

                if network is not None:
                    if network not in node.networks:
                        node.networks.append(network)
                    if node not in network.nodes:
                        network.nodes.append(node)
                if node not in self.diagram.nodes:
                    self.diagram.nodes.append(node)

            elif isinstance(stmt, parser.Network):
                subnetwork = Network.get(stmt.id)
                subnetwork.label = stmt.id

                if subnetwork not in self.diagram.networks:
                    self.diagram.networks.append(subnetwork)

                substmt = namedtuple('Statements', 'stmts')([])
                for s in stmt.stmts:
                    if isinstance(s, parser.Attr):
                        subnetwork.set_attribute(s)
                    else:
                        substmt.stmts.append(s)

                self.instantiate(subnetwork, group, substmt)

            elif isinstance(stmt, parser.Group):
                subgroup = NodeGroup.get(stmt.id)

                if subgroup not in self.diagram.groups:
                    self.diagram.groups.append(subgroup)

                substmt = namedtuple('Statements', 'stmts')([])
                for s in stmt.stmts:
                    if isinstance(s, parser.Attr):
                        subgroup.set_attribute(s)
                    else:
                        substmt.stmts.append(s)

                self.instantiate(network, subgroup, substmt)

            elif isinstance(stmt, parser.Peer):
                nodes = []
                for edge in stmt.edges:
                    nodes.append(DiagramNode.get(edge.from_node))
                    nodes.append(DiagramNode.get(edge.to_node))

                for node in nodes:
                    if node.group is None:
                        node.group = self.diagram
                    if node not in self.diagram.nodes:
                        self.diagram.nodes.append(node)

                if len(nodes[0].networks) == 0:
                    nw = Network.create_anonymous([nodes[0]])
                    if nw:
                        self.diagram.networks.append(nw)

                for edge in stmt.edges:
                    from_node = DiagramNode.get(edge.from_node)
                    to_node = DiagramNode.get(edge.to_node)

                    _nodes = [from_node, to_node]
                    nw = Network.create_anonymous(_nodes, edge.attrs)
                    if nw:
                        self.diagram.networks.append(nw)

            elif isinstance(stmt, parser.Route):
                nodes = [DiagramNode.get(n) for n in stmt.nodes]
                for node1, node2 in zip(nodes[:-1], nodes[1:]):
                    route = Route(node1, node2)
                    route.set_attributes(stmt.attrs)
                    self.diagram.routes.append(route)

            elif isinstance(stmt, parser.Attr):
                self.diagram.set_attribute(stmt)

            elif isinstance(stmt, parser.Extension):
                if stmt.type == 'class':
                    name = unquote(stmt.name)
                    Diagram.classes[name] = stmt
                elif stmt.type == 'plugin':
                    self.diagram.set_plugin(stmt.name, stmt.attrs)

            elif isinstance(stmt, parser.Statements):
                self.instantiate(network, group, stmt)

        return network