Passed
Push — main ( 25fd53...ab2bda )
by Eran
02:05
created

graphinate.materializers   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 6

1 Function

Rating   Name   Duplication   Size   Complexity  
B materialize() 0 37 6
1
import functools
2
import json
3
from collections.abc import Callable, Mapping
4
from enum import Enum
5
from typing import Optional
6
7
from .. import builders, modeling, server
8
from .matplotlib import plot
9
10
11
class Materializers(Enum):
12
    """Materializers Enum
13
14
    Attributes:
15
        D3Graph: create a D3 Graph and print it to stdout
16
        GraphQL: create a GraphQL Schema and serve it in a web server
17
        NetworkX: create a NetworkX Graph and plot+show it with matplotlib
18
        NetworkX_with_edge_labels: create a NetworkX Graph and plot+show it with matplotlib
19
    """
20
    D3Graph: tuple = (builders.D3Builder, lambda d: print(json.dumps(d, indent=2, default=str)))
21
    GraphQL: tuple = (builders.GraphQLBuilder, server.graphql)
22
    NetworkX: tuple = (builders.NetworkxBuilder, plot)
23
    NetworkX_with_edge_labels: tuple = (builders.NetworkxBuilder, functools.partial(plot, with_edge_labels=True))
24
25
26
def materialize(model: modeling.GraphModel,
27
                graph_type: builders.GraphType = builders.GraphType.Graph,
28
                default_node_attributes: Optional[Mapping] = None,
29
                builder: Optional[type[builders.Builder]] = None,
30
                builder_output_handler: Optional[Callable] = None,
31
                **kwargs):
32
    """
33
    Materialize a GraphModel using a Builder and an Actualizer
34
35
    Args:
36
        model: GraphModel - the model to be materialized
37
        graph_type: GraphType - the type of graph to be built.
38
                                Default is Graph.
39
        default_node_attributes: Mapping - A Mapping containing attributes that are added to all nodes.
40
        builder: Builder - the builder to be used to build the graph.
41
        builder_output_handler: function that will consume the resulting built graph and
42
                    outputs it (e.g., display, serve, print, etc.).
43
        **kwargs:
44
45
46
    Returns:
47
        None
48
    """
49
    if builder is None and builder_output_handler is None:
50
        raise ValueError("Missing: builder, actualizer")
51
52
    if builder:
53
        graph = builders.build(builder,
54
                               model,
55
                               graph_type,
56
                               default_node_attributes=default_node_attributes,
57
                               **kwargs)
58
59
        if builder_output_handler and callable(builder_output_handler):
60
            builder_output_handler(graph)
61
        else:
62
            print(graph)
63
64
65
__all__ = ('materialize', 'plot')
66