Exemplo n.º 1
0
    def build(cls, tree, config=None, layout=True):
        DiagramNode.clear()
        DiagramEdge.clear()
        NodeGroup.clear()
        Diagram.clear()

        return cls(tree, config, layout).run()
Exemplo n.º 2
0
    def instantiate(self, group, tree):
        for stmt in tree.stmts:
            # Translate Node having group attribute to Group
            if isinstance(stmt, parser.Node):
                group_attr = [a for a in stmt.attrs if a.name == 'group']
                if group_attr:
                    group_id = group_attr[-1]
                    stmt.attrs.remove(group_id)

                    if group_id.value != group.id:
                        stmt = parser.Group(group_id.value, [stmt])

            # Instantiate statements
            if isinstance(stmt, parser.Node):
                node = DiagramNode.get(stmt.id)
                node.set_attributes(stmt.attrs)
                self.belong_to(node, group)

            elif isinstance(stmt, parser.Edge):
                from_nodes = [DiagramNode.get(n) for n in stmt.from_nodes]
                to_nodes = [DiagramNode.get(n) for n in stmt.to_nodes]

                for node in from_nodes + to_nodes:
                    self.belong_to(node, group)

                for node1 in from_nodes:
                    for node2 in to_nodes:
                        edge = DiagramEdge.get(node1, node2)
                        edge.set_dir(stmt.edge_type)
                        edge.set_attributes(stmt.attrs)

            elif isinstance(stmt, parser.Group):
                subgroup = NodeGroup.get(stmt.id)
                subgroup.level = group.level + 1
                self.belong_to(subgroup, group)
                self.instantiate(subgroup, stmt)

            elif isinstance(stmt, parser.Attr):
                group.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,
                                            config=self.config)

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

        group.update_order()
        return group
Exemplo n.º 3
0
    def pagesize(self, width, height):
        margin = self.metrics.page_margin
        padding = self.metrics.page_padding

        dummy = DiagramNode(None)
        dummy.xy = XY(width - 1, height - 1)
        x, y = self._node_bottomright(dummy, use_padding=False)
        x_span = self.span_width[width]
        y_span = self.span_height[height]
        return Size(x + margin.x + padding[1] + x_span,
                    y + margin.y + padding[2] + y_span)
Exemplo n.º 4
0
    def pagesize(self, width, height):
        margin = self.metrics.page_margin
        padding = self.metrics.page_padding

        dummy = DiagramNode(None)
        dummy.xy = XY(width - 1, height - 1)
        x, y = self._node_bottomright(dummy, use_padding=False)
        x_span = self.span_width[width]
        y_span = self.span_height[height]
        return Size(x + margin.x + padding[1] + x_span,
                    y + margin.y + padding[2] + y_span)
Exemplo n.º 5
0
    def instantiate(self, group, tree):
        for stmt in tree.stmts:
            # Translate Node having group attribute to Group
            if isinstance(stmt, parser.Node):
                group_attr = [a for a in stmt.attrs if a.name == 'group']
                if group_attr:
                    group_id = group_attr[-1]
                    stmt.attrs.remove(group_id)

                    if group_id.value != group.id:
                        stmt = parser.Group(group_id.value, [stmt])

            # Instantiate statements
            if isinstance(stmt, parser.Node):
                node = DiagramNode.get(stmt.id)
                node.set_attributes(stmt.attrs)
                self.belong_to(node, group)

            elif isinstance(stmt, parser.Edge):
                from_nodes = [DiagramNode.get(n) for n in stmt.from_nodes]
                to_nodes = [DiagramNode.get(n) for n in stmt.to_nodes]

                for node in from_nodes + to_nodes:
                    self.belong_to(node, group)

                for node1 in from_nodes:
                    for node2 in to_nodes:
                        edge = DiagramEdge.get(node1, node2)
                        edge.set_dir(stmt.edge_type)
                        edge.set_attributes(stmt.attrs)

            elif isinstance(stmt, parser.Group):
                subgroup = NodeGroup.get(stmt.id)
                subgroup.level = group.level + 1
                self.belong_to(subgroup, group)
                self.instantiate(subgroup, stmt)

            elif isinstance(stmt, parser.Attr):
                group.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,
                                            config=self.config)

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

        group.update_order()
        return group
Exemplo n.º 6
0
    def test_FormulaImagePlugin_on_created(self):
        plugin = FormulaImagePlugin(None)

        node = DiagramNode('id')
        self.assertFalse(hasattr(node, 'resizable'))
        plugin.on_created(node)

        self.assertTrue(hasattr(node, 'resizable'))
        self.assertEqual(False, node.resizable)
Exemplo n.º 7
0
    def test_FormulaImagePlugin_on_resizable_changing(self):
        plugin = FormulaImagePlugin(None)
        node = DiagramNode('id')

        plugin.on_created(node)
        plugin.on_attr_changing(node, Attr('resizable', 'true'))
        self.assertEqual(True, node.resizable)

        plugin.on_attr_changing(node, Attr('resizable', 'false'))
        self.assertEqual(False, node.resizable)

        plugin.on_attr_changing(node, Attr('resizable', 'TRUE'))
        self.assertEqual(True, node.resizable)

        plugin.on_attr_changing(node, Attr('resizable', 'FALSE'))
        self.assertEqual(False, node.resizable)

        plugin.on_attr_changing(node, Attr('resizable', 'TRUE'))
        self.assertEqual(True, node.resizable)

        plugin.on_attr_changing(node, Attr('resizable', 'UNKNOWN'))
        self.assertEqual(False, node.resizable)