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/>.
20 from nepi.util import guid
22 guid_gen = guid.GuidGenerator()
24 class Attributes(object):
26 super(Attributes, self).__init__()
27 self._attributes = dict()
29 def __getattr__(self, name):
31 return self._attributes[name]
33 return super(Attributes, self).__getattribute__(name)
35 def __setattr__(self, name, value):
38 old = self._attributes[name]
39 del self._attributes[name]
42 self._attributes[name] = value
45 return super(Attributes, self).__setattr__(name, value)
47 class Connections(object):
49 super(Connections, self).__init__()
50 self._connections = set()
52 def __getattr__(self, guid_or_label):
54 for b in self._connections:
55 if guid_or_label in [b.guid, b.label]:
58 return super(Connections, self).__getattribute__(guid_or_label)
61 def __init__(self, label = None, guid = None):
62 super(Box, self).__init__()
63 self._guid = guid_gen.next(guid)
64 self._a = Attributes()
65 self._c = Connections()
67 self.label = label or self._guid
69 # Graphical information to draw box
81 return self._a._attributes.keys()
96 def connections(self):
97 return set(self._c._connections)
102 def tdel(self, name):
103 self._tags.remove(name)
105 def thas(self, name):
106 return name in self._tags
108 def connect(self, box, cascade = True):
109 self._c._connections.add(box)
111 box.connect(self, cascade = False)
113 def disconnect(self, box, cascade = True):
114 self._c._connections.remove(box)
116 box.disconnect(self, cascade = False)
118 def is_connected(self, box):
119 return box in self.connections