Module osbot_utils.graphs.mermaid.Mermaid

Expand source code
from osbot_utils.graphs.mermaid.Mermaid__Renderer   import Mermaid__Renderer
from osbot_utils.graphs.mermaid.Mermaid__Edge       import Mermaid__Edge
from osbot_utils.graphs.mermaid.Mermaid__Graph      import Mermaid__Graph
from osbot_utils.graphs.mermaid.models.Mermaid__Diagram_Direction import Diagram__Direction
from osbot_utils.graphs.mermaid.models.Mermaid__Diagram__Type import Diagram__Type
from osbot_utils.utils.Python_Logger            import Python_Logger
from osbot_utils.base_classes.Kwargs_To_Self    import Kwargs_To_Self

class Mermaid(Kwargs_To_Self):
    graph             : Mermaid__Graph
    renderer          : Mermaid__Renderer
    logger            : Python_Logger

    # todo add support for storing the data in sqlite so that the search for existing nodes is efficient
    def add_edge(self, from_node_key, to_node_key, label=None,attributes=None):
        nodes_by_id = self.graph.data().nodes__by_key()
        from_node   = nodes_by_id.get(from_node_key)
        to_node     = nodes_by_id.get(to_node_key)
        if not from_node:
            from_node = self.add_node(key=from_node_key)
        if not to_node:
            to_node = self.add_node(key=to_node_key)

        # todo: add back the protection/detection that we get from MGraph class of allow_circle_edges and allow_duplicate_edges
        mermaid_edge = Mermaid__Edge(from_node=from_node, to_node=to_node, label=label, attributes=attributes)
        self.graph.edges.append(mermaid_edge)
        return mermaid_edge

    def add_directive(self, directive):
        self.renderer.config.directives.append(directive)
        return self

    def add_node(self, **kwargs):
        return self.graph.add_node(**kwargs)

    def code(self):
        return self.renderer.code(self.nodes(), self.edges())

    def code_markdown(self):
        #self.code_create()
        self.code()
        rendered_lines = self.renderer.mermaid_code
        markdown = ['#### Mermaid Graph',
                    "```mermaid"        ,
                    *rendered_lines     ,
                    "```"               ]

        return '\n'.join(markdown)

    def edges(self):
        return self.graph.edges

    def print_code(self):
        print(self.code())

    def nodes(self):
        return self.graph.nodes

    def set_direction(self, direction):
        if isinstance(direction, Diagram__Direction):
            self.renderer.diagram_direction = direction
        elif isinstance(direction, str) and direction in Diagram__Direction.__members__:
            self.renderer.diagram_direction = Diagram__Direction[direction]
        return self                             # If the value can't be set (not a valid name), do nothing

    def set_diagram_type(self, diagram_type):
        if isinstance(diagram_type, Diagram__Type):
            self.renderer.diagram_type = diagram_type

    def save(self, target_file=None):
        file_path = target_file or '/tmp/mermaid.md'

        with open(file_path, 'w') as file:
            file.write(self.code_markdown())
        return file_path

Classes

class Mermaid (**kwargs)

A mixin class to strictly assign keyword arguments to pre-defined instance attributes during initialization.

This base class provides an init method that assigns values from keyword arguments to instance attributes. If an attribute with the same name as a key from the kwargs is defined in the class, it will be set to the value from kwargs. If the key does not match any predefined attribute names, an exception is raised.

This behavior enforces strict control over the attributes of instances, ensuring that only predefined attributes can be set at the time of instantiation and avoids silent attribute creation which can lead to bugs in the code.

Usage

class MyConfigurableClass(Kwargs_To_Self): attribute1 = 'default_value' attribute2 = True attribute3 : str attribute4 : list attribute4 : int = 42

# Other methods can be added here

Correctly override default values by passing keyword arguments

instance = MyConfigurableClass(attribute1='new_value', attribute2=False)

This will raise an exception as 'attribute3' is not predefined

instance = MyConfigurableClass(attribute3='invalid_attribute')

this will also assign the default value to any variable that has a type defined. In the example above the default values (mapped by default__kwargs and locals) will be: attribute1 = 'default_value' attribute2 = True attribute3 = '' # default value of str attribute4 = [] # default value of list attribute4 = 42 # defined value in the class

Note

It is important that all attributes which may be set at instantiation are predefined in the class. Failure to do so will result in an exception being raised.

Methods

init(**kwargs): The initializer that handles the assignment of keyword arguments to instance attributes. It enforces strict attribute assignment rules, only allowing attributes that are already defined in the class to be set.

Initialize an instance of the derived class, strictly assigning provided keyword arguments to corresponding instance attributes.

Parameters

**kwargs: Variable length keyword arguments.

Raises

