pull/1066/head
phuoc.tran 10 months ago
parent 718f641bfd
commit 415c2b102e

@ -139,7 +139,8 @@ class Diagram:
if isinstance(outformat, list): if isinstance(outformat, list):
for one_format in outformat: for one_format in outformat:
if not self._validate_outformat(one_format): if not self._validate_outformat(one_format):
raise ValueError(f'"{one_format}" is not a valid output format') raise ValueError(
f'"{one_format}" is not a valid output format')
else: else:
if not self._validate_outformat(outformat): if not self._validate_outformat(outformat):
raise ValueError(f'"{outformat}" is not a valid output format') raise ValueError(f'"{outformat}" is not a valid output format')
@ -478,7 +479,8 @@ class Edge:
if label: if label:
# Graphviz complaining about using label for edges, so replace it with xlabel. # Graphviz complaining about using label for edges, so replace it with xlabel.
# Update: xlabel option causes the misaligned label position: https://github.com/mingrammer/diagrams/issues/83 # Update: xlabel option causes the misaligned label position:
# https://github.com/mingrammer/diagrams/issues/83
self._attrs["label"] = label self._attrs["label"] = label
if color: if color:
self._attrs["color"] = color self._attrs["color"] = color
@ -490,7 +492,8 @@ class Edge:
"""Implement Self - Node or Edge and Self - [Nodes]""" """Implement Self - Node or Edge and Self - [Nodes]"""
return self.connect(other) return self.connect(other)
def __rsub__(self, other: Union[List["Node"], List["Edge"]]) -> List["Edge"]: def __rsub__(self, other: Union[List["Node"],
List["Edge"]]) -> List["Edge"]:
"""Called for [Nodes] or [Edges] - Self because list don't have __sub__ operators.""" """Called for [Nodes] or [Edges] - Self because list don't have __sub__ operators."""
return self.append(other) return self.append(other)
@ -504,15 +507,23 @@ class Edge:
self.reverse = True self.reverse = True
return self.connect(other) return self.connect(other)
def __rrshift__(self, other: Union[List["Node"], List["Edge"]]) -> List["Edge"]: def __rrshift__(self,
other: Union[List["Node"],
List["Edge"]]) -> List["Edge"]:
"""Called for [Nodes] or [Edges] >> Self because list of Edges don't have __rshift__ operators.""" """Called for [Nodes] or [Edges] >> Self because list of Edges don't have __rshift__ operators."""
return self.append(other, forward=True) return self.append(other, forward=True)
def __rlshift__(self, other: Union[List["Node"], List["Edge"]]) -> List["Edge"]: def __rlshift__(self,
other: Union[List["Node"],
List["Edge"]]) -> List["Edge"]:
"""Called for [Nodes] or [Edges] << Self because list of Edges don't have __lshift__ operators.""" """Called for [Nodes] or [Edges] << Self because list of Edges don't have __lshift__ operators."""
return self.append(other, reverse=True) return self.append(other, reverse=True)
def append(self, other: Union[List["Node"], List["Edge"]], forward=None, reverse=None) -> List["Edge"]: def append(self,
other: Union[List["Node"],
List["Edge"]],
forward=None,
reverse=None) -> List["Edge"]:
result = [] result = []
for o in other: for o in other:
if isinstance(o, Edge): if isinstance(o, Edge):
@ -521,7 +532,12 @@ class Edge:
self._attrs = o.attrs.copy() self._attrs = o.attrs.copy()
result.append(o) result.append(o)
else: else:
result.append(Edge(o, forward=forward, reverse=reverse, **self._attrs)) result.append(
Edge(
o,
forward=forward,
reverse=reverse,
**self._attrs))
return result return result
def connect(self, other: Union["Node", "Edge", List["Node"]]): def connect(self, other: Union["Node", "Edge", List["Node"]]):

@ -27,7 +27,8 @@ def _format_description(description):
""" """
wrapper = textwrap.TextWrapper(width=40, max_lines=3) wrapper = textwrap.TextWrapper(width=40, max_lines=3)
lines = [html.escape(line) for line in wrapper.wrap(description)] lines = [html.escape(line) for line in wrapper.wrap(description)]
lines += [""] * (3 - len(lines)) # fill up with empty lines so it is always three # fill up with empty lines so it is always three
lines += [""] * (3 - len(lines))
return "<br/>".join(lines) return "<br/>".join(lines)

@ -62,7 +62,8 @@ def gen_apidoc(pvd: str, typ_paths: dict) -> str:
name = _gen_class_name(path) name = _gen_class_name(path)
resource_path = os.path.join(resource_root, path) resource_path = os.path.join(resource_root, path)
alias = cfg.ALIASES[pvd].get(typ, {}).get(name) alias = cfg.ALIASES[pvd].get(typ, {}).get(name)
typ_classes[typ].append({"name": name, "alias": alias, "resource_path": resource_path}) typ_classes[typ].append(
{"name": name, "alias": alias, "resource_path": resource_path})
return tmpl.render(pvd=pvd, typ_classes=typ_classes) return tmpl.render(pvd=pvd, typ_classes=typ_classes)

