Beispiel #1
0
class Function(Node):
    def __init__(self, name, args=(), kwargs=(), body=None):
        check_kwargs(kwargs)
        self.name, self.args, self.kwargs, = name, args, kwargs
        self.body = None if body is None else Body(body)

    def consumes_body(self):
        return self.body is None

    def set_body(self, *statements):
        self.body = Body(*statements)

    def mentors(self, other):
        return isinstance(other, Function)

    def showhead(self):
        return 'Function: %s' % self.name

    def sections(self):
        for arg in filter(is_node, self.args):
            for s in arg.sections():
                yield s
        for v in filter(is_node, dict(self.kwargs).values()):
            for s in v.sections():
                yield s
        if self.body:
            for s in self.body.sections():
                yield s

    def children(self):
        args = ('arg: \n%s' % a for a in self.args)
        kwargs = ('kwarg (%s): \n%s' % a for a in self.kwargs)
        return chain(args, kwargs, self.body.statements if self.body else ())

    def accepts(self, settings, tag):
        return settings.accepts(self.name, tag)

    def evaluate(self, settings, **fallthrough):
        settings.context.push()
        function = settings.function(self.name)
        evaluate = getattr(function, 'evaluate', True)
        eval = (lambda n: settings.eval(n)) if evaluate else (lambda n: n)
        args = list(map(eval, self.args))
        if self.body:
            args.append(eval(self.body))
        kwargs = dict((k, eval(v)) for (k, v) in self.kwargs)
        kwargs.update(fallthrough)
        if getattr(function, 'give_settings', False) or not evaluate:
            kwargs['settings'] = settings
        elif getattr(function, 'give_context', False):
            kwargs['context'] = settings.context
        result = function(*args, **kwargs)
        settings.context.pop()
        return settings.eval(result)
Beispiel #2
0
class Element(Node):
    def __init__(self, tag, properties, args, attributes):
        self.tag, self.args = tag, args
        self.close, self.strip, self.body = False, False, None
        self.attributes = ListDict()
        for (name, value) in attributes:
            self.attributes.add(name, value)
        for (name, value) in properties:
            self.attributes.add(name, value)
        self.oneline = False

    def set_mode(self, mode):
        self.close = '/' in mode
        self.strip = '>' in mode

    def consumes_body(self):
        return self.body is None and not self.close

    def set_body(self, *statements):
        if self.close:
            raise ConversionError('You are trying to put a body '
                                  'in a self-closing tag.')
        self.body = Body(*statements)

    def sections(self):
        for arg in filter(is_node, self.args):
            for s in arg.sections():
                yield s
        for v in filter(is_node, chain(self.attributes.lists())):
            for s in v.sections():
                yield s
        if self.body:
            for s in self.body.sections():
                yield s

    def showhead(self):
        mode = '/' if self.close else ''
        mode += '>' if self.strip else ''
        return 'Element: %s%s' % (self.tag, mode)

    def children(self):
        extra = self.body.statements if self.body else ()
        return chain(self.attributes.items(), extra)

    def evaluate(self, settings):
        attrs = self.attributes.dict().items()
        attrs = dict((k, map(settings.eval, vs)) for (k, vs) in attrs)
        body = settings.eval(self.body)
        renderer = settings.element(self.tag)
        args = map(settings.eval, self.args)
        return renderer(self.tag, args, attrs, body,
                        self.close, self.strip, self.oneline, settings)
Beispiel #3
0
 def set_body(self, *statements):
     self.body = Body(*statements)
Beispiel #4
0
 def __init__(self, name, args=(), kwargs=(), body=None):
     check_kwargs(kwargs)
     self.name, self.args, self.kwargs, = name, args, kwargs
     self.body = None if body is None else Body(body)
Beispiel #5
0
 def set_body(self, *statements):
     if self.close:
         raise ConversionError('You are trying to put a body '
                               'in a self-closing tag.')
     self.body = Body(*statements)