Ejemplo n.º 1
0
    def __init__(
        self,
        block_start_string="{%",
        block_end_string="%}",
        variable_start_string="{{",
        variable_end_string="}}",
        comment_start_string="{#",
        comment_end_string="#}",
        trim_blocks=False,
        auto_escape=False,
        default_filters=None,
        template_charset="utf-8",
        charset="utf-8",
        namespace=None,
        loader=None,
        filters=None,
        tests=None,
        context_class=Context,
        undefined_singleton=SilentUndefined,
        disable_regexps=False,
        friendly_traceback=True,
        translator_factory=None,
        template_translator=PythonTranslator,
    ):
        """
        Here the possible initialization parameters:

        ========================= ============================================
        `block_start_string` *    the string marking the begin of a block.
                                  this 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 ``'{{'``.
        `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 ``'#}'``.
        `trim_blocks` *           If this is set to ``True`` the first newline
                                  after a block is removed (block, not
                                  variable tag!). Defaults to ``False``.
        `auto_escape`             If this is set to ``True`` Jinja will
                                  automatically escape all variables using xml
                                  escaping methods. If you don't want to
                                  escape a string you have to wrap it in a
                                  ``Markup`` object from the
                                  ``jinja.datastructure`` module. If
                                  `auto_escape` is ``True`` there will be also
                                  a ``Markup`` object in the template
                                  namespace to define partial html fragments.
                                  Note that we do not recommend this feature.
        `default_filters`         list of tuples in the form (``filter_name``,
                                  ``arguments``) where ``filter_name`` is the
                                  name of a registered filter and
                                  ``arguments`` a tuple with the filter
                                  arguments. The filters specified here will
                                  always be applied when printing data to the
                                  template. *new in Jinja 1.1*
        `template_charset`        The charset of the templates. Defaults
                                  to ``'utf-8'``.
        `charset`                 Charset of all string input data. Defaults
                                  to ``'utf-8'``.
        `namespace`               Global namespace for all templates.
        `loader`                  Specify a template loader.
        `filters`                 dict of filters or the default filters if
                                  not defined.
        `tests`                   dict of tests of the default tests if not
                                  defined.
        `context_class`           the context class this template should use.
                                  See the `Context` documentation for more
                                  details.
        `undefined_singleton`     The singleton value that is used for missing
                                  variables. *new in Jinja 1.1*
        `disable_regexps`         Disable support for regular expresssions.
        `friendly_traceback`      Set this to `False` to disable the developer
                                  friendly traceback rewriting. Whenever an
                                  runtime or syntax error occours jinja will
                                  try to make a developer friendly traceback
                                  that shows the error in the template line.
                                  This however can be annoying when debugging
                                  broken functions that are called from the
                                  template. *new in Jinja 1.1*
        `translator_factory`      A callback function that is called with
                                  the context as first argument to get the
                                  translator for the current instance.
                                  *new in Jinja 1.2*
        `template_translator`     An class that defines a static method called
                                  process which can be used to process the 
                                  template's AST into a compiled python module.
                                  *new in Jinja 1.2*
        ========================= ============================================

        All of these variables except those marked with a star (*) are
        modifiable after environment initialization.
        """

        # lexer / parser information
        self.block_start_string = block_start_string
        self.block_end_string = block_end_string
        self.variable_start_string = variable_start_string
        self.variable_end_string = variable_end_string
        self.comment_start_string = comment_start_string
        self.comment_end_string = comment_end_string
        self.trim_blocks = trim_blocks

        # other stuff
        self.template_charset = template_charset
        self.charset = charset
        self.loader = loader
        if filters is None:
            filters = DEFAULT_FILTERS.copy()
        self.filters = filters
        if tests is None:
            tests = DEFAULT_TESTS.copy()
        self.tests = tests
        self.default_filters = default_filters or []
        self.context_class = context_class
        self.undefined_singleton = undefined_singleton
        self.disable_regexps = disable_regexps
        self.friendly_traceback = friendly_traceback

        # global namespace
        if namespace is None:
            namespace = DEFAULT_NAMESPACE.copy()
        self.globals = namespace

        # jinja 1.0 compatibility
        if auto_escape:
            self.default_filters.append(("escape", (True,)))
            self.globals["Markup"] = Markup

        # and here the translator factory
        self.translator_factory = translator_factory

        # and here the AST translator
        self.template_translator = template_translator

        # create lexer
        self.lexer = Lexer(self)
