How to use the chameleon.core.types.value function in Chameleon

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 / genshi / expressions.py View on Github external
def translate(self, string):
        if isinstance(string, str):
            string = string.decode('utf-8')

        return types.value(string.strip())
github malthe / chameleon / src / chameleon / core / clauses.py View on Github external
def write(self, stream):
        temp = stream.save()
        self.assign.begin(stream, temp)
        stream.out(types.value(temp))
        self.assign.end(stream)
        stream.restore()
github malthe / chameleon / src / chameleon / core / clauses.py View on Github external
def _assign(self, variable, value, stream):
        stream.annotate(value)
        symbols = stream.symbols.as_dict()
        variable = variable % symbols

        if isinstance(value, types.expression):
            if value.symbol_mapping:
                stream.symbol_mapping.update(value.symbol_mapping)

        if isinstance(value, types.template):
            value = types.value(value % symbols)
        if isinstance(value, types.value):
            stream.write("%s = %s" % (variable, value))
        elif isinstance(value, types.join):
            parts = []
            _v_count = 0

            for part in value:
                if isinstance(part, types.expression):
                    stream.symbol_mapping.update(part.symbol_mapping)
                if isinstance(part, types.template):
                    part = types.value(part % symbols)
                if isinstance(part, (types.parts, types.join)):
                    _v = stream.save()
                    assign = Assign(part, _v)
                    assign.begin(stream)
                    assign.end(stream)
github malthe / chameleon / src / chameleon / core / clauses.py View on Github external
def end(self, stream):
        if self.ret is not None:
            self.ret.begin(stream)
            stream.write('return _ret')
            self.ret.end(stream)
        
        stream.outdent()

        if self.dictionary is not None:
            assign = Assign(
                types.value(self.name), "%s['%s']" % \
                (self.dictionary, self.name))
            assign.begin(stream)
            assign.end(stream)
github malthe / chameleon / src / chameleon / zpt / expressions.py View on Github external
>>> try: translate('abc:def:ghi')
        ... except SyntaxError, e: 'abc:def:ghi' in format_exc(e)
        True
        """

        if isinstance(string, unicode):
            string = string.encode('utf-8')

        if string:
            expression = string.strip()
            parse(expression, 'eval')

            if isinstance(string, str):
                string = string.decode('utf-8')

            return types.value(string.strip())
github malthe / chameleon / src / chameleon / core / clauses.py View on Github external
if value.symbol_mapping:
                stream.symbol_mapping.update(value.symbol_mapping)

        if isinstance(value, types.template):
            value = types.value(value % symbols)
        if isinstance(value, types.value):
            stream.write("%s = %s" % (variable, value))
        elif isinstance(value, types.join):
            parts = []
            _v_count = 0

            for part in value:
                if isinstance(part, types.expression):
                    stream.symbol_mapping.update(part.symbol_mapping)
                if isinstance(part, types.template):
                    part = types.value(part % symbols)
                if isinstance(part, (types.parts, types.join)):
                    _v = stream.save()
                    assign = Assign(part, _v)
                    assign.begin(stream)
                    assign.end(stream)
                    _v_count +=1
                    parts.append(_v)
                elif isinstance(part, types.value):
                    parts.append(part)
                elif isinstance(part, unicode):
                    if stream.encoding:
                        parts.append(repr(part.encode(stream.encoding)))
                    else:
                        parts.append(repr(part))
                elif isinstance(part, str):
                    parts.append(repr(part))
github malthe / chameleon / src / chameleon / core / clauses.py View on Github external
stream.write("%s = %s" % (stream.symbols.tmp, expr))
        write("if getattr(%(tmp)s, '__class__', type(%(tmp)s)) not in (str, unicode, int, float):")
        stream.indent()
        write("try:")
        stream.indent()
        write("%(tmp)s = %(tmp)s.__html__")
        stream.outdent()
        write("except:")
        stream.indent()
        write("%%(tmp)s = %s" % translate_expression("%(tmp)s"))
        stream.outdent()
        write("else:")
        stream.indent()
        write("%(tmp)s = %(tmp)s()")
        self._maybe_validate(stream)
        stream.out(types.value(stream.symbols.tmp))
        write("%(tmp)s = None")
        stream.outdent()
        stream.outdent()

        write("if %(tmp)s is not None:")
        stream.indent()

        # only output unicode strings
        stream.ensure_unicode(stream.symbols.tmp)

        # escape markup
        if not self.structure:
            stream.escape(stream.symbols.tmp)

        self._maybe_validate(stream)
        stream.out(types.value(symbols['tmp']))