2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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.
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.
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/>.
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
21 from nepi.util import guid
23 guid_gen = guid.GuidGenerator()
25 class Attributes(object):
27 super(Attributes, self).__init__()
28 self._attributes = dict()
30 def __getattr__(self, name):
32 return self._attributes[name]
34 return super(Attributes, self).__getattribute__(name)
36 def __setattr__(self, name, value):
39 old = self._attributes[name]
40 del self._attributes[name]
43 self._attributes[name] = value
46 return super(Attributes, self).__setattr__(name, value)
48 class Connections(object):
50 super(Connections, self).__init__()
51 self._connections = set()
53 def __getattr__(self, guid_or_label):
55 for b in self._connections:
56 if guid_or_label in [b.guid, b.label]:
59 return super(Connections, self).__getattribute__(guid_or_label)
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()
68 self.label = label or self._guid
70 # Graphical information to draw box
82 return self._a._attributes.keys()
97 def connections(self):
98 return set(self._c._connections)
100 def tadd(self, name):
103 def tdel(self, name):
104 self._tags.remove(name)
106 def thas(self, name):
107 return name in self._tags
109 def connect(self, box, cascade = True):
110 self._c._connections.add(box)
112 box.connect(self, cascade = False)
114 def disconnect(self, box, cascade = True):
115 self._c._connections.remove(box)
117 box.disconnect(self, cascade = False)
119 def is_connected(self, box):
120 return box in self.connections