Exception
If a key from kwargs does not correspond to any attribute pre-defined in the class, an exception is raised to prevent setting an undefined attribute.
Expand source code
class Mermaid(Kwargs_To_Self):
    graph             : Mermaid__Graph
    renderer          : Mermaid__Renderer
    logger            : Python_Logger

    # todo add support for storing the data in sqlite so that the search for existing nodes is efficient
    def add_edge(self, from_node_key, to_node_key, label=None,attributes=None):
        nodes_by_id = self.graph.data().nodes__by_key()
        from_node   = nodes_by_id.get(from_node_key)
        to_node     = nodes_by_id.get(to_node_key)
        if not from_node:
            from_node = self.add_node(key=from_node_key)
        if not to_node:
            to_node = self.add_node(key=to_node_key)

        # todo: add back the protection/detection that we get from MGraph class of allow_circle_edges and allow_duplicate_edges
        mermaid_edge = Mermaid__Edge(from_node=from_node, to_node=to_node, label=label, attributes=attributes)
        self.graph.edges.append(mermaid_edge)
        return mermaid_edge

    def add_directive(self, directive):
        self.renderer.config.directives.append(directive)
        return self

    def add_node(self, **kwargs):
        return self.graph.add_node(**kwargs)

    def code(self):
        return self.renderer.code(self.nodes(), self.edges())

    def code_markdown(self):
        #self.code_create()
        self.code()
        rendered_lines = self.renderer.mermaid_code
        markdown = ['#### Mermaid Graph',
                    "```mermaid"        ,
                    *rendered_lines     ,
                    "```"               ]

        return '\n'.join(markdown)

    def edges(self):
        return self.graph.edges

    def print_code(self):
        print(self.code())

    def nodes(self):
        return self.graph.nodes

    def set_direction(self, direction):
        if isinstance(direction, Diagram__Direction):
            self.renderer.diagram_direction = direction
        elif isinstance(direction, str) and direction in Diagram__Direction.__members__:
            self.renderer.diagram_direction = Diagram__Direction[direction]
        return self                             # If the value can't be set (not a valid name), do nothing

    def set_diagram_type(self, diagram_type):
        if isinstance(diagram_type, Diagram__Type):
            self.renderer.diagram_type = diagram_type

    def save(self, target_file=None):
        file_path = target_file or '/tmp/mermaid.md'

        with open(file_path, 'w') as file:
            file.write(self.code_markdown())
        return file_path

Ancestors

Class variables

var graphMermaid__Graph
var loggerPython_Logger
var rendererMermaid__Renderer

Methods

def add_directive(self, directive)
Expand source code
def add_directive(self, directive):
    self.renderer.config.directives.append(directive)
    return self
def add_edge(self, from_node_key, to_node_key, label=None, attributes=None)
Expand source code
def add_edge(self, from_node_key, to_node_key, label=None,attributes=None):
    nodes_by_id = self.graph.data().nodes__by_key()
    from_node   = nodes_by_id.get(from_node_key)
    to_node     = nodes_by_id.get(to_node_key)
    if not from_node:
        from_node = self.add_node(key=from_node_key)
    if not to_node:
        to_node = self.add_node(key=to_node_key)

    # todo: add back the protection/detection that we get from MGraph class of allow_circle_edges and allow_duplicate_edges
    mermaid_edge = Mermaid__Edge(from_node=from_node, to_node=to_node, label=label, attributes=attributes)
    self.graph.edges.append(mermaid_edge)
    return mermaid_edge
def add_node(self, **kwargs)
Expand source code
def add_node(self, **kwargs):
    return self.graph.add_node(**kwargs)
def code(self)
Expand source code
def code(self):
    return self.renderer.code(self.nodes(), self.edges())
def code_markdown(self)
Expand source code
def code_markdown(self):
    #self.code_create()
    self.code()
    rendered_lines = self.renderer.mermaid_code
    markdown = ['#### Mermaid Graph',
                "```mermaid"        ,
                *rendered_lines     ,
                "```"               ]

    return '\n'.join(markdown)
def edges(self)
Expand source code
def edges(self):
    return self.graph.edges
def nodes(self)
Expand source code
def nodes(self):
    return self.graph.nodes
def print_code(self)
Expand source code
def print_code(self):
    print(self.code())
def save(self, target_file=None)
Expand source code
def save(self, target_file=None):
    file_path = target_file or '/tmp/mermaid.md'

    with open(file_path, 'w') as file:
        file.write(self.code_markdown())
    return file_path
def set_diagram_type(self, diagram_type)
Expand source code
def set_diagram_type(self, diagram_type):
    if isinstance(diagram_type, Diagram__Type):
        self.renderer.diagram_type = diagram_type
def set_direction(self, direction)
Expand source code
def set_direction(self, direction):
    if isinstance(direction, Diagram__Direction):
        self.renderer.diagram_direction = direction
    elif isinstance(direction, str) and direction in Diagram__Direction.__members__:
        self.renderer.diagram_direction = Diagram__Direction[direction]
    return self                             # If the value can't be set (not a valid name), do nothing

Inherited members