Skip to content

Commit

Permalink
fix to address #17 and add new ig_kwargs argument
Browse files Browse the repository at this point in the history
  • Loading branch information
dmulyalin committed Feb 11, 2023
1 parent 3594e00 commit 72f9ac3
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 100 deletions.
18 changes: 10 additions & 8 deletions N2G/plugins/diagrams/N2G_DrawIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,20 @@ def dump_file(self, filename=None, folder="./Output/"):
with open(os.path.join(folder, filename), "w") as outfile:
outfile.write(self.dump_xml())

def layout(self, algo="kk", **kwargs):
def layout(self, algo="kk", ig_kwargs=None, **kwargs):
"""
Method to calculate graph layout using Python
`igraph <https://igraph.org/python/doc/tutorial/tutorial.html#layout-algorithms>`_
`igraph <https://igraph.org/python/versions/latest/tutorial.html>`_
library
**Parameters**
* ``algo`` (str) name of layout algorithm to use, default is 'kk'. Reference
`Layout algorithms` table below for valid algo names
* ``ig_kwargs`` (dict) arguments to use to instantiate igraph's Graph instance
as per `documentation <https://igraph.org/python/versions/latest/api/igraph.Graph.html#__init__>`_
* ``kwargs`` any additional kwargs to pass to igraph ``Graph.layout`` method
as per `documentation <https://igraph.org/python/versions/latest/api/igraph.Graph.html#layout>`_
**Layout algorithms**
Expand Down Expand Up @@ -492,19 +495,18 @@ def layout(self, algo="kk", **kwargs):
raise SystemExit(
"Failed to import igraph, install - pip install python-igraph"
)
ig_kwargs = ig_kwargs or {}
# iterate over diagrams and layout elements
for diagram in self.drawing.findall("./diagram"):
igraph_graph = ig()
igraph_graph = ig(**ig_kwargs)
self.go_to_diagram(diagram.attrib["name"])
# populate igraph with nodes and edges from object tags
for item in self.current_root.iterfind("./object"):
# add edges, item[0] refernece to object's mxCell child tag
if item[0].get("source") and item[0].get("target"):
igraph_graph.add_vertex(name=item[0].get("source"))
igraph_graph.add_vertex(name=item[0].get("target"))
igraph_graph.add_edge(
source=item[0].get("source"), target=item[0].get("target")
)
src = igraph_graph.add_vertex(name=item[0].get("source"))
tgt = igraph_graph.add_vertex(name=item[0].get("target"))
igraph_graph.add_edge(source=src, target=tgt)
# add nodes
else:
igraph_graph.add_vertex(name=item.get("id"))
Expand Down
50 changes: 25 additions & 25 deletions tests/Output/should_be_test_xlsx_data_base.drawio
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
<mxfile compressed="false" type="device">
<mxfile type="device" compressed="false">
<diagram id="Page-1" name="Page-1">
<mxGraphModel arrows="1" connect="1" dx="1360" dy="864" fold="1" grid="1" gridSize="10" guides="1" math="0" page="1" pageHeight="1169" pageScale="1" pageWidth="827" shadow="1" tooltips="1">
<mxGraphModel dx="1360" dy="864" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="1">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<object bottom_label="core" description="Core Router" id="r1" label="r1" pic="" top_label="1.1.1.1">
<mxCell parent="1" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1">
<mxGeometry as="geometry" height="60" width="120" x="617" y="0" />
<object id="r1" label="r1" pic="" bottom_label="core" top_label="1.1.1.1" description="Core Router">
<mxCell style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="472" y="848" width="120" height="60" as="geometry" />
</mxCell>
</object><object bottom_label="core" description="Core Router" id="r2" label="r2" pic="" top_label="2.2.2.2">
<mxCell parent="1" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1">
<mxGeometry as="geometry" height="60" width="120" x="1089" y="535" />
</object><object id="r2" label="r2" pic="" bottom_label="core" top_label="2.2.2.2" description="Core Router">
<mxCell style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="241" y="533" width="120" height="60" as="geometry" />
</mxCell>
</object><object bottom_label="edge" description="Edge Router" id="r3" label="r3" pic="" top_label="3.3.3.3">
<mxCell parent="1" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1">
<mxGeometry as="geometry" height="60" width="120" x="864" y="864" />
</object><object id="r3" label="r3" pic="" bottom_label="edge" top_label="3.3.3.3" description="Edge Router">
<mxCell style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="702" y="0" width="120" height="60" as="geometry" />
</mxCell>
</object><mxCell connectable="0" id="4c783d026b64f7f34abadd5be66e427d-src" parent="4c783d026b64f7f34abadd5be66e427d" style="labelBackgroundColor=#ffffff;;" value="Gi1/1" vertex="1">
<mxGeometry as="geometry" relative="1" x="-0.5">
</object><mxCell id="4c783d026b64f7f34abadd5be66e427d-src" value="Gi1/1" style="labelBackgroundColor=#ffffff;;" vertex="1" connectable="0" parent="4c783d026b64f7f34abadd5be66e427d">
<mxGeometry x="-0.5" relative="1" as="geometry">
<mxPoint as="offset" />
</mxGeometry>
</mxCell><mxCell connectable="0" id="4c783d026b64f7f34abadd5be66e427d-trgt" parent="4c783d026b64f7f34abadd5be66e427d" style="labelBackgroundColor=#ffffff;;" value="Gi3/4" vertex="1">
<mxGeometry as="geometry" relative="-1" x="0.5">
</mxCell><mxCell id="4c783d026b64f7f34abadd5be66e427d-trgt" value="Gi3/4" style="labelBackgroundColor=#ffffff;;" vertex="1" connectable="0" parent="4c783d026b64f7f34abadd5be66e427d">
<mxGeometry x="0.5" relative="-1" as="geometry">
<mxPoint as="offset" />
</mxGeometry>
</mxCell><object description="DF link between R1 and R2" id="4c783d026b64f7f34abadd5be66e427d" label="DF-10Km" source="r1" src_label="Gi1/1" target="r2" trgt_label="Gi3/4">
<mxCell edge="1" parent="1" source="r1" style="endArrow=none;" target="r2">
<mxGeometry as="geometry" relative="1" />
</mxCell><object id="4c783d026b64f7f34abadd5be66e427d" label="DF-10Km" description="DF link between R1 and R2" src_label="Gi1/1" trgt_label="Gi3/4" source="r1" target="r2">
<mxCell style="endArrow=none;" edge="1" parent="1" source="r1" target="r2">
<mxGeometry relative="1" as="geometry" />
</mxCell>
</object><mxCell connectable="0" id="e6f937d44e23fd838cb4d793ac7ec326-src" parent="e6f937d44e23fd838cb4d793ac7ec326" style="labelBackgroundColor=#ffffff;;" value="10GE2/1/1" vertex="1">
<mxGeometry as="geometry" relative="1" x="-0.5">
</object><mxCell id="e6f937d44e23fd838cb4d793ac7ec326-src" value="10GE2/1/1" style="labelBackgroundColor=#ffffff;;" vertex="1" connectable="0" parent="e6f937d44e23fd838cb4d793ac7ec326">
<mxGeometry x="-0.5" relative="1" as="geometry">
<mxPoint as="offset" />
</mxGeometry>
</mxCell><mxCell connectable="0" id="e6f937d44e23fd838cb4d793ac7ec326-trgt" parent="e6f937d44e23fd838cb4d793ac7ec326" style="labelBackgroundColor=#ffffff;;" value="Ten1/1" vertex="1">
<mxGeometry as="geometry" relative="-1" x="0.5">
</mxCell><mxCell id="e6f937d44e23fd838cb4d793ac7ec326-trgt" value="Ten1/1" style="labelBackgroundColor=#ffffff;;" vertex="1" connectable="0" parent="e6f937d44e23fd838cb4d793ac7ec326">
<mxGeometry x="0.5" relative="-1" as="geometry">
<mxPoint as="offset" />
</mxGeometry>
</mxCell><object description="DF link between R3 and R2" id="e6f937d44e23fd838cb4d793ac7ec326" label="DF-32Km" source="r3" src_label="10GE2/1/1" target="r2" trgt_label="Ten1/1">
<mxCell edge="1" parent="1" source="r3" style="endArrow=none;" target="r2">
<mxGeometry as="geometry" relative="1" />
</mxCell><object id="e6f937d44e23fd838cb4d793ac7ec326" label="DF-32Km" description="DF link between R3 and R2" src_label="10GE2/1/1" trgt_label="Ten1/1" source="r3" target="r2">
<mxCell style="endArrow=none;" edge="1" parent="1" source="r3" target="r2">
<mxGeometry relative="1" as="geometry" />
</mxCell>
</object></root>
</mxGraphModel>
Expand Down
50 changes: 25 additions & 25 deletions tests/Output/should_be_test_xlsx_data_tanslate_headers.drawio
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
<mxfile compressed="false" type="device">
<mxfile type="device" compressed="false">
<diagram id="Page-1" name="Page-1">
<mxGraphModel arrows="1" connect="1" dx="1360" dy="864" fold="1" grid="1" gridSize="10" guides="1" math="0" page="1" pageHeight="1169" pageScale="1" pageWidth="827" shadow="1" tooltips="1">
<mxGraphModel dx="1360" dy="864" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="1">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<object bottom_label="core" description="Core Router" id="r1" label="r1" pic="" top_label="1.1.1.1">
<mxCell parent="1" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1">
<mxGeometry as="geometry" height="60" width="120" x="617" y="0" />
<object id="r1" label="r1" pic="" bottom_label="core" top_label="1.1.1.1" description="Core Router">
<mxCell style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="472" y="848" width="120" height="60" as="geometry" />
</mxCell>
</object><object bottom_label="core" description="Core Router" id="r2" label="r2" pic="" top_label="2.2.2.2">
<mxCell parent="1" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1">
<mxGeometry as="geometry" height="60" width="120" x="1089" y="535" />
</object><object id="r2" label="r2" pic="" bottom_label="core" top_label="2.2.2.2" description="Core Router">
<mxCell style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="241" y="533" width="120" height="60" as="geometry" />
</mxCell>
</object><object bottom_label="edge" description="Edge Router" id="r3" label="r3" pic="" top_label="3.3.3.3">
<mxCell parent="1" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1">
<mxGeometry as="geometry" height="60" width="120" x="864" y="864" />
</object><object id="r3" label="r3" pic="" bottom_label="edge" top_label="3.3.3.3" description="Edge Router">
<mxCell style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="702" y="0" width="120" height="60" as="geometry" />
</mxCell>
</object><mxCell connectable="0" id="4c783d026b64f7f34abadd5be66e427d-src" parent="4c783d026b64f7f34abadd5be66e427d" style="labelBackgroundColor=#ffffff;;" value="Gi1/1" vertex="1">
<mxGeometry as="geometry" relative="1" x="-0.5">
</object><mxCell id="4c783d026b64f7f34abadd5be66e427d-src" value="Gi1/1" style="labelBackgroundColor=#ffffff;;" vertex="1" connectable="0" parent="4c783d026b64f7f34abadd5be66e427d">
<mxGeometry x="-0.5" relative="1" as="geometry">
<mxPoint as="offset" />
</mxGeometry>
</mxCell><mxCell connectable="0" id="4c783d026b64f7f34abadd5be66e427d-trgt" parent="4c783d026b64f7f34abadd5be66e427d" style="labelBackgroundColor=#ffffff;;" value="Gi3/4" vertex="1">
<mxGeometry as="geometry" relative="-1" x="0.5">
</mxCell><mxCell id="4c783d026b64f7f34abadd5be66e427d-trgt" value="Gi3/4" style="labelBackgroundColor=#ffffff;;" vertex="1" connectable="0" parent="4c783d026b64f7f34abadd5be66e427d">
<mxGeometry x="0.5" relative="-1" as="geometry">
<mxPoint as="offset" />
</mxGeometry>
</mxCell><object description="DF link between R1 and R2" id="4c783d026b64f7f34abadd5be66e427d" label="DF-10Km" source="r1" src_label="Gi1/1" target="r2" trgt_label="Gi3/4">
<mxCell edge="1" parent="1" source="r1" style="endArrow=none;" target="r2">
<mxGeometry as="geometry" relative="1" />
</mxCell><object id="4c783d026b64f7f34abadd5be66e427d" label="DF-10Km" description="DF link between R1 and R2" src_label="Gi1/1" trgt_label="Gi3/4" source="r1" target="r2">
<mxCell style="endArrow=none;" edge="1" parent="1" source="r1" target="r2">
<mxGeometry relative="1" as="geometry" />
</mxCell>
</object><mxCell connectable="0" id="e6f937d44e23fd838cb4d793ac7ec326-src" parent="e6f937d44e23fd838cb4d793ac7ec326" style="labelBackgroundColor=#ffffff;;" value="10GE2/1/1" vertex="1">
<mxGeometry as="geometry" relative="1" x="-0.5">
</object><mxCell id="e6f937d44e23fd838cb4d793ac7ec326-src" value="10GE2/1/1" style="labelBackgroundColor=#ffffff;;" vertex="1" connectable="0" parent="e6f937d44e23fd838cb4d793ac7ec326">
<mxGeometry x="-0.5" relative="1" as="geometry">
<mxPoint as="offset" />
</mxGeometry>
</mxCell><mxCell connectable="0" id="e6f937d44e23fd838cb4d793ac7ec326-trgt" parent="e6f937d44e23fd838cb4d793ac7ec326" style="labelBackgroundColor=#ffffff;;" value="Ten1/1" vertex="1">
<mxGeometry as="geometry" relative="-1" x="0.5">
</mxCell><mxCell id="e6f937d44e23fd838cb4d793ac7ec326-trgt" value="Ten1/1" style="labelBackgroundColor=#ffffff;;" vertex="1" connectable="0" parent="e6f937d44e23fd838cb4d793ac7ec326">
<mxGeometry x="0.5" relative="-1" as="geometry">
<mxPoint as="offset" />
</mxGeometry>
</mxCell><object description="DF link between R3 and R2" id="e6f937d44e23fd838cb4d793ac7ec326" label="DF-32Km" source="r3" src_label="10GE2/1/1" target="r2" trgt_label="Ten1/1">
<mxCell edge="1" parent="1" source="r3" style="endArrow=none;" target="r2">
<mxGeometry as="geometry" relative="1" />
</mxCell><object id="e6f937d44e23fd838cb4d793ac7ec326" label="DF-32Km" description="DF link between R3 and R2" src_label="10GE2/1/1" trgt_label="Ten1/1" source="r3" target="r2">
<mxCell style="endArrow=none;" edge="1" parent="1" source="r3" target="r2">
<mxGeometry relative="1" as="geometry" />
</mxCell>
</object></root>
</mxGraphModel>
Expand Down
Loading

0 comments on commit 72f9ac3

Please sign in to comment.