Skip to content

Commit

Permalink
continued the refactoring of the Dot code into MGraph__Export__Dot
Browse files Browse the repository at this point in the history
  • Loading branch information
DinisCruz committed Feb 2, 2025
1 parent 212c211 commit 9dc6693
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 42 deletions.
44 changes: 11 additions & 33 deletions mgraph_db/mgraph/actions/MGraph__Export.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
from xml.dom import minidom
from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement
from mgraph_db.mgraph.actions.exporters.MGraph__Export__Dot import MGraph__Export__Dot
from mgraph_db.mgraph.actions.exporters.MGraph__Export__Dot import MGraph__Export__Dot, MGraph__Export__Dot__Config
from osbot_utils.utils.Files import temp_file, file_create
from mgraph_db.mgraph.actions.MGraph__Data import MGraph__Data
from mgraph_db.mgraph.domain.Domain__MGraph__Graph import Domain__MGraph__Graph
from osbot_utils.type_safe.Type_Safe import Type_Safe

class MGraph__Export(Type_Safe):
graph: Domain__MGraph__Graph
graph : Domain__MGraph__Graph
dot_config : MGraph__Export__Dot__Config

def data(self): # Access to graph data
return MGraph__Data(graph=self.graph)

def export_dot(self):
return MGraph__Export__Dot(graph=self.graph)
return MGraph__Export__Dot(graph=self.graph, config=self.dot_config)

def to__mgraph_json(self): # Export full graph data
return self.graph.model.data.json()
Expand Down Expand Up @@ -56,36 +57,13 @@ def to__xml(self) -> str:
to_elem.text = str(edge.to_node_id())
return self.format_xml(root, indent=' ')

def to__dot(self, show_value=False, show_edge_ids=True) -> str: # Export as DOT graph
lines = ['digraph {']

with self.data() as _:
for node in _.nodes(): # Output nodes with data
node_attrs = []
if node.node_data:
node_items = node.node_data.__dict__.items()
if node_items:
for field_name, field_value in node_items:
node_attrs.append(f'{field_name}="{field_value}"')
if show_value and (field_name =='value' or field_name =='name'):
node_attrs.append(f'label="{field_value}"')
else:
if show_value:
label = type(node.node.data).__name__.split('__').pop().lower()
node_attrs.append(f'label="{label}"')

attrs_str = f' [{", ".join(node_attrs)}]' if node_attrs else ''
lines.append(f' "{node.node_id}"{attrs_str}')

for edge in _.edges(): # Output edges with IDs
if show_edge_ids:
edge_label = f" {edge.edge_id}"
else:
edge_label = ""
lines.append(f' "{edge.from_node_id()}" -> "{edge.to_node_id()}" [label="{edge_label}"]')

lines.append('}')
return '\n'.join(lines)
def to__dot(self, show_value=False, show_edge_ids=True) -> str: # Export as DOT graph
# dot_exporter = MGraph__Export__Dot(graph = self.graph ,
# config = MGraph__Export__Dot__Config(
# show_value = show_value ,
# show_edge_ids = show_edge_ids))
return self.export_dot().process_graph()
#return dot_exporter.format_output()

def to__dot_types(self):
return self.export_dot().to_types_view()
Expand Down
10 changes: 10 additions & 0 deletions mgraph_db/mgraph/actions/MGraph__Screenshot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Type
from mgraph_db.mgraph.actions.MGraph__Export import MGraph__Export
from mgraph_db.mgraph.domain.Domain__MGraph__Graph import Domain__MGraph__Graph
from osbot_utils.decorators.methods.cache_on_self import cache_on_self
from osbot_utils.type_safe.Type_Safe import Type_Safe
from osbot_utils.utils.Env import get_env, not_in_github_action
from osbot_utils.utils.Files import file_create_from_bytes
Expand All @@ -22,6 +23,14 @@ class MGraph__Screenshot(Type_Safe):
def dot_to_png(self, dot_code):
return self.create_screenshot__from__dot_code(dot_code=dot_code)

def dot(self):
dot_code = self.export().to__dot()
png_bytes = self.dot_to_png(dot_code)
return png_bytes

