forked from evo-company/hiku
/
graph.py
53 lines (39 loc) · 1.42 KB
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from collections import OrderedDict
from .types import to_instance
def _kw_only(names, kwargs):
kwargs = kwargs.copy()
values = [kwargs.pop(name, None) for name in names]
if kwargs:
raise TypeError('Unknown keyword arguments: {}'
.format(', '.join(kwargs.keys())))
else:
return values
class Field(object):
def __init__(self, name, *other, **kwargs):
if not len(other):
raise TypeError('Missing required argument')
elif len(other) == 1:
type_, func = None, other[0]
elif len(other) == 2:
type_, func = other
else:
raise TypeError('More positional arguments ({}) than expected (2)'
.format(len(other)))
doc, = _kw_only(['doc'], kwargs)
self.name = name
self.type = to_instance(type_) if type_ is not None else None
self.func = func
self.doc = doc
class Edge(object):
def __init__(self, name, fields, **kwargs):
self.name = name
self.fields = OrderedDict((f.name, f) for f in fields)
self.doc, = _kw_only(['doc'], kwargs)
class Link(object):
def __init__(self, name, requires, entity, func, to_list=False, **kwargs):
self.name = name
self.requires = requires
self.entity = entity
self.func = func
self.to_list = to_list
self.doc, = _kw_only(['doc'], kwargs)