Packages update
This commit is contained in:
+24
-2
@@ -13,7 +13,8 @@ import math
|
||||
from random import choice
|
||||
from operator import itemgetter
|
||||
from itertools import imap, groupby
|
||||
from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode
|
||||
from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode, \
|
||||
unicode_urlencode
|
||||
from jinja2.runtime import Undefined
|
||||
from jinja2.exceptions import FilterArgumentError
|
||||
|
||||
@@ -70,6 +71,26 @@ def do_forceescape(value):
|
||||
return escape(unicode(value))
|
||||
|
||||
|
||||
def do_urlencode(value):
|
||||
"""Escape strings for use in URLs (uses UTF-8 encoding). It accepts both
|
||||
dictionaries and regular strings as well as pairwise iterables.
|
||||
|
||||
.. versionadded:: 2.7
|
||||
"""
|
||||
itemiter = None
|
||||
if isinstance(value, dict):
|
||||
itemiter = value.iteritems()
|
||||
elif not isinstance(value, basestring):
|
||||
try:
|
||||
itemiter = iter(value)
|
||||
except TypeError:
|
||||
pass
|
||||
if itemiter is None:
|
||||
return unicode_urlencode(value)
|
||||
return u'&'.join(unicode_urlencode(k) + '=' +
|
||||
unicode_urlencode(v) for k, v in itemiter)
|
||||
|
||||
|
||||
@evalcontextfilter
|
||||
def do_replace(eval_ctx, s, old, new, count=None):
|
||||
"""Return a copy of the value with all occurrences of a substring
|
||||
@@ -797,5 +818,6 @@ FILTERS = {
|
||||
'round': do_round,
|
||||
'groupby': do_groupby,
|
||||
'safe': do_mark_safe,
|
||||
'xmlattr': do_xmlattr
|
||||
'xmlattr': do_xmlattr,
|
||||
'urlencode': do_urlencode
|
||||
}
|
||||
|
||||
+15
-2
@@ -30,6 +30,8 @@ to_string = unicode
|
||||
#: the identity function. Useful for certain things in the environment
|
||||
identity = lambda x: x
|
||||
|
||||
_last_iteration = object()
|
||||
|
||||
|
||||
def markup_join(seq):
|
||||
"""Concatenation that escapes if necessary and converts to unicode."""
|
||||
@@ -270,6 +272,7 @@ class LoopContext(object):
|
||||
def __init__(self, iterable, recurse=None):
|
||||
self._iterator = iter(iterable)
|
||||
self._recurse = recurse
|
||||
self._after = self._safe_next()
|
||||
self.index0 = -1
|
||||
|
||||
# try to get the length of the iterable early. This must be done
|
||||
@@ -288,7 +291,7 @@ class LoopContext(object):
|
||||
return args[self.index0 % len(args)]
|
||||
|
||||
first = property(lambda x: x.index0 == 0)
|
||||
last = property(lambda x: x.index0 + 1 == x.length)
|
||||
last = property(lambda x: x._after is _last_iteration)
|
||||
index = property(lambda x: x.index0 + 1)
|
||||
revindex = property(lambda x: x.length - x.index0)
|
||||
revindex0 = property(lambda x: x.length - x.index)
|
||||
@@ -299,6 +302,12 @@ class LoopContext(object):
|
||||
def __iter__(self):
|
||||
return LoopContextIterator(self)
|
||||
|
||||
def _safe_next(self):
|
||||
try:
|
||||
return next(self._iterator)
|
||||
except StopIteration:
|
||||
return _last_iteration
|
||||
|
||||
@internalcode
|
||||
def loop(self, iterable):
|
||||
if self._recurse is None:
|
||||
@@ -344,7 +353,11 @@ class LoopContextIterator(object):
|
||||
def next(self):
|
||||
ctx = self.context
|
||||
ctx.index0 += 1
|
||||
return next(ctx._iterator), ctx
|
||||
if ctx._after is _last_iteration:
|
||||
raise StopIteration()
|
||||
next_elem = ctx._after
|
||||
ctx._after = ctx._safe_next()
|
||||
return next_elem, ctx
|
||||
|
||||
|
||||
class Macro(object):
|
||||
|
||||
@@ -11,6 +11,10 @@
|
||||
import re
|
||||
import sys
|
||||
import errno
|
||||
try:
|
||||
from urllib.parse import quote_from_bytes as url_quote
|
||||
except ImportError:
|
||||
from urllib import quote as url_quote
|
||||
try:
|
||||
from thread import allocate_lock
|
||||
except ImportError:
|
||||
@@ -349,6 +353,21 @@ def generate_lorem_ipsum(n=5, html=True, min=20, max=100):
|
||||
return Markup(u'\n'.join(u'<p>%s</p>' % escape(x) for x in result))
|
||||
|
||||
|
||||
def unicode_urlencode(obj, charset='utf-8'):
|
||||
"""URL escapes a single bytestring or unicode string with the
|
||||
given charset if applicable to URL safe quoting under all rules
|
||||
that need to be considered under all supported Python versions.
|
||||
|
||||
If non strings are provided they are converted to their unicode
|
||||
representation first.
|
||||
"""
|
||||
if not isinstance(obj, basestring):
|
||||
obj = unicode(obj)
|
||||
if isinstance(obj, unicode):
|
||||
obj = obj.encode(charset)
|
||||
return unicode(url_quote(obj))
|
||||
|
||||
|
||||
class LRUCache(object):
|
||||
"""A simple LRU Cache implementation."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user