PyCallTag - Directly call python function or attribute of a module

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from django import template
from django.utils.translation import gettext_lazy as _
import re

register = template.Library()

r_identifers = re.compile(r'[\w.]+')
class PyCallNode(template.Node):
    def __init__(self, expr_string, var_name):
        self.expr_string = expr_string
        self.var_name = var_name

    def __repr__(self):
        return "<PyCall node>"

    def render(self, context):
        clist = list(context)
        clist.reverse()
        d = {}
        d['_'] = _
        for c in clist:
            d.update(c)
        m = r_identifers.match(self.expr_string)
        if m:
            module, func = m.group().rsplit('.', 1)
            funcstring = self.expr_string[len(module) + 1:]
            mod = __import__(module, {}, {}, [''])
            d[func] = getattr(mod, func)
        else:
            raise template.TemplateSyntaxError, "The arguments of %r tag should be module.function(...)" % 'pycall'
            
        if self.var_name:
            context[self.var_name] = eval(funcstring, d)
            return ''
        else:
            return str(eval(funcstring, d))

def do_pycall(parser, token):
    try:
        tag_name, arg = token.contents.split(None, 1)
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires arguments" % token.contents[0]
    
    m = re.search(r'(.*?)\s+as\s+(\w+)', arg)
    if m:
        expr_string, var_name = m.groups()
    else:
        if not arg:
            raise template.TemplateSyntaxError, "The arguments of %r tag should be module.function(...)" % tag_name
            
        expr_string, var_name = arg, None
            
    return PyCallNode(expr_string, var_name)
do_pycall = register.tag("pycall", do_pycall)

More like this

  1. Get the full request path by limodou 7 years, 1 month ago
  2. Use the WSGIAccessScript Directive to secure static files based on the Django session by LuckiDog 2 years, 11 months ago
  3. Custom mod_python AuthenHandler by aeby 6 years, 8 months ago
  4. Quickly check templates while sketching them out by Amr Mostafa 6 years, 12 months ago
  5. set_paths by amitu 5 years, 6 months ago

Comments

cricogik (on December 20, 2010):

Good programmers don't use eval ;-) Rest of them had to heavy childhood.

#

(Forgotten your password?)