|
@@ -19,18 +19,20 @@ def normalize(val):
|
|
|
to a Python object """
|
|
to a Python object """
|
|
|
|
|
|
|
|
if val.find('-') != -1:
|
|
if val.find('-') != -1:
|
|
|
- val = val.replace('-','_')
|
|
|
|
|
|
|
+ val = val.replace('-', '_')
|
|
|
|
|
|
|
|
return val
|
|
return val
|
|
|
|
|
|
|
|
|
|
+
|
|
|
def denormalize(val):
|
|
def denormalize(val):
|
|
|
""" De-normalize a string """
|
|
""" De-normalize a string """
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if val.find('_') != -1:
|
|
if val.find('_') != -1:
|
|
|
- val = val.replace('_','-')
|
|
|
|
|
|
|
+ val = val.replace('_', '-')
|
|
|
|
|
|
|
|
return val
|
|
return val
|
|
|
|
|
|
|
|
|
|
+
|
|
|
class SpecialDict(dict):
|
|
class SpecialDict(dict):
|
|
|
""" A dictionary type which allows direct attribute
|
|
""" A dictionary type which allows direct attribute
|
|
|
access to its keys """
|
|
access to its keys """
|
|
@@ -64,24 +66,25 @@ class SpecialDict(dict):
|
|
|
# New attribute
|
|
# New attribute
|
|
|
self[name] = value
|
|
self[name] = value
|
|
|
|
|
|
|
|
|
|
+
|
|
|
class CompositeDict(SpecialDict):
|
|
class CompositeDict(SpecialDict):
|
|
|
""" A class which works like a hierarchical dictionary.
|
|
""" A class which works like a hierarchical dictionary.
|
|
|
This class is based on the Composite design-pattern """
|
|
This class is based on the Composite design-pattern """
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
ID = 0
|
|
ID = 0
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def __init__(self, name=''):
|
|
def __init__(self, name=''):
|
|
|
|
|
|
|
|
if name:
|
|
if name:
|
|
|
self._name = name
|
|
self._name = name
|
|
|
else:
|
|
else:
|
|
|
- self._name = ''.join(('id#',str(self.__class__.ID)))
|
|
|
|
|
|
|
+ self._name = ''.join(('id#', str(self.__class__.ID)))
|
|
|
self.__class__.ID += 1
|
|
self.__class__.ID += 1
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
self._children = []
|
|
self._children = []
|
|
|
# Link back to father
|
|
# Link back to father
|
|
|
self._father = None
|
|
self._father = None
|
|
|
- self[self._name] = SpecialDict()
|
|
|
|
|
|
|
+ self[self._name] = SpecialDict()
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
def __getattr__(self, name):
|
|
|
|
|
|
|
@@ -101,7 +104,8 @@ class CompositeDict(SpecialDict):
|
|
|
return child
|
|
return child
|
|
|
else:
|
|
else:
|
|
|
attr = getattr(self[self._name], name)
|
|
attr = getattr(self[self._name], name)
|
|
|
- if attr: return attr
|
|
|
|
|
|
|
+ if attr:
|
|
|
|
|
+ return attr
|
|
|
|
|
|
|
|
raise AttributeError('no attribute named %s' % name)
|
|
raise AttributeError('no attribute named %s' % name)
|
|
|
|
|
|
|
@@ -306,17 +310,18 @@ class CompositeDict(SpecialDict):
|
|
|
self._children.append(child)
|
|
self._children.append(child)
|
|
|
self.__setChildDict(child)
|
|
self.__setChildDict(child)
|
|
|
|
|
|
|
|
-if __name__=="__main__":
|
|
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
window = CompositeDict('Window')
|
|
window = CompositeDict('Window')
|
|
|
frame = window.addChild('Frame')
|
|
frame = window.addChild('Frame')
|
|
|
tfield = frame.addChild('Text Field')
|
|
tfield = frame.addChild('Text Field')
|
|
|
- tfield.setAttribute('size','20')
|
|
|
|
|
|
|
+ tfield.setAttribute('size', '20')
|
|
|
|
|
|
|
|
btn = frame.addChild('Button1')
|
|
btn = frame.addChild('Button1')
|
|
|
- btn.setAttribute('label','Submit')
|
|
|
|
|
|
|
+ btn.setAttribute('label', 'Submit')
|
|
|
|
|
|
|
|
btn = frame.addChild('Button2')
|
|
btn = frame.addChild('Button2')
|
|
|
- btn.setAttribute('label','Browse')
|
|
|
|
|
|
|
+ btn.setAttribute('label', 'Browse')
|
|
|
|
|
|
|
|
# print(window)
|
|
# print(window)
|
|
|
# print(window.Frame)
|
|
# print(window.Frame)
|