goblin.fileio.graphson._prep_edge()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nop 2
dl 0
loc 25
rs 9.45
c 0
b 0
f 0
1
import collections
2
try:
3
    import ujson as json
4
except ImportError:
5
    import json
6
7
from gremlin_python.structure.io.graphsonV3d0 import GraphSONWriter
8
from goblin.element import Vertex, Edge, VertexProperty
9
from goblin.manager import ListVertexPropertyManager
10
11
12
writer = GraphSONWriter()
13
14
15
AdjList = collections.namedtuple("AdjList", "vertex inE outE")
16
17
vp_id = 10
18
19
20
def dump(fpath, *adj_lists, mode="w"):
21
    """Convert Goblin elements to GraphSON"""
22
    with open(fpath, mode) as f:
23
        for adj_list in adj_lists:
24
            dumped = dumps(adj_list)
25
            f.write(dumped + '\n')
26
27
28
def dumps(adj_list):
29
    """Convert Goblin elements to GraphSON"""
30
    vertex = _prep_vertex(adj_list.vertex)
31
    for inE in adj_list.inE:
32
        prepped = _prep_edge(inE, "inV")
33
        label = inE.__label__
34
        vertex["inE"].setdefault(label, [])
35
        vertex["inE"][label].append(prepped)
36
    for outE in adj_list.outE:
37
        prepped = _prep_edge(outE, "outV")
38
        label = outE.__label__
39
        vertex["outE"].setdefault(label, [])
40
        vertex["outE"][label].append(prepped)
41
    return json.dumps(vertex)
42
43
44
def _prep_edge(e, t):
45
    if t == 'inV':
46
        other = "outV"
47
        other_id = e.source.id
48
    elif t == 'outV':
49
        other = "inV"
50
        other_id = e.target.id
51
    else:
52
        raise RuntimeError('Invalid edge type')
53
    edge = {
54
        "id": {
55
            "@type": "g:Int32",
56
            "@value": e.id,
57
58
        },
59
        other: {
60
            "@type": "g:Int32",
61
            "@value": other_id,
62
        },
63
        "properties": {}
64
    }
65
    for db_name, (ogm_name, _) in e.__mapping__.db_properties.items():
66
        edge["properties"][db_name] = writer.toDict(getattr(e, ogm_name))
67
68
    return edge
69
70
71
def _prep_vertex(v):
72
    global vp_id
73
    mapping = v.__mapping__
74
    properties = v.__properties__
75
    vertex = {
76
            "id": {
77
                "@type": "g:Int32",
78
                "@value": v.id
79
            },
80
            "label": v.__label__,
81
            "properties": {},
82
            "outE": {},
83
            "inE": {}
84
    }
85
86
87
88
    for db_name, (ogm_name, _) in mapping.db_properties.items():
89
        prop = properties[ogm_name]
90
        vertex["properties"].setdefault(db_name, [])
91
        if isinstance(prop, VertexProperty):
92
            prop = getattr(v, ogm_name)
93
            if isinstance(prop, ListVertexPropertyManager):
94
                for p in prop:
95
                    value = p.value
96
                    vp = _prep_vp(p, value, v, db_name)
97
                    vp_id += 1
98
                    vertex["properties"][db_name].append(vp)
99
                continue
100
            else:
101
                value = prop.value
102
        else:
103
            value = getattr(v, ogm_name)
104
        vp = _prep_vp(prop, value, v, db_name)
105
        vp_id += 1
106
        vertex["properties"][db_name].append(vp)
107
    return vertex
108
109
110
def _prep_vp(prop, value, v, db_name):
111
    vp = {
112
            "id": {
113
                "@type": "g:Int64",
114
                "@value": vp_id
115
            },
116
            "value": writer.toDict(value),
117
            "properties": {}
118
    }
119
    if isinstance(prop, VertexProperty):
120
        for db_name, (ogm_name, _) in prop.__mapping__.db_properties.items():
121
            vp["properties"][db_name] = writer.toDict(getattr(prop, ogm_name))
122
    return vp
123
124
125
126
def _dump_edge(e):
127
    pass
128