Creating new branch nepi-3.1-multirun
[nepi.git] / src / nepi / design / box.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 #
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20
21 from nepi.util import guid
22
23 guid_gen = guid.GuidGenerator()
24
25 class Attributes(object):
26     def __init__(self):
27         super(Attributes, self).__init__()
28         self._attributes = dict()
29
30     def __getattr__(self, name):
31         try:
32             return self._attributes[name]
33         except:
34             return super(Attributes, self).__getattribute__(name)
35
36     def __setattr__(self, name, value):
37         try:
38             if value == None:
39                 old = self._attributes[name]
40                 del self._attributes[name]
41                 return old
42
43             self._attributes[name] = value
44             return value
45         except:
46             return super(Attributes, self).__setattr__(name, value)
47
48 class Connections(object):
49     def __init__(self):
50         super(Connections, self).__init__()
51         self._connections = set()
52
53     def __getattr__(self, guid_or_label):
54         try:
55             for b in self._connections:
56                 if guid_or_label in [b.guid, b.label]:
57                     return b
58         except:
59             return super(Connections, self).__getattribute__(guid_or_label)
60
61 class Box(object):
62     def __init__(self, label = None, guid = None):
63         super(Box, self).__init__()
64         self._guid = guid_gen.next(guid)
65         self._a = Attributes()
66         self._c = Connections()
67         self._tags = set()
68         self.label = label or self._guid
69
70         # Graphical information to draw box
71         self.x = 0
72         self.y = 0
73         self.width = 4
74         self.height = 4
75
76     @property
77     def tags(self):
78         return self._tags
79
80     @property
81     def attributes(self):
82         return self._a._attributes.keys()
83
84     @property
85     def a(self):
86         return self._a
87
88     @property
89     def c(self):
90         return self._c
91
92     @property
93     def guid(self):
94         return self._guid
95
96     @property
97     def connections(self):
98         return set(self._c._connections)
99
100     def tadd(self, name):
101         self._tags.add(name)
102
103     def tdel(self, name):
104         self._tags.remove(name)
105
106     def thas(self, name):
107         return name in self._tags
108
109     def connect(self, box, cascade = True):
110         self._c._connections.add(box)
111         if cascade:
112             box.connect(self, cascade = False)
113
114     def disconnect(self, box, cascade = True):
115         self._c._connections.remove(box)
116         if cascade:
117             box.disconnect(self, cascade = False)
118
119     def is_connected(self, box):
120         return box in self.connections
121