@ -203,7 +203,8 @@ def svg2png2(pvd: str) -> None:
def _convert(base: str, path: str): def _convert(base: str, path: str):
path_src = os.path.join(base, path) path_src = os.path.join(base, path)
path_dest = path_src.replace(".svg", ".png") path_dest = path_src.replace(".svg", ".png")
subprocess.run([cfg.CMD_SVG2PNG_IM, *cfg.CMD_SVG2PNG_IM_OPTS, path_src, path_dest]) subprocess.run([cfg.CMD_SVG2PNG_IM, *
cfg.CMD_SVG2PNG_IM_OPTS, path_src, path_dest])
subprocess.run(["rm", path_src]) subprocess.run(["rm", path_src])
for root, _, files in os.walk(resource_dir(pvd)): for root, _, files in os.walk(resource_dir(pvd)):

@ -16,7 +16,8 @@ from diagrams.c4 import (
class C4Test(unittest.TestCase): class C4Test(unittest.TestCase):
def setUp(self): def setUp(self):
self.name = "diagram-" + "".join([random.choice(string.hexdigits) for n in range(7)]).lower() self.name = "diagram-" + \
"".join([random.choice(string.hexdigits) for n in range(7)]).lower()
def tearDown(self): def tearDown(self):
setdiagram(None) setdiagram(None)
@ -29,8 +30,14 @@ class C4Test(unittest.TestCase):
def test_nodes(self): def test_nodes(self):
with Diagram(name=self.name, show=False): with Diagram(name=self.name, show=False):
person = Person("person", "A person.") person = Person("person", "A person.")
container = Container("container", "Java application", "The application.") container = Container(
database = Database("database", "Oracle database", "Stores information.") "container",
"Java application",
"The application.")
database = Database(
"database",
"Oracle database",
"Stores information.")
def test_external_nodes(self): def test_external_nodes(self):
with Diagram(name=self.name, show=False): with Diagram(name=self.name, show=False):

@ -247,56 +247,122 @@ class EdgeTest(unittest.TestCase):
with Cluster(): with Cluster():
node1 = Node("node1") node1 = Node("node1")
nodes = [Node("node2"), Node("node3")] nodes = [Node("node2"), Node("node3")]
self.assertEqual(nodes - Edge(color="red") - Edge(color="green") - node1, node1) self.assertEqual(
nodes -
Edge(
color="red") -
Edge(
color="green") -
node1,
node1)
def test_node_to_node_with_attributes(self): def test_node_to_node_with_attributes(self):
with Diagram(name=os.path.join(self.name, "node_to_node_with_attributes"), show=False): with Diagram(name=os.path.join(self.name, "node_to_node_with_attributes"), show=False):
with Cluster(): with Cluster():
node1 = Node("node1") node1 = Node("node1")
node2 = Node("node2") node2 = Node("node2")
self.assertEqual(node1 << Edge(color="red", label="1.1") << node2, node2) self.assertEqual(
self.assertEqual(node1 >> Edge(color="green", label="1.2") >> node2, node2) node1 << Edge(
self.assertEqual(node1 << Edge(color="blue", label="1.3") >> node2, node2) color="red",
label="1.1") << node2,
node2)
self.assertEqual(
node1 >> Edge(
color="green",
label="1.2") >> node2,
node2)
self.assertEqual(
node1 << Edge(
color="blue",
label="1.3") >> node2,
node2)
def test_node_to_node_with_additional_attributes(self): def test_node_to_node_with_additional_attributes(self):
with Diagram(name=os.path.join(self.name, "node_to_node_with_additional_attributes"), show=False): with Diagram(name=os.path.join(self.name, "node_to_node_with_additional_attributes"), show=False):
with Cluster(): with Cluster():
node1 = Node("node1") node1 = Node("node1")
node2 = Node("node2") node2 = Node("node2")
self.assertEqual(node1 << Edge(color="red", label="2.1") << Edge(color="blue") << node2, node2) self.assertEqual(
self.assertEqual(node1 >> Edge(color="green", label="2.2") >> Edge(color="red") >> node2, node2) node1 << Edge(
self.assertEqual(node1 << Edge(color="blue", label="2.3") >> Edge(color="black") >> node2, node2) color="red",
label="2.1") << Edge(
color="blue") << node2,
node2)
self.assertEqual(
node1 >> Edge(
color="green",
label="2.2") >> Edge(
color="red") >> node2,
node2)
self.assertEqual(
node1 << Edge(
color="blue",
label="2.3") >> Edge(
color="black") >> node2,
node2)
def test_nodes_to_node_with_attributes_loop(self): def test_nodes_to_node_with_attributes_loop(self):
with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_loop"), show=False): with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_loop"), show=False):
with Cluster(): with Cluster():
node = Node("node") node = Node("node")
self.assertEqual(node >> Edge(color="red", label="3.1") >> node, node) self.assertEqual(
self.assertEqual(node << Edge(color="green", label="3.2") << node, node) node >> Edge(
self.assertEqual(node >> Edge(color="blue", label="3.3") << node, node) color="red",
self.assertEqual(node << Edge(color="pink", label="3.4") >> node, node) label="3.1") >> node,
node)
self.assertEqual(
node << Edge(
color="green",
label="3.2") << node,
node)
self.assertEqual(
node >> Edge(
color="blue",
label="3.3") << node,
node)
self.assertEqual(
node << Edge(
color="pink",
label="3.4") >> node,
node)
def test_nodes_to_node_with_attributes_bothdirectional(self): def test_nodes_to_node_with_attributes_bothdirectional(self):
with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_bothdirectional"), show=False): with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_bothdirectional"), show=False):
with Cluster(): with Cluster():
node1 = Node("node1") node1 = Node("node1")
nodes = [Node("node2"), Node("node3")] nodes = [Node("node2"), Node("node3")]
self.assertEqual(nodes << Edge(color="green", label="4") >> node1, node1) self.assertEqual(
nodes << Edge(
color="green",
label="4") >> node1,
node1)
def test_nodes_to_node_with_attributes_bidirectional(self): def test_nodes_to_node_with_attributes_bidirectional(self):
with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_bidirectional"), show=False): with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_bidirectional"), show=False):
with Cluster(): with Cluster():
node1 = Node("node1") node1 = Node("node1")
nodes = [Node("node2"), Node("node3")] nodes = [Node("node2"), Node("node3")]
self.assertEqual(nodes << Edge(color="blue", label="5") >> node1, node1) self.assertEqual(
nodes << Edge(
color="blue",
label="5") >> node1,
node1)
def test_nodes_to_node_with_attributes_onedirectional(self): def test_nodes_to_node_with_attributes_onedirectional(self):
with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_onedirectional"), show=False): with Diagram(name=os.path.join(self.name, "nodes_to_node_with_attributes_onedirectional"), show=False):
with Cluster(): with Cluster():
node1 = Node("node1") node1 = Node("node1")
nodes = [Node("node2"), Node("node3")] nodes = [Node("node2"), Node("node3")]
self.assertEqual(nodes >> Edge(color="red", label="6.1") >> node1, node1) self.assertEqual(
self.assertEqual(nodes << Edge(color="green", label="6.2") << node1, node1) nodes >> Edge(
color="red",
label="6.1") >> node1,
node1)
self.assertEqual(
nodes << Edge(
color="green",
label="6.2") << node1,
node1)
def test_nodes_to_node_with_additional_attributes_directional(self): def test_nodes_to_node_with_additional_attributes_directional(self):
with Diagram(name=os.path.join(self.name, "nodes_to_node_with_additional_attributes_directional"), show=False): with Diagram(name=os.path.join(self.name, "nodes_to_node_with_additional_attributes_directional"), show=False):
@ -304,11 +370,19 @@ class EdgeTest(unittest.TestCase):
node1 = Node("node1") node1 = Node("node1")
nodes = [Node("node2"), Node("node3")] nodes = [Node("node2"), Node("node3")]
self.assertEqual( self.assertEqual(
nodes >> Edge(color="red", label="6.1") >> Edge(color="blue", label="6.2") >> node1, node1 nodes >> Edge(
) color="red",
label="6.1") >> Edge(
color="blue",
label="6.2") >> node1,
node1)
self.assertEqual( self.assertEqual(
nodes << Edge(color="green", label="6.3") << Edge(color="pink", label="6.4") << node1, node1 nodes << Edge(
) color="green",
label="6.3") << Edge(
color="pink",
label="6.4") << node1,
node1)
class ResourcesTest(unittest.TestCase): class ResourcesTest(unittest.TestCase):
@ -319,5 +393,12 @@ class ResourcesTest(unittest.TestCase):
exceeded. exceeded.
""" """
resources_dir = pathlib.Path(__file__).parent.parent / "resources" resources_dir = pathlib.Path(__file__).parent.parent / "resources"
max_depth = max(os.path.relpath(d, resources_dir).count(os.sep) + 1 for d, _, _ in os.walk(resources_dir)) max_depth = max(
os.path.relpath(
d,
resources_dir).count(
os.sep) +
1 for d,
_,
_ in os.walk(resources_dir))
self.assertLessEqual(max_depth, 2) self.assertLessEqual(max_depth, 2)

Loading…
Cancel
Save