autolabel flag in diagrams that modifies label

pull/482/head
austen 5 years ago
parent ff90ff93bd
commit 6ed0d861a2

@ -83,6 +83,7 @@ class Diagram:
direction: str = "LR", direction: str = "LR",
curvestyle: str = "ortho", curvestyle: str = "ortho",
outformat: str = "png", outformat: str = "png",
autolabel: bool = False,
show: bool = True, show: bool = True,
graph_attr: dict = {}, graph_attr: dict = {},
node_attr: dict = {}, node_attr: dict = {},
@ -137,6 +138,7 @@ class Diagram:
self.dot.edge_attr.update(edge_attr) self.dot.edge_attr.update(edge_attr)
self.show = show self.show = show
self.autolabel = autolabel
def __str__(self) -> str: def __str__(self) -> str:
return str(self.dot) return str(self.dot)
@ -296,11 +298,20 @@ class Node:
self._id = self._rand_id() self._id = self._rand_id()
self.label = label self.label = label
# Node must be belong to a diagrams.
self._diagram = getdiagram()
if self._diagram is None:
raise EnvironmentError("Global diagrams context not set up")
if self._diagram.autolabel:
# import pdb; pdb.set_trace()
self.label = self.__class__.__name__ + "\n" + self.label
# fmt: off # fmt: off
# If a node has an icon, increase the height slightly to avoid # If a node has an icon, increase the height slightly to avoid
# that label being spanned between icon image and white space. # that label being spanned between icon image and white space.
# Increase the height by the number of new lines included in the label. # Increase the height by the number of new lines included in the label.
padding = 0.4 * (label.count('\n')) padding = 0.4 * (self.label.count('\n'))
self._attrs = { self._attrs = {
"shape": "none", "shape": "none",
"height": str(self._height + padding), "height": str(self._height + padding),
@ -310,10 +321,6 @@ class Node:
# fmt: on # fmt: on
self._attrs.update(attrs) self._attrs.update(attrs)
# Node must be belong to a diagrams.
self._diagram = getdiagram()
if self._diagram is None:
raise EnvironmentError("Global diagrams context not set up")
self._cluster = getcluster() self._cluster = getcluster()
# If a node is in the cluster context, add it to cluster. # If a node is in the cluster context, add it to cluster.

@ -107,6 +107,12 @@ class DiagramTest(unittest.TestCase):
Node("node1") Node("node1")
self.assertTrue(os.path.exists(f"{self.name}.png")) self.assertTrue(os.path.exists(f"{self.name}.png"))
def test_autolabel(self):
with Diagram(name=os.path.join(self.name, "nodes_to_node"), show=False):
node1 = Node("node1")
self.assertTrue(node1.label,"Node\nnode1")
class ClusterTest(unittest.TestCase): class ClusterTest(unittest.TestCase):
def setUp(self): def setUp(self):

Loading…
Cancel
Save