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 <[email protected] > 1720def normalize (val ):
1821""" Normalize a string so that it can be used as an attribute
1922 to a Python object """
20-
23+
2124if val .find ('-' ) != - 1 :
2225val = val .replace ('-' , '_' )
2326
@@ -65,7 +68,7 @@ def __setattr__(self, name, value):
6568else :
6669# New attribute
6770self [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):
106109attr = getattr (self [self ._name ], name )
107110if attr :
108111return attr
109-
112+
110113raise AttributeError ('no attribute named %s' % name )
111114
112115def isRoot (self ):
113116""" Return whether I am a root component or not """
114117
115118# If I don't have a parent, I am root
116119return not self ._father
117-
120+
118121def isLeaf (self ):
119122""" Return whether I am a leaf component or not """
120123
@@ -128,15 +131,15 @@ def getName(self):
128131
129132def getIndex (self , child ):
130133""" Return the index of the child ConfigInfo object 'child' """
131-
134+
132135if child in self ._children :
133136return self ._children .index (child )
134137else :
135138return - 1
136139
137140def getDict (self ):
138141""" Return the contained dictionary """
139-
142+
140143return self [self ._name ]
141144
142145def getProperty (self , child , key ):
@@ -156,25 +159,25 @@ def setProperty(self, child, key, value):
156159childDict = self .getInfoDict (child )
157160if childDict :
158161childDict [key ] = value
159-
162+
160163def getChildren (self ):
161164""" Return the list of immediate children of this object """
162-
165+
163166return self ._children
164167
165168def getAllChildren (self ):
166169""" Return the list of all children of this object """
167-
170+
168171l = []
169172for child in self ._children :
170173l .append (child )
171174l .extend (child .getAllChildren ())
172-
175+
173176return l
174177
175178def getChild (self , name ):
176179""" Return the immediate child object with the given name """
177-
180+
178181for child in self ._children :
179182if child .getName () == name :
180183return 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+
189192for child in self .getAllChildren ():
190193if child .getName () == name :
191194return 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+
199202children = []
200-
203+
201204for child in self .getAllChildren ():
202205if child .getName () == name :
203206children .append (child )
204207
205208return children
206-
209+
207210def getPropertyDict (self ):
208211""" Return the property dictionary """
209-
212+
210213d = self .getChild ('__properties' )
211214if d :
212215return d .getDict ()
213216else :
214217return {}
215-
218+
216219def getParent (self ):
217220""" Return the person who created me """
218221
219222return self ._father
220-
223+
221224def __setChildDict (self , child ):
222225""" Private method to set the dictionary of the child
223226 object 'child' in the internal dictionary """
224-
227+
225228d = self [self ._name ]
226229d [child .getName ()] = child .getDict ()
227230
@@ -236,25 +239,25 @@ def setParent(self, father):
236239# child is orphaned - see addChild and addChild2
237240# methods !
238241self ._father = father
239-
242+
240243def setName (self , name ):
241- """ Set the name of this ConfigInfo object to 'name' """
244+ """ Set the name of this ConfigInfo object to 'name' """
242245
243246self ._name = name
244247
245248def setDict (self , d ):
246249""" Set the contained dictionary """
247-
250+
248251self [self ._name ] = d .copy ()
249-
252+
250253def setAttribute (self , name , value ):
251254""" Set a name value pair in the contained dictionary """
252-
255+
253256self [self ._name ][name ] = value
254257
255258def getAttribute (self , name ):
256259""" Return value of an attribute from the contained dictionary """
257-
260+
258261return self [self ._name ][name ]
259262
260263def 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+
268271if type (name ) != str :
269272raise ValueError ('Argument should be a string!' )
270-
273+
271274child = self .getChild (name )
272275if child :
273276# print 'Child %s present!' % name
@@ -278,22 +281,22 @@ def addChild(self, name, force=False):
278281child = self .__class__ (name )
279282self ._children [index ] = child
280283child .setParent (self )
281-
284+
282285self .__setChildDict (child )
283286return child
284287else :
285288child = self .__class__ (name )
286289child .setParent (self )
287-
290+
288291self ._children .append (child )
289292self .__setChildDict (child )
290293
291294return child
292-
295+
293296def addChild2 (self , child ):
294297""" Add the child object 'child'. If it is already present,
295298 it is overwritten by default """
296-
299+
297300currChild = self .getChild (child .getName ())
298301if currChild :
299302index = self .getIndex (currChild )
@@ -303,10 +306,10 @@ def addChild2(self, child):
303306# Unset the existing child's parent
304307currChild .setParent (None )
305308del currChild
306-
309+
307310self .__setChildDict (child )
308311else :
309- child .setParent (self )
312+ child .setParent (self )
310313self ._children .append (child )
311314self .__setChildDict (child )
312315
@@ -316,7 +319,7 @@ def addChild2(self, child):
316319frame = window .addChild ('Frame' )
317320tfield = frame .addChild ('Text Field' )
318321tfield .setAttribute ('size' , '20' )
319-
322+
320323btn = frame .addChild ('Button1' )
321324btn .setAttribute ('label' , 'Submit' )
322325
0 commit comments