summaryrefslogtreecommitdiff
path: root/src-py/model/graph.py
blob: 86a195d4a367423ac99635600f35150fa1cb1b64 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Attr_Diff(dict):
    """
    Represents a change to note attributes, where nodes can represent
    either logical nodes or logical links, and attributes can be added,
    changed or removed
    
    Example:
            attr_diff = {n_id: {'attr_write': {'attr_0': 0,
                                               'attr_1': 'a'},
                                'attr_remove': ['attr_2'] }
                        }
    """
    def __init__(self):
        pass

    def init_node_attr_diff(self, n_id):
        ret = {'attr_write': {},
                      'attr_remove': []}
        self[n_id] = ret
        return ret

    def add_node_attr_write(self, n_id, attr_name, attr_val):
        n_attr_diff = self.get(n_id)
        if None == n_attr_diff:
            n_attr_diff = self.init_node_attr_diff(n_id)
        n_attr_diff['attr_write'][attr_name] = attr_val

    def add_node_attr_rm(self, n_id, attr_name):
        n_attr_diff = self.get(n_id)
        if None == n_attr_diff:
            n_attr_diff = self.init_node_attr_diff(n_id)
        n_attr_diff['attr_remove'].append(attr_name)

class Topo_Diff(object):
    """
    Represents a change to the graph topology
    """
    def __init__(self, link_set_rm=[],
                       node_set_rm=[],
                       node_set_add=[],
                       link_set_add=[]):

        self.link_set_rm = link_set_rm
        self.node_set_rm = node_set_rm
        self.node_set_add = node_set_add
        self.link_set_add = link_set_add

    def __str__(self):
        return __name__ + ': ' + ', '.join('%s: %s' % (k, v) for k, v in self.__dict__.items())

    def check_validity(self, topo_diff_dict):
        """
        Topo_Diff may represent invalid operations, eg. adding a link while
         removing it's end-point - this stub should check for that
        """
        pass

    @staticmethod
    def from_dict(topo_diff_dict):
        """
        construct from dict - no node/link constructor set must be provided
        """
        ret = Topo_Diff()

        # merge keys - this allows constructor argument omission (link_set_rm,
        # node_set_rm, etc.) such as when constructing from POST JSON data
        for k, _ in ret.__dict__.items():
            v = topo_diff_dict.get(k)
            if None != v:
                ret.__dict__[k] = v
        return ret