1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
1
4
"""
2
5
A class which defines a composite object which can store
3
6
hieararchical dictionaries with names.
4
7
5
8
This 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,
7
10
like a Composite.
8
11
9
12
Created Anand B Pillai <abpillai@gmail.com>
17
20
def normalize (val ):
18
21
""" Normalize a string so that it can be used as an attribute
19
22
to a Python object """
20
-
23
+
21
24
if val .find ('-' ) != - 1 :
22
25
val = val .replace ('-' , '_' )
23
26
@@ -65,7 +68,7 @@ def __setattr__(self, name, value):
65
68
else :
66
69
# New attribute
67
70
self [name ] = value
68
-
71
+
69
72
70
73
class CompositeDict (SpecialDict ):
71
74
""" A class which works like a hierarchical dictionary.
@@ -106,15 +109,15 @@ def __getattr__(self, name):
106
109
attr = getattr (self [self ._name ], name )
107
110
if attr :
108
111
return attr
109
-
112
+
110
113
raise AttributeError ('no attribute named %s' % name )
111
114
112
115
def isRoot (self ):
113
116
""" Return whether I am a root component or not """
114
117
115
118
# If I don't have a parent, I am root
116
119
return not self ._father
117
-
120
+
118
121
def isLeaf (self ):
119
122
""" Return whether I am a leaf component or not """
120
123
@@ -128,15 +131,15 @@ def getName(self):
128
131
129
132
def getIndex (self , child ):
130
133
""" Return the index of the child ConfigInfo object 'child' """
131
-
134
+
132
135
if child in self ._children :
133
136
return self ._children .index (child )
134
137
else :
135
138
return - 1
136
139
137
140
def getDict (self ):
138
141
""" Return the contained dictionary """
139
-
142
+
140
143
return self [self ._name ]
141
144
142
145
def getProperty (self , child , key ):
@@ -156,25 +159,25 @@ def setProperty(self, child, key, value):
156
159
childDict = self .getInfoDict (child )
157
160
if childDict :
158
161
childDict [key ] = value
159
-
162
+
160
163
def getChildren (self ):
161
164
""" Return the list of immediate children of this object """
162
-
165
+
163
166
return self ._children
164
167
165
168
def getAllChildren (self ):
166
169
""" Return the list of all children of this object """
167
-
170
+
168
171
l = []
169
172
for child in self ._children :
170
173
l .append (child )
171
174
l .extend (child .getAllChildren ())
172
-
175
+
173
176
return l
174
177
175
178
def getChild (self , name ):
176
179
""" Return the immediate child object with the given name """
177
-
180
+
178
181
for child in self ._children :
179
182
if child .getName () == name :
180
183
return child
@@ -185,7 +188,7 @@ def findChild(self, name):
185
188
# Note - this returns the first child of the given name
186
189
# any other children with similar names down the tree
187
190
# is not considered.
188
-
191
+
189
192
for child in self .getAllChildren ():
190
193
if child .getName () == name :
191
194
return child
@@ -195,33 +198,33 @@ def findChildren(self, name):
195
198
196
199
# Note: this returns a list of all the children of a given
197
200
# name, irrespective of the depth of look-up.
198
-
201
+
199
202
children = []
200
-
203
+
201
204
for child in self .getAllChildren ():
202
205
if child .getName () == name :
203
206
children .append (child )
204
207
205
208
return children
206
-
209
+
207
210
def getPropertyDict (self ):
208
211
""" Return the property dictionary """
209
-
212
+
210
213
d = self .getChild ('__properties' )
211
214
if d :
212
215
return d .getDict ()
213
216
else :
214
217
return {}
215
-
218
+
216
219
def getParent (self ):
217
220
""" Return the person who created me """
218
221
219
222
return self ._father
220
-
223
+
221
224
def __setChildDict (self , child ):
222
225
""" Private method to set the dictionary of the child
223
226
object 'child' in the internal dictionary """
224
-
227
+
225
228
d = self [self ._name ]
226
229
d [child .getName ()] = child .getDict ()
227
230
@@ -236,25 +239,25 @@ def setParent(self, father):
236
239
# child is orphaned - see addChild and addChild2
237
240
# methods !
238
241
self ._father = father
239
-
242
+
240
243
def setName (self , name ):
241
- """ Set the name of this ConfigInfo object to 'name' """
244
+ """ Set the name of this ConfigInfo object to 'name' """
242
245
243
246
self ._name = name
244
247
245
248
def setDict (self , d ):
246
249
""" Set the contained dictionary """
247
-
250
+
248
251
self [self ._name ] = d .copy ()
249
-
252
+
250
253
def setAttribute (self , name , value ):
251
254
""" Set a name value pair in the contained dictionary """
252
-
255
+
253
256
self [self ._name ][name ] = value
254
257
255
258
def getAttribute (self , name ):
256
259
""" Return value of an attribute from the contained dictionary """
257
-
260
+
258
261
return self [self ._name ][name ]
259
262
260
263
def addChild (self , name , force = False ):
@@ -264,10 +267,10 @@ def addChild(self, name, force=False):
264
267
265
268
This function returns the child object, whether
266
269
new or existing """
267
-
270
+
268
271
if type (name ) != str :
269
272
raise ValueError ('Argument should be a string!' )
270
-
273
+
271
274
child = self .getChild (name )
272
275
if child :
273
276
# print 'Child %s present!' % name
@@ -278,22 +281,22 @@ def addChild(self, name, force=False):
278
281
child = self .__class__ (name )
279
282
self ._children [index ] = child
280
283
child .setParent (self )
281
-
284
+
282
285
self .__setChildDict (child )
283
286
return child
284
287
else :
285
288
child = self .__class__ (name )
286
289
child .setParent (self )
287
-
290
+
288
291
self ._children .append (child )
289
292
self .__setChildDict (child )
290
293
291
294
return child
292
-
295
+
293
296
def addChild2 (self , child ):
294
297
""" Add the child object 'child'. If it is already present,
295
298
it is overwritten by default """
296
-
299
+
297
300
currChild = self .getChild (child .getName ())
298
301
if currChild :
299
302
index = self .getIndex (currChild )
@@ -303,10 +306,10 @@ def addChild2(self, child):
303
306
# Unset the existing child's parent
304
307
currChild .setParent (None )
305
308
del currChild
306
-
309
+
307
310
self .__setChildDict (child )
308
311
else :
309
- child .setParent (self )
312
+ child .setParent (self )
310
313
self ._children .append (child )
311
314
self .__setChildDict (child )
312
315
@@ -316,7 +319,7 @@ def addChild2(self, child):
316
319
frame = window .addChild ('Frame' )
317
320
tfield = frame .addChild ('Text Field' )
318
321
tfield .setAttribute ('size' , '20' )
319
-
322
+
320
323
btn = frame .addChild ('Button1' )
321
324
btn .setAttribute ('label' , 'Submit' )
322
325
0 commit comments