[19/22] AMBARI-7138. Ambari RPM deals with jinja2 dependency incorrectly (aonishuk)
http://git-wip-us.apache.org/repos/asf/ambari/blob/658360a5/ambari-common/src/main/python/ambari_jinja2/ambari_jinja2/environment.py -- diff --git a/ambari-common/src/main/python/ambari_jinja2/ambari_jinja2/environment.py b/ambari-common/src/main/python/ambari_jinja2/ambari_jinja2/environment.py new file mode 100644 index 000..b3d6bc7 --- /dev/null +++ b/ambari-common/src/main/python/ambari_jinja2/ambari_jinja2/environment.py @@ -0,0 +1,1118 @@ +# -*- coding: utf-8 -*- + +ambari_jinja2.environment +~~ + +Provides a class that holds runtime and parsing time options. + +:copyright: (c) 2010 by the Jinja Team. +:license: BSD, see LICENSE for more details. + +import os +import sys +from ambari_jinja2 import nodes +from ambari_jinja2.defaults import * +from ambari_jinja2.lexer import get_lexer, TokenStream +from ambari_jinja2.parser import Parser +from ambari_jinja2.optimizer import optimize +from ambari_jinja2.compiler import generate +from ambari_jinja2.runtime import Undefined, new_context +from ambari_jinja2.exceptions import TemplateSyntaxError, TemplateNotFound, \ + TemplatesNotFound +from ambari_jinja2.utils import import_string, LRUCache, Markup, missing, \ + concat, consume, internalcode, _encode_filename + + +# for direct template usage we have up to ten living environments +_spontaneous_environments = LRUCache(10) + +# the function to create jinja traceback objects. This is dynamically +# imported on the first exception in the exception handler. +_make_traceback = None + + +def get_spontaneous_environment(*args): +Return a new spontaneous environment. A spontaneous environment is an +unnamed and unaccessible (in theory) environment that is used for +templates generated from a string and not from the file system. + +try: +env = _spontaneous_environments.get(args) +except TypeError: +return Environment(*args) +if env is not None: +return env +_spontaneous_environments[args] = env = Environment(*args) +env.shared = True +return env + + +def create_cache(size): +Return the cache class for the given size. +if size == 0: +return None +if size 0: +return {} +return LRUCache(size) + + +def copy_cache(cache): +Create an empty copy of the given cache. +if cache is None: +return None +elif type(cache) is dict: +return {} +return LRUCache(cache.capacity) + + +def load_extensions(environment, extensions): +Load the extensions from the list and bind it to the environment. +Returns a dict of instanciated environments. + +result = {} +for extension in extensions: +if isinstance(extension, basestring): +extension = import_string(extension) +result[extension.identifier] = extension(environment) +return result + + +def _environment_sanity_check(environment): +Perform a sanity check on the environment. +assert issubclass(environment.undefined, Undefined), 'undefined must ' \ + 'be a subclass of undefined because filters depend on it.' +assert environment.block_start_string != \ + environment.variable_start_string != \ + environment.comment_start_string, 'block, variable and comment ' \ + 'start strings must be different' +assert environment.newline_sequence in ('\r', '\r\n', '\n'), \ + 'newline_sequence set to unknown line ending string.' +return environment + + +class Environment(object): +rThe core component of Jinja is the `Environment`. It contains +important shared variables like configuration, filters, tests, +globals and others. Instances of this class may be modified if +they are not shared and if no template was loaded so far. +Modifications on environments after the first template was loaded +will lead to surprising effects and undefined behavior. + +Here the possible initialization parameters: + +`block_start_string` +The string marking the begin of a block. Defaults to ``'{%'``. + +`block_end_string` +The string marking the end of a block. Defaults to ``'%}'``. + +`variable_start_string` +The string marking the begin of a print statement. +Defaults to ``'{{'``. + +`variable_end_string` +The string marking the end of a print statement. Defaults to +``'}}'``. + +`comment_start_string` +The string marking the begin of a comment. Defaults to ``'{#'``. + +`comment_end_string` +The string marking the end of a comment. Defaults to ``'#}'``. + +`line_statement_prefix` +If given and a string, this will be used as prefix for line based +statements. See also :ref:`line-statements`. + +`line_comment_prefix` +If given and a
[19/22] AMBARI-7138. Ambari RPM deals with jinja2 dependency incorrectly (aonishuk)
http://git-wip-us.apache.org/repos/asf/ambari/blob/7c3ea59f/ambari-common/src/main/python/ambari_jinja2/ambari_jinja2/environment.py -- diff --git a/ambari-common/src/main/python/ambari_jinja2/ambari_jinja2/environment.py b/ambari-common/src/main/python/ambari_jinja2/ambari_jinja2/environment.py new file mode 100644 index 000..b3d6bc7 --- /dev/null +++ b/ambari-common/src/main/python/ambari_jinja2/ambari_jinja2/environment.py @@ -0,0 +1,1118 @@ +# -*- coding: utf-8 -*- + +ambari_jinja2.environment +~~ + +Provides a class that holds runtime and parsing time options. + +:copyright: (c) 2010 by the Jinja Team. +:license: BSD, see LICENSE for more details. + +import os +import sys +from ambari_jinja2 import nodes +from ambari_jinja2.defaults import * +from ambari_jinja2.lexer import get_lexer, TokenStream +from ambari_jinja2.parser import Parser +from ambari_jinja2.optimizer import optimize +from ambari_jinja2.compiler import generate +from ambari_jinja2.runtime import Undefined, new_context +from ambari_jinja2.exceptions import TemplateSyntaxError, TemplateNotFound, \ + TemplatesNotFound +from ambari_jinja2.utils import import_string, LRUCache, Markup, missing, \ + concat, consume, internalcode, _encode_filename + + +# for direct template usage we have up to ten living environments +_spontaneous_environments = LRUCache(10) + +# the function to create jinja traceback objects. This is dynamically +# imported on the first exception in the exception handler. +_make_traceback = None + + +def get_spontaneous_environment(*args): +Return a new spontaneous environment. A spontaneous environment is an +unnamed and unaccessible (in theory) environment that is used for +templates generated from a string and not from the file system. + +try: +env = _spontaneous_environments.get(args) +except TypeError: +return Environment(*args) +if env is not None: +return env +_spontaneous_environments[args] = env = Environment(*args) +env.shared = True +return env + + +def create_cache(size): +Return the cache class for the given size. +if size == 0: +return None +if size 0: +return {} +return LRUCache(size) + + +def copy_cache(cache): +Create an empty copy of the given cache. +if cache is None: +return None +elif type(cache) is dict: +return {} +return LRUCache(cache.capacity) + + +def load_extensions(environment, extensions): +Load the extensions from the list and bind it to the environment. +Returns a dict of instanciated environments. + +result = {} +for extension in extensions: +if isinstance(extension, basestring): +extension = import_string(extension) +result[extension.identifier] = extension(environment) +return result + + +def _environment_sanity_check(environment): +Perform a sanity check on the environment. +assert issubclass(environment.undefined, Undefined), 'undefined must ' \ + 'be a subclass of undefined because filters depend on it.' +assert environment.block_start_string != \ + environment.variable_start_string != \ + environment.comment_start_string, 'block, variable and comment ' \ + 'start strings must be different' +assert environment.newline_sequence in ('\r', '\r\n', '\n'), \ + 'newline_sequence set to unknown line ending string.' +return environment + + +class Environment(object): +rThe core component of Jinja is the `Environment`. It contains +important shared variables like configuration, filters, tests, +globals and others. Instances of this class may be modified if +they are not shared and if no template was loaded so far. +Modifications on environments after the first template was loaded +will lead to surprising effects and undefined behavior. + +Here the possible initialization parameters: + +`block_start_string` +The string marking the begin of a block. Defaults to ``'{%'``. + +`block_end_string` +The string marking the end of a block. Defaults to ``'%}'``. + +`variable_start_string` +The string marking the begin of a print statement. +Defaults to ``'{{'``. + +`variable_end_string` +The string marking the end of a print statement. Defaults to +``'}}'``. + +`comment_start_string` +The string marking the begin of a comment. Defaults to ``'{#'``. + +`comment_end_string` +The string marking the end of a comment. Defaults to ``'#}'``. + +`line_statement_prefix` +If given and a string, this will be used as prefix for line based +statements. See also :ref:`line-statements`. + +`line_comment_prefix` +If given and a