Module osbot_utils.graphs.mermaid.Mermaid__Node

Expand source code
from enum import Enum

from osbot_utils.base_classes.Kwargs_To_Self import Kwargs_To_Self
from osbot_utils.graphs.mermaid.configs.Mermaid__Node__Config import Mermaid__Node__Config
from osbot_utils.graphs.mermaid.models.Mermaid__Node__Shape import Mermaid__Node__Shape
from osbot_utils.graphs.mgraph.MGraph__Node import MGraph__Node
from osbot_utils.utils.Str import safe_str

LINE_PADDING = '    '

class Mermaid__Node(MGraph__Node):

    config : Mermaid__Node__Config

    def render_node(self, include_padding=True):
        left_char, right_char = self.config.node_shape.value

        if self.config.markdown:
            label = f'`{self.label}`'
        else:
            label = self.label


        if self.config.show_label is False:
            node_code = f'{self.key}'
        else:
            if self.config.wrap_with_quotes is False:
                node_code = f'{self.key}{left_char}{label}{right_char}'
            else:
                node_code = f'{self.key}{left_char}"{label}"{right_char}'

        if include_padding:
            node_code = f'{LINE_PADDING}{node_code}'
        return node_code

    def markdown(self, value=True):
        self.config.markdown = value
        return self

    def shape(self, shape=None):
        self.config.node_shape = Mermaid__Node__Shape.get_shape(shape)
        return self


    def shape_asymmetric        (self): self.config.node_shape = Mermaid__Node__Shape.asymmetric        ; return self
    def shape_circle            (self): self.config.node_shape = Mermaid__Node__Shape.circle            ; return self
    def shape_cylindrical       (self): self.config.node_shape = Mermaid__Node__Shape.cylindrical       ; return self
    def shape_default           (self): self.config.node_shape = Mermaid__Node__Shape.default           ; return self
    def shape_double_circle     (self): self.config.node_shape = Mermaid__Node__Shape.double_circle     ; return self
    def shape_hexagon           (self): self.config.node_shape = Mermaid__Node__Shape.hexagon           ; return self
    def shape_parallelogram     (self): self.config.node_shape = Mermaid__Node__Shape.parallelogram     ; return self
    def shape_parallelogram_alt (self): self.config.node_shape = Mermaid__Node__Shape.parallelogram_alt ; return self
    def shape_stadium           (self): self.config.node_shape = Mermaid__Node__Shape.stadium           ; return self
    def shape_subroutine        (self): self.config.node_shape = Mermaid__Node__Shape.subroutine        ; return self
    def shape_rectangle         (self): self.config.node_shape = Mermaid__Node__Shape.rectangle         ; return self
    def shape_rhombus           (self): self.config.node_shape = Mermaid__Node__Shape.rhombus           ; return self
    def shape_round_edges       (self): self.config.node_shape = Mermaid__Node__Shape.round_edges       ; return self
    def shape_trapezoid         (self): self.config.node_shape = Mermaid__Node__Shape.trapezoid         ; return self
    def shape_trapezoid_alt     (self): self.config.node_shape = Mermaid__Node__Shape.trapezoid_alt     ; return self



    def wrap_with_quotes(self, value=True):
        self.config.wrap_with_quotes = value
        return self

    def show_label(self, value=True):
        self.config.show_label = value
        return self

Classes