Ejemplo n.º 2
0
    def __init__(self,
                 block_start_string='{%',
                 block_end_string='%}',
                 variable_start_string='{{',
                 variable_end_string='}}',
                 comment_start_string='{#',
                 comment_end_string='#}',
                 trim_blocks=False,
                 auto_escape=False,
                 default_filters=None,
                 template_charset='utf-8',
                 charset='utf-8',
                 namespace=None,
                 loader=None,
                 filters=None,
                 tests=None,
                 context_class=Context,
                 undefined_singleton=SilentUndefined,
                 disable_regexps=False,
                 friendly_traceback=True,
                 translator_factory=None):
        """
        Here the possible initialization parameters:

        ========================= ============================================
        `block_start_string` *    the string marking the begin of a block.
                                  this 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 ``'{{'``.
        `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 ``'#}'``.
        `trim_blocks` *           If this is set to ``True`` the first newline
                                  after a block is removed (block, not
                                  variable tag!). Defaults to ``False``.
        `auto_escape`             If this is set to ``True`` Jinja will
                                  automatically escape all variables using xml
                                  escaping methods. If you don't want to
                                  escape a string you have to wrap it in a
                                  ``Markup`` object from the
                                  ``jinja.datastructure`` module. If
                                  `auto_escape` is ``True`` there will be also
                                  a ``Markup`` object in the template
                                  namespace to define partial html fragments.
                                  Note that we do not recommend this feature.
        `default_filters`         list of tuples in the form (``filter_name``,
                                  ``arguments``) where ``filter_name`` is the
                                  name of a registered filter and
                                  ``arguments`` a tuple with the filter
                                  arguments. The filters specified here will
                                  always be applied when printing data to the
                                  template. *new in Jinja 1.1*
        `template_charset`        The charset of the templates. Defaults
                                  to ``'utf-8'``.
        `charset`                 Charset of all string input data. Defaults
                                  to ``'utf-8'``.
        `namespace`               Global namespace for all templates.
        `loader`                  Specify a template loader.
        `filters`                 dict of filters or the default filters if
                                  not defined.
        `tests`                   dict of tests of the default tests if not
                                  defined.
        `context_class`           the context class this template should use.
                                  See the `Context` documentation for more
                                  details.
        `undefined_singleton`     The singleton value that is used for missing
                                  variables. *new in Jinja 1.1*
        `disable_regexps`         Disable support for regular expresssions.
        `friendly_traceback`      Set this to `False` to disable the developer
                                  friendly traceback rewriting. Whenever an
                                  runtime or syntax error occours jinja will
                                  try to make a developer friendly traceback
                                  that shows the error in the template line.
                                  This however can be annoying when debugging
                                  broken functions that are called from the
                                  template. *new in Jinja 1.1*
        `translator_factory`      A callback function that is called with
                                  the context as first argument to get the
                                  translator for the current instance.
                                  *new in Jinja 1.2*
        ========================= ============================================

        All of these variables except those marked with a star (*) are
        modifiable after environment initialization.
        """

        # lexer / parser information
        self.block_start_string = block_start_string
        self.block_end_string = block_end_string
        self.variable_start_string = variable_start_string
        self.variable_end_string = variable_end_string
        self.comment_start_string = comment_start_string
        self.comment_end_string = comment_end_string
        self.trim_blocks = trim_blocks

        # other stuff
        self.template_charset = template_charset
        self.charset = charset
        self.loader = loader
        if filters is None:
            filters = DEFAULT_FILTERS.copy()
        self.filters = filters
        if tests is None:
            tests = DEFAULT_TESTS.copy()
        self.tests = tests
        self.default_filters = default_filters or []
        self.context_class = context_class
        self.undefined_singleton = undefined_singleton
        self.disable_regexps = disable_regexps
        self.friendly_traceback = friendly_traceback

        # global namespace
        if namespace is None:
            namespace = DEFAULT_NAMESPACE.copy()
        self.globals = namespace

        # jinja 1.0 compatibility
        if auto_escape:
            self.default_filters.append(('escape', (True, )))
            self.globals['Markup'] = Markup

        # and here the translator factory
        self.translator_factory = translator_factory

        # create lexer
        self.lexer = Lexer(self)
