Passed
Push — main ( a15a5c...c48b5c )
by Eran
01:12
created

graphinate.materializers   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 9

1 Function

Rating   Name   Duplication   Size   Complexity  
C materialize() 0 43 9
1
import functools
2
import json
3
import os
4
from collections.abc import Mapping
5
from enum import Enum
6
from typing import Callable, Optional
7
8
from .. import builders, modeling, server
9
from ..tools.gui import modal_radiobutton_chooser
10
from .matplotlib import plot
11
12
ENABLE_GUI = bool(os.getenv('GRAPHINATE_ENABLE_GUI', True))
13
14
15
class Materializers(Enum):
16
    """Materializers Enum
17
18
    Attributes:
19
        D3Graph: create a D3 Graph and print it to stdout
20
        GraphQL: create a GraphQL Schema and serve it in a web server
21
        NetworkX: create a NetworkX Graph and plot+show it with matplotlib
22
        NetworkX_with_edge_labels: create a NetworkX Graph and plot+show it with matplotlib
23
    """
24
    D3Graph: tuple = (builders.D3Builder, lambda d: print(json.dumps(d, indent=2, default=str)))
25
    GraphQL: tuple = (builders.GraphQLBuilder, server.graphql)
26
    NetworkX: tuple = (builders.NetworkxBuilder, plot)
27
    NetworkX_with_edge_labels: tuple = (builders.NetworkxBuilder, functools.partial(plot, with_edge_labels=True))
28
29
30
def materialize(model: modeling.GraphModel,
31
                title: Optional[str] = None,
32
                graph_type: builders.GraphType = builders.GraphType.Graph,
33
                default_node_attributes: Optional[Mapping] = None,
34
                builder: Optional[type[builders.Builder]] = None,
35
                actualizer: Optional[Callable] = None,
36
                **kwargs):
37
    """
38
39
    Args:
40
        model: GraphModel
41
        title: the GraphModel name
42
        graph_type: GraphType
43
        default_node_attributes:
44
        builder: Builder instance
45
        actualizer: function that will consume the resulting built graph and
46
                    actualizes it (e.g. display, serve, print etc.)
47
        **kwargs:
48
49
    Returns:
50
        None
51
    """
52
    title = title or model.name
53
    if ENABLE_GUI and builder is None and actualizer is None:
54
        result = modal_radiobutton_chooser(title,
55
                                           options={m.name: m.value for m in Materializers},
56
                                           default=(None, None))
57
        builder, actualizer = result[1]
58
59
    if builder is None and actualizer is None:
60
        raise ValueError("Missing: builder, actualizer")
61
62
    if builder:
63
        graph = builders.build(builder,
64
                               model,
65
                               graph_type,
66
                               default_node_attributes=default_node_attributes,
67
                               **kwargs)
68
69
        if actualizer and callable(actualizer):
70
            actualizer(graph)
71
        else:
72
            print(graph)
73
74
75
__all__ = ('materialize', 'plot')
76