Python CompositeRouteFunction Example

说明

python compositeroutefunction示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Python

命名空间/包名称: piecrustrouting

示例#1
文件: jinjaengine.py项目: sinistersnare/PieCrust2

    def __init__(self, app, *args, **kwargs):
        self.app = app

        # Before we create the base Environement, let's figure out the options
        # we want to pass to it.
        twig_compatibility_mode = app.config.get("jinja/twig_compatibility")

        # Disable auto-reload when we're baking.
        if app.config.get("baker/is_baking"):
            kwargs.setdefault("auto_reload", False)

        # Let the user override most Jinja options via the site config.
        for name in [
            "block_start_string",
            "block_end_string",
            "variable_start_string",
            "variable_end_string",
            "comment_start_string",
            "comment_end_string",
            "line_statement_prefix",
            "line_comment_prefix",
            "trim_blocks",
            "lstrip_blocks",
            "newline_sequence",
            "keep_trailing_newline",
        ]:
            val = app.config.get("jinja/" + name)
            if val is not None:
                kwargs.setdefault(name, val)

        # Twig trims blocks.
        if twig_compatibility_mode is True:
            kwargs["trim_blocks"] = True

        # All good! Create the Environment.
        super(PieCrustEnvironment, self).__init__(*args, **kwargs)

        # Now add globals and filters.
        self.globals.update({"fail": raise_exception, "highlight_css": get_highlight_css})

        self.filters.update(
            {
                "keys": get_dict_keys,
                "values": get_dict_values,
                "paginate": self._paginate,
                "formatwith": self._formatWith,
                "markdown": lambda v: self._formatWith(v, "markdown"),
                "textile": lambda v: self._formatWith(v, "textile"),
                "nocache": add_no_cache_parameter,
                "wordcount": get_word_count,
                "stripoutertag": strip_outer_tag,
                "stripslash": strip_slash,
                "titlecase": title_case,
                "atomdate": get_xml_date,
                "xmldate": get_xml_date,
                "emaildate": get_email_date,
                "date": get_date,
            }
        )

        # Backwards compatibility with Twig.
        if twig_compatibility_mode is True:
            self.filters["raw"] = self.filters["safe"]
            self.globals["pcfail"] = raise_exception

        # Add route functions.
        for route in app.routes:
            name = route.template_func_name
            func = self.globals.get(name)
            if func is None:
                func = CompositeRouteFunction()
                func.addFunc(route)
                self.globals[name] = func
            elif isinstance(func, CompositeRouteFunction):
                self.globals[name].addFunc(route)
            else:
                raise Exception("Route function '%s' collides with an " "existing function or template data." % name)

示例#2
文件: jinjaengine.py项目: ronCYA/PieCrust2

    def __init__(self, app, *args, **kwargs):
        self.app = app

        # Before we create the base Environement, let's figure out the options
        # we want to pass to it.
        twig_compatibility_mode = app.config.get('jinja/twig_compatibility')

        # Disable auto-reload when we're baking.
        if app.config.get('baker/is_baking'):
            kwargs.setdefault('auto_reload', False)

        # Let the user override most Jinja options via the site config.
        for name in ['block_start_string', 'block_end_string',
                     'variable_start_string', 'variable_end_string',
                     'comment_start_string', 'comment_end_string',
                     'line_statement_prefix', 'line_comment_prefix',
                     'trim_blocks', 'lstrip_blocks',
                     'newline_sequence', 'keep_trailing_newline']:
            val = app.config.get('jinja/' + name)
            if val is not None:
                kwargs.setdefault(name, val)

        # Twig trims blocks.
        if twig_compatibility_mode is True:
            kwargs['trim_blocks'] = True

        # All good! Create the Environment.
        super(PieCrustEnvironment, self).__init__(*args, **kwargs)

        # Now add globals and filters.
        self.globals.update({
                'fail': raise_exception,
                'highlight_css': get_highlight_css})

        self.filters.update({
                'keys': get_dict_keys,
                'values': get_dict_values,
                'paginate': self._paginate,
                'formatwith': self._formatWith,
                'markdown': lambda v: self._formatWith(v, 'markdown'),
                'textile': lambda v: self._formatWith(v, 'textile'),
                'nocache': add_no_cache_parameter,
                'wordcount': get_word_count,
                'stripoutertag': strip_outer_tag,
                'stripslash': strip_slash,
                'titlecase': title_case,
                'atomdate': get_xml_date,
                'xmldate': get_xml_date,
                'emaildate': get_email_date,
                'date': get_date})

        # Backwards compatibility with Twig.
        if twig_compatibility_mode is True:
            self.filters['raw'] = self.filters['safe']
            self.globals['pcfail'] = raise_exception

        # Add route functions.
        for route in app.routes:
            name = route.template_func_name
            func = self.globals.get(name)
            if func is None:
                func = CompositeRouteFunction()
                func.addFunc(route)
                self.globals[name] = func
            elif isinstance(func, CompositeRouteFunction):
                self.globals[name].addFunc(route)
            else:
                raise Exception("Route function '%s' collides with an "
                                "existing function or template data." %
                                name)

展开阅读全文