def dot_config(self):
return self.export().dot_config

def dot__just_ids(self):
dot_code = self.export().to__dot()
png_bytes = self.dot_to_png(dot_code)
Expand All @@ -48,6 +57,7 @@ def create_screenshot__from__dot_code(self, dot_code):
method_params = {'dot_source': dot_code}
return self.execute_request(method_path, method_params)

@cache_on_self
def export(self):
return self.export_class(graph=self.graph)

Expand Down
5 changes: 0 additions & 5 deletions mgraph_db/mgraph/actions/exporters/MGraph__Export__Dot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Dict, Any, List, Optional
from mgraph_db.mgraph.actions.exporters.MGraph__Export__Base import MGraph__Export__Base
from mgraph_db.mgraph.domain.Domain__MGraph__Graph import Domain__MGraph__Graph
from osbot_utils.type_safe.Type_Safe import Type_Safe

class MGraph__Export__Dot__Config(Type_Safe):
Expand All @@ -13,10 +12,6 @@ class MGraph__Export__Dot__Config(Type_Safe):
class MGraph__Export__Dot(MGraph__Export__Base):
config: MGraph__Export__Dot__Config

def __init__(self, graph: Domain__MGraph__Graph, config: Optional[MGraph__Export__Dot__Config] = None):
super().__init__(graph=graph)
self.config = config or MGraph__Export__Dot__Config()

def create_node_data(self, node) -> Dict[str, Any]: # Override to create DOT-specific node data
attrs = []
if node.node_data:
Expand Down
21 changes: 19 additions & 2 deletions tests/unit/mgraph/actions/exporters/test_MGraph__Export__Dot.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from unittest import TestCase
from mgraph_db.mgraph.actions.MGraph__Export import MGraph__Export
from mgraph_db.mgraph.actions.exporters.MGraph__Export__Dot import MGraph__Export__Dot, MGraph__Export__Dot__Config
from mgraph_db.mgraph.domain.Domain__MGraph__Graph import Domain__MGraph__Graph
from mgraph_db.mgraph.models.Model__MGraph__Graph import Model__MGraph__Graph
from mgraph_db.mgraph.schemas.Schema__MGraph__Graph import Schema__MGraph__Graph
from mgraph_db.providers.simple.MGraph__Simple__Test_Data import MGraph__Simple__Test_Data
from osbot_utils.utils.Env import load_dotenv
from osbot_utils.utils.Files import file_exists, file_delete


class test_MGraph__Export__Dot(TestCase):

Expand Down Expand Up @@ -160,6 +162,21 @@ def test_custom_config(self):
assert 'ranksep=1.2' in styled_output

def test__mgraph_export(self):
with MGraph__Export(graph=self.domain_graph) as _:
with self.simple_graph.export() as _:
assert _.to__dot_types () == self.exporter.to_types_view ()
assert _.to__dot_schema() == self.exporter.to_schema_view()
assert _.to__dot () == self.exporter.process_graph()


def test__mgraph_screenshot(self):
load_dotenv()
target_file = './dot-file.png'

with self.simple_graph.screenshot(target_file=target_file) as _:
#_.dot__just_values()
#_.dot__just_types()
_.dot_config().show_value = True
_.dot_config().show_edge_ids = False
_.dot()
assert file_exists(target_file)
assert file_delete(target_file)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setUpClass(cls):
pytest.skip("Tests need fixing after Time_Series class is fixed")
load_dotenv()
cls.screenshot_file = './time-series.png'
cls.delete_on_exit = False
cls.delete_on_exit = True

def setUp(self):
self.graph = MGraph__Time_Series() # Create fresh graph
Expand All @@ -23,7 +23,9 @@ def setUp(self):

def tearDown(self):
with self.graph.screenshot(target_file=self.screenshot_file) as _:
_.dot__just_types()
_.dot_config().show_value = True
_.dot_config().show_edge_ids = False
_.dot()
assert file_exists(self.screenshot_file)
if self.delete_on_exit:
assert file_delete(self.screenshot_file) is True
Expand Down

0 comments on commit 9dc6693

Please sign in to comment.