Skip to content

Commit

Permalink
more major refactoring on Mermaids class
Browse files Browse the repository at this point in the history
wired up more ignored tests like test_Mermaid__Render
refactored Mermaid__Render__Config into Schema__Mermaid__Render__Config
  • Loading branch information
DinisCruz committed Jan 11, 2025
1 parent 33439a5 commit aeb976a
Show file tree
Hide file tree
Showing 26 changed files with 258 additions and 302 deletions.
2 changes: 1 addition & 1 deletion mgraph_ai/mgraph/actions/MGraph__Edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class MGraph__Edit(Type_Safe):
graph: MGraph__Graph

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

Expand Down
24 changes: 14 additions & 10 deletions mgraph_ai/mgraph/domain/MGraph__Edge.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from typing import Any, List
from osbot_utils.helpers.Random_Guid import Random_Guid
from osbot_utils.helpers.Safe_Id import Safe_Id
from mgraph_ai.mgraph.domain.MGraph__Node import MGraph__Node
from mgraph_ai.mgraph.models.Model__MGraph__Attribute import Model__MGraph__Attribute
from mgraph_ai.mgraph.schemas.Schema__MGraph__Attribute import Schema__MGraph__Attribute
from mgraph_ai.mgraph.domain.MGraph__Attribute import MGraph__Attribute
from mgraph_ai.mgraph.models.Model__MGraph__Edge import Model__MGraph__Edge
from mgraph_ai.mgraph.models.Model__MGraph__Graph import Model__MGraph__Graph
from osbot_utils.type_safe.Type_Safe import Type_Safe
from typing import Any, List
from mgraph_ai.mgraph.schemas.Schema__MGraph__Edge__Config import Schema__MGraph__Edge__Config
from osbot_utils.helpers.Random_Guid import Random_Guid
from osbot_utils.helpers.Safe_Id import Safe_Id
from mgraph_ai.mgraph.domain.MGraph__Node import MGraph__Node
from mgraph_ai.mgraph.models.Model__MGraph__Attribute import Model__MGraph__Attribute
from mgraph_ai.mgraph.schemas.Schema__MGraph__Attribute import Schema__MGraph__Attribute
from mgraph_ai.mgraph.domain.MGraph__Attribute import MGraph__Attribute
from mgraph_ai.mgraph.models.Model__MGraph__Edge import Model__MGraph__Edge
from mgraph_ai.mgraph.models.Model__MGraph__Graph import Model__MGraph__Graph
from osbot_utils.type_safe.Type_Safe import Type_Safe

class MGraph__Edge(Type_Safe): # Domain class for edges
edge : Model__MGraph__Edge # Reference to edge model
graph: Model__MGraph__Graph # Reference to graph model

def config(self) -> Schema__MGraph__Edge__Config:
return self.edge.data.edge_config

def edge_id(self) -> Random_Guid: # Get edge ID
return self.edge.data.edge_config.edge_id

Expand Down
53 changes: 32 additions & 21 deletions mgraph_ai/providers/mermaid/actions/Mermaid__Edit.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from typing import Dict
from mgraph_ai.mgraph.schemas.Schema__MGraph__Attribute import Schema__MGraph__Attribute
from mgraph_ai.providers.mermaid.domain.Mermaid__Edge import Mermaid__Edge
from osbot_utils.helpers.Safe_Id import Safe_Id
from mgraph_ai.providers.mermaid.actions.Mermaid__Data import Mermaid__Data
from mgraph_ai.providers.mermaid.actions.Mermaid__Render import Mermaid__Render
from mgraph_ai.providers.mermaid.configs.Mermaid__Render__Config import Mermaid__Render__Config
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Diagram__Type import Schema__Mermaid__Diagram__Type
from osbot_utils.decorators.methods.cache_on_self import cache_on_self
from mgraph_ai.mgraph.actions.MGraph__Edit import MGraph__Edit
from mgraph_ai.providers.mermaid.domain.Mermaid__Graph import Mermaid__Graph
from typing import Dict

from mgraph_ai.providers.mermaid.domain.Mermaid__Node import Mermaid__Node
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Render__Config import Schema__Mermaid__Render__Config
from osbot_utils.utils.Misc import random_text
from mgraph_ai.mgraph.schemas.Schema__MGraph__Attribute import Schema__MGraph__Attribute
from mgraph_ai.providers.mermaid.domain.Mermaid__Edge import Mermaid__Edge
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Diagram_Direction import Schema__Mermaid__Diagram__Direction
from osbot_utils.helpers.Safe_Id import Safe_Id
from mgraph_ai.providers.mermaid.actions.Mermaid__Data import Mermaid__Data
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Diagram__Type import Schema__Mermaid__Diagram__Type
from osbot_utils.decorators.methods.cache_on_self import cache_on_self
from mgraph_ai.mgraph.actions.MGraph__Edit import MGraph__Edit
from mgraph_ai.providers.mermaid.domain.Mermaid__Graph import Mermaid__Graph


class Mermaid__Edit(MGraph__Edit):
Expand Down Expand Up @@ -41,20 +44,28 @@ def add_edge(self, from_node_key:Safe_Id, to_node_key:Safe_Id, label:str=None, a
return edge


def code(self) -> str:
return self.graph_render().code()

@cache_on_self
def data(self):
return Mermaid__Data(graph=self.graph) # todo: look at the best way to do this (i.e. give access to this class the info inside data)

@cache_on_self
def graph_render(self) -> Mermaid__Render: # todo: review this since we really shouldn't need be able to access the Mermaid__Render here
return Mermaid__Render(graph=self.graph)
def new_edge(self):
from_node_key = random_text('node', lowercase=True)
to_node_key = random_text('node', lowercase=True)
return self.add_edge(from_node_key, to_node_key)

def render_config(self) -> Mermaid__Render__Config: # todo: review this since we really should be able to access the Mermaid__Render__Config outside the Mermaid__Render object
return self.graph_render().config
def render_config(self) -> Schema__Mermaid__Render__Config: # todo: review this since we really should be able to access the Mermaid__Render__Config outside the Mermaid__Render object
return self.graph.model.data.render_config

def set_diagram_type(self, diagram_type):
def set_diagram_type(self, diagram_type): # todo: should this be moved into the render class?
if isinstance(diagram_type, Schema__Mermaid__Diagram__Type):
self.graph_render().diagram_type = diagram_type
self.render_config().diagram_type = diagram_type

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

def new_node(self, **kwargs) -> Mermaid__Node:
return super().new_node(**kwargs)
53 changes: 32 additions & 21 deletions mgraph_ai/providers/mermaid/actions/Mermaid__Render.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
from osbot_utils.utils.Str import safe_str
from mgraph_ai.providers.mermaid.domain.Mermaid__Node import LINE_PADDING, Mermaid__Node
from mgraph_ai.providers.mermaid.domain.Mermaid__Graph import Mermaid__Graph
from mgraph_ai.providers.mermaid.configs.Mermaid__Render__Config import Mermaid__Render__Config
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Diagram_Direction import Schema__Mermaid__Diagram__Direction
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Diagram__Type import Schema__Mermaid__Diagram__Type
from osbot_utils.type_safe.Type_Safe import Type_Safe


class Mermaid__Render(Type_Safe):
config : Mermaid__Render__Config
diagram_direction : Schema__Mermaid__Diagram__Direction = Schema__Mermaid__Diagram__Direction.LR
diagram_type : Schema__Mermaid__Diagram__Type = Schema__Mermaid__Diagram__Type.graph
graph : Mermaid__Graph
mermaid_code : List

Expand All @@ -30,40 +26,48 @@ def code_create(self, recreate=False):
_.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:
for directive in _.config().directives:
_.add_line(f'%%{{{directive}}}%%')
_.add_line(self.graph_header())
if self.config.add_nodes:
if self.config().add_nodes:
for node in self.graph.nodes():
node_code = self.render_node(node)
_.add_line(node_code)
if self.config.line_before_edges:
if self.config().line_before_edges:
_.add_line('')
for edge in self.graph.edges():
edge_code = self.render_edge(edge)
_.add_line(edge_code)
return self

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

return '\n'.join(markdown)

def config(self):
return self.graph.model.data.render_config

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}'
value = self.config().diagram_type.name
return f'{value} {self.config().diagram_direction.name}'

def render_edge(self,edge):
from_node = self.graph.node(edge.from_node_id)
to_node = self.graph.node(edge.to_node_id)
from_node_key = safe_str(from_node.key)
to_node_key = safe_str(to_node .key)
if edge.config.output_node_from:
from_node = self.graph.node(edge.from_node_id())
to_node = self.graph.node(edge.to_node_id ())
from_node_key = safe_str(from_node.node_key())
to_node_key = safe_str(to_node .node_key())
if edge.config().output_node_from:
from_node_key = self.render_node(from_node, include_padding=False)
if edge.config.output_node_to:
if edge.config().output_node_to:
to_node_key = self.render_node(to_node, include_padding=False )
if edge.config.edge_mode == 'lr_using_pipe':
if edge.config().edge_mode == 'lr_using_pipe':
link_code = f'-->|{edge.label}|'
elif edge.label:
link_code = f'--"{edge.label}"-->'
Expand Down Expand Up @@ -100,4 +104,11 @@ def render_node(self, node: Mermaid__Node, include_padding=True):

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

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
6 changes: 0 additions & 6 deletions mgraph_ai/providers/mermaid/configs/Mermaid__Edge__Config.py

