Example #1
0
    def __call__(self, stream, directives, ctxt, **vars):
        info = ctxt._choice_stack and ctxt._choice_stack[-1]
        if not info:
            raise TemplateRuntimeError('"when" directives can only be used '
                                       'inside a "choose" directive',
                                       self.filename, *stream.next()[2][1:])
        if info[0]:
            return []
        if not self.expr and not info[1]:
            raise TemplateRuntimeError('either "choose" or "when" directive '
                                       'must have a test expression',
                                       self.filename, *stream.next()[2][1:])
        if info[1]:
            value = info[2]
            if self.expr:
                matched = value == _eval_expr(self.expr, ctxt, vars)
            else:
                matched = bool(value)
        else:
            matched = bool(_eval_expr(self.expr, ctxt, vars))
        info[0] = matched
        if not matched:
            return []

        return _apply_directives(stream, directives, ctxt, vars)
Example #2
0
def _simplify_stream(stream, ctxt, vars):
    # consumes stream, send a list
    parts = []
    for idx, (kind, data, pos) in enumerate(stream):
        if kind is TEXT:
            parts.append(data)
        elif kind is EXPR:
            value = _eval_expr(data, ctxt, vars)
            if hasattr(value, '__html__'):
                value = _unpack(value)
            if hasattr(value, '__next__') or hasattr(value, 'next'):
                while hasattr(value, '__next__') or hasattr(value, 'next'):
                    value = list(value)
                    value = _simplify_stream(value, ctxt, vars)
                if not isinstance(value, text_type):
                    stream[idx:idx + 1] = value
                else:
                    stream[idx] = (TEXT, value, pos)
            elif isinstance(value, bytestring_type):
                value = value.decode('utf8', 'replace')
            elif not isinstance(value, text_type):
                value = text_type(value)
            parts.append(value)
        else:
            return stream
    return u''.join(parts)
Example #3
0
 def __call__(self, stream, directives, ctxt, **vars):
     info = [False, bool(self.expr), None]
     if self.expr:
         info[2] = _eval_expr(self.expr, ctxt, vars)
     ctxt._choice_stack.append(info)
     for event in _apply_directives(stream, directives, ctxt, vars):
         yield event
     ctxt._choice_stack.pop()
Example #4
0
 def process(self, stream, directives, ctxt, vars):
     if self.bind is not None:
         bind = self.bind
     elif self.expr is None:
         bind = None
     else:
         bind = _eval_expr(self.expr, ctxt, vars)
     return _rewrite_stream(stream, directives, ctxt, vars, bind)
Example #5
0
 def process(self, stream, directives, ctxt, vars):
     if self.bind is not None:
         bind = self.bind
     elif self.expr is None:
         bind = None
     else:
         bind = _eval_expr(self.expr, ctxt, vars)
     return _rewrite_stream(stream, directives, ctxt, vars, bind)
Example #6
0
 def _generate():
     if _eval_expr(self.expr, ctxt, **vars):
         stream.next()  # skip start tag
         previous = stream.next()
         for event in stream:
             yield previous
             previous = event
     else:
         for event in stream:
             yield event
Example #7
0
 def __call__(self, stream, directives, ctxt, **vars):
     frame = {}
     ctxt.push(frame)
     for targets, expr in self.vars:
         value = _eval_expr(expr, ctxt, vars)
         for assign in targets:
             assign(frame, value)
     for event in _apply_directives(stream, directives, ctxt, vars):
         yield event
     ctxt.pop()
Example #8
0
 def _generate():
     if not self.expr or _eval_expr(self.expr, ctxt, vars):
         next(stream) # skip start tag
         previous = next(stream)
         for event in stream:
             yield previous
             previous = event
     else:
         for event in stream:
             yield event
Example #9
0
 def __call__(self, stream, directives, ctxt, **vars):
     frame = {}
     ctxt.push(frame)
     for targets, expr in self.vars:
         value = _eval_expr(expr, ctxt, vars)
         for assign in targets:
             assign(frame, value)
     for event in _apply_directives(stream, directives, ctxt, vars):
         yield event
     ctxt.pop()
Example #10
0
 def _generate():
     if not self.expr or _eval_expr(self.expr, ctxt, vars):
         stream.next() # skip start tag
         previous = stream.next()
         for event in stream:
             yield previous
             previous = event
     else:
         for event in stream:
             yield event
Example #11
0
    def __call__(self, stream, directives, ctxt, **vars):
        iterable = _eval_expr(self.expr, ctxt, vars)
        if iterable is None:
            return

        assign = self.assign
        scope = {}
        stream = list(stream)
        for item in iterable:
            assign(scope, item)
            ctxt.push(scope)
            for event in _apply_directives(stream, directives, ctxt, vars):
                yield event
            ctxt.pop()
Example #12
0
    def __call__(self, stream, directives, ctxt, **vars):
        iterable = _eval_expr(self.expr, ctxt, vars)
        if iterable is None:
            return

        assign = self.assign
        scope = {}
        stream = list(stream)
        for item in iterable:
            assign(scope, item)
            ctxt.push(scope)
            for event in _apply_directives(stream, directives, ctxt, vars):
                yield event
            ctxt.pop()
Example #13
0
 def inject(self, mapping, ctxt, vars):
     """Inject the translated key and interpolated value into *mapping*."""
     raw = self.raw_value
     if raw.__class__ is unicode:
         final_value = raw
     else:
         parts = []
         for kind, value, pos in raw:
             if kind is TEXT:
                 parts.append(value)
             else:
                 value = _eval_expr(value, ctxt, vars)
                 parts.append(unicode(value))
         final_value = u''.join(parts)
     mapping[_to_context.get(self._name, self._name)] = final_value
Example #14
0
 def inject(self, mapping, ctxt, vars):
     """Inject the translated key and interpolated value into *mapping*."""
     raw = self.raw_value
     if raw.__class__ is unicode:
         final_value = raw
     else:
         parts = []
         for kind, value, pos in raw:
             if kind is TEXT:
                 parts.append(value)
             else:
                 value = _eval_expr(value, ctxt, vars)
                 parts.append(unicode(value))
         final_value = u''.join(parts)
     mapping[_to_context.get(self._name, self._name)] = final_value
Example #15
0
 def _generate():
     kind, (tag, attrib), pos = stream.next()
     attrs = _eval_expr(self.expr, ctxt, vars)
     if attrs:
         if isinstance(attrs, Stream):
             try:
                 attrs = iter(attrs).next()
             except StopIteration:
                 attrs = []
         elif not isinstance(attrs, list):  # assume it's a dict
             attrs = attrs.items()
         attrib |= [(QName(n), v is not None and unicode(v).strip()
                     or None) for n, v in attrs]
     yield kind, (tag, attrib), pos
     for event in stream:
         yield event
Example #16
0
 def _generate():
     kind, (tag, attrib), pos  = stream.next()
     attrs = _eval_expr(self.expr, ctxt, vars)
     if attrs:
         if isinstance(attrs, Stream):
             try:
                 attrs = iter(attrs).next()
             except StopIteration:
                 attrs = []
         elif not isinstance(attrs, list): # assume it's a dict
             attrs = attrs.items()
         attrib -= [name for name, val in attrs if val is None]
         attrib |= [(QName(name), unicode(val).strip()) for name, val
                    in attrs if val is not None]
     yield kind, (tag, attrib), pos
     for event in stream:
         yield event
Example #17
0
 def _generate():
     kind, (tag, attrib), pos = stream.next()
     attrs = _eval_expr(self.expr, ctxt, **vars)
     if attrs:
         if isinstance(attrs, Stream):
             try:
                 attrs = iter(attrs).next()
             except StopIteration:
                 attrs = []
         elif not isinstance(attrs, list):  # assume it's a dict
             attrs = attrs.items()
         attrib -= [name for name, val in attrs if val is None]
         attrib |= [(QName(name), unicode(val).strip())
                    for name, val in attrs if val is not None]
     yield kind, (tag, attrib), pos
     for event in stream:
         yield event
