-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
major improvement on MGraph__Json__Export
added use of new OSBot_Utils Json.json__equals__list_and_set method which helps with comparing dicts that have sets (whose order is not stable)
- Loading branch information
Showing
10 changed files
with
117 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,84 @@ | ||
from typing import Union, Dict, List, Optional, Any | ||
from mgraph_ai.providers.json.actions.exporters.MGraph__Export__Json__Dot import MGraph__Export__Json__Dot | ||
from mgraph_ai.providers.json.actions.exporters.MGraph__Export__Json__Mermaid import MGraph__Export__Json__Mermaid | ||
from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__Dict import Model__MGraph__Json__Node__Dict | ||
from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__List import Model__MGraph__Json__Node__List | ||
from mgraph_ai.providers.json.models.Model__MGraph__Json__Node__Value import Model__MGraph__Json__Node__Value | ||
from mgraph_ai.providers.json.schemas.Schema__MGraph__Json__Node__Value import Schema__MGraph__Json__Node__Value | ||
from osbot_utils.utils.Files import file_save | ||
from osbot_utils.utils.Json import json_dumps | ||
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__Dict import Domain__MGraph__Json__Node__Dict | ||
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__List import Domain__MGraph__Json__Node__List | ||
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Node__Value import Domain__MGraph__Json__Node__Value | ||
from mgraph_ai.providers.json.domain.Domain__MGraph__Json__Graph import Domain__MGraph__Json__Graph | ||
from mgraph_ai.mgraph.actions.MGraph__Export import MGraph__Export | ||
from mgraph_ai.mgraph.index.MGraph__Index import MGraph__Index | ||
from osbot_utils.helpers.Obj_Id import Obj_Id | ||
|
||
class MGraph__Json__Export(MGraph__Export): # JSON export handler | ||
class MGraph__Json__Export(MGraph__Export): | ||
graph: Domain__MGraph__Json__Graph | ||
|
||
def to_dict(self) -> Union[Dict, List, Any]: # Export to Python object | ||
def process_value_node(self, node_id: Obj_Id) -> Any: | ||
domain_node = self.graph.node(node_id) | ||
model_node = domain_node.node | ||
if isinstance(model_node, Model__MGraph__Json__Node__Value): | ||
return model_node.value | ||
return None | ||
|
||
def process_dict_node(self, node_id: Obj_Id, index: MGraph__Index) -> Dict[str, Any]: | ||
result = {} | ||
for edge_id in index.edges_ids__from__node_id(node_id): | ||
property_id = index.edge_to_nodes()[edge_id][1] | ||
property_node = self.graph.node(property_id) | ||
|
||
if property_node: | ||
property_name = property_node.node_data.name | ||
value_edges = index.edges_ids__from__node_id(property_id) | ||
if value_edges: | ||
value_node_id = index.edge_to_nodes()[value_edges[0]][1] | ||
result[property_name] = self.process_node(value_node_id, index) | ||
return result | ||
|
||
def process_list_node(self, node_id: Obj_Id, index: MGraph__Index) -> List[Any]: | ||
result = [] | ||
for edge_id in index.edges_ids__from__node_id(node_id): | ||
item_id = index.edge_to_nodes()[edge_id][1] | ||
item_value = self.process_node(item_id, index) | ||
result.append(item_value) | ||
return result | ||
|
||
def process_node(self, node_id: Obj_Id, index: MGraph__Index) -> Any: | ||
domain_node = self.graph.node(node_id) | ||
model_node = domain_node.node | ||
if not domain_node: | ||
return None | ||
|
||
if isinstance(model_node, Model__MGraph__Json__Node__Value): return self.process_value_node(node_id) | ||
elif isinstance(model_node, Model__MGraph__Json__Node__Dict ): return self.process_dict_node (node_id, index) | ||
elif isinstance(model_node, Model__MGraph__Json__Node__List ): return self.process_list_node (node_id, index) | ||
return None | ||
|
||
def to_dict(self) -> Union[Dict, List, Any]: | ||
root_content = self.graph.root_content() | ||
if not root_content: | ||
return None | ||
|
||
if isinstance(root_content, Domain__MGraph__Json__Node__Dict): | ||
return root_content.properties() | ||
elif isinstance(root_content, Domain__MGraph__Json__Node__List): | ||
return root_content.items() | ||
elif isinstance(root_content, Domain__MGraph__Json__Node__Value): | ||
return root_content.value | ||
index = MGraph__Index.from_graph(self.graph) # todo: replace with index = self.data().index() | ||
return self.process_node(root_content.node_id, index) | ||
|
||
def to_string(self, indent: Optional[int] = None) -> str: # Export to JSON string | ||
def to_string(self, indent: Optional[int] = None) -> str: | ||
data = self.to_dict() | ||
return json_dumps(data, indent=indent) | ||
|
||
def to_dot(self): | ||
return MGraph__Export__Json__Dot(graph=self.graph) | ||
|
||
def to_file(self, file_path: str, indent: Optional[int] = None) -> bool: # Export to JSON file | ||
def to_file(self, file_path: str, indent: Optional[int] = None) -> bool: | ||
file_contents = self.to_string(indent=indent) | ||
if file_contents: | ||
file_save(contents=file_contents, path=file_path) | ||
return True | ||
return False | ||
|
||
def to_dot(self): | ||
return MGraph__Export__Json__Dot(graph=self.graph) | ||
|
||
def to_mermaid(self) -> MGraph__Export__Json__Mermaid: | ||
return MGraph__Export__Json__Mermaid(graph=self.graph) | ||
return MGraph__Export__Json__Mermaid(graph=self.graph) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters