How to use Chameleon - 10 common examples

To help you get started, we’ve selected a few Chameleon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github malthe / chameleon / src / chameleon / zpt / language.py View on Github external
if parent is not None and parent.strip_text and parent[-1] is self.element:
                        tail = tail.rstrip('\n ')

                parent = self.element.getparent()
                if parent is not None and not parent.node._interpolation_enabled:
                    return (tail,)

                parts = self.element.translator.split(tail)
                if self.element.tag == '{http://www.w3.org/1999/xhtml}cdata':
                    return parts

                if not self._interpolation_escape:
                    return parts

                return tuple(
                    isinstance(part, types.expression) and types.escape(part) or \
                    self.element.meta_structure is True and part or \
                    utils.htmlescape(part) for part in parts)

            return ()
github malthe / chameleon / src / chameleon / zpt / expressions.py View on Github external
"""

        variables = []
        for var in string.split(','):
            var = var.strip()

            if var in ('repeat',):
                raise ValueError, "Invalid variable name '%s' (reserved)." % var

            if var.startswith('_') and not var.startswith('_tmp'):
                raise ValueError(
                    "Invalid variable name '%s' (starts with an underscore)." % var)

            variables.append(var)

        return types.declaration(variables)
github malthe / chameleon / src / chameleon / genshi / expressions.py View on Github external
"""

        variables = []
        for var in string.split(','):
            var = var.strip()

            if var in ('repeat',):
                raise ValueError, "Invalid variable name '%s' (reserved)." % var

            if var.startswith('_') and not var.startswith('_tmp'):
                raise ValueError(
                    "Invalid variable name '%s' (starts with an underscore)." % var)

            variables.append(var)

        return types.declaration(variables)
github malthe / chameleon / src / chameleon / core / codegen.py View on Github external
"""

        if isinstance(node.value, ast.Name) and \
           (node.value.id.startswith('_') or node.value.id in SYMBOLS):
            return ASTTransformer.visit_Attribute(self, node)

        ## This should be spellable as
        ##         return ast.Call(
        ##             ast.Name(config.SYMBOLS.lookup_attr, ast.Load()),
        ##             [self.visit(node.value), ast.Str(node.attr)],
        ##             [], None, None)
        ## .. except Python 2.5 doesn't allow it.

        call = ast.Call()
        name = ast.Name()
        name.id = config.SYMBOLS.lookup_attr
        name.ctx = ast.Load()
        call.func = name
        string = ast.Str()
        string.s = node.attr
        args = [self.visit(node.value), string]
        call.args = args
        call.keywords = []
        call.starargs = None
        call.kwargs = None
        return call
github malthe / chameleon / src / chameleon / genshi / language.py View on Github external
if parent is not None and parent.strip_text and parent[-1] is self.element:
                        tail = tail.rstrip('\n ')

                parent = self.element.getparent()
                if parent is not None and not parent.node._interpolation_enabled:
                    return (tail,)

                parts = self.element.translator.split(tail)
                if self.element.tag == '{http://www.w3.org/1999/xhtml}cdata':
                    return parts

                if not self._interpolation_escape:
                    return parts

                return tuple(
                    isinstance(part, types.expression) and types.escape(part) or \
                    self.element.meta_structure is True and part or \
                    utils.htmlescape(part) for part in parts)

            return ()
github malthe / chameleon / src / chameleon / zpt / language.py View on Github external
parent = self.element.getparent()
                if parent is not None and self.element.strip_text and parent[0] is self.element:
                    text = text.lstrip('\n ')

                if not self._interpolation_enabled:
                    return (text,)

                parts = self.element.translator.split(text)
                if self.element.tag == '{http://www.w3.org/1999/xhtml}cdata':
                    return parts

                if not self._interpolation_escape:
                    return parts

                return tuple(
                    isinstance(part, types.expression) and types.escape(part) or \
                    self.element.meta_structure is True and part or \
                    utils.htmlescape(part) for part in parts)

            return ()
github malthe / chameleon / src / chameleon / genshi / language.py View on Github external
parent = self.element.getparent()
                if parent is not None and self.element.strip_text and parent[0] is self.element:
                    text = text.lstrip('\n ')

                if not self._interpolation_enabled:
                    return (text,)

                parts = self.element.translator.split(text)
                if self.element.tag == '{http://www.w3.org/1999/xhtml}cdata':
                    return parts

                if not self._interpolation_escape:
                    return parts

                return tuple(
                    isinstance(part, types.expression) and types.escape(part) or \
                    self.element.meta_structure is True and part or \
                    utils.htmlescape(part) for part in parts)

            return ()
github malthe / chameleon / src / chameleon / codegen.py View on Github external
def require(self, value):
        if value is None:
            return load("None")

        if isinstance(value, NATIVE_NUMBERS):
            return ast.Num(value)

        node = self.imports.get(value)
        if node is None:
            # we come up with a unique symbol based on the class name
            name = "_%s" % getattr(value, '__name__', str(value)).\
                   rsplit('.', 1)[-1]
            node = load(name)
            self.imports[value] = store(node.id)

        return node
github malthe / chameleon / src / chameleon / codegen.py View on Github external
self.lines = []

        while self.imports:
            value, node = self.imports.popitem()

            if isinstance(value, types.ModuleType):
                stmt = ast.Import(
                    names=[ast.alias(name=value.__name__, asname=node.id)])
            elif hasattr(value, '__name__'):
                path = reverse_builtin_map.get(value)
                if path is None:
                    path = value.__module__
                    name = value.__name__
                stmt = ast.ImportFrom(
                    module=path,
                    names=[ast.alias(name=name, asname=node.id)],
                    level=0,
                )
            else:
                raise TypeError(value)

            self.visit(stmt)

        # Clear last import
        self.flush()

        # Stich together lines
        self.lines += defines + body
github malthe / chameleon / src / chameleon / codegen.py View on Github external
def wrapper(*vargs, **kwargs):
        symbols = dict(zip(args, vargs + defaults))
        symbols.update(kwargs)

        class Visitor(ast.NodeVisitor):
            def visit_FunctionDef(self, node):
                self.generic_visit(node)

                name = symbols.get(node.name, self)
                if name is not self:
                    node_annotations[node] = ast.FunctionDef(
                        name=name,
                        args=node.args,
                        body=node.body,
                        decorator_list=getattr(node, "decorator_list", []),
                        )

            def visit_Name(self, node):
                value = symbols.get(node.id, self)
                if value is not self:
                    if isinstance(value, basestring):