Ejemplo n.º 3
0
class Environment(object):
    """
    The Jinja environment.

    The core component of Jinja is the `Environment`. It contains
    important shared variables like configuration, filters, tests,
    globals and others.
    """

    def __init__(
        self,
        block_start_string="{%",
        block_end_string="%}",
        variable_start_string="{{",
        variable_end_string="}}",
        comment_start_string="{#",
        comment_end_string="#}",
        trim_blocks=False,
        auto_escape=False,
        default_filters=None,
        template_charset="utf-8",
        charset="utf-8",
        namespace=None,
        loader=None,
        filters=None,
        tests=None,
        context_class=Context,
        undefined_singleton=SilentUndefined,
        disable_regexps=False,
        friendly_traceback=True,
        translator_factory=None,
        template_translator=PythonTranslator,
    ):
        """
        Here the possible initialization parameters:

        ========================= ============================================
        `block_start_string` *    the string marking the begin of a block.
                                  this 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 ``'{{'``.
        `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 ``'#}'``.
        `trim_blocks` *           If this is set to ``True`` the first newline
                                  after a block is removed (block, not
                                  variable tag!). Defaults to ``False``.
        `auto_escape`             If this is set to ``True`` Jinja will
                                  automatically escape all variables using xml
                                  escaping methods. If you don't want to
                                  escape a string you have to wrap it in a
                                  ``Markup`` object from the
                                  ``jinja.datastructure`` module. If
                                  `auto_escape` is ``True`` there will be also
                                  a ``Markup`` object in the template
                                  namespace to define partial html fragments.
                                  Note that we do not recommend this feature.
        `default_filters`         list of tuples in the form (``filter_name``,
                                  ``arguments``) where ``filter_name`` is the
                                  name of a registered filter and
                                  ``arguments`` a tuple with the filter
                                  arguments. The filters specified here will
                                  always be applied when printing data to the
                                  template. *new in Jinja 1.1*
        `template_charset`        The charset of the templates. Defaults
                                  to ``'utf-8'``.
        `charset`                 Charset of all string input data. Defaults
                                  to ``'utf-8'``.
        `namespace`               Global namespace for all templates.
        `loader`                  Specify a template loader.
        `filters`                 dict of filters or the default filters if
                                  not defined.
        `tests`                   dict of tests of the default tests if not
                                  defined.
        `context_class`           the context class this template should use.
                                  See the `Context` documentation for more
                                  details.
        `undefined_singleton`     The singleton value that is used for missing
                                  variables. *new in Jinja 1.1*
        `disable_regexps`         Disable support for regular expresssions.
        `friendly_traceback`      Set this to `False` to disable the developer
                                  friendly traceback rewriting. Whenever an
                                  runtime or syntax error occours jinja will
                                  try to make a developer friendly traceback
                                  that shows the error in the template line.
                                  This however can be annoying when debugging
                                  broken functions that are called from the
                                  template. *new in Jinja 1.1*
        `translator_factory`      A callback function that is called with
                                  the context as first argument to get the
                                  translator for the current instance.
                                  *new in Jinja 1.2*
        `template_translator`     An class that defines a static method called
                                  process which can be used to process the 
                                  template's AST into a compiled python module.
                                  *new in Jinja 1.2*
        ========================= ============================================

        All of these variables except those marked with a star (*) are
        modifiable after environment initialization.
        """

        # lexer / parser information
        self.block_start_string = block_start_string
        self.block_end_string = block_end_string
        self.variable_start_string = variable_start_string
        self.variable_end_string = variable_end_string
        self.comment_start_string = comment_start_string
        self.comment_end_string = comment_end_string
        self.trim_blocks = trim_blocks

        # other stuff
        self.template_charset = template_charset
        self.charset = charset
        self.loader = loader
        if filters is None:
            filters = DEFAULT_FILTERS.copy()
        self.filters = filters
        if tests is None:
            tests = DEFAULT_TESTS.copy()
        self.tests = tests
        self.default_filters = default_filters or []
        self.context_class = context_class
        self.undefined_singleton = undefined_singleton
        self.disable_regexps = disable_regexps
        self.friendly_traceback = friendly_traceback

        # global namespace
        if namespace is None:
            namespace = DEFAULT_NAMESPACE.copy()
        self.globals = namespace

        # jinja 1.0 compatibility
        if auto_escape:
            self.default_filters.append(("escape", (True,)))
            self.globals["Markup"] = Markup

        # and here the translator factory
        self.translator_factory = translator_factory

        # and here the AST translator
        self.template_translator = template_translator

        # create lexer
        self.lexer = Lexer(self)

    def loader(self, value):
        """
        Get or set the template loader.
        """
        self._loader = LoaderWrapper(self, value)

    loader = property(lambda s: s._loader, loader, doc=loader.__doc__)

    def parse(self, source, filename=None):
        """
        Parse the sourcecode and return the abstract syntax tree. This tree
        of nodes is used by the `translators`_ to convert the template into
        executable source- or bytecode.

        .. _translators: translators.txt
        """
        parser = Parser(self, source, filename)
        return parser.parse()

    def lex(self, source, filename=None):
        """
        Lex the given sourcecode and return a generator that yields tokens.
        The stream returned is not usable for Jinja but can be used if
        Jinja templates should be processed by other tools (for example
        syntax highlighting etc)

        The tuples are returned in the form ``(lineno, token, value)``.
        """
        return self.lexer.tokeniter(source, filename)

    def from_string(self, source):
        """
        Load and parse a template source and translate it into eval-able
        Python code. This code is wrapped within a `Template` class that
        allows you to render it.
        """
        from jinja.translators.python import PythonTranslator

        try:
            rv = PythonTranslator.process(self, Parser(self, source).parse(), source)
        except TemplateSyntaxError, e:
            # on syntax errors rewrite the traceback if wanted
            if not self.friendly_traceback:
                raise
            from jinja.debugger import raise_syntax_error

            if __debug__:
                __traceback_hide__ = True
            raise_syntax_error(e, self, source)
        else:
