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

# Source Code for Module Products.ZenUtils.template

     1  ###########################################################################
     2  #
     3  # This program is part of Zenoss Core, an open source monitoring platform.
     4  # Copyright (C) 2010, Zenoss Inc.
     5  #
     6  # This program is free software; you can redistribute it and/or modify it
     7  # under the terms of the GNU General Public License version 2 or (at your
     8  # option) any later version as published by the Free Software Foundation.
     9  #
    10  # For complete information please visit: http://www.zenoss.com/oss/
    11  #
    12  ###########################################################################
    13
    14 -class SpecialDict(object):
    15 -    def __init__(self, data):
    16          self_dict = object.__getattribute__(self, '__dict__')
    17          self_dict.update(data)
    18
    19 -    def __getattr__(self, item):
    20          self_dict = object.__getattribute__(self, '__dict__')
    21          try:
    22              return self_dict[item]
    23          except KeyError:
    24              return None
    25
    26 -class Template(object):
    27 -    def __init__(self, template_body):
    28          self.template_body = template_body
    29
    30 -    def fill(self, **kwargs):
    31          def dict_to_obj(d):
    32              for k,v in d.iteritems():
    33                  if isinstance(v, dict):
    34                      d[k] = dict_to_obj(v)
    35              return SpecialDict(d)
    36
    37          return self.template_body.format(**dict_to_obj(kwargs).__dict__)
    38