コード例 #1
0
        def prepare_dec(func):
            self.params, xx, xxx, defaults = getargspec(func)
            self.cached_function = cache_function(func, uid_extra='*'+describe_func(func))

            def actual_func(self, request, tl, one, two, module, extra, prog):
                #   Construct argument list
                param_name_list = ['self', 'request', 'tl', 'one', 'two', 'module', 'extra', 'prog']
                param_list = [self, request, tl, one, two, module, extra, prog]
                args_for_func = []
                for i in range(len(param_list)):
                    if param_name_list[i] in parent_obj.params:
                        args_for_func.append(param_list[i])
                return parent_obj.cached_function(*args_for_func)
            
            return actual_func
コード例 #2
0
ファイル: template.py プロジェクト: sweettea/ESP-Website
            class InclusionNode(template.Node):

                def __init__(in_self, takes_context, vars_to_resolve, kwargs):
                    in_self.vars_to_resolve = vars_to_resolve
                    
                def __str__(in_self):
                    return '<IN>'

                def render_given_args(in_self, args):
                    dict = cached_function(*args)

                    if not getattr(in_self, 'nodelist', False):
                        from django.template.loader import get_template, select_template
                        if hasattr(file_name, '__iter__'):
                            t = select_template(file_name)
                        else:
                            t = get_template(file_name)
                        in_self.nodelist = t.nodelist
                    return in_self.nodelist.render(context_class(dict, autoescape=in_self._context.autoescape))

                if not disable:
                    render_given_args = cache_function(render_given_args, uid_extra='*'+describe_func(func))
                    render_given_args.get_or_create_token(('args',))
                    def render_map(**kwargs):
                        
                        #   If the key set is empty, we can just flush everything.
                        if kwargs == {}:
                            result = {}
                        else:
                            #   Otherwise, prepare a key set embedding the argument list in the 'args' key
                            result_args = []
                            for key in params:
                                if key in kwargs:
                                    result_args.append(kwargs[key])
                                else:
                                    result_args.append(None)
                            result = {'args': result_args}
                            
                        #   Flush everything if we got a wildcard
                        for key in kwargs:
                            if is_wildcard(kwargs[key]):
                                result = {}
                        return result
                        
                    render_given_args.depend_on_cache(cached_function, render_map)

                def render(in_self, context):
                    resolved_vars = []
                    for var in in_self.vars_to_resolve:
                        try:
                            resolved_vars.append(var.resolve(context))
                        except:
                            print 'Could not resolve %s' % var                            
                            resolved_vars.append(None)

                    if takes_context:
                        args = [context] + resolved_vars
                    else:
                        args = resolved_vars

                    in_self._context = context

                    return in_self.render_given_args(args)
コード例 #3
0
ファイル: template.py プロジェクト: sweettea/ESP-Website
        def prepare_dec(func):
            params, xx, xxx, defaults = getargspec(func)
            from esp.cache.function import describe_func
            cached_function = cache_function(func)

            if takes_context:
                if params[0] == 'context':
                    params = params[1:]
                else:
                    raise template.TemplateSyntaxError, "Any tag function decorated with takes_context=True must have a first argument of 'context'"

            class InclusionNode(template.Node):

                def __init__(in_self, takes_context, vars_to_resolve, kwargs):
                    in_self.vars_to_resolve = vars_to_resolve
                    
                def __str__(in_self):
                    return '<IN>'

                def render_given_args(in_self, args):
                    dict = cached_function(*args)

                    if not getattr(in_self, 'nodelist', False):
                        from django.template.loader import get_template, select_template
                        if hasattr(file_name, '__iter__'):
                            t = select_template(file_name)
                        else:
                            t = get_template(file_name)
                        in_self.nodelist = t.nodelist
                    return in_self.nodelist.render(context_class(dict, autoescape=in_self._context.autoescape))

                if not disable:
                    render_given_args = cache_function(render_given_args, uid_extra='*'+describe_func(func))
                    render_given_args.get_or_create_token(('args',))
                    def render_map(**kwargs):
                        
                        #   If the key set is empty, we can just flush everything.
                        if kwargs == {}:
                            result = {}
                        else:
                            #   Otherwise, prepare a key set embedding the argument list in the 'args' key
                            result_args = []
                            for key in params:
                                if key in kwargs:
                                    result_args.append(kwargs[key])
                                else:
                                    result_args.append(None)
                            result = {'args': result_args}
                            
                        #   Flush everything if we got a wildcard
                        for key in kwargs:
                            if is_wildcard(kwargs[key]):
                                result = {}
                        return result
                        
                    render_given_args.depend_on_cache(cached_function, render_map)

                def render(in_self, context):
                    resolved_vars = []
                    for var in in_self.vars_to_resolve:
                        try:
                            resolved_vars.append(var.resolve(context))
                        except:
                            print 'Could not resolve %s' % var                            
                            resolved_vars.append(None)

                    if takes_context:
                        args = [context] + resolved_vars
                    else:
                        args = resolved_vars

                    in_self._context = context

                    return in_self.render_given_args(args)

            compile_func = partial(template.generic_tag_compiler,
                params=params, varargs=[], varkw={},
                defaults=defaults, name=func.__name__,
                takes_context=takes_context, node_class=InclusionNode)
            compile_func.__doc__ = func.__doc__
            register.tag(func.__name__, compile_func)
            func.cached_function = cached_function
            return func