AMBARI-3659. Resource Management. Support inline templates (Andrew Onischuk via dlysnichenko)
Project: http://git-wip-us.apache.org/repos/asf/incubator-ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ambari/commit/267239e4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ambari/tree/267239e4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ambari/diff/267239e4 Branch: refs/heads/trunk Commit: 267239e4018681c23689ec09b872a67057d23740 Parents: 84274b4 Author: Lisnichenko Dmitro <[email protected]> Authored: Fri Nov 1 17:17:01 2013 +0200 Committer: Lisnichenko Dmitro <[email protected]> Committed: Fri Nov 1 17:17:01 2013 +0200 ---------------------------------------------------------------------- .../python/resource_management/core/source.py | 22 +++++-- .../python/resource_management/core/utils.py | 62 +++----------------- 2 files changed, 25 insertions(+), 59 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/267239e4/ambari-agent/src/main/python/resource_management/core/source.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/core/source.py b/ambari-agent/src/main/python/resource_management/core/source.py index 3a0035f..84d27b1 100644 --- a/ambari-agent/src/main/python/resource_management/core/source.py +++ b/ambari-agent/src/main/python/resource_management/core/source.py @@ -1,7 +1,7 @@ from __future__ import with_statement from resource_management.core import environment -__all__ = ["Source", "Template", "StaticFile", "DownloadSource"] +__all__ = ["Source", "Template", "InlineTemplate", "StaticFile", "DownloadSource"] import hashlib import os @@ -39,11 +39,15 @@ class StaticFile(Source): try: - from jinja2 import Environment, BaseLoader, TemplateNotFound + from jinja2 import Environment, BaseLoader, TemplateNotFound, FunctionLoader except ImportError: class Template(Source): def __init__(self, name, variables=None, env=None): - raise Exception("Jinja2 required for Template") + raise Exception("Jinja2 required for Template/InlineTemplate") + + class InlineTemplate(Source): + def __init__(self, name, variables=None, env=None): + raise Exception("Jinja2 required for Template/InlineTemplate") else: class TemplateLoader(BaseLoader): def __init__(self, env=None): @@ -72,9 +76,10 @@ else: params = self.env.config.params variables = params if params else variables self.context = variables.copy() if variables else {} - self.template_env = Environment(loader=TemplateLoader(self.env), - autoescape=False) - self.template = self.template_env.get_template(self.name) + if not hasattr(self, 'template_env'): + self.template_env = Environment(loader=TemplateLoader(self.env), + autoescape=False) + self.template = self.template_env.get_template(self.name) def get_content(self): self.context.update( @@ -85,6 +90,11 @@ else: ) rendered = self.template.render(self.context) return rendered + "\n" if not rendered.endswith('\n') else rendered + + class InlineTemplate(Template): + def __init__(self, name, variables=None, env=None): + self.template_env = Environment(loader=FunctionLoader(lambda text: text)) + super(InlineTemplate, self).__init__(name, variables, env) class DownloadSource(Source): http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/267239e4/ambari-agent/src/main/python/resource_management/core/utils.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/resource_management/core/utils.py b/ambari-agent/src/main/python/resource_management/core/utils.py index 4a00576..856c2a6 100644 --- a/ambari-agent/src/main/python/resource_management/core/utils.py +++ b/ambari-agent/src/main/python/resource_management/core/utils.py @@ -66,68 +66,24 @@ class AttributeDictionary(object): def __setstate__(self, state): super(AttributeDictionary, self).__setattr__("_dict", state) -class ParamsAttributeDictionary(object): +class ParamsAttributeDictionary(AttributeDictionary): """ This class can store user parameters - and support some features necessary for substitution to work. + and it supports some features necessary for substitution to work. """ def __init__(self, substitutor, *args, **kwargs): - d = kwargs - if len(args)==1: - d = args[0] - super(ParamsAttributeDictionary, self).__setattr__("_dict", d) - super(ParamsAttributeDictionary, self).__setattr__("substitutor", substitutor) - - def __setattr__(self, name, value): - self[name] = value - - def __setitem__(self, name, value): - self._dict[name] = self._convert_value(value) + super(ParamsAttributeDictionary, self).__init__(*args, **kwargs) + super(AttributeDictionary, self).__setattr__("substitutor", substitutor) def __getitem__(self, name): val = self.substitutor.get_subdict(name, self._dict) return self._convert_value(val) - def _convert_value(self, value): - if isinstance(value, dict) and not isinstance(value, ParamsAttributeDictionary): - return ParamsAttributeDictionary(self.substitutor, value) - return value - def copy(self): - return self.__class__(self._dict.copy()) - - def update(self, *args, **kwargs): - self._dict.update(*args, **kwargs) - - def items(self): - return self._dict.items() - - def values(self): - return self._dict.values() - - def keys(self): - return self._dict.keys() - - def pop(self, *args, **kwargs): - return self._dict.pop(*args, **kwargs) - - def get(self, *args, **kwargs): - return self._dict.get(*args, **kwargs) - - def __repr__(self): - return self._dict.__repr__() + return ParamsAttributeDictionary(self.substitutor, self._dict) def __unicode__(self): - return self._dict.__unicode__() - - def __str__(self): - return self._dict.__str__() - - def __iter__(self): - return self._dict.__iter__() - - def __getstate__(self): - return self._dict - - def __setstate__(self, state): - super(ParamsAttributeDictionary, self).__setattr__("_dict", state) \ No newline at end of file + if isinstance(self._dict, str): + return self._dict.__unicode__() + else: + return str(self._dict) \ No newline at end of file