class Mermaid__Node (**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__Node(MGraph__Node):

    config : Mermaid__Node__Config

    def render_node(self, include_padding=True):
        left_char, right_char = self.config.node_shape.value

        if self.config.markdown:
            label = f'`{self.label}`'
        else:
            label = self.label


        if self.config.show_label is False:
            node_code = f'{self.key}'
        else:
            if self.config.wrap_with_quotes is False:
                node_code = f'{self.key}{left_char}{label}{right_char}'
            else:
                node_code = f'{self.key}{left_char}"{label}"{right_char}'

        if include_padding:
            node_code = f'{LINE_PADDING}{node_code}'
        return node_code

    def markdown(self, value=True):
        self.config.markdown = value
        return self

    def shape(self, shape=None):
        self.config.node_shape = Mermaid__Node__Shape.get_shape(shape)
        return self


    def shape_asymmetric        (self): self.config.node_shape = Mermaid__Node__Shape.asymmetric        ; return self
    def shape_circle            (self): self.config.node_shape = Mermaid__Node__Shape.circle            ; return self
    def shape_cylindrical       (self): self.config.node_shape = Mermaid__Node__Shape.cylindrical       ; return self
    def shape_default           (self): self.config.node_shape = Mermaid__Node__Shape.default           ; return self
    def shape_double_circle     (self): self.config.node_shape = Mermaid__Node__Shape.double_circle     ; return self
    def shape_hexagon           (self): self.config.node_shape = Mermaid__Node__Shape.hexagon           ; return self
    def shape_parallelogram     (self): self.config.node_shape = Mermaid__Node__Shape.parallelogram     ; return self
    def shape_parallelogram_alt (self): self.config.node_shape = Mermaid__Node__Shape.parallelogram_alt ; return self
    def shape_stadium           (self): self.config.node_shape = Mermaid__Node__Shape.stadium           ; return self
    def shape_subroutine        (self): self.config.node_shape = Mermaid__Node__Shape.subroutine        ; return self
    def shape_rectangle         (self): self.config.node_shape = Mermaid__Node__Shape.rectangle         ; return self
    def shape_rhombus           (self): self.config.node_shape = Mermaid__Node__Shape.rhombus           ; return self
    def shape_round_edges       (self): self.config.node_shape = Mermaid__Node__Shape.round_edges       ; return self
    def shape_trapezoid         (self): self.config.node_shape = Mermaid__Node__Shape.trapezoid         ; return self
    def shape_trapezoid_alt     (self): self.config.node_shape = Mermaid__Node__Shape.trapezoid_alt     ; return self



    def wrap_with_quotes(self, value=True):
        self.config.wrap_with_quotes = value
        return self

    def show_label(self, value=True):
        self.config.show_label = value
        return self

Ancestors

Class variables

var configMermaid__Node__Config

Methods

def markdown(self, value=True)
Expand source code
def markdown(self, value=True):
    self.config.markdown = value
    return self
def render_node(self, include_padding=True)
Expand source code
def render_node(self, include_padding=True):
    left_char, right_char = self.config.node_shape.value

    if self.config.markdown:
        label = f'`{self.label}`'
    else:
        label = self.label


    if self.config.show_label is False:
        node_code = f'{self.key}'
    else:
        if self.config.wrap_with_quotes is False:
            node_code = f'{self.key}{left_char}{label}{right_char}'
        else:
            node_code = f'{self.key}{left_char}"{label}"{right_char}'

    if include_padding:
        node_code = f'{LINE_PADDING}{node_code}'
    return node_code
def shape(self, shape=None)
Expand source code
def shape(self, shape=None):
    self.config.node_shape = Mermaid__Node__Shape.get_shape(shape)
    return self
def shape_asymmetric(self)
Expand source code
def shape_asymmetric        (self): self.config.node_shape = Mermaid__Node__Shape.asymmetric        ; return self
def shape_circle(self)
Expand source code
def shape_circle            (self): self.config.node_shape = Mermaid__Node__Shape.circle            ; return self
def shape_cylindrical(self)
Expand source code
def shape_cylindrical       (self): self.config.node_shape = Mermaid__Node__Shape.cylindrical       ; return self
def shape_default(self)
Expand source code
def shape_default           (self): self.config.node_shape = Mermaid__Node__Shape.default           ; return self
def shape_double_circle(self)
Expand source code
def shape_double_circle     (self): self.config.node_shape = Mermaid__Node__Shape.double_circle     ; return self
def shape_hexagon(self)
Expand source code
def shape_hexagon           (self): self.config.node_shape = Mermaid__Node__Shape.hexagon           ; return self
def shape_parallelogram(self)
Expand source code
def shape_parallelogram     (self): self.config.node_shape = Mermaid__Node__Shape.parallelogram     ; return self
def shape_parallelogram_alt(self)
Expand source code
def shape_parallelogram_alt (self): self.config.node_shape = Mermaid__Node__Shape.parallelogram_alt ; return self
def shape_rectangle(self)
Expand source code
def shape_rectangle         (self): self.config.node_shape = Mermaid__Node__Shape.rectangle         ; return self
def shape_rhombus(self)
Expand source code
def shape_rhombus           (self): self.config.node_shape = Mermaid__Node__Shape.rhombus           ; return self
def shape_round_edges(self)
Expand source code
def shape_round_edges       (self): self.config.node_shape = Mermaid__Node__Shape.round_edges       ; return self
def shape_stadium(self)
Expand source code
def shape_stadium           (self): self.config.node_shape = Mermaid__Node__Shape.stadium           ; return self
def shape_subroutine(self)
Expand source code
def shape_subroutine        (self): self.config.node_shape = Mermaid__Node__Shape.subroutine        ; return self
def shape_trapezoid(self)
Expand source code
def shape_trapezoid         (self): self.config.node_shape = Mermaid__Node__Shape.trapezoid         ; return self
def shape_trapezoid_alt(self)
Expand source code
def shape_trapezoid_alt     (self): self.config.node_shape = Mermaid__Node__Shape.trapezoid_alt     ; return self
def show_label(self, value=True)
Expand source code
def show_label(self, value=True):
    self.config.show_label = value
    return self
def wrap_with_quotes(self, value=True)
Expand source code
def wrap_with_quotes(self, value=True):
    self.config.wrap_with_quotes = value
    return self

Inherited members