This file was deleted.

This file was deleted.

28 changes: 20 additions & 8 deletions mgraph_ai/providers/mermaid/domain/Mermaid__Edge.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
from mgraph_ai.mgraph.domain.MGraph__Edge import MGraph__Edge
from mgraph_ai.providers.mermaid.models.Model__Mermaid__Edge import Model__Mermaid__Edge
from mgraph_ai.providers.mermaid.models.Model__Mermaid__Graph import Model__Mermaid__Graph
from mgraph_ai.mgraph.domain.MGraph__Edge import MGraph__Edge
from mgraph_ai.providers.mermaid.models.Model__Mermaid__Edge import Model__Mermaid__Edge
from mgraph_ai.providers.mermaid.models.Model__Mermaid__Graph import Model__Mermaid__Graph
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Edge__Config import Schema__Mermaid__Edge__Config
from osbot_utils.type_safe.methods.type_safe_property import set_as_property


class Mermaid__Edge(MGraph__Edge):
edge : Model__Mermaid__Edge
graph : Model__Mermaid__Graph
label : str
label = set_as_property('edge.data', 'label')


def config(self) -> Schema__Mermaid__Edge__Config:
return super().config()

def edge_mode(self, edge_mode):
self.config.edge_mode = edge_mode
self.config().edge_mode = edge_mode
return self

def edge_mode__lr_using_pipe(self):
return self.edge_mode('lr_using_pipe')

def from_node_id(self):
return self.edge.from_node_id()

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

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

def to_node_id(self):
return self.edge.to_node_id()
10 changes: 5 additions & 5 deletions mgraph_ai/providers/mermaid/domain/Mermaid__Graph.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Type
from mgraph_ai.mgraph.domain.MGraph__Graph import MGraph__Graph
from mgraph_ai.providers.mermaid.domain.Mermaid__Edge import Mermaid__Edge
from mgraph_ai.providers.mermaid.domain.Mermaid__Node import Mermaid__Node
from mgraph_ai.providers.mermaid.models.Model__Mermaid__Graph import Model__Mermaid__Graph
from typing import Type
from mgraph_ai.mgraph.domain.MGraph__Graph import MGraph__Graph
from mgraph_ai.providers.mermaid.domain.Mermaid__Edge import Mermaid__Edge
from mgraph_ai.providers.mermaid.domain.Mermaid__Node import Mermaid__Node
from mgraph_ai.providers.mermaid.models.Model__Mermaid__Graph import Model__Mermaid__Graph

class Mermaid__Graph(MGraph__Graph):
model : Model__Mermaid__Graph
Expand Down
6 changes: 6 additions & 0 deletions mgraph_ai/providers/mermaid/domain/Mermaid__Node.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
from typing import Any

from mgraph_ai.mgraph.domain.MGraph__Node import MGraph__Node
from mgraph_ai.providers.mermaid.models.Model__Mermaid__Graph import Model__Mermaid__Graph
from mgraph_ai.providers.mermaid.models.Model__Mermaid__Node import Model__Mermaid__Node
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Node__Shape import Schema__Mermaid__Node__Shape
from osbot_utils.type_safe.methods.type_safe_property import set_as_property

LINE_PADDING = ' '

class Mermaid__Node(MGraph__Node):
node : Model__Mermaid__Node
graph: Model__Mermaid__Graph

label = set_as_property('node.data', 'label')
key = set_as_property('node.data', 'key')

def config(self):
return self.node.data.node_config

Expand Down
75 changes: 0 additions & 75 deletions mgraph_ai/providers/mermaid/domain/__to_do__Mermaid.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from mgraph_ai.mgraph.schemas.Schema__MGraph__Edge__Config import Schema__MGraph__Edge__Config
from mgraph_ai.providers.mermaid.schemas.Schema__Mermaid__Node import Schema__Mermaid__Node


class Schema__Mermaid__Edge__Config(Schema__MGraph__Edge__Config):
from_node_type: Type[Schema__Mermaid__Node]
to_node_type : Type[Schema__Mermaid__Node]
edge_mode : str
output_node_from : bool = False
output_node_to : bool = False
Loading

0 comments on commit aeb976a

Please sign in to comment.