Python jinja2._compat.text_type() Examples

The following are 30 code examples of jinja2._compat.text_type(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module jinja2._compat , or try the search function .
Example #1
Source File: filters.py    From recruit with Apache License 2.0 6 votes vote down vote up
def do_replace(eval_ctx, s, old, new, count=None):
    """Return a copy of the value with all occurrences of a substring
    replaced with a new one. The first argument is the substring
    that should be replaced, the second is the replacement string.
    If the optional third argument ``count`` is given, only the first
    ``count`` occurrences are replaced:

    .. sourcecode:: jinja

        {{ "Hello World"|replace("Hello", "Goodbye") }}
            -> Goodbye World

        {{ "aaaaargh"|replace("a", "d'oh, ", 2) }}
            -> d'oh, d'oh, aaargh
    """
    if count is None:
        count = -1
    if not eval_ctx.autoescape:
        return text_type(s).replace(text_type(old), text_type(new), count)
    if hasattr(old, '__html__') or hasattr(new, '__html__') and \
       not hasattr(s, '__html__'):
        s = escape(s)
    else:
        s = soft_unicode(s)
    return s.replace(soft_unicode(old), soft_unicode(new), count) 
Example #2
Source File: nativetypes.py    From recruit with Apache License 2.0 6 votes vote down vote up
def native_concat(nodes):
    """Return a native Python type from the list of compiled nodes. If the
    result is a single node, its value is returned. Otherwise, the nodes are
    concatenated as strings. If the result can be parsed with
    :func:`ast.literal_eval`, the parsed value is returned. Otherwise, the
    string is returned.
    """
    head = list(islice(nodes, 2))

    if not head:
        return None

    if len(head) == 1:
        out = head[0]
    else:
        out = u''.join([text_type(v) for v in chain(head, nodes)])

    try:
        return literal_eval(out)
    except (ValueError, SyntaxError, MemoryError):
        return out 
Example #3
Source File: nativetypes.py    From recruit with Apache License 2.0 6 votes vote down vote up
def native_concat(nodes):
    """Return a native Python type from the list of compiled nodes. If the
    result is a single node, its value is returned. Otherwise, the nodes are
    concatenated as strings. If the result can be parsed with
    :func:`ast.literal_eval`, the parsed value is returned. Otherwise, the
    string is returned.
    """
    head = list(islice(nodes, 2))

    if not head:
        return None

    if len(head) == 1:
        out = head[0]
    else:
        out = u''.join([text_type(v) for v in chain(head, nodes)])

    try:
        return literal_eval(out)
    except (ValueError, SyntaxError, MemoryError):
        return out 
Example #4
Source File: filters.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def do_replace(eval_ctx, s, old, new, count=None):
    """Return a copy of the value with all occurrences of a substring
    replaced with a new one. The first argument is the substring
    that should be replaced, the second is the replacement string.
    If the optional third argument ``count`` is given, only the first
    ``count`` occurrences are replaced:

    .. sourcecode:: jinja

        {{ "Hello World"|replace("Hello", "Goodbye") }}
            -> Goodbye World

        {{ "aaaaargh"|replace("a", "d'oh, ", 2) }}
            -> d'oh, d'oh, aaargh
    """
    if count is None:
        count = -1
    if not eval_ctx.autoescape:
        return text_type(s).replace(text_type(old), text_type(new), count)
    if hasattr(old, '__html__') or hasattr(new, '__html__') and \
       not hasattr(s, '__html__'):
        s = escape(s)
    else:
        s = soft_unicode(s)
    return s.replace(soft_unicode(old), soft_unicode(new), count) 
Example #5
Source File: nativetypes.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def native_concat(nodes):
    """Return a native Python type from the list of compiled nodes. If the
    result is a single node, its value is returned. Otherwise, the nodes are
    concatenated as strings. If the result can be parsed with
    :func:`ast.literal_eval`, the parsed value is returned. Otherwise, the
    string is returned.
    """
    head = list(islice(nodes, 2))

    if not head:
        return None

    if len(head) == 1:
        out = head[0]
    else:
        out = u''.join([text_type(v) for v in chain(head, nodes)])

    try:
        return literal_eval(out)
    except (ValueError, SyntaxError, MemoryError):
        return out 
Example #6
Source File: nativetypes.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def native_concat(nodes):
    """Return a native Python type from the list of compiled nodes. If the
    result is a single node, its value is returned. Otherwise, the nodes are
    concatenated as strings. If the result can be parsed with
    :func:`ast.literal_eval`, the parsed value is returned. Otherwise, the
    string is returned.
    """
    head = list(islice(nodes, 2))

    if not head:
        return None

    if len(head) == 1:
        out = head[0]
    else:
        out = u''.join([text_type(v) for v in chain(head, nodes)])

    try:
        return literal_eval(out)
    except (ValueError, SyntaxError, MemoryError):
        return out 
Example #7
Source File: nativetypes.py    From OpenXR-SDK-Source with Apache License 2.0 6 votes vote down vote up
def native_concat(nodes):
    """Return a native Python type from the list of compiled nodes. If the
    result is a single node, its value is returned. Otherwise, the nodes are
    concatenated as strings. If the result can be parsed with
    :func:`ast.literal_eval`, the parsed value is returned. Otherwise, the
    string is returned.
    """
    head = list(islice(nodes, 2))

    if not head:
        return None

    if len(head) == 1:
        out = head[0]
    else:
        out = u''.join([text_type(v) for v in chain(head, nodes)])

    try:
        return literal_eval(out)
    except (ValueError, SyntaxError, MemoryError):
        return out 
Example #8
Source File: filters.py    From jbox with MIT License 5 votes vote down vote up
def do_forceescape(value):
    """Enforce HTML escaping.  This will probably double escape variables."""
    if hasattr(value, '__html__'):
        value = value.__html__()
    return escape(text_type(value)) 
Example #9
Source File: tests.py    From OpenXR-SDK-Source with Apache License 2.0 5 votes vote down vote up
def test_lower(value):
    """Return true if the variable is lowercased."""
    return text_type(value).islower() 
Example #10
Source File: filters.py    From jbox with MIT License 5 votes vote down vote up
def do_center(value, width=80):
    """Centers the value in a field of a given width."""
    return text_type(value).center(width) 
Example #11
Source File: filters.py    From jbox with MIT License 5 votes vote down vote up
def do_striptags(value):
    """Strip SGML/XML tags and replace adjacent whitespace by one space.
    """
    if hasattr(value, '__html__'):
        value = value.__html__()
    return Markup(text_type(value)).striptags() 
Example #12
Source File: filters.py    From jbox with MIT License 5 votes vote down vote up
def do_mark_unsafe(value):
    """Mark a value as unsafe.  This is the reverse operation for :func:`safe`."""
    return text_type(value) 
Example #13
Source File: nodes.py    From jbox with MIT License 5 votes vote down vote up
def as_const(self, eval_ctx=None):
        eval_ctx = get_eval_context(self, eval_ctx)
        return ''.join(text_type(x.as_const(eval_ctx)) for x in self.nodes) 
Example #14
Source File: exceptions.py    From OpenXR-SDK-Source with Apache License 2.0 5 votes vote down vote up
def __init__(self, names=(), message=None):
        if message is None:
            message = u'none of the templates given were found: ' + \
                      u', '.join(imap(text_type, names))
        TemplateNotFound.__init__(self, names and names[-1] or None, message)
        self.templates = list(names) 
Example #15
Source File: filters.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def do_mark_unsafe(value):
    """Mark a value as unsafe.  This is the reverse operation for :func:`safe`."""
    return text_type(value) 
Example #16
Source File: nodes.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def as_const(self, eval_ctx=None):
        rv = self.value
        if PY2 and type(rv) is text_type and \
           self.environment.policies['compiler.ascii_str']:
            try:
                rv = rv.encode('ascii')
            except UnicodeError:
                pass
        return rv 
Example #17
Source File: bccache.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def get_cache_key(self, name, filename=None):
        """Returns the unique hash key for this template name."""
        hash = sha1(name.encode('utf-8'))
        if filename is not None:
            filename = '|' + filename
            if isinstance(filename, text_type):
                filename = filename.encode('utf-8')
            hash.update(filename)
        return hash.hexdigest() 
Example #18
Source File: exceptions.py    From OpenXR-SDK-Source with Apache License 2.0 5 votes vote down vote up
def __init__(self, message=None):
            if message is not None:
                message = text_type(message).encode('utf-8')
            Exception.__init__(self, message) 
Example #19
Source File: tests.py    From jbox with MIT License 5 votes vote down vote up
def test_upper(value):
    """Return true if the variable is uppercased."""
    return text_type(value).isupper() 
Example #20
Source File: tests.py    From jbox with MIT License 5 votes vote down vote up
def test_lower(value):
    """Return true if the variable is lowercased."""
    return text_type(value).islower() 
Example #21
Source File: exceptions.py    From jbox with MIT License 5 votes vote down vote up
def __init__(self, names=(), message=None):
        if message is None:
            message = u'none of the templates given were found: ' + \
                      u', '.join(imap(text_type, names))
        TemplateNotFound.__init__(self, names and names[-1] or None, message)
        self.templates = list(names) 
Example #22
Source File: exceptions.py    From jbox with MIT License 5 votes vote down vote up
def __init__(self, message=None):
            if message is not None:
                message = text_type(message).encode('utf-8')
            Exception.__init__(self, message) 
Example #23
Source File: filters.py    From OpenXR-SDK-Source with Apache License 2.0 5 votes vote down vote up
def do_mark_unsafe(value):
    """Mark a value as unsafe.  This is the reverse operation for :func:`safe`."""
    return text_type(value) 
Example #24
Source File: bccache.py    From recruit with Apache License 2.0 5 votes vote down vote up
def get_cache_key(self, name, filename=None):
        """Returns the unique hash key for this template name."""
        hash = sha1(name.encode('utf-8'))
        if filename is not None:
            filename = '|' + filename
            if isinstance(filename, text_type):
                filename = filename.encode('utf-8')
            hash.update(filename)
        return hash.hexdigest() 
Example #25
Source File: nodes.py    From recruit with Apache License 2.0 5 votes vote down vote up
def as_const(self, eval_ctx=None):
        eval_ctx = get_eval_context(self, eval_ctx)
        return ''.join(text_type(x.as_const(eval_ctx)) for x in self.nodes) 
Example #26
Source File: nodes.py    From recruit with Apache License 2.0 5 votes vote down vote up
def as_const(self, eval_ctx=None):
        rv = self.value
        if PY2 and type(rv) is text_type and \
           self.environment.policies['compiler.ascii_str']:
            try:
                rv = rv.encode('ascii')
            except UnicodeError:
                pass
        return rv 
Example #27
Source File: filters.py    From recruit with Apache License 2.0 5 votes vote down vote up
def do_striptags(value):
    """Strip SGML/XML tags and replace adjacent whitespace by one space.
    """
    if hasattr(value, '__html__'):
        value = value.__html__()
    return Markup(text_type(value)).striptags() 
Example #28
Source File: filters.py    From recruit with Apache License 2.0 5 votes vote down vote up
def do_center(value, width=80):
    """Centers the value in a field of a given width."""
    return text_type(value).center(width) 
Example #29
Source File: exceptions.py    From misp42splunk with GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, message=None):
            if message is not None:
                message = text_type(message).encode('utf-8')
            Exception.__init__(self, message) 
Example #30
Source File: exceptions.py    From RSSNewsGAE with Apache License 2.0 5 votes vote down vote up
def __init__(self, message=None):
            if message is not None:
                message = text_type(message).encode('utf-8')
            Exception.__init__(self, message)