Module osbot_utils.graphs.mermaid.Mermaid__Renderer

Expand source code
from typing import List

from osbot_utils.base_classes.Kwargs_To_Self                        import Kwargs_To_Self
from osbot_utils.graphs.mermaid.configs.Mermaid__Render__Config import Mermaid__Render__Config
from osbot_utils.graphs.mermaid.models.Mermaid__Diagram_Direction   import Diagram__Direction
from osbot_utils.graphs.mermaid.models.Mermaid__Diagram__Type       import Diagram__Type


class Mermaid__Renderer(Kwargs_To_Self):
    config            : Mermaid__Render__Config
    mermaid_code      : List
    diagram_direction : Diagram__Direction = Diagram__Direction.LR
    diagram_type      : Diagram__Type      = Diagram__Type.graph


    def add_line(self, line):
        self.mermaid_code.append(line)
        return line

    def code(self, nodes, edges):
        self.code_create(nodes, edges)
        return '\n'.join(self.mermaid_code)

    def code_create(self, nodes, edges, recreate=False):
        with self as _:
            if recreate:                            # if recreate is True, reset the code
                _.reset_code()
            elif self.mermaid_code:                 # if the code has already been created, don't create it
                return self                         #   todo: find a better way to do this, namely around the concept of auto detecting (on change) when the recreation needs to be done (vs being able to use the previously calculated data)
            for directive in _.config.directives:
                _.add_line(f'%%{{{directive}}}%%')
            _.add_line(self.graph_header())
            if self.config.add_nodes:
                for node in nodes:
                    node_code = node.render_node()
                    _.add_line(node_code)
            if self.config.line_before_edges:
                _.add_line('')
            for edge in edges:
                edge_code = edge.render_edge()
                _.add_line(edge_code)
        return self



    def graph_header(self):
        # if type(self.diagram_type.value) is str:
        #     value = self.diagram_type.value
        # else:
        #     value = self.diagram_type.name
        value = self.diagram_type.name
        return f'{value} {self.diagram_direction.name}'

    def reset_code(self):
        self.mermaid_code = []
        return self

Classes

class Mermaid__Renderer (**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__Renderer(Kwargs_To_Self):
    config            : Mermaid__Render__Config
    mermaid_code      : List
    diagram_direction : Diagram__Direction = Diagram__Direction.LR
    diagram_type      : Diagram__Type      = Diagram__Type.graph


    def add_line(self, line):
        self.mermaid_code.append(line)
        return line

    def code(self, nodes, edges):
        self.code_create(nodes, edges)
        return '\n'.join(self.mermaid_code)

    def code_create(self, nodes, edges, recreate=False):
        with self as _:
            if recreate:                            # if recreate is True, reset the code
                _.reset_code()
            elif self.mermaid_code:                 # if the code has already been created, don't create it
                return self                         #   todo: find a better way to do this, namely around the concept of auto detecting (on change) when the recreation needs to be done (vs being able to use the previously calculated data)
            for directive in _.config.directives:
                _.add_line(f'%%{{{directive}}}%%')
            _.add_line(self.graph_header())
            if self.config.add_nodes:
                for node in nodes:
                    node_code = node.render_node()
                    _.add_line(node_code)
            if self.config.line_before_edges:
                _.add_line('')
            for edge in edges:
                edge_code = edge.render_edge()
                _.add_line(edge_code)
        return self



    def graph_header(self):
        # if type(self.diagram_type.value) is str:
        #     value = self.diagram_type.value
        # else:
        #     value = self.diagram_type.name
        value = self.diagram_type.name
        return f'{value} {self.diagram_direction.name}'

    def reset_code(self):
        self.mermaid_code = []
        return self

Ancestors

Class variables

var configMermaid__Render__Config
var diagram_directionDiagram__Direction
var diagram_typeDiagram__Type
var mermaid_code : List

Methods

def add_line(self, line)
Expand source code
def add_line(self, line):
    self.mermaid_code.append(line)
    return line
def code(self, nodes, edges)
Expand source code
def code(self, nodes, edges):
    self.code_create(nodes, edges)
    return '\n'.join(self.mermaid_code)
def code_create(self, nodes, edges, recreate=False)
Expand source code
def code_create(self, nodes, edges, recreate=False):
    with self as _:
        if recreate:                            # if recreate is True, reset the code
            _.reset_code()
        elif self.mermaid_code:                 # if the code has already been created, don't create it
            return self                         #   todo: find a better way to do this, namely around the concept of auto detecting (on change) when the recreation needs to be done (vs being able to use the previously calculated data)
        for directive in _.config.directives:
            _.add_line(f'%%{{{directive}}}%%')
        _.add_line(self.graph_header())
        if self.config.add_nodes:
            for node in nodes:
                node_code = node.render_node()
                _.add_line(node_code)
        if self.config.line_before_edges:
            _.add_line('')
        for edge in edges:
            edge_code = edge.render_edge()
            _.add_line(edge_code)
    return self
def graph_header(self)
Expand source code
def graph_header(self):
    # if type(self.diagram_type.value) is str:
    #     value = self.diagram_type.value
    # else:
    #     value = self.diagram_type.name
    value = self.diagram_type.name
    return f'{value} {self.diagram_direction.name}'
def reset_code(self)
Expand source code
def reset_code(self):
    self.mermaid_code = []
    return self

Inherited members