22Export a graph as text (GraphViz DOT, Mermaid, GraphML, ELK JSON) or as a
33rendered image (via the GraphViz command line tools).
44
5- This module is reached through :meth:`pgraph.PGraph._BaseGraph .export`, which
5+ This module is reached through :meth:`pgraph.PGraph.BaseGraph .export`, which
66dispatches on the format name or the file extension. Each text exporter is a
77plain function ``f(graph, **options) -> str``, registered with
88:func:`register_exporter`, so a user can add a format without touching the
@@ -27,7 +27,7 @@ def gexf(graph, *, version="1.3") -> str:
2727from typing import IO , TYPE_CHECKING , Any , Callable
2828
2929if TYPE_CHECKING :
30- from .PGraph import BaseVertex , Edge , _BaseGraph
30+ from .PGraph import BaseVertex , Edge , BaseGraph
3131
3232TextExporter = Callable [..., str ]
3333
@@ -75,6 +75,23 @@ def decorator(func: TextExporter) -> TextExporter:
7575 return decorator
7676
7777
78+ def get_exporter (name : str ) -> TextExporter :
79+ """
80+ Look up a text exporter by format name
81+
82+ :param name: format name, eg. ``"dot"``
83+ :return: the function ``f(graph, **options) -> str``
84+ :raises ValueError: no text exporter has that name; the message lists those
85+ that do
86+ """
87+ try :
88+ return _TEXT [name ]
89+ except KeyError :
90+ raise ValueError (
91+ f"unknown export format { name !r} , expected one of { formats ()} "
92+ ) from None
93+
94+
7895def formats () -> list [str ]:
7996 """
8097 Names of all supported export formats
@@ -114,20 +131,20 @@ def format_from_filename(filename: str | os.PathLike | IO | None) -> str | None:
114131# helpers shared by the exporters
115132
116133
117- def _is_directed (graph : _BaseGraph ) -> bool :
134+ def _is_directed (graph : BaseGraph ) -> bool :
118135 from .PGraph import DGraph # not at module level: PGraph imports this module
119136
120137 return isinstance (graph , DGraph )
121138
122139
123- def _ordered_edges (graph : _BaseGraph ) -> list [tuple [Edge , BaseVertex , BaseVertex ]]:
140+ def _ordered_edges (graph : BaseGraph ) -> list [tuple [Edge , BaseVertex , BaseVertex ]]:
124141 """
125142 Edges in a reproducible order
126143
127144 ``graph.edges()`` is a set, so its order changes from run to run and the
128145 exported text would not be stable (useless for diffs and golden files).
129- Instead walk the vertices in creation order and each vertex's own edge
130- list , keeping the first sight of every edge. Identity is by object, not by
146+ Instead walk the vertices in creation order and each vertex's own
147+ outgoing edges , keeping the first sight of every edge. Identity is by object, not by
131148 endpoints, so parallel edges are all kept, and an undirected edge (held by
132149 both ends, twice for a self-loop) is listed once.
133150
@@ -136,15 +153,15 @@ def _ordered_edges(graph: _BaseGraph) -> list[tuple[Edge, BaseVertex, BaseVertex
136153 seen : set [int ] = set ()
137154 edges : list [tuple [Edge , BaseVertex , BaseVertex ]] = []
138155 for vertex in graph :
139- for e in vertex ._edgelist :
156+ for e in vertex .out_edges () :
140157 if id (e ) in seen or e .v1 is None or e .v2 is None :
141158 continue
142159 seen .add (id (e ))
143160 edges .append ((e , e .v1 , e .v2 ))
144161 return edges
145162
146163
147- def _component_colors (graph : _BaseGraph ) -> dict [BaseVertex , str ]:
164+ def _component_colors (graph : BaseGraph ) -> dict [BaseVertex , str ]:
148165 """
149166 Map each vertex to a ``#rrggbb`` colour for its component, as plot() does
150167 """
@@ -194,7 +211,7 @@ def _dot_quote(text: str) -> str:
194211
195212@register_exporter ("dot" , ".dot" , ".gv" )
196213def dot (
197- graph : _BaseGraph ,
214+ graph : BaseGraph ,
198215 * ,
199216 direction : str | None = None ,
200217 colorcomponents : bool = False ,
@@ -243,7 +260,7 @@ def dot(
243260
244261@register_exporter ("mermaid" , ".mmd" , ".mermaid" )
245262def mermaid (
246- graph : _BaseGraph ,
263+ graph : BaseGraph ,
247264 * ,
248265 direction : str = "TB" ,
249266 colorcomponents : bool = False ,
@@ -292,7 +309,7 @@ def mermaid(
292309
293310
294311@register_exporter ("graphml" , ".graphml" )
295- def graphml (graph : _BaseGraph ) -> str :
312+ def graphml (graph : BaseGraph ) -> str :
296313 """
297314 GraphML XML text
298315
@@ -357,7 +374,7 @@ def key(key_id: str, domain: str, name: str, type_: str) -> str:
357374
358375@register_exporter ("elk" , ".elk.json" , ".elk" )
359376def elk (
360- graph : _BaseGraph ,
377+ graph : BaseGraph ,
361378 * ,
362379 size : tuple [float , float ] = (60 , 40 ),
363380 layout_options : dict [str , str ] | None = None ,
@@ -433,7 +450,7 @@ def elk(
433450
434451
435452def render (
436- graph : _BaseGraph ,
453+ graph : BaseGraph ,
437454 fmt : str ,
438455 * ,
439456 engine : str | None = None ,
0 commit comments