1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+
14"""
25A class which defines a composite object which can store
36hieararchical dictionaries with names.
47
58This class is same as a hiearchical dictionary, but it
6- provides methods to add/access/modify children by name,
9+ provides methods to add/access/modify children by name,
710like a Composite.
811
912Created Anand B Pillai <abpillai@gmail.com>
1720def normalize (val ):
1821 """ Normalize a string so that it can be used as an attribute
1922 to a Python object """
20-
23+
2124 if val .find ('-' ) != - 1 :
2225 val = val .replace ('-' , '_' )
2326
@@ -65,7 +68,7 @@ def __setattr__(self, name, value):
6568 else :
6669 # New attribute
6770 self [name ] = value
68-
71+
6972
7073class CompositeDict (SpecialDict ):
7174 """ A class which works like a hierarchical dictionary.
@@ -106,15 +109,15 @@ def __getattr__(self, name):
106109 attr = getattr (self [self ._name ], name )
107110 if attr :
108111 return attr
109-
112+
110113 raise AttributeError ('no attribute named %s' % name )
111114
112115 def isRoot (self ):
113116 """ Return whether I am a root component or not """
114117
115118 # If I don't have a parent, I am root
116119 return not self ._father
117-
120+
118121 def isLeaf (self ):
119122 """ Return whether I am a leaf component or not """
120123
@@ -128,15 +131,15 @@ def getName(self):
128131
129132 def getIndex (self , child ):
130133 """ Return the index of the child ConfigInfo object 'child' """
131-
134+
132135 if child in self ._children :
133136 return self ._children .index (child )
134137 else :
135138 return - 1
136139
137140 def getDict (self ):
138141 """ Return the contained dictionary """
139-
142+
140143 return self [self ._name ]
141144
142145 def getProperty (self , child , key ):
@@ -156,25 +159,25 @@ def setProperty(self, child, key, value):
156159 childDict = self .getInfoDict (child )
157160 if childDict :
158161 childDict [key ] = value
159-
162+
160163 def getChildren (self ):
161164 """ Return the list of immediate children of this object """
162-
165+
163166 return self ._children
164167
165168 def getAllChildren (self ):
166169 """ Return the list of all children of this object """
167-
170+
168171 l = []
169172 for child in self ._children :
170173 l .append (child )
171174 l .extend (child .getAllChildren ())
172-
175+
173176 return l
174177
175178 def getChild (self , name ):
176179 """ Return the immediate child object with the given name """
177-
180+
178181 for child in self ._children :
179182 if child .getName () == name :
180183 return child
@@ -185,7 +188,7 @@ def findChild(self, name):
185188 # Note - this returns the first child of the given name
186189 # any other children with similar names down the tree
187190 # is not considered.
188-
191+
189192 for child in self .getAllChildren ():
190193 if child .getName () == name :
191194 return child
@@ -195,33 +198,33 @@ def findChildren(self, name):
195198
196199 # Note: this returns a list of all the children of a given
197200 # name, irrespective of the depth of look-up.
198-
201+
199202 children = []
200-
203+
201204 for child in self .getAllChildren ():
202205 if child .getName () == name :
203206 children .append (child )
204207
205208 return children
206-
209+
207210 def getPropertyDict (self ):
208211 """ Return the property dictionary """
209-
212+
210213 d = self .getChild ('__properties' )
211214 if d :
212215 return d .getDict ()
213216 else :
214217 return {}
215-
218+
216219 def getParent (self ):
217220 """ Return the person who created me """
218221
219222 return self ._father
220-
223+
221224 def __setChildDict (self , child ):
222225 """ Private method to set the dictionary of the child
223226 object 'child' in the internal dictionary """
224-
227+
225228 d = self [self ._name ]
226229 d [child .getName ()] = child .getDict ()
227230
@@ -236,25 +239,25 @@ def setParent(self, father):
236239 # child is orphaned - see addChild and addChild2
237240 # methods !
238241 self ._father = father
239-
242+
240243 def setName (self , name ):
241- """ Set the name of this ConfigInfo object to 'name' """
244+ """ Set the name of this ConfigInfo object to 'name' """
242245
243246 self ._name = name
244247
245248 def setDict (self , d ):
246249 """ Set the contained dictionary """
247-
250+
248251 self [self ._name ] = d .copy ()
249-
252+
250253 def setAttribute (self , name , value ):
251254 """ Set a name value pair in the contained dictionary """
252-
255+
253256 self [self ._name ][name ] = value
254257
255258 def getAttribute (self , name ):
256259 """ Return value of an attribute from the contained dictionary """
257-
260+
258261 return self [self ._name ][name ]
259262
260263 def addChild (self , name , force = False ):
@@ -264,10 +267,10 @@ def addChild(self, name, force=False):
264267
265268 This function returns the child object, whether
266269 new or existing """
267-
270+
268271 if type (name ) != str :
269272 raise ValueError ('Argument should be a string!' )
270-
273+
271274 child = self .getChild (name )
272275 if child :
273276 # print 'Child %s present!' % name
@@ -278,22 +281,22 @@ def addChild(self, name, force=False):
278281 child = self .__class__ (name )
279282 self ._children [index ] = child
280283 child .setParent (self )
281-
284+
282285 self .__setChildDict (child )
283286 return child
284287 else :
285288 child = self .__class__ (name )
286289 child .setParent (self )
287-
290+
288291 self ._children .append (child )
289292 self .__setChildDict (child )
290293
291294 return child
292-
295+
293296 def addChild2 (self , child ):
294297 """ Add the child object 'child'. If it is already present,
295298 it is overwritten by default """
296-
299+
297300 currChild = self .getChild (child .getName ())
298301 if currChild :
299302 index = self .getIndex (currChild )
@@ -303,10 +306,10 @@ def addChild2(self, child):
303306 # Unset the existing child's parent
304307 currChild .setParent (None )
305308 del currChild
306-
309+
307310 self .__setChildDict (child )
308311 else :
309- child .setParent (self )
312+ child .setParent (self )
310313 self ._children .append (child )
311314 self .__setChildDict (child )
312315
@@ -316,7 +319,7 @@ def addChild2(self, child):
316319 frame = window .addChild ('Frame' )
317320 tfield = frame .addChild ('Text Field' )
318321 tfield .setAttribute ('size' , '20' )
319-
322+
320323 btn = frame .addChild ('Button1' )
321324 btn .setAttribute ('label' , 'Submit' )
322325
0 commit comments