Skip to content

Commit

Permalink
add docstring to flowchart module
Browse files Browse the repository at this point in the history
  • Loading branch information
ouhammmourachid committed Nov 18, 2023
1 parent d0995df commit f42c2e9
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
27 changes: 27 additions & 0 deletions mermaid/flowchart/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
"""FlowChart module.
This module provides the FlowChart class for creating and manipulating flowcharts.
It also provides utility classes for representing nodes and links in a flowchart.
Classes:
FlowChart: Represents a flowchart.
Node: Represents a node in a flowchart.
Link: Represents a link between nodes in a flowchart.
"""
from mermaid.graph import Graph

from .link import Link, LinkHead, LinkShape
from .node import Node


class FlowChart(Graph):
"""FlowChart class.
This class represents a flowchart.
Attributes:
orientation (str): The orientation of the flowchart.
nodes (list[Node]): The nodes in the flowchart.
links (list[Link]): The links between nodes in the flowchart.
"""
def __init__(self,
title: str,
nodes: list[Node] = None,
links: list[Link] = None,
orientation: str = 'TB') -> None:
"""Initialize a new FlowChart.
Args:
title (str): The title of the flowchart.
nodes (list[Node]): The nodes in the flowchart.
links (list[Link]): The links between nodes in the flowchart.
orientation (str): The orientation of the flowchart.
"""
super().__init__(title, '')
self.orientation: str = orientation
self.nodes: list[Node] = nodes if nodes is not None else []
Expand Down
38 changes: 38 additions & 0 deletions mermaid/flowchart/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@


class LinkShape(Enum):
"""LinkShape Enum.
This enum represents the different shapes a link can have in a flowchart.
"""
NORMAL = 'normal'
DOTTED = 'dotted'
THICK = 'thick'
HIDEN = 'hiden'


class LinkHead(Enum):
"""LinkHead Enum.
This enum represents the different types of heads a link can have in a flowchart.
"""
NONE = 'none'
ARROW = 'arrow'
LEFT_ARROW = 'left-arrow'
Expand All @@ -35,13 +43,35 @@ class LinkHead(Enum):


class Link:
"""Link class.
This class represents a link between nodes in a flowchart.
Attributes:
origin (Node): The origin node of the link.
end (Node): The end node of the link.
shape (Union[str, LinkShape]): The shape of the link.
head_left (Union[str, LinkHead]): The head of the link at the origin node.
head_right (Union[str, LinkHead]): The head of the link at the end node.
message (str): The message of the link.
"""
def __init__(self,
origin: Node,
end: Node,
shape: Union[str, LinkShape] = 'normal',
head_left: Union[str, LinkHead] = 'none',
head_right: Union[str, LinkHead] = 'arrow',
message: str = '') -> None:
"""Initialize a new Link.
Args:
origin (Node): The origin node of the link.
end (Node): The end node of the link.
shape (Union[str, LinkShape]): The shape of the link.
head_left (Union[str, LinkHead]): The head of the link at the origin node.
head_right (Union[str, LinkHead]): The head of the link at the end node.
message (str): The message of the link.
"""
self.oigin: Node = origin
self.end: Node = end
self.head_left: str = LINK_HEADS[
Expand All @@ -53,6 +83,14 @@ def __init__(self,
self.message: str = f'|{message}|' if message else message

def __str__(self) -> str:
"""Return a string representation of the link.
If the link has a message, it includes the message in the string representation.
Otherwise, it returns a string representation of a link without a message.
Returns:
str: A string representation of the link.
"""
if self.message:
element: list[str] = [
self.oigin.id_,
Expand Down
37 changes: 37 additions & 0 deletions mermaid/flowchart/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

@dataclass
class NodeShape:
"""NodeShape class.
This class represents the shape of a node in a flowchart.
Attributes:
start (str): The start character of the node shape.
end (str): The end character of the node shape.
"""
start: str
end: str

Expand Down Expand Up @@ -35,14 +43,35 @@ class NodeShape:


class Node:
"""Node class.
This class represents a node in a flowchart.
Attributes:
id_ (str): The ID of the node.
content (str): The content of the node.
shape (str): The shape of the node.
sub_nodes (list[Node]): The sub-nodes of the node.
href (str): The hyperlink reference of the node.
href_type (str): The type of the hyperlink reference of the node.
"""
def __init__(self,
id_: str,
content: str = '',
shape: str = 'normal',
sub_nodes: list['Node'] = None,
href: str = None,
href_type: str = 'blank') -> None:
"""Initialize a new Node.
Args:
id_ (str): The ID of the node.
content (str): The content of the node.
shape (str): The shape of the node.
sub_nodes (list[Node]): The sub-nodes of the node.
href (str): The hyperlink reference of the node.
href_type (str): The type of the hyperlink reference of the node.
"""
self.id_: str = text_to_snake_case(id_)
self.content: str = content if content else id_
self.shape: NodeShape = NODE_SHAPES[shape]
Expand All @@ -52,6 +81,14 @@ def __init__(self,
'Node'] = sub_nodes if sub_nodes is not None else []

def __str__(self) -> str:
"""Return a string representation of the node.
If the node has sub-nodes, it returns a string representation of a subgraph.
Otherwise, it returns a string representation of a single node.
Returns:
str: A string representation of the node.
"""
string: str = ''
if len(self.sub_nodes):
string = '\n'.join([
Expand Down

0 comments on commit f42c2e9

Please sign in to comment.