Example #18
0
 def _generate():
     kind, (tag, attrib), pos  = next(stream)
     attrs = _eval_expr(self.expr, ctxt, vars)
     if attrs:
         if isinstance(attrs, Stream):
             try:
                 attrs = next(iter(attrs))
             except StopIteration:
                 attrs = []
         elif not isinstance(attrs, list): # assume it's a dict
             attrs = list(attrs.items())
         attrib |= [
             (QName(n), v is not None and str(v).strip() or None)
             for n, v in attrs
         ]
     yield kind, (tag, attrib), pos
     for event in stream:
         yield event
Example #19
0
    def process(self, stream, directives, ctxt, vars):
        try:
            render_context = ctxt['flatland_render_context']
        except KeyError:
            ctxt['flatland_render_context'] = render_context = Context()

        if 'filters' not in self.attributes:
            attrs = self.attributes
        else:
            attrs = self.attributes.copy()
            attrs['filters'] = _eval_expr(Expression(attrs['filters']),
                                          ctxt, vars)

        render_context.push()
        render_context.update(attrs)
        assert not directives
        for event in stream:
            yield event
        render_context.pop()
Example #20
0
    def process(self, stream, directives, ctxt, vars):
        try:
            render_context = ctxt['flatland_render_context']
        except KeyError:
            ctxt['flatland_render_context'] = render_context = Context()

        if 'filters' not in self.attributes:
            attrs = self.attributes
        else:
            attrs = self.attributes.copy()
            attrs['filters'] = _eval_expr(Expression(attrs['filters']), ctxt,
                                          vars)

        render_context.push()
        render_context.update(attrs)
        assert not directives
        for event in stream:
            yield event
        render_context.pop()
Example #21
0
 def function(*args, **kwargs):
     scope = {}
     args = list(args) # make mutable
     for name in self.args:
         if args:
             scope[name] = args.pop(0)
         else:
             if name in kwargs:
                 val = kwargs.pop(name)
             else:
                 val = _eval_expr(self.defaults.get(name), ctxt, vars)
             scope[name] = val
     if not self.star_args is None:
         scope[self.star_args] = args
     if not self.dstar_args is None:
         scope[self.dstar_args] = kwargs
     ctxt.push(scope)
     for event in _apply_directives(stream, directives, ctxt, vars):
         yield event
     ctxt.pop()
Example #22
0
 def function(*args, **kwargs):
     scope = {}
     args = list(args) # make mutable
     for name in self.args:
         if args:
             scope[name] = args.pop(0)
         else:
             if name in kwargs:
                 val = kwargs.pop(name)
             else:
                 val = _eval_expr(self.defaults.get(name), ctxt, vars)
             scope[name] = val
     if not self.star_args is None:
         scope[self.star_args] = args
     if not self.dstar_args is None:
         scope[self.dstar_args] = kwargs
     ctxt.push(scope)
     for event in _apply_directives(stream, directives, ctxt, vars):
         yield event
     ctxt.pop()
Example #23
0
def _simplify_stream(stream, ctxt, vars):
    # consumes stream, send a list
    parts = []
    for idx, (kind, data, pos) in enumerate(stream):
        if kind is TEXT:
            parts.append(data)
        elif kind is EXPR:
            value = _eval_expr(data, ctxt, vars)
            if hasattr(value, '__html__'):
                value = value.__html__()
            if hasattr(value, '__next__') or hasattr(value, 'next'):
                while hasattr(value, '__next__') or hasattr(value, 'next'):
                    value = list(value)
                    value = _simplify_stream(value, ctxt, vars)
                if not isinstance(value, unicode):
                    stream[idx:idx + 1] = value
                else:
                    stream[idx] = (TEXT, value, pos)
            elif not isinstance(value, unicode):
                value = unicode(value)
            parts.append(value)
        else:
            return stream
    return u''.join(parts)
Example #24
0
 def __call__(self, stream, directives, ctxt, **vars):
     value = _eval_expr(self.expr, ctxt, vars)
     if value:
         return _apply_directives(stream, directives, ctxt, vars)
     return []
Example #25
0
 def __call__(self, stream, directives, ctxt, **vars):
     value = _eval_expr(self.expr, ctxt, vars)
     if value:
         return _apply_directives(stream, directives, ctxt, vars)
     return []