Added LICENSE
[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
20 from nepi.util import guid
21
22 guid_gen = guid.GuidGenerator()
23
24 class Attributes(object):
25     def __init__(self):
26         super(Attributes, self).__init__()
27         self._attributes = dict()
28
29     def __getattr__(self, name):
30         try:
31             return self._attributes[name]
32         except:
33             return super(Attributes, self).__getattribute__(name)
34
35     def __setattr__(self, name, value):
36         try:
37             if value == None:
38                 old = self._attributes[name]
39                 del self._attributes[name]
40                 return old
41
42             self._attributes[name] = value
43             return value
44         except:
45             return super(Attributes, self).__setattr__(name, value)
46
47 class Connections(object):
48     def __init__(self):
49         super(Connections, self).__init__()
50         self._connections = set()
51
52     def __getattr__(self, guid_or_label):
53         try:
54             for b in self._connections:
55                 if guid_or_label in [b.guid, b.label]:
56                     return b
57         except:
58             return super(Connections, self).__getattribute__(guid_or_label)
59
60 class Box(object):
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()
66         self._tags = set()
67         self.label = label or self._guid
68
69         # Graphical information to draw box
70         self.x = 0
71         self.y = 0
72         self.width = 4
73         self.height = 4
74
75     @property
76     def tags(self):
77         return self._tags
78
79     @property
80     def attributes(self):
81         return self._a._attributes.keys()
82
83     @property
84     def a(self):
85         return self._a
86
87     @property
88     def c(self):
89         return self._c
90
91     @property
92     def guid(self):
93         return self._guid
94
95     @property
96     def connections(self):
97         return set(self._c._connections)
98
99     def tadd(self, name):
100         self._tags.add(name)
101
102     def tdel(self, name):
103         self._tags.remove(name)
104
105     def thas(self, name):
106         return name in self._tags
107
108     def connect(self, box, cascade = True):
109         self._c._connections.add(box)
110         if cascade:
111             box.connect(self, cascade = False)
112
113     def disconnect(self, box, cascade = True):
114         self._c._connections.remove(box)
115         if cascade:
116             box.disconnect(self, cascade = False)
117
118     def is_connected(self, box):
119         return box in self.connections
120