Package Products :: Package ZenUtils :: Module template
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenUtils.template

 1  ############################################################################## 
 2  #  
 3  # Copyright (C) Zenoss, Inc. 2010, all rights reserved. 
 4  #  
 5  # This content is made available according to terms specified in 
 6  # License.zenoss under the directory where your Zenoss product is installed. 
 7  #  
 8  ############################################################################## 
 9   
10   
11 -class SpecialDict(object):
12 - def __init__(self, data):
13 self_dict = object.__getattribute__(self, '__dict__') 14 self_dict.update(data)
15
16 - def __getattr__(self, item):
17 self_dict = object.__getattribute__(self, '__dict__') 18 try: 19 return self_dict[item] 20 except KeyError: 21 return None
22
23 -class Template(object):
24 - def __init__(self, template_body):
25 self.template_body = template_body
26
27 - def fill(self, **kwargs):
28 def dict_to_obj(d): 29 for k,v in d.iteritems(): 30 if isinstance(v, dict): 31 d[k] = dict_to_obj(v) 32 return SpecialDict(d)
33 34 return self.template_body.format(**dict_to_obj(kwargs).__dict__)
35