Ejemplo n.º 4
0
class Environment(object):
    """
    The Jinja environment.

    The core component of Jinja is the `Environment`. It contains
    important shared variables like configuration, filters, tests,
    globals and others.
    """
    def __init__(self,
                 block_start_string='{%',
                 block_end_string='%}',
                 variable_start_string='{{',
                 variable_end_string='}}',
                 comment_start_string='{#',
                 comment_end_string='#}',
                 trim_blocks=False,
                 auto_escape=False,
                 default_filters=None,
                 template_charset='utf-8',
                 charset='utf-8',
                 namespace=None,
                 loader=None,
                 filters=None,
                 tests=None,
                 context_class=Context,
                 undefined_singleton=SilentUndefined,
                 disable_regexps=False,
                 friendly_traceback=True,
                 translator_factory=None):
        """
        Here the possible initialization parameters:

        ========================= ============================================
        `block_start_string` *    the string marking the begin of a block.
                                  this 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 ``'{{'``.
        `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 ``'#}'``.
        `trim_blocks` *           If this is set to ``True`` the first newline
                                  after a block is removed (block, not
                                  variable tag!). Defaults to ``False``.
        `auto_escape`             If this is set to ``True`` Jinja will
                                  automatically escape all variables using xml
                                  escaping methods. If you don't want to
                                  escape a string you have to wrap it in a
                                  ``Markup`` object from the
                                  ``jinja.datastructure`` module. If
                                  `auto_escape` is ``True`` there will be also
                                  a ``Markup`` object in the template
                                  namespace to define partial html fragments.
                                  Note that we do not recommend this feature.
        `default_filters`         list of tuples in the form (``filter_name``,
                                  ``arguments``) where ``filter_name`` is the
                                  name of a registered filter and
                                  ``arguments`` a tuple with the filter
                                  arguments. The filters specified here will
                                  always be applied when printing data to the
                                  template. *new in Jinja 1.1*
        `template_charset`        The charset of the templates. Defaults
                                  to ``'utf-8'``.
        `charset`                 Charset of all string input data. Defaults
                                  to ``'utf-8'``.
        `namespace`               Global namespace for all templates.
        `loader`                  Specify a template loader.
        `filters`                 dict of filters or the default filters if
                                  not defined.
        `tests`                   dict of tests of the default tests if not
                                  defined.
        `context_class`           the context class this template should use.
                                  See the `Context` documentation for more
                                  details.
        `undefined_singleton`     The singleton value that is used for missing
                                  variables. *new in Jinja 1.1*
        `disable_regexps`         Disable support for regular expresssions.
        `friendly_traceback`      Set this to `False` to disable the developer
                                  friendly traceback rewriting. Whenever an
                                  runtime or syntax error occours jinja will
                                  try to make a developer friendly traceback
                                  that shows the error in the template line.
                                  This however can be annoying when debugging
                                  broken functions that are called from the
                                  template. *new in Jinja 1.1*
        `translator_factory`      A callback function that is called with
                                  the context as first argument to get the
                                  translator for the current instance.
                                  *new in Jinja 1.2*
        ========================= ============================================

        All of these variables except those marked with a star (*) are
        modifiable after environment initialization.
        """

        # lexer / parser information
        self.block_start_string = block_start_string
        self.block_end_string = block_end_string
        self.variable_start_string = variable_start_string
        self.variable_end_string = variable_end_string
        self.comment_start_string = comment_start_string
        self.comment_end_string = comment_end_string
        self.trim_blocks = trim_blocks

        # other stuff
        self.template_charset = template_charset
        self.charset = charset
        self.loader = loader
        if filters is None:
            filters = DEFAULT_FILTERS.copy()
        self.filters = filters
        if tests is None:
            tests = DEFAULT_TESTS.copy()
        self.tests = tests
        self.default_filters = default_filters or []
        self.context_class = context_class
        self.undefined_singleton = undefined_singleton
        self.disable_regexps = disable_regexps
        self.friendly_traceback = friendly_traceback

        # global namespace
        if namespace is None:
            namespace = DEFAULT_NAMESPACE.copy()
        self.globals = namespace

        # jinja 1.0 compatibility
        if auto_escape:
            self.default_filters.append(('escape', (True, )))
            self.globals['Markup'] = Markup

        # and here the translator factory
        self.translator_factory = translator_factory

        # create lexer
        self.lexer = Lexer(self)

    def loader(self, value):
        """
        Get or set the template loader.
        """
        self._loader = LoaderWrapper(self, value)

    loader = property(lambda s: s._loader, loader, doc=loader.__doc__)

    def parse(self, source, filename=None):
        """
        Parse the sourcecode and return the abstract syntax tree. This tree
        of nodes is used by the `translators`_ to convert the template into
        executable source- or bytecode.

        .. _translators: translators.txt
        """
        parser = Parser(self, source, filename)
        return parser.parse()

    def lex(self, source, filename=None):
        """
        Lex the given sourcecode and return a generator that yields tokens.
        The stream returned is not usable for Jinja but can be used if
        Jinja templates should be processed by other tools (for example
        syntax highlighting etc)

        The tuples are returned in the form ``(lineno, token, value)``.
        """
        return self.lexer.tokeniter(source, filename)

    def from_string(self, source):
        """
        Load and parse a template source and translate it into eval-able
        Python code. This code is wrapped within a `Template` class that
        allows you to render it.
        """
        from jinja.translators.python import PythonTranslator
        try:
            rv = PythonTranslator.process(self,
                                          Parser(self, source).parse(), source)
        except TemplateSyntaxError, e:
            # on syntax errors rewrite the traceback if wanted
            if not self.friendly_traceback:
                raise
            from jinja.debugger import raise_syntax_error
            if __debug__:
                __traceback_hide__ = True
            raise_syntax_error(e, self, source)
        else: